左邊是我的 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”之外,我是否必須使用不同的引數?
uj5u.com熱心網友回復:
好的。擴張有效。cannyIm = cv2.dilate(cannyIm, kernel, iterations=1)在 Canny 程序之后添加就成功了。
完整代碼
# [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

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348697.html
下一篇:我正在嘗試實作:np.maximum.outerinPython3x但我收到此錯誤:NotImplementedError
