我是 CV2 和 python 的新手。
我有很多影像,它們有很多大大小小的彎曲結構。
我必須得到最大的一個輪廓。
但我失敗了。
我的代碼和圖片如下...
import cv2 as cv
img_color = cv.imread('ex1.png')
img_gray = cv.cvtColor(img_color, cv.COLOR_BGR2GRAY)
ret, img_binary = cv.threshold(img_gray, 127, 255, 0)
#dbg contours, hierarchy = cv.findContours(img_binary, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
_, contours, hierarchy = cv.findContours(img_binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
for cnt in contours:
cv.drawContours(img_color, [cnt], 0, (255, 0, 0), 3) # blue
#dbg cv.imshow("result", img_color)
#dbg cv.waitKey(0)
cv.imwrite('save_image1.png', img_color)
for cnt in contours:
hull = cv.convexHull(cnt)
cv.drawContours(img_color, [hull], 0, (0, 0, 255), 5)
#dbg cv.imshow("result", img_color)
#dbg cv.waitKey(0)
cv.imwrite('save_image2.png', img_color)
輸入影像(“ex1.png”)的示例就像......
結果輸出影像(“save_image2.png”)就像
......


但是,我想要檢索的內容如下所示......(任何藍色或紅色,我都可以使用它們;)
我的意思是,結果輪廓必須是包含所有內容的大輪廓。

感謝您在此處閱讀 util(;)
uj5u.com熱心網友回復:
您要求提供所有區域的輪廓/輪廓,而不僅僅是最大區域。所以這里是如何在 Python/OpenCV 中做到這一點。
- 讀取輸入
- 轉換為灰色
- 二進制閾值
- 獲取值大于0的所有點并轉置(因為numpy使用y,x約定而OpenCV想要x,y)
- 計算點的凸包
- 在輸入的副本上繪制折線
- 在黑色影像上繪制白色填充多邊形
- 獲取白色填充多邊形的輪廓
- 在輸入的副本上繪制輪廓
- 保存結果
輸入:

import cv2
import numpy as np
img_color = cv2.imread('ex1.png')
img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)
img_binary = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)[1]
# get convex hull
points = np.column_stack(np.where(img_binary.transpose() > 0))
hull = cv2.convexHull(points)
# draw convex hull on input image in green
result = img_color.copy()
cv2.polylines(result, [hull], True, (0,0,255), 2)
# draw white filled hull polygon on black background
mask = np.zeros_like(img_binary)
cv2.fillPoly(mask, [hull], 255)
# get the largest contour from result2
contours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
# draw contour on copy of input
contr = img_color.copy()
contr = cv2.drawContours(contr, [big_contour], 0, (0,0,255), 2)
# save result2
cv2.imwrite('ex1_convex_hull.png', result)
cv2.imwrite('ex1_convex_hull_contour.png', contr)
# show result2
cv2.imshow('result', result)
cv2.imshow('contr', contr)
cv2.waitKey(0)
cv2.destroyAllWindows()
生成的凸包:

凸包輪廓:

uj5u.com熱心網友回復:
這是在 Python/OpenCV 中執行此操作的一種方法。
用于max(contours, key=cv2.contourArea)獲得最大的一個。
輸入:

import cv2
img_color = cv2.imread('ex1.png')
img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)
img_binary = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)[1]
contours = cv2.findContours(img_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
img_contour = img_color.copy()
cv2.drawContours(img_contour, [big_contour], 0, (0,0,255), 2)
cv2.imwrite('ex1_contour.png', img_contour)
cv2.imshow('img_contour', img_contour)
cv2.waitKey(0)
cv2.destroyAllWindows()

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/517754.html
上一篇:OpenCVVideoWriter(Gstreamer NVENC)凍結超過3個流
下一篇:blitopencvcameracapturewithpygamethrowsTypeError:argument1mustbepygame.Surface,notcv2.VideoCapture
