We will see how the Hough transform works for line detection using the Hough transform method. To apply the Houghline method, it is desirable to first detect the edge of a specific image. For edge detection technique go to Edge detection

** Basics of the Houghline method **

The line can be represented as y = mx + c or in parametric form, as r = xcosθ + ysinθ, where r — the perpendicular distance from the origin to the line, and θ — the angle formed by this perpendicular line and the horizontal axis, measured in the meter. — clockwise (this direction depends on how you represent the coordinate system. This representation is used in OpenCV).

Thus, any line can be represented in these two terms (r, θ).

** The Halfline method works: **

- It first creates a two-dimensional array or accumulator (to hold the values of two parameters) and is initially set to zero.
- Let the rows denote r and the columns — (θ) theta.
- The size of the array depends on the required precision. Suppose you want 1 degree angle precision, you need 180 columns (the maximum degree for a straight line is 180).
- For r, the maximum possible distance is — this is the diagonal length of the image. Thus, with an accuracy of one pixel, the number of lines can be the diagonal length of the image.

** Example: **

Consider a 100 × 100 image with a horizontal line in the middle … Take the first point of the line. You know its (x, y) values. Now in a linear equation put the values θ (theta) = 0,1,2,…, 180 and check the r you get. For each pair (r, 0), the value is incremented by one in the accumulator in the corresponding cells (r, 0). So now in the battery cell (50.90) = 1 along with some other cells.

Now take the second point on the line. Do the same as above. Increase the values in the cells corresponding to (r, 0) you got. This time, cell (50.90) = 2. We`re actually voting for (r, 0) values. You continue this process for every point on the line. At each point, the cell (50.90) will be increased or scored, while other cells may or may not be voted on. Thus, at the end, cell (50.90) will receive the maximum number of votes. So if you search the accumulator for the maximum number of votes, you will get the value (50.90), which says there is a line in this image at a distance of 50 from the origin and at an angle of 90 degrees.

Everything explained above is encapsulated in an OpenCV function .HoughLines (). It just returns an array of (r, 0) values. r is measured in pixels, and 0 — in radians.

```
```
```
``` |

** Function development (cv2.HoughLines (edge, 1, np.pi / 180, 200)) : **

- The first parameter, "Input Image", must be a binary image, so apply the threshold boundary definition before finding applying a rough transformation.
- The second and third parameters — they are precision r and θ (theta) respectively.
- The fourth argument — it is a threshold that means the minimum number of votes he must receive to be considered a line.
- Remember that the number of votes depends on the number of dots on the line. Thus, it represents the minimum line length to be detected.

** Summing up the process **

- In the context of image analysis, the coordinates of the point (s) of the edge segments (i.e., X, Y) in the image are known and therefore serve as constants in the parametric line equation, while R (rho) and Theta ( θ) are the unknown variables we are looking for.
- If we plot the possible values (r) defined by each (theta), points in Cartesian space are mapped to curves (i.e. sinusoids) in the space of Hough polar parameters. This point-to-curve transform is the Hough transform for straight lines.
- The transform is implemented by quantizing the Hough parameter space into finite bins or battery cells. When the algorithm runs, each (X, Y) is converted to a sampled (r, 0) curve, and the accumulator cells (2D matrices) that lie along this curve are enlarged.
- The resulting peaks in the accumulator array are compelling proof that the corresponding straight line exists in the image.

** Applying the Hough transform: **

- It is used to highlight elements of a certain shape in the image.
- Allows gaps in the description of object boundaries and is relatively independent of image noise.
- Widely used in scanning, validation and barcode recognition

This article is courtesy of ** Pratima Upadhyay **. If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

X
# Submit new EBook