我正在使用 cvlib 来检测对象,并且我希望能够保存基于 bbox 坐标的裁剪图像。
我的代码中有这个:
def detect_object(img):
# Open image
image_stream = io.BytesIO(img)
image_stream.seek(0)
file_bytes = np.asarray(bytearray(image_stream.read()), dtype=np.uint8)
frame = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
# Detection
bbox, label, conf = cv.detect_common_objects(frame)
output_image = draw_bbox(frame, bbox, label, conf)
return output_image, bbox, label, conf
当我打印 bbox y 得到:
[3, -23, 1231, 731]
因此,我想使用这些坐标来裁剪原始图像并仅保存由这些坐标定义的检测到的对象
像这样的东西:
crop = output_image[bbox[2]:bbox[1], bbox[0]:bbox[3],:]
cv2.imwrite("crop.png", crop)
但是当我这样做时,我意识到作物不包含所需的对象,坐标是错误的。
我该如何解决?为什么我得到负坐标?
我的图像是 1280x720,所需的对象几乎占据了整个图像。