【发布时间】:2017-09-28 19:34:44
【问题描述】:
我有几张如下所示的图片: 图像大多在白色背景上。
我尝试使用 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 :即使我可以提取给定图像中最大的对象,也可以。
【问题讨论】:
标签: python image opencv opencv3.0 opencv-contour