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?
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

Result (inverted and non-inverted version)

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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With