<jupyter_start><jupyter_text>**Motivation**: We want to find some structured shapes (lines, circles) in a robust way.- We can characterize as its parameters:$$ y = mx+b $$Let's assign line ==> (m,b) Instead of this coefficient space $(m,b)$, we represent a line by a pair of $(r, \theta)$ - $r$ = Normal vector to the line from the origin.- $\theta$ = angle with $r$ and the horizontal axis. This is the **Hough space**. - Lines become points.- Points become sinusoidal curves. **Line detection**:- Get an edge map (detect edges through Canny).- Each point goes to the Hough space.- In the Hough space, "our" lines are intersection of those curves.- A Cartesian line is detected if it is the intersection of sufficiently many of the sinusoidal curves.  Line detection  1. Create edges<jupyter_code>lane_gray = cv2.cvtColor(lane, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(lane_gray, threshold1=100, threshold2 = 500) #Thresholds are from Canny method 
plt.imshow(edges, cmap="gray")<jupyter_output><empty_output><jupyter_text>2. Apply the Hough transform for lines<jupyter_code>lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi/180, threshold=60)
# rho: Resolution (step size) of the r-axis on Hough space
# theta: Step size for the theta-axis
# threshold: number of votes a candidate line should get
# CAREFUL WITH THE P

lines.shape
lane = cv2.imread("../opencv-np-workshop/data/img/lane.jpg")
for l in lines:
    x1, y1, x2, y2 = l[0] # array shape...
    cv2.line(lane, (x1,y1), (x2,y2), (0,255,0), 5)
plt.imshow(lane[:,:,::-1])<jupyter_output><empty_output><jupyter_text>Circle detection- Pass from the cartesian representation of the circle to a parameter space consisting of (center, radius)<jupyter_code>pepsi = cv2.imread("../opencv-np-workshop/data/img/pepsi.PNG")
plt.imshow(pepsi[:,:,::-1])<jupyter_output><empty_output><jupyter_text>1. Pass to gray scale<jupyter_code>pepsi_gray = cv2.cvtColor(pepsi, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(pepsi_gray, cv2.HOUGH_GRADIENT, dp=1.25, minDist=70)
# First parameter: cv2.HOUGH_GRADIENT, only algorithm implemented... 
# dp = Inverse of the step size on the Hough space
# minDist = Minimum distance between detected circles
circles # (1, n_circles,center_x, center_y, radius)
x, y, r = circles[0][0]
cv2.circle(pepsi, (x,y), r, (0,255,255), -1)
plt.imshow(pepsi[:,:,::-1])
circles = cv2.imread("../opencv-np-workshop/data/img/circles.PNG")
plt.imshow(circles[:,:,::-1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13867425782838552, 'fraction_numerical': 0.027653517690117934, 'mean_word_length': 5.259541984732825, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '586079', 'license': 'MIT', 'n_tokens_mistral': 910, 'n_tokens_neox': 875, 'n_words': 264, 'path': 'Day 3 - Hough transform.ipynb', 'repo_name': 'jpmaldonado/opencv-np-workshop', 'size': '2459'}