Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有cv2.rectangle()的多个边界框_Python_Opencv_Yolo_Bounding Box - Fatal编程技术网

Python 具有cv2.rectangle()的多个边界框

Python 具有cv2.rectangle()的多个边界框,python,opencv,yolo,bounding-box,Python,Opencv,Yolo,Bounding Box,我目前有边界框的坐标数据,包含在嵌套数据结构中,如下所示: defaultdict(list, {'giraffe': [{'conf': 0.9869, 'coords': {'center_x': 0.360333, 'center_y': 0.532274, 'height': 0.596343, 'width': 0.144651}},

我目前有边界框的坐标数据,包含在嵌套数据结构中,如下所示:

 defaultdict(list,
            {'giraffe': [{'conf': 0.9869,
               'coords': {'center_x': 0.360333,
                'center_y': 0.532274,
                'height': 0.596343,
                'width': 0.144651}},
              {'conf': 0.253321,
               'coords': {'center_x': 0.016296,
                'center_y': 0.565007,
                'height': 0.580526,
                'width': 0.03498}}],
             'zebra': [{'conf': 0.998863,
               'coords': {'center_x': 0.545974,
                'center_y': 0.693267,
                'height': 0.301859,
                'width': 0.257102}}]})
我想遍历数据结构(
img_obj_data
),并为每个对象类绘制每个对象的矩形

然后我想保存图像(绘制框),以便稍后打开

我的第一次尝试如下:

import cv2

img = cv2.imread(img_path)
img_h, img_w = img.shape[:2]

for obj_class in img_obj_data.keys():
    for sub_dict in img_obj_data[obj_class]:
        x, y, w, h = sub_dict['coords'].values()
        
        # coords cannot be floats
        x = int(x*img_w)
        y = int(y*img_h)
        x_max = int(w*img_w)
        y_max = int(y*img_h)

        cv2.rectangle(img, (x, y), (x_max, y_max), color=(0, 255, 0), thickness=2)
cv2.imwrite('/content/foobar.jpg', img)
我现在遇到了两个问题:

问题1) 边界框未与对象正确对齐,并从图像中裁剪出来。 (坐标最初是浮动的,我将它们乘以图像的宽度和高度,但我有一种预感,这是不正确的做法?)

问题2)

目前我的代码将所有框的颜色设置为相同。我如何才能做到这一点,使颜色不同的对象类

下面是有问题的图像,以及我的代码生成的图像:


下面是一个例子。 *如有必要,请根据您的环境切换
h
w

import cv2
from random import randint

img = cv2.imread(img_path)
img_h, img_w = img.shape[:2]
colors = {}

for obj_class in img_obj_data.keys():
    if obj_class not in colors:
        colors[obj_class] = [randint(0, 255), randint(0, 255), randint(0, 255)]

    for sub_dict in img_obj_data[obj_class]:
        x, y, h, w = sub_dict['coords'].values()
        # coords cannot be floats
        x_min = int((x-w/2)*img_w)
        y_min = int((y-h/2)*img_h)
        x_max = int((x+w/2)*img_w)
        y_max = int((y+h/2)*img_h)
        cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=colors[obj_class], thickness=2)
cv2.imwrite('/content/foobar.jpg', img)
此示例按类名随机更改颜色,但如果您知道类名,则可以提前定义
颜色


下面是一个例子。 *如有必要,请根据您的环境切换
h
w

import cv2
from random import randint

img = cv2.imread(img_path)
img_h, img_w = img.shape[:2]
colors = {}

for obj_class in img_obj_data.keys():
    if obj_class not in colors:
        colors[obj_class] = [randint(0, 255), randint(0, 255), randint(0, 255)]

    for sub_dict in img_obj_data[obj_class]:
        x, y, h, w = sub_dict['coords'].values()
        # coords cannot be floats
        x_min = int((x-w/2)*img_w)
        y_min = int((y-h/2)*img_h)
        x_max = int((x+w/2)*img_w)
        y_max = int((y+h/2)*img_h)
        cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=colors[obj_class], thickness=2)
cv2.imwrite('/content/foobar.jpg', img)
此示例按类名随机更改颜色,但如果您知道类名,则可以提前定义
颜色


能否将
x,y,w,h=sub_dict['coords']值()更改为
x,y,h,w=sub_dict['coords']值()
?这不是一个解决方案,而是要站在同一个舞台上。@pond27是的,我也想到了!将
x,y,w,h
的顺序切换到
x,y,h,w
会导致较大的边界框仍然未对齐。我的意思是,为了获得结果图像,应该切换w,h的片段。@pond27我刚刚重新生成了图像。我的代码片段是
x,y,w,h
,生成的图像与我在这里发布的图像相同。切换顺序仍然会复制未对齐的方框。@pond27我想你差点就搞定了。矩形是一种改进(它们构成动物的框架),但它们仍然很大,超出了图像范围。你能将
x,y,w,h=sub_dict['coords'].values()
更改为
x,y,h,w=sub_dict['coords'].values()
?这不是一个解决方案,而是要站在同一个舞台上。@pond27是的,我也想到了!将
x,y,w,h
的顺序切换到
x,y,h,w
会导致较大的边界框仍然未对齐。我的意思是,为了获得结果图像,应该切换w,h的片段。@pond27我刚刚重新生成了图像。我的代码片段是
x,y,w,h
,生成的图像与我在这里发布的图像相同。切换顺序仍然会复制未对齐的方框。@pond27我想你差点就搞定了。矩形是一种改进(它们将动物框起来),但它们仍然很大,超出了图像范围。