# Hough Transform

## Theory

A line can be represented as π¦ = ππ₯ + π or in parametric form, as π = π₯ cos π + π¦ sin π where π is the perpendicular distance from origin to the line, and π is the angle formed by this perpendicular line and horizontal axis measured in counter-clockwise ( That direction varies on how you represent the coordinate system. This representation is used in OpenCV). Check below image:

So if line is passing below the origin, it will have a positive rho and angle less than 180. If it is going above the origin, instead of taking angle greater than 180, angle is taken less than 180, and rho is taken negative. Any vertical line will have 0 degree and horizontal lines will have 90 degree.

Now letβs see how Hough Transform works for lines. Any line can be represented in these two terms, (π, π). So first it creates a 2D array or accumulator (to hold values of two parameters) and it is set to 0 initially. Let rows denote the π and columns denote the π. Size of array depends on the accuracy you need. Suppose you want the accuracy of angles to be 1 degree, you need 180 columns. For π, the maximum distance possible is the diagonal length of the image. So taking one pixel accuracy, number of rows can be diagonal length of the image.

Consider a 100x100 image with a horizontal line at the middle. Take the first point of the line. You know its (x,y) values. Now in the line equation, put the values π = 0, 1, 2, β¦., 180 and check the π you get. For every (π, π) pair, you increment value by one in our accumulator in its corresponding (π, π) cells. So now in accumulator, the cell (50,90) = 1 along with some other cells.

Now take the second point on the line. Do the same as above. Increment the the values in the cells corresponding to (π, π) you got. This time, the cell (50,90) = 2. What you actually do is voting the (π, π) values. You continue this process for every point on the line. At each point, the cell (50,90) will be incremented or voted up, while other cells may or may not be voted up. This way, at the end, the cell (50,90) will have maximum votes. So if you search the accumulator for maximum votes, you get the value (50,90) which says, there is a line in this image at distance 50 from origin and at angle 90 degrees.

Check out this cool animation: http://docs.opencv.org/3.0-beta/_images/houghlinesdemo.gif

This is how hough transform for lines works. It is simple, and may be you can implement it using Numpy on your own. Below is an image which shows the accumulator. Bright spots at some locations denotes they are the parameters of possible lines in the image.

## Now Let us Check out the code

import cv2
import numpy as np


Image is attached in the same folder

img = cv2.imread('sudoku.jpg')
img2=img.copy()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


#### We have used Canny edge detector here. Try using thresholding. For details about thresholding look into the respective tutorial.

edges = cv2.Canny(gray,50,150,apertureSize = 3)


In OpenCV,

##### cv2.HoughLines( )

performs the hough line transform as illustrated above.

It simply returns an array of (π, π) values. π is measured in pixels and π is measured in radians.

##### Its, parameters are:

First parameter, Input image should be a binary image, so we pass the thresholded or edge detected image.

Second and third parameters are π and π accuracies respectively.

##### Pass the image. π accuracy is a natural number. π accuracy is in radians. Threshold is a natural number(set it high to avoid false lines). Pass the required parameters.
lines=cv2.HoughLines(edges,1,np.pi/180,200)


NOTE: lines[0] is an array storing r and theta values for different lines detected in the image We have drawn the detected lines extending to a distance of 1000 above and below the given coordinates( π * cos(π) , π * sin(π))

### EXECUTE THE MARKDOWN CELL BELOW THE CODE CELL.

for rho,theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imwrite('houghlines.jpg',img)

True


## Probabilistic Hough Line Transform

In the hough transform, you can see that even for a line with two arguments, it takes a lot of computation. Probabilistic Hough Transform is an optimization of Hough Transform we saw. It doesnβt take all the points into consideration, instead take only a random subset of points and that is sufficient for line detection. Just we have to decrease the threshold. See below image which compare Hough Transform and Probabilistic Hough Transform in hough space.

This image illustrates Probabilistic Hough Transform better: <img src = houghlines4.png>

##### The function used is cv2.HoughLinesP(). It has two new arguments.

minLineLength - Minimum length of line. Line segments shorter than this are rejected.

maxLineGap - Maximum allowed gap between line segments to treat them as single line.

##### PLEASE NOTE THAT THE THRESHOLD FOR Probabilistic Hough Transform is LOWER. Pass the first 4 parameters as in cv2.HoughLines( ). Pass values to minLineLength and maxLineGap. Both are natural numbers.
minLineLength =
maxLineGap =
linesP = cv2.HoughLinesP()


### EXECUTE THE MARKDOWN CELL BELOW THIS CODE CELL.

for x1,y1,x2,y2 in linesP[0]:
cv2.line()

cv2.imwrite('houghlinesP.jpg',img2)


## HOUGH CIRCLE TRANSFORM

### Similar to a line, a circle can be represented using certain parameters. This drives us towards a transform for an image to detect circles in it.

#### Now we move on to the code:

##### Read the image here (preferably an image with circles) :
cimg = cv2.imread('/assets/images/documentation/computer_vision/Hough_Transform/opencv.png')
img_circ = cv2.cvtColor(cimg,cv2.COLOR_BGR2GRAY)

##### We blur the image for denoising.
img_circ = cv2.medianBlur(img_circ,5)


#### Complete the function to detect circles in the image.

##### Returns:
###### circles β Output vector of found circles. Each vector is encoded as a 3-element floating-point vector (x, y, radius) .
circles = cv2.HoughCircles()


Using np.around(), we round off array of (x,y,radius) to the nearest integer

### EXECUTE THE MARKDOWN CELL BELOW THIS CODE CELL.


circles = np.uint16(np.around(circles))

for i in circles[0,:]:
# draw the outer circle
cv2.circle()
# draw the center of the circle
cv2.circle()
cv2.imwrite('detected_circles.jpg',cimg)


Categories: