我试图在OpenCV中使用python实现发现here的算法。我是OpenCV的新手,对我一无所知。Python OpenCV轮廓树的层次结构
我想实现的算法,删除不相关的边界边界的基础上,他们有内部边界的数量的一部分。
- 如果当前边缘边界正好具有一个或两个内部边缘边界,内部边界可以忽略
- 如果当前边缘边界具有两个以上的内边缘的边界,它可以忽略不计
我无法确定从图像中提取的轮廓的树结构。
我的电流源:
import cv2
# Load the image
img = cv2.imread('test.png')
cv2.copyMakeBorder(img, 50,50,50,50,cv2.BORDER_CONSTANT, img, (255,255,255))
# Split out each channel
blue = cv2.split(img)[0]
green = cv2.split(img)[1]
red = cv2.split(img)[2]
# Run canny edge detection on each channel
blue_edges = cv2.Canny(blue, 1, 255)
green_edges = cv2.Canny(green, 1, 255)
red_edges = cv2.Canny(red, 1, 255)
# Join edges back into image
edges = blue_edges | green_edges | red_edges
# Find the contours
contours,hierarchy = cv2.findContours(edges.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# For each contour, find the bounding rectangle and draw it
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(edges,(x,y),(x+w,y+h),(200,200,200),2)
# Finally show the image
cv2.imshow('img',edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
我认为使用RETR_TREE会给我的等值线的一个很好的嵌套数组,但似乎并不如此。我如何检索轮廓的树状结构?
你可以找到这篇文章上的层次结构的详细信息:http://opencvpython.blogspot.com/2013/01/contours-5-hierarchy.html – 2013-01-11 11:40:47