使用opencv(python)在图像上绘制边框时出现错误

时间:2019-03-12 13:57:18

标签: python numpy opencv

环境:

  

python:3.6.3

     

opencv:3.4.2

     

windows 10

我想在图像上绘制边框,代码如下:

def draw_rect(img, bboxes):
    # img = img.copy()
    for bbox in list(bboxes[:, :4]):
        bbox = [int(float(co)) for co in bbox]
        bbox[2] += bbox[0]
        bbox[3] += bbox[1]
        cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 4)
    cv2.imshow('hh', img)
    cv2.waitKey(0)

img = cv2.imread(r'E:\dataset\steel\train_dataset\0B4F3CC3.jpg')

bboxes = [[123, 124, 500, 500], [200, 200, 400, 400]]
# img = img[:, :, ::-1]   # simple transform

draw_rect(img, np.array(bboxes))

此代码可以正常工作,但是当我在绘制bbox之前进行简单转换时。

def draw_rect(img, bboxes):
    # img = img.copy()
    for bbox in list(bboxes[:, :4]):
        bbox = [int(float(co)) for co in bbox]
        bbox[2] += bbox[0]
        bbox[3] += bbox[1]
        cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 4)
    cv2.imshow('hh', img)
    cv2.waitKey(0)

img = cv2.imread(r'E:\dataset\steel\train_dataset\0B4F3CC3.jpg')

bboxes = [[123, 124, 500, 500], [200, 200, 400, 400]]


img = img[:, :, ::-1]   # simple transform


draw_rect(img, np.array(bboxes))

运行上述代码,窗口上仅显示图像,没有任何边框。

但是当我如下添加代码img = img.copy()时:

def draw_rect(img, bboxes):
    img = img.copy()
    for bbox in list(bboxes[:, :4]):
        bbox = [int(float(co)) for co in bbox]
        bbox[2] += bbox[0]
        bbox[3] += bbox[1]
        cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 4)
    cv2.imshow('hh', img)
    cv2.waitKey(0)

img = cv2.imread(r'E:\dataset\steel\train_dataset\0B4F3CC3.jpg')

bboxes = [[123, 124, 500, 500], [200, 200, 400, 400]]
img = img[:, :, ::-1]   # simple transform

draw_rect(img, np.array(bboxes))

一切顺利。那么造成这种现象的原因是什么?

0 个答案:

没有答案