如何在使用霍夫圆(无轮廓)检测到的圆上绘制矩形?
How to draw a rectangle over a circle detected using Hough circle (without contours)?
我想在圆上画一个矩形,这是我在人眼图片上使用霍夫圆函数得到的。要绘制一个矩形,我需要根据 cv2.rectange 的语法得到边 x 坐标、边 y 坐标、高度和宽度。但是霍夫圆给了我 center-x, center-y, radius 作为输出。有没有办法通过操纵霍夫圆输出来绘制矩形?我有点无能为力,我找不到任何关于类似请求的明确解释...注意:我没有使用轮廓,因为它 suitable/working 不适合我的输入
提前致谢!
代码如下:
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
只需对你的圆做数学运算,找到与照片平行的矩形(正方形)(你可以支持旋转的矩形,涉及更多数学)。
X_min_rect = X_circle - R
Y_min_rect = Y_circle - R
其中 R 是圆的半径。
宽度和高度等于 2*R
我想在圆上画一个矩形,这是我在人眼图片上使用霍夫圆函数得到的。要绘制一个矩形,我需要根据 cv2.rectange 的语法得到边 x 坐标、边 y 坐标、高度和宽度。但是霍夫圆给了我 center-x, center-y, radius 作为输出。有没有办法通过操纵霍夫圆输出来绘制矩形?我有点无能为力,我找不到任何关于类似请求的明确解释...注意:我没有使用轮廓,因为它 suitable/working 不适合我的输入
提前致谢!
代码如下:
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
只需对你的圆做数学运算,找到与照片平行的矩形(正方形)(你可以支持旋转的矩形,涉及更多数学)。
X_min_rect = X_circle - R
Y_min_rect = Y_circle - R
其中 R 是圆的半径。
宽度和高度等于 2*R