简体   繁体   English

图像处理的边界检测算法

[英]Boundary detection algorithm for image processing

I need an algorithm that can recognize the overall shape of the input image.我需要一种可以识别输入图像整体形状的算法。 I tried edge detection and corner detection but they are identifying the dips and curves as well.我尝试了边缘检测和角点检测,但它们也在识别倾斜和曲线。

If I input an image, the algorithm should detect the points that are higher than the initial point unless it is the last point in the top of the image and for the bottom, the algorithm should identify those points which are lower than the initial one.如果我输入图像,算法应该检测高于初始点的点,除非它是图像顶部的最后一个点,对于底部,算法应该识别那些低于初始点的点。 Through this process the algorithm should be ale to create an overall boundary shape of the image and then return it.通过这个过程,算法应该可以创建图像的整体边界形状,然后将其返回。

Any suggestions or links?有什么建议或链接吗?

Input image:输入图像:

输入图像

Expected output:预期 output:

输出图像应该是黑色轮廓

Since you haven't provided any library, I have shown how it can be done using OpenCV.由于您没有提供任何库,我已经展示了如何使用 OpenCV 来完成它。

As mentioned in the comments, finding the convex hull is what you are looking for.正如评论中提到的,找到凸包是您正在寻找的。 A convex shape is one where no interior angles are greater than 180 degrees.凸形是内角不大于 180 度的形状。 For more simple explanation have a look at his blog page有关更简单的解释,请查看他的博客页面

The following code is written in Python using OpenCV:以下代码使用 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 contains the boundary points of all the shapes found in the binary image. contours包含在二值图像中找到的所有形状的边界点。 Notice the word boundary , since we only consider the shape of the white region.注意单词边界,因为我们只考虑白色区域的形状。 For this very purpose we use cv2.RETR_EXTERNAL flag while finding contours.为此,我们在查找轮廓时使用cv2.RETR_EXTERNAL标志。

And since the lines in the plot are not connected, we obtain more than one contour.并且由于 plot 中的线没有连接,因此我们获得了多个轮廓。 The following snippet collects the points of all the contours in a single variable:以下代码段将所有轮廓的点收集在一个变量中:

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 contains the boundary points of all the red pixels. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM