Hough transform for line detection helps to find lines in an image even if the points are not connected and the lines are not perfectly straight. It works with a voting procedure where each point in the image votes for all the possible lines that pass through it. The line with more votes corresponds to the most likely one.
In the following applet you can draw anything on the left and see the transformed image on the right. Each point in the original image contributes to many points (a curve) in the transformed image, and each point in the transformed image represents a straight line in the original image (you can move the mouse there to see it). The brightest points in the transformed image are likely to be caused by a straight line in the original image.
If you don't have the Java plug-in you can watch a video of it
If you draw a line, it corresponds to the brightest point of the Hough transform:
In fact, for each line two points are detected, this is because we are plotting degrees from 0 to 360, but plotting from 0 to 180 would be enough. In other words, you can ignore half of the transformed image.
In this transform the horizontal axis corresponds to the angle of the line and the vertical axis corresponds to its distance from the center of the image.
Several parallel lines are transformed to points in the same horizontal coordinate:
Even with just a few points, a line can be detected:
Short lines can be detected as well:
and sometimes other lines emerge:
The Hough transform for line detection applied to circles, detects tangent lines:
If you want to apply it to real images, you have to detect the edges first. In the case of the cameraman test image, it detects the tripod:
In noisy images, like this baboon, the noise may hide the real lines of the image:
In a random noise image, the diagonals will be detected, because the diagonals are the longest lines inside the square:
There are many variations of the Hough transform, that are used for circle detection or generic shape detection, but they are more complex and the transformed image (the “parameter space”) has more than two dimensions and it would be difficult to visualize it here.
In OpenCV, the function cvHoughLines2() with the CV_HOUGH_STANDARD mode implements the same line detection that we have been talking about, except that it returns the detected lines and not the transformed image. It can find line segments as well using the CV_HOUGH_PROBABILISTIC mode. The function cvHoughCircles() finds circles of any radius in an image.