Python 在图像中获得超过预期的轮廓

Python 在图像中获得超过预期的轮廓,python,opencv,image-processing,computer-vision,contour,Python,Opencv,Image Processing,Computer Vision,Contour,我有一张几乎90次由N组成的图像,但我使用以下代码得到了105个轮廓: gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #cv2.imshow("Image", gray) #cv2.waitKey(0) blurred = cv2.GaussianBlur(gray, (5, 5), 0) #blur to reduce noise #cv2.imshow("Image", blurred) #cv2.wa

我有一张几乎90次由
N
组成的图像,但我使用以下代码得到了105个轮廓:

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#cv2.imshow("Image", gray)
#cv2.waitKey(0)

blurred = cv2.GaussianBlur(gray, (5, 5), 0) #blur to reduce noise
#cv2.imshow("Image", blurred)
#cv2.waitKey(0)

# perform edge detection, find contours in the edge map, and sort the
# resulting contours from left-to-right
edged = cv2.Canny(blurred, 30, 150) #30, 150
#cv2.imwrite("test.png", edged)
#cv2.imshow("Image", edged)
#cv2.waitKey(0)

#find contours of characters(objects) in images
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
if cnts:
    cnts = sort_contours(cnts, method="left-to-right")[0]

cv2.drawContours(image, cnts, -1, (0, 0, 255), 2)
cv2.imwrite("all_contours.jpg", image) 
我在
Canny
findContours
函数中尝试了不同的组合,但无法得到与
image
N
个数相等的轮廓

我的形象:

具有轮廓的图像:

看轮廓图,我看不出问题出在哪里。 任何帮助或暗示都将不胜感激


附:这张图片是测试的理想图片。在真实场景中,图像将从网络摄像头中拍摄。

请不要使图像模糊

导入cv2
image=cv2.imread(“n.png”)
图像=cv2。调整大小(图像,(800800))
灰色=cv2.CVT颜色(图像,cv2.COLOR\u BGR2GRAY)
_,threshold=cv2.阈值(灰色,100255,cv2.阈值\u二进制\u INV)
轮廓,层次=cv2.findContours(阈值,cv2.RETR\u外部,cv2.CHAIN\u近似值\u无)
cv2.putText(图像,“图像中发现的轮廓:”+str(len(轮廓)),(20,20),cv2.FONT\u HERSHEY\u PLAIN,1,(255,0,0),1)
cv2.绘制等高线(图像,等高线,-1,(0,0,255),-1)
cv2.imshow(“轮廓图像”,图像)
cv2.等待键(0)
cv2.destroyAllWindows()

这是可行的,但此图像是测试的理想图像。在真实场景中,图像将从网络摄像头中获取。所以去除噪声和精明的边缘检测会更好(我猜)。您的意见是什么?您可以使用
双边筛选
而不是
高斯模糊
。双边过滤器将平滑您的图像,减少噪音,同时保留边缘。查看有关双边过滤器的更多信息