我有一套兩個黑白圖像[附加],我想爲每個圖像中的人物都放上矩形包圍盒。我知道cv2.dilate可能會有所幫助,但我看到的大多數示例都着眼於檢測包含最大像素強度的一個矩形,所以基本上它們會在圖像中放置一個大矩形。我想有兩個獨立的矩形。python中的單色圖像中的對象周圍的矩形邊界框?
UPDATE: 這是我的嘗試:
import numpy as np
import cv2
im = cv2.imread('splinet.png',0)
print im.shape
kernel = np.ones((50,50),np.uint8)
dilate = cv2.dilate(im,kernel,iterations = 10)
ret,thresh = cv2.threshold(im,127,255,0)
im3,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
plt.imshow(im,cmap='Greys_r')
#plt.imshow(im3,cmap='Greys_r')
for i in range(0, len(contours)):
if (i % 2 == 0):
cnt = contours[i]
#mask = np.zeros(im2.shape,np.uint8)
#cv2.drawContours(mask,[cnt],0,255,-1)
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(im,(x,y),(x+w,y+h),(255,255,0),5)
plt.imshow(im,cmap='Greys_r')
cv2.imwrite(str(i)+'.png', im)
cv2.destroyAllWindows()
和下面的輸出連接:正如你看到的,小箱子被製成,它不是超級清晰。
***'大部分例子'***這是什麼意思?你想要什麼? –
我的意思是我在網上找到的教程檢測到一個包含最大像素強度的矩形。我想在兩個圖像中的人身上放置邊界框 – GKS
作爲一個起點,您應該搜索「輪廓邊界框二值圖像」並在此處獲取一些代碼,其思想是對圖像進行閾值處理,然後找到輪廓,然後邊界框 – ZdaR