Line detection: Computer Vision VS Deep Neural Network

An analysis of line detection using a simple CV algorithm vs a complex neural network and a conclusion about when to use what method

Nicola Landro
3 min readMay 7, 2022

Line detection have to many application: for example estimate the perspective into an image or create a vectorized image, endge detection and more. In this article I do not speack about application, but I show how to use two algorithm, we will compare them and conclude when we want to use witch ones.

An example of classical canny+hough transform line detection

Line Detection with CV

A base idea with a canny detection and a probabilistic hough transform using opencv-python (we can have more information on this link), the simple way to read an image and obtains lines is the following:

import cv2 as cvrc = cv.imread(cv.samples.findFile(path), cv.IMREAD_GRAYSCALE)
dst = cv.Canny(src, 50, 200, None, 3)
linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10)

A colab notebook full demo is available here.

Neural approach

There are many neural approach, for example some ones use hough tansform like Deep Hough Transform for Semantic Line Detection that also have a web demo:

Example of Deep Hough Transform

But we also have other approach like LETR (Line Segment Detection) that use wireframe dataset to find more lines that can describe 3D object (web demo)

Example of LETR (with resnet50 and 256*256 image scaling, it is possible to obtain better results with resnet 101 and more image resolution, use the web demo to test more)

The first method finde infinite line that cover all the image, so it can be usefull to find the better perspective lines for example to automatize fSpy (for an application example of usage of fSpy see Open Source Architecture Rendering). The second one find segments of element border that can be used to create a 3D model to texture with the image in few time, and by using also an extimed normal map(for example using XTConsistency some visual results here) we have a good 3D model of a room using low poly meshes.

Conclusion

If we need to solve simple task like to detect line of a table or a sudoku or a tennis field, with specific condition of light that do not variate much the computer vision approach is your solution: simple, controllable and no much computational power is needed.

But for more variable task like to find the perspective for a real world image the neural models can perform better and can receive more generalized images.

The deep learning solve the problems into a more generalized way and that is very good, but if you have a simple problem solve it simple KISS!

Note that everything I’ve shown above is open source, so use it and contribute!

--

--

Nicola Landro

Linux user and Open Source fun. Deep learning PhD. , Full stack web developer, Mobile developer, cloud engineer and Musitian.