首页 > 解决方案 > saving the bounding box image

问题描述

Instead of trying to draw the bounding box over the image, i am trying to save it as a new image.

When i was getting [ymin, xmax, ymax, xmin] points, i was doing this.

import cv2 
import numpy as np

image = cv2.imread('ballet_106_0.jpg')
image = np.array(image)

boxes = [21, 511, 41, 420 ]
ymin, xmax , ymax ,xmin = boxes

im2 = image[ymin:ymax,xmin:xmax,:]
cv2.imwrite('bboximage.jpg',im2)

But if i only get the x and y points along with the height and width. I'm not sure how i could index the numpy array.

Any suggestions would be really helpful ,Thanks in advance.

标签: pythonnumpyopencvimage-processing

解决方案


您的代码看起来不错,尽管这一行:

image = np.array(image)

不是必需的,好像一切顺利cv2.imread产生np.array,但是如果cv2.imread失败它返回None,这可能是您的问题的根源,请在您的下面添加以下行cv2.imread

print(type(image))

如果它打印None,则很可能意味着ballet_106_0.jpg您的目录中没有图像。

编辑:要转换x,y,height,widthx/y-min/max值,只需执行

ymin = y
ymax = y+height
xmin = x
xmax = x+width

推荐阅读