gpt4 book ai didi

c++ - 如何消除直线

转载 作者:太空狗 更新时间:2023-10-29 21:10:33 26 4
gpt4 key购买 nike

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

enter image description here

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

最佳答案

如果您只想检测椭圆形并且不想使用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

关于c++ - 如何消除直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53761831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com