Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b4d4608

Browse files
Line detection
1 parent 08b7bdd commit b4d4608

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import cv2
2+
import numpy as np
3+
from tkinter.filedialog import *
4+
import tkinter as tk
5+
6+
window=tk.Tk()
7+
window.title("Line detection using Hough transform")
8+
window.geometry('380x100')
9+
label = tk.Label(window, text="Choose an option").grid(row=0,column=0)
10+
# displaying a menu window for choosing transforms
11+
12+
def hough():
13+
photo = askopenfilename()
14+
img = cv2.imread(photo)
15+
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
16+
edges = cv2.Canny(gray,50,150,apertureSize=3)
17+
18+
lines = cv2.HoughLines(edges,1,np.pi/180,500)
19+
for line in lines:
20+
rho,theta = line[0]
21+
# defining lines with the parameters returned by HoughLines()
22+
a=np.cos(theta)
23+
b=np.sin(theta)
24+
x0 = a*rho
25+
y0 = b*rho
26+
x1 = int(x0 + 1000*(-b))
27+
y1 = int(y0 + 1000*(a))
28+
x2 = int(x0 - 1000*(-b))
29+
y2 = int(y0 - 1000*(a))
30+
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),4) # drawing lines on image to mark
31+
cv2.imwrite('houghlines.jpg',img)
32+
cv2.imshow("houghlines",img)
33+
cv2.waitKey(5000)
34+
cv2.destroyAllWindows()
35+
36+
def houghP():
37+
photo = askopenfilename()
38+
img = cv2.imread(photo)
39+
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
40+
edges = cv2.Canny(gray,50,150,apertureSize=3)
41+
42+
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=100,maxLineGap=10)
43+
for line in lines:
44+
x1,y1,x2,y2 = line[0] # endpoints are returned by HoughLinesP()
45+
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),4) #drawing lines by joining those
46+
cv2.imwrite('houghlinesP.jpg',img)
47+
cv2.imshow("houghlinesP",img)
48+
cv2.waitKey(5000)
49+
cv2.destroyAllWindows()
50+
51+
52+
rad1 = tk.Radiobutton(window,text='Hough Transform', value=1, command=hough)
53+
rad2 = tk.Radiobutton(window,text='Probabilistic Hough Transform', value=2, command=houghP)
54+
55+
rad1.grid(row=1,column=0)
56+
rad2.grid(row=2,column=0)
57+
58+
label = tk.Label(window, text="Check the output image in this folder you are working in").grid(row=3,column=0)
59+
60+
window.mainloop()

‎Line_Detection_HoughLine/ReadMe.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Line detection
2+
This python script will allow us to identify lines in an image depending on the user's choice to select one of the two options: HoughTransform and Probabilistic HoughTransform
3+
4+
## Setup Instructions
5+
### Install python3
6+
sudo apt-get install python3
7+
### Install pip (package installer for python)
8+
sudo apt-get install python3-pip
9+
### Install Numpy library with pip
10+
pip3 install numpy
11+
### Install OpenCV library with pip
12+
pip3 install opencv-python
13+
### Install tkinter library
14+
sudo apt-get install python3-tk
15+
16+
## Details/Output
17+
A dialog box appears with two transform options, hough transform and probabilistic hough transform.
18+
Selecting any one option prompts user to select an image and detects lines in it using the selected transform.
19+
The output image is written/stored in the current folder.
20+
21+
## Author
22+
Github: invigorzz313

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /