[英]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.