python opencv 打开相机检测圆点_ide

 原图

python opencv 打开相机检测圆点_opencv_02

 结果

# coding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt


def mainFigure(img):
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


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()
# opencv
im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (255, 0, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# plt
# im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (0, 0, 255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
color_img = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB)

# plt.imshow(color_img,interpolation='bicubic')
# fname = "key points"
# titlestr = '%s found %d keypoints' % (fname, len(keypoints))
# plt.title(titlestr)
# fig.canvas.set_window_title(titlestr)
# plt.show()

cv2.imshow('findCorners', color_img)
cv2.waitKey()


def main(img, fig):

params = cv2.SimpleBlobDetector_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


gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Detect blobs.
# cv2.imshow('findCorners', img)
# cv2.waitKey(1)
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)

if keypoints is not None:
# print(len(keypoints))

# opencv
im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (255, 0, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# plt
# im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (0, 0, 255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

color_img = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB)


# # plt
# fname = "key points"
# titlestr = '%s found %d keypoints' % (fname, len(keypoints))
# fig.canvas.set_window_title(titlestr)
# plt.title(titlestr)
# # plt.show()
# plt.imshow(color_img)
# plt.pause(0.1)
# fig.clf()

# opencv
if len(keypoints) > 200:
cv2.putText(color_img, ("circles num = % d" % len(keypoints)), (50, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
else:
cv2.putText(color_img, ("Notice: % s" % "Change Position"), (50, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)

cv2.imshow('findCorners', color_img)
cv2.waitKey(1)



if __name__ == "__main__":

# 图片测试
# # img = cv2.imread("circles/circels.jpg",1)
img = cv2.imread("circles/Snap_001.jpg",1)
mainFigure(img)


# # # 打开相机
# capture = cv2.VideoCapture(0)
# print("hello world")
#
# fig = plt.figure()
# # plt.ion()
#
# if capture is None:
# print("Fail to open camera")
# else:
# while (True):
# # 获取一帧
# ret, frame = capture.read()
#
# if frame is None:
# print("Fail to grab")
# continue
# else:
# # 将这帧转换为灰度图
# # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# main(frame, fig)
# # cv2.imshow('frame', frame)
# key = cv2.waitKey(1)
# if key == ord('q'):
# break