我想在人眼圖片上使用霍夫圓函式獲得的圓上繪制一個矩形。根據 cv2.rectange 的語法,要繪制一個矩形,我需要 edge-x-coord、edge-y-coord、height 和 width。但是霍夫圓給了我中心-x,中心-y,半徑作為輸出。有沒有辦法通過操縱霍夫圓輸出來繪制一個矩形?我有點不知道該怎么做,我找不到任何關于類似請求的明確解釋......注意:我沒有使用輪廓,因為它不適合/與我的輸入配合得很好
提前致謝!
這是代碼:
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
uj5u.com熱心網友回復:
只需在你的圓圈上進行數學運算即可找到與照片平行的矩形(正方形)(您可以支持涉及更多數學運算的旋轉矩形)。
X_min_rect = X_circle - R
Y_min_rect = Y_circle - R
其中R是圓的半徑。
寬度和高度等于 2*R
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447012.html
標籤:Python opencv 计算机视觉 霍夫变换 虹膜识别
