简体   繁体   中英

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.

Your code looks ok, though this line:

image = np.array(image)

is not required, as if everything goes well cv2.imread produce np.array , however if cv2.imread fails it returns None , which might be source of your problem, please add following line below your cv2.imread :

print(type(image))

if it prints None , it most probably means that there is not ballet_106_0.jpg image in your directory.

EDIT: To convert x,y,height,width to x/y-min/max values simply do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM