繁体   English   中英

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

[英]How to find center pixel value of bounding box in 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.circle 从该点绘制圆圈时,它绘制的圆圈没有任何错误 我如何访问点 img_read[center_x][center_y]) 的像素强度值? 我也尝试过 img_read[center_x,center_y] 但得到了同样的错误

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

#Read the image & get the dimensions  
    img_read= cv2.imread(r"C:\Users\Desktop\test_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))

OUTPUT:中心像素的颜色是 - 红色:152,绿色:152,蓝色:152

尝试:

img_read[center_y,center_x] 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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