脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python+OpenCV实现图片中的圆形检测

Python+OpenCV实现图片中的圆形检测

2022-11-24 10:09天人合一peng Python

这篇文章主要介绍了如何利用Python+OpenCV实现检测图片中的圆形,文中的示例代码讲解详细,感兴趣的小伙伴快跟随小编一起学习一下

效果展示

Python+OpenCV实现图片中的圆形检测

Python+OpenCV实现图片中的圆形检测

Python+OpenCV实现图片中的圆形检测

中心的三个没检测到

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import cv2
import numpy as np
import matplotlib.pyplot as plt
 
w = 20
h = 5
params = cv2.SimpleBlobDetector_Params()
# Setup SimpleBlobDetector parameters.
print('params')
print(params)
print(type(params))
 
 
# Filter by Area.
params.filterByArea = True
params.minArea = 10e1
params.maxArea = 10e3
 
params.minDistBetweenBlobs = 25
 
 
# params.filterByColor = True
params.filterByConvexity = False
 
# tweak these as you see fit
# Filter by Circularity
# params.filterByCircularity = False
# params.minCircularity = 0.2
 
# params.blobColor = 0
# # # Filter by Convexity
# params.filterByConvexity = True
# params.minConvexity = 0.87
 
# Filter by Inertia
# params.filterByInertia = True
# params.filterByInertia = False
# params.minInertiaRatio = 0.01
 
 
# img = cv2.imread("circles/circels.jpg",1)
img = cv2.imread("circles/Snap_001.jpg",1)
 
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Detect blobs.
# image = cv2.resize(gray_img, (int(img.shape[1]/4),int(img.shape[0]/4)), 1, 1, cv2.INTER_LINEAR)
# image = cv2.resize(gray_img, dsize=None, fx=0.25, fy=0.25, interpolation=cv2.INTER_LINEAR)
minThreshValue = 120
_, gray = cv2.threshold(gray, minThreshValue, 255, cv2.THRESH_BINARY)
gray = cv2.resize(gray, dsize=None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
# plt.imshow(gray)
# cv2.imshow("gray",gray)
 
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(gray)
 
print(len(keypoints))
 
 
fig = plt.figure()
# im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (0, 0, 255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (0, 0, 255),  cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
 
plt.imshow(cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB),interpolation='bicubic')
fname = "key points"
titlestr = '%s found %d keypoints' % (fname, len(keypoints))
plt.title(titlestr)
plt.show()
 
# cv2.imshow("graykey",gray)
# cv2.waitKey()
 
fig.canvas.set_window_title(titlestr)
 
ret, corners = cv2.findCirclesGrid(gray, (w, h), flags=(cv2.CALIB_CB_SYMMETRIC_GRID + cv2.CALIB_CB_CLUSTERING ), blobDetector=detector )
if corners is not None:
    cv2.drawChessboardCorners(img, (w, h), corners, corners is not None)
    print("find blob")
# # cv2.imshow('findCorners', img)
# cv2.waitKey()
    plt.imshow(img)
plt.show()

以上就是Python+OpenCV实现图片中的圆形检测的详细内容,更多关于Python OpenCV圆形检测的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/moonlightpeng/article/details/124002491

延伸 · 阅读

精彩推荐