简体   繁体   English

图片中的笔画检测算法检测线条和曲线

[英]Stroke detection algorithm in pictures to detect lines and curves

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.我正在寻找一种算法来检测图片中的所有线条(包括曲线等),以便我可以在绘图程序(如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?你对我的算法有什么改进,或者更好?

Try Canny Edge Detection which is a popular edge detection algorithm.试试 Canny Edge Detection,这是一种流行的边缘检测算法。 It's already implemented in OpenCV as cv2.Canny() .它已经在 OpenCV 中实现为cv2.Canny() Using a screenshoted input image, here's the result:使用截图输入图像,结果如下:

Input image输入图像

Result (inverted and non-inverted version)结果(倒置和非倒置版本)

Here's an implementation in Python OpenCV这是 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注意:要自动确定上下阈值,请查看Zero-parameter, automatic Canny edge detection with Python and OpenCV

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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