如何在opencv python中找到边界框的中心像素值?



我试图找到边界框中心的像素强度

为了达到这个目的,我找到边界框的中心坐标,并得到该坐标的像素强度,如下所示

img_read= cv2.imread(r'image.png')
cv2.rectangle(img_read,(xmin,ymin),(xmax,ymax),(0,0,255),3)
center_x = int((xmin+xmax)//2)
center_y = int((ymin+ymax)//2)
print(center_x,center_y)
cv2.circle(img_read,(center_x,center_y),50,(0,0,255),3)
print('Pixel intensity at:',img_read[center_x][center_y])
plt.imshow(img_read[:,:,::-1])

当我运行这个时,我得到如下错误

IndexError: index 859 is out of bounds for axis 0 with size 815

但是当我尝试用cv2从该点画圆时。它画圆没有任何错误我如何访问像素强度值在点img_read[center_x][center_y]) ?我也尝试过这个img_read[center_x,center_y],但得到相同的错误

任何解决此问题的帮助或建议将不胜感激。

#Read the image & get the dimensions  
img_read= cv2.imread(r"C:UsersDesktoptest_center_px.tiff")
dimensions = img_read.shape
h, w=dimensions[0], dimensions[1]            
#create the bounding box if necessary (not in mine)       
domain = cv2.rectangle(img_read,(0,0),(w,h),(255,0,0),20)
plt.imshow(domain,cmap='gray')

center_x = w/2
center_y = h/2
#all we need to do is pass in the (x, y)-coordinates as image[y, x]
(b, g, r) = img_read[np.int16(center_y), np.int16(center_x)]
print("Color at center pixel is - Red: {}, Green: {}, Blue: {}".format(r, g, b))

输出:中心像素的颜色为-红:152,绿:152,蓝:152

尝试:

img_read[center_y,center_x] 

最新更新