Skip to content

Commit 12401f2

Browse files
committed
improve
1 parent d0f011a commit 12401f2

File tree

12 files changed

+265
-0
lines changed

12 files changed

+265
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

tutorials/.DS_Store

0 Bytes
Binary file not shown.

tutorials/Image_Processing/.DS_Store

0 Bytes
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Hough Line Transform
2+
"Hough Transform" is a method to detect any shape with a mathematical shape equation, here describe how hough transform work with line detection.
3+
4+
* Provide Hough Line Transform implement from scatch. [(Code)]
5+
6+
## Outline
7+
- Hough Line Transform
8+
- Probabilistic Hough Line Transform
9+
10+
## Step of the process
11+
* (1) Find edges from the source image
12+
* (2) Generate a matrix by 'theta' and 'rho'
13+
* (3) Convert (x, y) Coordinate System to (theta, rho) Coordinate System
14+
* (4) Use threshold to select the Line
15+
16+
### Step 1. Find edges from the source image
17+
18+
* Read image and convert to grayscale using cv2.cvtColor().
19+
* Get edge image by using Canny Edge Detection, cv2.Canny().
20+
21+
![](README_IMG/step1.png)
22+
23+
### Step 2. Generate a matrix by "theta" and "rho"
24+
25+
* General line equation: y = ax+b
26+
* Hough Line Transform use another line representation: x * cos(theta)+y * sin(theta) = rho
27+
28+
![](README_IMG/step2.png)
29+
30+
### Step 3. Convert (x, y) Coordinate System to (theta, rho) Coordinate System
31+
Calculate (theta, rho) for each edge pixel (x, y) then add 1 to the matrix (theta, rho).
32+
33+
![](README_IMG/step3.png)
34+
35+
### Step 4. Use threshold to select the Line
36+
Get the several high value in matrix then use that 'theta' and 'rho' value to draw the line.
37+
38+
```python
39+
a = np.cos(theta) x0 = a * rho
40+
b = np.sin(theta) y0 = b * rho
41+
42+
x1 = int(x0 + 1000*(-b))
43+
y1 = int(y0 + 1000*(a))
44+
x2 = int(x0 - 1000*(-b))
45+
y2 = int(y0 - 1000*(a))
46+
47+
Line: (x1, y1), (x2, y2)
48+
```
49+
50+
### Useful link:
51+
52+
- [Changing Colorspace](https://github.com/Hank-Tsou/Computer-Vision-OpenCV-Python/tree/master/tutorials/Image_Processing/1_Changing_colorspace)
53+
- [Canny Edge Detection](https://github.com/Hank-Tsou/Computer-Vision-OpenCV-Python/tree/master/tutorials/Image_Processing/6_Canny_Edge_Detection)
54+
- [Youtube: How Hough Transform works](https://www.youtube.com/watch?v=4zHbI-fFIlI)
55+
56+
## Hough Line Transform
57+
```
58+
- File name: Hough_Line_Transform.py
59+
- Input image: pool.jpg
60+
- Command Line: python Hough_Line_Transform.py -i pool.jpg
61+
```
62+
63+
* Main Function: lines = cv2.HoughLines(src_img, rho, theta, threshold)
64+
```python
65+
* rho: Distance resolution of the accumulator in pixels.
66+
* theta: Angle resolution of the accumulator in radians. (np.pi/180)
67+
* threshold: Accumulator threshold, line selection.
68+
```
69+
```
70+
Draw Line: cv2.line(src_img, (x1,y1), (x2,y2), Color, thickness)
71+
```
72+
## Probabilistic Hough Line Transform
73+
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. --(OpenCV-Python documentation)
74+
75+
* Main Function: lines = cv2.HoughLinesP(image, rho, theta, threshold, minLineLength, maxLineGap)
76+
```python
77+
* minLineLength: Minimum line length. Line segments shorter than that are rejected.
78+
* maxLineGap: Maximum allowed gap between points on the same line to link them.
79+
```
80+
```
81+
NOTE: This method returns the two endpoints of lines. HoughLines() only return the parameters of lines.
82+
```
83+
84+
![](README_IMG/line.png)
85+
86+
## Code
87+
- [Hough Line Transform](https://github.com/Hank-Tsou/Computer-Vision-OpenCV-Python/tree/master/tutorials/Image_Processing/11_Hough_Line_Transform)
88+
89+
## Improvements
90+
91+
## License
92+
93+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
94+
95+
## Reference & Acknowledgments
96+
97+
* OpenCV-Python Tutorial: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html
98+
* (Hough Line) https://docs.opencv.org/2.4/modules/imgproc/doc/feature_detection.html?highlight=houghlines
99+
* (Draw Line) https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
#------------------------------------#
3+
# Author: Yueh-Lin Tsou #
4+
# Update: 7/25/2019 #
5+
# E-mail: [email protected] #
6+
#------------------------------------#
7+
8+
"""-----------------------------------------
9+
Improve hough line transform on pool table
10+
------------------------------------------"""
11+
"""
12+
Improvement: Use color range to select the target, function table_selection()
13+
"""
14+
15+
import numpy as np # use numpy library as np for array object
16+
import cv2 # opencv-python
17+
18+
# segment the pool table used color information
19+
def table_selection(image):
20+
boundary = [ ([60, 60, 0], [190, 150, 55])] # color boundary for the pool table
21+
22+
# create NumPy arrays from the boundary
23+
for (lower, upper) in boundary:
24+
lower = np.array(lower, dtype = "uint8")
25+
upper = np.array(upper, dtype = "uint8")
26+
27+
mask = cv2.inRange(image, lower, upper) # create the mask for the selected region
28+
result = cv2.bitwise_and(image, image, mask = mask) # remain only the pool table
29+
30+
return result # return table selection
31+
32+
# Canny Edge Detection
33+
def edge_detection(image):
34+
gray = cv2.cvtColor(table_img, cv2.COLOR_BGR2GRAY) # convert image in to grayscale
35+
gray = cv2.GaussianBlur(gray,(3, 3),0) # use gaussian blur
36+
edged = cv2.Canny(gray,50,150,apertureSize = 3) # Canny Edge Detection
37+
return edged # return edge image
38+
39+
# Hough Lines Detection (with "cv2.HoughLines")
40+
def hough_line(image, edge_img):
41+
# Call function "cv2.HoughLines" and adjust the parameter for the function
42+
line = cv2.HoughLines(edge_img,rho=2,theta=np.pi/180, threshold=170)
43+
44+
# calsulate the line point use "theta" and "rho" value
45+
46+
# Parameters ---------------------------------------------#
47+
# rho – Distance resolution of the accumulator in pixels. #
48+
# theta – Angle resolution of the accumulator in radians. #
49+
#---------------------------------------------------------#
50+
51+
for i in range(len(line)): # calculate points for each line and draw the four lines
52+
for rho,theta in line[i]:
53+
a, b = np.cos(theta), np.sin(theta) # calculate slop scale
54+
x, y = a*rho, b*rho # calculate one point (x0, y0) on the line
55+
x1, y1 = int(x + 600*(-b)), int(y + 600*(a)) # calculate two end points of the line
56+
x2, y2 = int(x - 1500*(-b)), int(y - 1500*(a)) # calculate two end points of the line
57+
58+
cv2.line(image,(x1,y1),(x2,y2),(0,255,255),1) # draw line on the image
59+
60+
return image # return image with four lines for the pool table
61+
62+
63+
# -------------------------- main -------------------------- #
64+
if __name__ == '__main__':
65+
# command >> python table_color.py
66+
67+
image = cv2.imread('pool.jpg') # read image
68+
table_img = table_selection(image) # segment the pool table used color information
69+
edge_img = edge_detection(table_img) # Canny Edge Detection
70+
HL_line_img = hough_line(image, edge_img) # Hough Lines Detection
71+
72+
# show result image
73+
cv2.imshow("images", HL_line_img)
74+
cv2.waitKey(0) # system pause, press esc to exit
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
#------------------------------------#
3+
# Author: Yueh-Lin Tsou #
4+
# Update: 7/25/2019 #
5+
# E-mail: [email protected] #
6+
#------------------------------------#
7+
8+
"""-----------------------------------------
9+
Improve hough line transform on pool tabke
10+
------------------------------------------"""
11+
"""
12+
Improvement: Use target mask to select the target, function table_selection(), largest_contours()
13+
"""
14+
15+
import numpy as np # use numpy library as np for array object
16+
import cv2 # opencv-python
17+
18+
# segment the pool table used color information
19+
def table_selection(ts_image):
20+
boundaries = [ ([60, 60, 0], [190, 150, 35])] # color boundary for the pool table
21+
22+
# create NumPy arrays from the boundary
23+
for (lower, upper) in boundaries:
24+
lower = np.array(lower, dtype = "uint8")
25+
upper = np.array(upper, dtype = "uint8")
26+
27+
ts_mask = cv2.inRange(ts_image, lower, upper) # create the mask for the selected region
28+
result = cv2.bitwise_and(ts_image, ts_image, mask = ts_mask) # remain only the pool table
29+
30+
return result, ts_mask # return table selection and mask
31+
32+
# Canny Edge Detection
33+
def edge_detection(image):
34+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert image in to grayscale
35+
gray = cv2.GaussianBlur(gray,(3, 3),0) # use gaussian blur
36+
edged = cv2.Canny(gray,50,150,apertureSize = 3) # Canny Edge Detection
37+
return edged # return edge image
38+
39+
# Hough Lines Detection (with "cv2.HoughLines")
40+
def hough_line(image, edge_img):
41+
# Call function "cv2.HoughLines" and adjust the parameter for the function
42+
line = cv2.HoughLines(edge_img,rho=2,theta=np.pi/180, threshold=150)
43+
p, q, r = line.shape
44+
# calsulate the line point use "theta" and "rho" value
45+
46+
# Parameters ---------------------------------------------#
47+
# rho – Distance resolution of the accumulator in pixels. #
48+
# theta – Angle resolution of the accumulator in radians. #
49+
#---------------------------------------------------------#
50+
51+
for i in range(p): # calculate points for each line and draw the four lines
52+
for rho,theta in line[i]:
53+
a, b = np.cos(theta), np.sin(theta) # calculate slop scale
54+
x, y = a*rho, b*rho # calculate one point (x0, y0) on the line
55+
x1, y1 = int(x + 600*(-b)), int(y + 600*(a)) # calculate two end points of the line
56+
x2, y2 = int(x - 1500*(-b)), int(y - 1500*(a)) # calculate two end points of the line
57+
58+
cv2.line(image,(x1,y1),(x2,y2),(0,255,255),1) # draw line on the image
59+
60+
return image # return image with four lines for the pool table
61+
62+
# select the largest pool table
63+
def largest_contours(ori_image, lc_image, lc_mask):
64+
ret,thresh = cv2.threshold(lc_mask, 40, 255, 0) # convert the mask image into black and white
65+
66+
# find contours
67+
im,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
68+
69+
if len(contours) != 0: # if has contour
70+
c = max(contours, key = cv2.contourArea) # find the largest contour area
71+
72+
mask = np.zeros(ori_image.shape, dtype='uint8') # create a empty mask
73+
mask = cv2.drawContours(mask, [c], -1, (225,225,225), -1) # create a mask for the largest pool table
74+
75+
gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) # convert the mask into grayscale
76+
output = cv2.bitwise_and(ori_image, ori_image, mask=gray) #create the image only the largest pool table
77+
78+
return output, mask # return the largest pool table image and it's mask
79+
80+
# -------------------------- main -------------------------- #
81+
if __name__ == '__main__':
82+
# command >> python table_mask.py
83+
84+
ori_image = cv2.imread('pool.jpg') # read image
85+
table_img, table_img_mask = table_selection(ori_image) # segment the pool table used color information
86+
seg_obj, obj_mask = largest_contours(ori_image, table_img, table_img_mask) # slect the largest pool table
87+
edge_img = edge_detection(seg_obj) # Canny Edge Detection
88+
line_img = hough_line(ori_image, edge_img) # Hough Lines Detection
89+
90+
# show result image
91+
cv2.imshow("images", line_img)
92+
cv2.waitKey(0) # system pause, press esc to exit
Binary file not shown.

0 commit comments

Comments
 (0)