You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

86 lines
3.5 KiB

import cv2
import numpy as np
from cv2 import waitKey
imgRaw=cv2.imread('images/target/bowa_target/min.jpg',cv2.IMREAD_GRAYSCALE)#images/target/rp80max3.jpg
imgColor=cv2.imread('images/target/bowa_target/min.jpg') #images/target/rp80max3.jpg
ret, binary_img = cv2.threshold(imgRaw, 180, 255, cv2.THRESH_BINARY)
#cv2.imshow('binary_img', binary_img)
def circle_detect(img):
#高斯滤波
#img = cv2.GaussianBlur(img, (3, 3), 1)
#cv2.imshow('gsmh', gaussianBlur)
# 圆心距 canny阈值 最小半径 最大半径
circlesFloat = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT_ALT, 2, 10, param1=50, param2=0.9, minRadius=10, maxRadius=0)
print("==========")
# 创建一个0行, 2列的空数组
if circlesFloat is not None:
num_circles = circlesFloat.shape[1] # 获取检测到的圆的数量
print("圆的数量",num_circles)
# 提取圆心坐标(保留小数)
centers = [(float(x), float(y),float(r)) for x, y, r in circlesFloat[0, :]]
font = cv2.FONT_HERSHEY_SIMPLEX
color = (255, 0, 0) # 蓝色
scale = 1
# 打印圆心坐标
for center3d in centers:
center=(center3d[0], center3d[1])
# center_txt=f"{float(center[0]),float(center[1])}"
text=f"center:{center},r:{center3d[2]}"
print(text)
centerInt=tuple(int(x) for x in center)
cv2.putText(img, text, centerInt, font, scale, color,2)
circles = np.uint16(np.around(circlesFloat)) # 4舍5入, 然后转为uint16
for i in circles[0, :]:
print("画圆", i)
# 绘制圆心
center=(i[0], i[1])
cv2.circle(img, center, 2, (0, 255, 0), 6)
# 绘制外圆
cv2.circle(img, center, i[2], (0, 0, 255), 2)
def circle_detect2(img):
#高斯滤波
#img = cv2.GaussianBlur(img, (3, 3), 1)
#cv2.imshow('gsmh', gaussianBlur)
# 圆心距 canny阈值 最小半径 最大半径
circlesFloat = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT_ALT, 2, 10, param1=50, param2=0.9, minRadius=10, maxRadius=0)
print("==========")
# 创建一个0行, 2列的空数组
if circlesFloat is not None:
num_circles = circlesFloat.shape[1] # 获取检测到的圆的数量
print("圆的数量",num_circles)
# 提取圆心坐标(保留小数)
centers = [(float(x), float(y),float(r)) for x, y, r in circlesFloat[0, :]]
font = cv2.FONT_HERSHEY_SIMPLEX
color = (255, 0, 0) # 蓝色
scale = 1
# 打印圆心坐标
for center3d in centers:
center=(center3d[0], center3d[1])
# center_txt=f"{float(center[0]),float(center[1])}"
text=f"center:{center},r:{center3d[2]}"
print(text)
centerInt=tuple(int(x) for x in center)
cv2.putText(img, text, centerInt, font, scale, color,2)
circles = np.uint16(np.around(circlesFloat)) # 4舍5入, 然后转为uint16
for i in circles[0, :]:
print("画圆", i)
# 绘制圆心
center=(i[0], i[1])
cv2.circle(img, center, 2, (0, 255, 0), 6)
# 绘制外圆
cv2.circle(img, center, i[2], (0, 0, 255), 2)
if __name__ == '__main__':
circle_detect(binary_img)
cv2.imshow('tagCircle', imgColor)
waitKey(0)
cv2.destroyAllWindows()