[英]Image processing library/algorithm in c/c++ for contour coordinate detection
[英]Boundary detection algorithm for image processing
我需要一種可以識別輸入圖像整體形狀的算法。 我嘗試了邊緣檢測和角點檢測,但它們也在識別傾斜和曲線。
如果我輸入圖像,算法應該檢測高於初始點的點,除非它是圖像頂部的最后一個點,對於底部,算法應該識別那些低於初始點的點。 通過這個過程,算法應該可以創建圖像的整體邊界形狀,然后將其返回。
有什么建議或鏈接嗎?
輸入圖像:
預期 output:
由於您沒有提供任何庫,我已經展示了如何使用 OpenCV 來完成它。
正如評論中提到的,找到凸包是您正在尋找的。 凸形是內角不大於 180 度的形狀。 有關更簡單的解釋,請查看他的博客頁面
以下代碼使用 OpenCV 在 Python 中編寫:
# read the input image
img = cv2.imread('red_plot.png')
img2 = img.copy()
# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# obtain binary image, such that the red pixels are white
th = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
# find contours (shapes with white pixels)
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours
包含在二值圖像中找到的所有形狀的邊界點。 注意單詞邊界,因為我們只考慮白色區域的形狀。 為此,我們在查找輪廓時使用cv2.RETR_EXTERNAL
標志。
並且由於 plot 中的線沒有連接,因此我們獲得了多個輪廓。 以下代碼段將所有輪廓的點收集在一個變量中:
cc = np.array([[0, 0]], dtype=int)
for i, c in enumerate(contours):
c_modified = c.reshape(len(contours[i]), 2)
cc = np.concatenate((cc, c_modified), axis = 0)
cc2 = cc[1:]
combined_contour = np.reshape(cc2, (cc2.shape[0], 1, cc2.shape[1]))
combined_contour
包含所有紅色像素的邊界點。
# finding convex hull
hull = cv2.convexHull(combined_contour, False)
# draw the result
color = (0, 255, 0)
img2 = cv2.drawContours(img2, [hull], -1, color, 3)
cv2.imshow('Convex hull', img2)
cv2.waitKey(0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.