CV Lab7
CV Lab7
Nasihatkon
Spring 2025
Img = cv2.imread('sudoku.jpg')
cv2.imshow("E", E)
cv2.imshow("Img", Img)
cv2.waitKey(0)
cv2.destroyAllWindows()
● Would a very fine angle resolution (e.g., np.pi/1000) improve results? Why or
why not?
● What kind of lines (strong/weak edges) are prioritized when min_votes is
high?
File: hough_circle.py
import numpy as np
import cv2
I = cv2.imread('coffee.jpg')
G = cv2.cvtColor(I,cv2.COLOR_BGR2GRAY) # -> Grayscale
G = cv2.GaussianBlur(G, (3,3), 0); # Gaussian blur
canny_high_threshold = 200
min_votes = 100 # minimum no. of votes to be considered as a circle
min_centre_distance = 40 # minimum distance between the centers of detected circles
resolution = 1 # resolution of parameters (centre, radius) relative to image resolution
circles = cv2.HoughCircles(G,cv2.HOUGH_GRADIENT,
resolution,min_centre_distance,
param1=canny_high_threshold,
param2=min_votes,minRadius=0,maxRadius=100)
for c in circles[0,:]:
x = c[0] # x coordinate of the centre
y = c[1] # y coordinate of the centre
r = c[2] # radius
# draw the circle
cv2.circle(I,(x,y), r, (0,255,0),2)
# draw the circle center
cv2.circle(I,(x,y),2,(0,0,255),2)
cv2.imshow("I",I)
cv2.waitKey(0)
cv2.destroyAllWindows()
● Change the parameters of cv2.HoughCircles and see how each of them
affect detection.
● Can you explain why some circles disappear when min_votes is increased?
● What if you set it to a very large value (e.g., 100)?
● How does changing canny_high_threshold affect edge detection before circle
detection?
● How does setting minRadius=20 and maxRadius=50 change the output
compared to minRadius=0 and maxRadius=100?
Fundamentals of Computer Vision (Undergrad) - B. Nasihatkon
Spring 2025
Write a piece of code to perform this task using a hough circle transform. Change the
file task1.py to perform the task. Play with the parameters until you get the desired
results.
File: task1.py
import numpy as np
import cv2
I = cv2.imread('coins.jpg')
G = cv2.cvtColor(I,cv2.COLOR_BGR2GRAY)
G = cv2.GaussianBlur(G, (5,5), 0);
canny_high_threshold = 160
min_votes = 30 # minimum no. of votes to be considered as a circle
min_centre_distance = 40
circles = np.array([[10,10]])
for c in circles[0,:]:
x = 100
y = 100
r = 40
cv2.circle(I,(x,y), r, (0,255,0),2)
print(circles.shape)
n = 100
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(I,'There are %d coins!'%n,(400,40), font, 1,(255,0,0),2)
cv2.imshow("I",I)
cv2.waitKey(0)
References
● Hough Line Transform
● Hough Circle Transform