我想要在圆圈上画一个矩形,,我用Hough圆函数在人眼的图片上画了一个矩形。要绘制矩形,我需要边-x-coord,边-y-coord,高度和宽度,按照cv2.矩形的语法。但是霍夫圆给了我中心x,中心y,半径作为输出。有没有一种通过操作Hough圆输出来绘制矩形的方法?我有点不知所措,我也找不到任何关于类似要求的明确解释……注:我没有使用等高线,因为它不适合/与我的输入工作良好。
提前感谢!
以下是代码:
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发布于 2022-03-22 11:36:54
只需在你的圆圈上做数学,找到与照片平行的矩形(正方形)(你可以支持旋转的矩形和更多的数学)。
X_min_rect = X_circle -R
Y_min_rect = Y_circle -R
其中R是圆的辐射。
宽度和高度等于2*R
https://stackoverflow.com/questions/71571238
复制相似问题