I want to draw a rectangle over a circle which I obtained using the Hough circle function over a picture of a human eye. To draw a rectangle I need edge-x-coord, edge-y-coord, height and width as per the syntax of cv2.rectange. But Hough circle gives me center-x, center-y, radius as output. Is there a way to draw a rectangle by manipulating Hough circle outputs? Im kinda clueless what to do about it and I cant find any clear explanation on similar requests... Note: I didn't use contours since its not suitable/working well with my inputs
Thanks in advance!
Here is the code:
import cv2
import numpy as np
#show image
def display_image(name,current_image):
cv2.imshow(name,current_image)
cv2.waitKey(0)
def image_processing(current_image):
#Grayscaling
grayscaled_image = cv2.cvtColor(current_image, cv2.COLOR_BGR2GRAY)
#display_image("Gray",grayscaled_image)
#Inverting
inverted_image = cv2.bitwise_not(grayscaled_image)
#display_image("Invert",inverted_image)
#Removing Reflection
kernel = np.ones((5, 5), np.uint8)
blackhat_image = cv2.morphologyEx(inverted_image,cv2.MORPH_BLACKHAT,kernel)
#display_image("Backhat",blackhat_image)
removed_refection = cv2.addWeighted(src1=inverted_image,alpha=0.5,src2=blackhat_image,beta=0.5,gamma=0)
#display_image("Removed reflection",removed_refection)
image_without_reflection = cv2.medianBlur(removed_refection, 5)
#display_image("No reflection",image_without_reflection)
#Thresholding
_,thresholded_image= cv2.threshold(image_without_reflection,100,255,cv2.THRESH_BINARY)
#display_image("Thresholded",thresholded_image)
#Canny
region_of_interest = cv2.bitwise_not(thresholded_image)
canny_image = cv2.Canny(region_of_interest, 200, 100)
return canny_image
def iris_detection(image):
circles = cv2.HoughCircles(processed_image, cv2.HOUGH_GRADIENT, 1, 20, param1 = 200, param2 = 20, minRadius = 0)
if circles is not None:
#Mark circles co-ordinates
inner_circle = np.uint16(np.around(circles[0][0])).tolist()
cv2.circle(current_image, (inner_circle[0], inner_circle[1]), inner_circle[2], (0, 255, 0), 1)
display_image("Final",current_image)
radius = inner_circle[2]*0.2645833333
diameter = radius * 2
print("The Radius of the iris is:",radius,"mm")
print("The Diameter of the iris is:",diameter,"mm")
#input
current_image = cv2.imread("eye.png", 1)
display_image("Original",current_image)
#Image pre-processing
processed_image = image_processing(current_image)
display_image("Processed Image",processed_image)
#Iris Detection using Hough circles
iris_detection(processed_image)
cv2.destroyAllWindows()```
[Input][1][1]: https://i.stack.imgur.com/oKxC0.png
[output][1][1]: https://i.stack.imgur.com/mjypO.png
CodePudding user response:
Just do the math on your circle to find the rectangle(square) parallel to the photo(you can support rotated rectangles with more math involved).
X_min_rect = X_circle - R
Y_min_rect = Y_circle - R
Where R is the radious of the circle.
Width and Height is equal to 2*R