欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2022-07-23 09:59:50|栏目:Python代码|点击:

效果展示

中心的三个没检测到

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中partial()基础用法说明

栏    目:Python代码

下一篇:利用pyecharts绘制地理散点图

本文标题:Python+OpenCV实现图片中的圆形检测

本文地址:http://www.codeinn.net/misctech/208615.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有