在这里插入图片描述

前言

前提条件

相关介绍

以下是一个Labelme标注json文件示例

{
  "version": "4.5.6",
  "flags": {},
  "shapes": [
    {
      "label": "dog",
      "points": [
        [
          121.0,
          233.0
        ],
        [
          223.0,
          232.0
        ],
        [
          246.0,
          334.0
        ],
        [
          121.0,
          337.0
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    }
  ],
  "lineColor": [
    0,
    255,
    0,
    128
  ],
  "fillColor": [
    255,
    0,
    0,
    128
  ],
  "imagePath": "img_001.jpg",
  "imageData": "iVBORw0KGgoAAAANSUhEUgAA...",
  "imageHeight": 600,
  "imageWidth": 800
}

实验环境

过滤掉特定区域内的矩形框

在这里插入图片描述

方法一:直接法(for循环遍历

代码实现

import cv2
import copy
import numpy as np

def is_rect_inside(rect, filtered_rects):  
    for filtered_rect in filtered_rects:  
        if (rect[1] >= filtered_rect[1] and rect[1] + rect[3] <= filtered_rect[1] + filtered_rect[3] and  
            rect[2] >= filtered_rect[2] and rect[2] + rect[4] <= filtered_rect[2] + filtered_rect[4]):  
            return True  
    return False  


def filter_rect(rects_list,labels_list,scores_list,filtered_rects,pad_x=50,pad_y=50):
    '''
    合并重叠输入参数: 
        rects_list :[[占位符,x,y,w,h,占位符],[占位符,x,y,w,h,占位符],...]
        labels_list :[0,1,...]
        scores_list :[0.8,0.15,...]
        filtered_rects: [[占位符,x,y,w,h,占位符],[占位符,x,y,w,h,占位符],...]

    返回:
        过滤后的rects_list : [[占位符,x,y,w,h,占位符],[占位符,x,y,w,h,占位符],...]
        过滤后的labels_list : [0,1,...]
        过滤后的scores_list : [0.8,0.15,...]
    '''
    new_rects_list = []
    new_labels_list = []
    new_scores_list = []

    for index,rect in enumerate(rects_list):
        if not is_rect_inside(rect, filtered_rects):
            new_rects_list.append(rect)
            new_labels_list.append(labels_list[index])
            new_scores_list.append(scores_list[index])
        

    return new_rects_list,new_labels_list,new_scores_list


if __name__=="__main__":
    # 特定区域(蓝色区域)
    filtered_rects = [[2.0,390,390,60,60,0.0],[2.0,90,90,250,250,0.0]]
    # 原始矩形框
    rects_list = [[2.0,10,10,15,15,0.0],[2.0,20,20,10,10,0.0],[2.0,100,100,150,150,0.0],  
                [2.0,200,200,100,100,0.0],[2.0,400,400,15,15,0.0],[2.0,420,420,10,10,0.0]] # [占位符,x,y,w,h,占位符]
    # print("原始的矩形框:",rects_list)
    labels_list = [0,1,2,3,2,1]
    scores_list = [0.8,0.9,0.5,0.6,0.7,0.3]
    
    img = np.ones([512, 512, 3], np.uint8)
    for _,x,y,w,h,_ in rects_list:
        img = cv2.rectangle(img, (x,y), (x+w,y+h), (0, 255, 0), 2)
    for _,x,y,w,h,_ in filtered_rects:
        img = cv2.rectangle(img, (x,y), (x+w,y+h), (255, 0, 0), 2)
    cv2.imshow('origin', img)
    # cv2.imwrite('origin.jpg', img)


    new_rects_list,new_labels_list,new_scores_list = filter_rect(rects_list,labels_list,scores_list,filtered_rects,pad_x=50,pad_y=50)
    # print("过滤后的矩形框,类别,置信度:",new_rects_list,new_labels_list,new_scores_list)

    img = np.ones([512, 512, 3], np.uint8) 
    for _,x,y,w,h,_ in new_rects_list:
        img = cv2.rectangle(img, (x,y), (x+w,y+h), (0, 0, 255), 2)
    for _,x,y,w,h,_ in filtered_rects:
        img = cv2.rectangle(img, (x,y), (x+w,y+h), (255, 0, 0), 2)
    cv2.imshow('filtered', img)
    # cv2.imwrite('filtered.jpg', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

输出结果

在这里插入图片描述

方法二:列表推导

代码实现

import cv2
import copy
import numpy as np

def is_rect_inside(rect, filtered_rects):  
    for filtered_rect in filtered_rects:  
        if (rect[1] >= filtered_rect[1] and rect[1] + rect[3] <= filtered_rect[1] + filtered_rect[3] and  
            rect[2] >= filtered_rect[2] and rect[2] + rect[4] <= filtered_rect[2] + filtered_rect[4]):  
            return True  
    return False  


if __name__=="__main__":
    # 特定区域(蓝色区域)
    filtered_rects = [[2.0,390,390,60,60,0.0],[2.0,90,90,250,250,0.0]]
    # 原始矩形
    rects_list = [[2.0,10,10,15,15,0.0],[2.0,20,20,10,10,0.0],[2.0,100,100,150,150,0.0],  
                [2.0,200,200,100,100,0.0],[2.0,400,400,15,15,0.0],[2.0,420,420,10,10,0.0]] # [占位符,x,y,w,h,占位符] 
    # print("原始的矩形框:",rects_list)
    labels_list = [0,1,2,3,2,1]
    scores_list = [0.8,0.9,0.5,0.6,0.7,0.3] 

    img = np.ones([512, 512, 3], np.uint8)
    for _,x,y,w,h,_ in rects_list:
        img = cv2.rectangle(img, (x,y), (x+w,y+h), (0, 255, 0), 2)
    for _,x,y,w,h,_ in filtered_rects:
        img = cv2.rectangle(img, (x,y), (x+w,y+h), (255, 0, 0), 2)
    cv2.imshow('origin', img)
    # cv2.imwrite('origin.jpg', img)
    
    print("原始的矩形框:", rects_list)  
    
    filtered_rects_list = [rect for rect in rects_list if not is_rect_inside(rect, filtered_rects)]
    filtered_labels_list = [labels_list[index] for index,rect in enumerate(rects_list) if not is_rect_inside(rect, filtered_rects)]
    filtered_scores_list = [scores_list[index] for index,rect in enumerate(rects_list) if not is_rect_inside(rect, filtered_rects)]
    
    print("过滤后的矩形框,类别,置信度:", filtered_rects_list,filtered_labels_list,filtered_scores_list)

    img = np.ones([512, 512, 3], np.uint8) 
    for _,x,y,w,h,_ in filtered_rects_list:
        img = cv2.rectangle(img, (x,y), (x+w,y+h), (0, 0, 255), 2)
    for _,x,y,w,h,_ in filtered_rects:
        img = cv2.rectangle(img, (x,y), (x+w,y+h), (255, 0, 0), 2)
    cv2.imshow('filtered', img)
    # cv2.imwrite('filtered.jpg', img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

输出结果

在这里插入图片描述

原文地址:https://blog.csdn.net/FriendshipTang/article/details/134745746

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_43158.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注