0
我有幾張圖像,如下所示: 圖像主要在白色背景上。Opencv提取最大感興趣的區域
我試着用opencv連接的組件檢測到兩件衣服。 試圖採取最大的兩個連接組件,不幸的是,我失敗了。
我相信這是可能的,但由於我是opencv的新手,有人可以告訴我們可以做些什麼來檢測以下圖像中的多件衣服嗎?
任何幫助表示讚賞
代碼,我試圖在python:
#Read the image and conver to grayscale
img = cv2.imread('t.jpg' , 0)
#Applt the median filter on the image
#med = cv2.medianBlur(image,5) # 5 is a fairly small kernel size
#Apply an edge detection filter
laplacian = cv2.Laplacian(img,cv2.CV_64F)
laplacian = laplacian.astype(np.uint8)
ret,thresh1 = cv2.threshold(laplacian,127,255,cv2.THRESH_BINARY)
src = thresh1
src = np.array(src, np.uint8)
ret, thresh = cv2.threshold(src,10,255,cv2.THRESH_BINARY)
# You need to choose 4 or 8 for connectivity type
connectivity =8
# Perform the operation
output = cv2.connectedComponentsWithStats(thresh, connectivity, cv2.CV_32S)
# Get the results
# The first cell is the number of labels
num_labels = output[0]
# The second cell is the label matrix
labels = output[1]
# The third cell is the stat matrix
stats = output[2]
# The fourth cell is the centroid matrix
centroids = output[3]
src = cv2.cvtColor(src,cv2.COLOR_GRAY2RGB)
for stat in stats:
x , y ,w , h ,a = stat
cv2.rectangle(src,(x,y),(x+w,y+h),(0,0,255),2)
# write original image with added contours to disk
#cv2.imwrite('contoured.jpg', image)
cv2.imshow("Image", src)
#cv2.waitKey(0)
cv2.waitKey(0)
cv2.destroyAllWindows()
我上面的代碼
NB輸出:其優良即使我可以提取給定圖像中的最大對象。