基于梯度的圆检测python代码,随机霍夫圆变换python代码及识别准确率分析python代码
时间: 2023-11-12 18:11:49 浏览: 58
基于梯度的圆检测Python代码:
```python
import cv2
import numpy as np
img = cv2.imread('circle.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
随机霍夫圆变换Python代码:
```python
import cv2
import numpy as np
img = cv2.imread('circle.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
识别准确率分析Python代码:
```python
import cv2
import numpy as np
img = cv2.imread('circle.png',0)
img = cv2.medianBlur(img,5)
# 使用霍夫圆变换检测圆
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
# 如果检测到圆
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
# 计算识别准确率
total_pixels = img.shape[0] * img.shape[1]
circle_pixels = np.sum(img == 255)
accuracy = circle_pixels / total_pixels * 100
print('识别准确率:{}%'.format(accuracy))
cv2.imshow('detected circles',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文