如何消除直线

时间:2018-12-13 12:26:14

标签: c++ opencv

我下面有canny边缘检测器的输出

enter image description here

我想消除那些直线。输出图像应仅包含椭圆形。我尝试使用findContours()并找到最大的轮廓并将其消除。但是我对结果不满意。 有什么办法可以有效地做到这一点?

1 个答案:

答案 0 :(得分:3)

如果您只想检测椭圆形 而不想使用HoughLines,则可以如下使用SimpleBlobDetector

注意:我用python编写了此代码,因为它的实现速度更快。该代码非常容易转换为C ++ IMO

import numpy as np
import cv2
image = cv2.imread("remove_lines.jpg")
imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply morph closing to improve the connection between ovals (complete the ovals)
kernel = np.ones((5,5), np.uint8)
imgray = cv2.morphologyEx(imgray, cv2.MORPH_CLOSE, kernel=kernel)

# Set parameters for blob detection
params = cv2.SimpleBlobDetector_Params()
# This is to remove any "small" blobs
params.filterByArea = True
params.minArea = 200
# bit easy on the circularity here, circle = 1
params.filterByCircularity = True
params.minCircularity = 0.7
# Convexity = Area of Blob / Area of its convex hull 
params.filterByConvexity = True
params.minConvexity = 0.1
# Measure how elongated the shape is, line = 0, circle = 1, very easy here as well
params.filterByInertia = True
params.minInertiaRatio = 0.01

detector = cv2.SimpleBlobDetector_create(params)

keypoints = detector.detect(imgray)

im_with_points = cv2.drawKeypoints(image, keypoints, np.array([]), (0, 0, 255), 
cv2.DrawMatchesFlags_DRAW_RICH_KEYPOINTS)

cv2.imshow("Keypoints", im_with_points)
cv2.waitKey(0)

结果:

Result