当 Canny 图像如此清晰时,为什么使用 findContours() 找到的轮廓都被分解成碎片?
Why contours found using findContours() are all broken up into pieces when the Canny Image is so clear?
左边是我的Canny Image。右边是我的原始图像,轮廓用不同颜色标记。
这让我很烦恼...为什么当 Canny Image 如此清晰时,cv2.findContours() 函数仍然没有给我一个 包裹大矩形的单个封闭轮廓 ??
原始输入图像:
我的代码:
# [get colored raw image]
rawIm = cv2.imread("sample1.jpg");
# [gray]
print("--------- Image Modification: Gray and Invert --------")
grayIm = cv2.cvtColor(rawIm, cv2.COLOR_BGR2GRAY);
grayIm = inverte(grayIm) # invert the black to white
# [blurring]
print("--------- Image Modification: Blur --------")
blurredIm = cv2.medianBlur(grayIm, 5);
# [dilate]
print("--------- Image Modification: Dilated --------")
kernel = np.ones((7,7), np.uint8)
dilatedIm = cv2.dilate(blurredIm, kernel, iterations=1)
# [get edges]
cannyIm = cv2.Canny(dilatedIm, lowThreshold, lowThreshold*cannyThresholdRatio)
# [FIND CONTOURS]
cnts, hier = cv2.findContours(cannyIm.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# [DRAW CONTOURS]
for cnt in cnts:
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
cv2.drawContours(rawIm, [cnt], -1, color , 6)
cv2.imshow("CANNY", resize(cannyIm, percentageShrink))
cv2.imshow("CONTOURS", resize(rawIm, percentageShrink))
是这个问题吗?
cv2.findContours(cannyIm.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
除了“cv2.RETR_LIST”和“cv2.CHAIN_APPROX_SIMPLE”之外,我必须使用不同的参数吗?
好的。扩张有效。在 Canny 过程之后添加 cannyIm = cv2.dilate(cannyIm, kernel, iterations=1)
就成功了。
完整代码
# [get colored raw image]
rawIm = cv2.imread("sample1.jpg");
# [gray]
print("--------- Image Modification: Gray and Invert --------")
grayIm = cv2.cvtColor(rawIm, cv2.COLOR_BGR2GRAY);
grayIm = inverte(grayIm) # invert the black to white
# [blurring]
print("--------- Image Modification: Blur --------")
blurredIm = cv2.medianBlur(grayIm, 5);
# [dilate]
print("--------- Image Modification: Dilated --------")
kernel = np.ones((7,7), np.uint8)
dilatedIm = cv2.dilate(blurredIm, kernel, iterations=1)
# [get edges]
cannyIm = cv2.Canny(dilatedIm, lowThreshold, lowThreshold*cannyThresholdRatio)
cannyIm = cv2.dilate(cannyIm, kernel, iterations=1)
# [FIND CONTOURS]
cnts, hier = cv2.findContours(cannyIm.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# [DRAW CONTOURS]
for cnt in cnts:
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
cv2.drawContours(rawIm, [cnt], -1, color , 6)
cv2.imshow("CANNY", resize(cannyIm, percentageShrink))
cv2.imshow("CONTOURS", resize(rawIm, percentageShrink))
谢谢@fmw42
左边是我的Canny Image。右边是我的原始图像,轮廓用不同颜色标记。
这让我很烦恼...为什么当 Canny Image 如此清晰时,cv2.findContours() 函数仍然没有给我一个 包裹大矩形的单个封闭轮廓 ??
原始输入图像:
我的代码:
# [get colored raw image]
rawIm = cv2.imread("sample1.jpg");
# [gray]
print("--------- Image Modification: Gray and Invert --------")
grayIm = cv2.cvtColor(rawIm, cv2.COLOR_BGR2GRAY);
grayIm = inverte(grayIm) # invert the black to white
# [blurring]
print("--------- Image Modification: Blur --------")
blurredIm = cv2.medianBlur(grayIm, 5);
# [dilate]
print("--------- Image Modification: Dilated --------")
kernel = np.ones((7,7), np.uint8)
dilatedIm = cv2.dilate(blurredIm, kernel, iterations=1)
# [get edges]
cannyIm = cv2.Canny(dilatedIm, lowThreshold, lowThreshold*cannyThresholdRatio)
# [FIND CONTOURS]
cnts, hier = cv2.findContours(cannyIm.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# [DRAW CONTOURS]
for cnt in cnts:
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
cv2.drawContours(rawIm, [cnt], -1, color , 6)
cv2.imshow("CANNY", resize(cannyIm, percentageShrink))
cv2.imshow("CONTOURS", resize(rawIm, percentageShrink))
是这个问题吗?
cv2.findContours(cannyIm.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
除了“cv2.RETR_LIST”和“cv2.CHAIN_APPROX_SIMPLE”之外,我必须使用不同的参数吗?
好的。扩张有效。在 Canny 过程之后添加 cannyIm = cv2.dilate(cannyIm, kernel, iterations=1)
就成功了。
完整代码
# [get colored raw image]
rawIm = cv2.imread("sample1.jpg");
# [gray]
print("--------- Image Modification: Gray and Invert --------")
grayIm = cv2.cvtColor(rawIm, cv2.COLOR_BGR2GRAY);
grayIm = inverte(grayIm) # invert the black to white
# [blurring]
print("--------- Image Modification: Blur --------")
blurredIm = cv2.medianBlur(grayIm, 5);
# [dilate]
print("--------- Image Modification: Dilated --------")
kernel = np.ones((7,7), np.uint8)
dilatedIm = cv2.dilate(blurredIm, kernel, iterations=1)
# [get edges]
cannyIm = cv2.Canny(dilatedIm, lowThreshold, lowThreshold*cannyThresholdRatio)
cannyIm = cv2.dilate(cannyIm, kernel, iterations=1)
# [FIND CONTOURS]
cnts, hier = cv2.findContours(cannyIm.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# [DRAW CONTOURS]
for cnt in cnts:
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
cv2.drawContours(rawIm, [cnt], -1, color , 6)
cv2.imshow("CANNY", resize(cannyIm, percentageShrink))
cv2.imshow("CONTOURS", resize(rawIm, percentageShrink))
谢谢@fmw42