英文:
Stroke detection algorithm in pictures to detect lines and curves
问题
我正在寻找一种能够检测图片中的所有线条,包括曲线等的算法,以便我可以在绘图程序(如画图)中使用我的软件重新绘制它。目前,我只希望将其重新绘制成黑白两色。我的方法是制作图片的模板,然后尝试将所有黑色像素视为线条,最终进行绘制。线条计算大致如下:
- 对于每个像素
- 点 p = (x, y)
- 线条列表 line
- 当 p 未标记时
- 标记 p
- p = 相邻最暗像素 // 像素亮度通过像素亮度除以2加上8个相邻像素亮度除以16来计算
- 将 p 添加到 line
- 结束循环
- 绘制线条
- 结束循环
我的方法可以工作,但效果不是很好。一些轮廓被检测为两条线。
您是否有改进我的算法的方法,或者有更好的算法?
英文:
I am looking for an algorithm that detects all lines including curves etc. in a picture so i can redraw it using my software in a drawing program like paint. For now i only want it to be repainted in black and white. My approach was to make a stencil of the picture and try to read all black pixels as lines and finally paint it. The line calculation goes something like that:
* for every pixel
* Point p = (x, y)
* List<Point> line
* while p is not marked
* mark p
* p = adjacent darkest pixel //brightness of a pixel is calculated by pixel luminance divided by 2 + luminance of the 8 adjacent pixels divided by 16
* add p to line
* end while
* draw line
* end for
My approach works, but not very well. Some outlines get detected as two lines.
Do you have some improvements to my algorithm, or an even better one?
答案1
得分: 3
尝试使用Canny边缘检测,这是一种常用的边缘检测算法。它已经在OpenCV中实现为cv2.Canny()
。使用屏幕截图作为输入图像,以下是结果:
输入图像
结果(反转和非反转版本)
这是Python OpenCV中的实现示例:
import cv2
# 加载图像,转换为灰度图像,并进行Canny边缘检测
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
canny = 255 - cv2.Canny(gray, 120, 255, 1)
# 显示图像
cv2.imshow('canny', canny)
cv2.waitKey()
注意: 要自动确定下限和上限阈值,请参考使用Python和OpenCV进行零参数自动Canny边缘检测。
英文:
Try Canny Edge Detection which is a popular edge detection algorithm. It's already implemented in OpenCV as cv2.Canny()
. Using a screenshoted input image, here's the result:
Input image
<img src="https://i.stack.imgur.com/CQpfU.png" width="250">
Result (inverted and non-inverted version)
<img src="https://i.stack.imgur.com/JWpq8.png" width="250">
<img src="https://i.stack.imgur.com/wJslM.png" width="250">
Here's an implementation in Python OpenCV
import cv2
# Load image, convert to grayscale, and perform Canny edge detection
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
canny = 255 - cv2.Canny(gray, 120, 255, 1)
# Show image
cv2.imshow('canny', canny)
cv2.waitKey()
Note: To automatically determine the lower and upper thresholds, take a look at Zero-parameter, automatic Canny edge detection with Python and OpenCV
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论