Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit e2407f8

Browse files
authored
Merge pull request #281 from salonishah01/Hough_Transform
Hough transform #9-3
2 parents 0fbc003 + af63b88 commit e2407f8

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import cv2
4+
image=cv2.imread('image.jfif')
5+
img=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
6+
edges = cv2.Canny(img,100,200)
7+
8+
def hough_line(edges):
9+
theta= np.arange(0,180,1) ## Theta 0 - 180 degree
10+
cos = np.cos(np.deg2rad(theta))
11+
sin = np.sin(np.deg2rad(theta))
12+
13+
# Generate a accumulator matrix to store the values
14+
rho_range = int(np.hypot(edges.shape[0] , edges.shape[1])) #max value of rho size =2*diagonal_length
15+
accumulator = np.zeros((2 * rho_range, len(theta)), dtype=np.uint8)
16+
17+
# Threshold to get edges pixel location (x,y)
18+
edge_pixels = np.where(edges == 255) #returns b bool matrix (True if edge_point=255; False is edge_point!=255)
19+
coordinates = list(zip(edge_pixels[0], edge_pixels[1])) #to access the edge points
20+
21+
# Calculate rho value for each edge location (x,y) with all the theta range
22+
for p in range(len(coordinates)):
23+
for t in range(len(theta)):
24+
rho = int(round(coordinates[p][1] * cos[t] + coordinates[p][0] * sin[t])) #compute rho=x*cost+y*sint
25+
accumulator[rho, t] += 1 #increment the accumulator cell
26+
return accumulator
27+
28+
accumulator=hough_line(edges)
29+
30+
# Function to do hough line transform\
31+
32+
#print(len(accumulator>=90)
33+
# Threshold some high values then draw the line
34+
edge_pixels = np.where(accumulator > 75)
35+
36+
coordinates = list(zip(edge_pixels[0], edge_pixels[1]))
37+
38+
# Use line equation to draw detected line on an original image
39+
for i in range(0, len(coordinates)):
40+
a = np.cos(np.deg2rad(coordinates[i][1]))
41+
b = np.sin(np.deg2rad(coordinates[i][1]))
42+
x0 = a*coordinates[i][0]
43+
y0 = b*coordinates[i][0]
44+
45+
x1 = int(x0 + 255*(-b)) #here 255(any constant) is multiplied to strech the lines over the edges
46+
y1 = int(y0 + 255*(a))
47+
x2 = int(x0 - 255*(-b))
48+
y2 = int(y0 - 255*(a))
49+
50+
51+
cv2.line(image,(x1,y1),(x2,y2),(0,255,0),1)
52+
53+
cv2.imwrite('Linedetection_without_opencv.png',image)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import cv2
2+
import numpy as np
3+
img = cv2.imread("image.jfif")
4+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
5+
edges = cv2.Canny(gray, 75, 150)
6+
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 30, maxLineGap=250)
7+
for line in lines:
8+
x1, y1, x2, y2 = line[0]
9+
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 128), 1)
10+
cv2.imwrite('lines__detected.png',img)
10.7 KB
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
HOUGH TRANSFORM
2+
3+
Hough transform can be used to detect lines, circles or other parametric curves. The goal is to find the location of lines in images. Hough transform can detect lines, circles and other structures if their parametric equation is known. It can give robust detection under noise and partial occlusion It can give robust detection under noise and partial occlusion.
4+
5+
Algorithm
6+
7+
1. Perform Edge detection on the original image by suitable edge detection technique.
8+
2. Decide on the range of ρ and θ. Often, the range of θ is [ 0, 180 ] degrees and ρ is [ -d,d ] where d is the length of the edge image’s diagonal.
9+
3. Create a 2D array called the accumulator representing the Hough Space with dimension (num_rhos, num_thetas) and initialize all its values to zero.
10+
4. For every pixel on the edge image, check whether the pixel is an edge pixel. If it is an edge pixel, loop through all possible values of θ, calculate the corresponding ρ, find the θ and ρ index in the accumulator, and increment the accumulator base on those index pairs.
11+
5. Loop through all the values in the accumulator. If the value is larger than a certain threshold, get the ρ and θ index, get the value of ρ and θ from the index pair which can then be converted back to the form of y=ax+b.
12+
13+
14+
INPUT IMAGE:
15+
16+
![image](https://user-images.githubusercontent.com/46890827/94119467-9486b280-fe6c-11ea-9a52-e0bc50e14df2.png)
17+
18+
OUTPUT IMAGE:
19+
20+
![image](https://user-images.githubusercontent.com/46890827/94119579-bc761600-fe6c-11ea-8309-f606768b1c87.png)
21+

0 commit comments

Comments
 (0)