import cv2
import numpy as np
import xml.etree.ElementTree as ET
w = 7
h = 6
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 0.001)
def find_corners(img):
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
params = cv2.SimpleBlobDetector_Params()
params.maxArea = 10e4
params.minArea = 10
params.minDistBetweenBlobs = 1
blobDetector = cv2.SimpleBlobDetector_create(params)
# ret, corners = cv2.findCirclesGrid(img, (w, h), cv2.CALIB_CB_ASYMMETRIC_GRID, blobDetector, None)
ret, corners = cv2.findCirclesGrid(img, (w, h), cv2.CALIB_CB_SYMMETRIC_GRID, blobDetector, None)
if ret:
cv2.cornerSubPix(img, corners, (w, h), (1, 1), criteria)
# cv2.find4QuadCornerSubpix(img, corners, (w, h))
return corners
return None
# 打开相机测试
capture = cv2.VideoCapture(1)
while True:
ret, img = capture.read()
# frame = cv2.flip(frame,1) #镜像操作
cv2.imshow("video", img)
key = cv2.waitKey(1)
# print(key)
if key == ord('q'): # 判断是哪一个键按下
break
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
image = cv2.resize(gray_img, (img.shape[1] * 2, img.shape[0] * 2))
minThreshValue = 120
_, binary = cv2.threshold(gray_img, minThreshValue, 255, cv2.THRESH_BINARY)
cv2.imshow("image", binary)
corners = find_corners(binary)
if corners is not None:
# print(corners)
# 计算距离
dis_real = np.sqrt(np.power((w - 1) * 0.05, 2) + np.power((h - 1) * 0.05, 2))
lr_points = corners[-1] - corners[0]
dis_imge = np.sqrt(np.power(lr_points[0][0], 2) + np.power(lr_points[0][1], 2))
DPI = dis_real / dis_imge
configFile_xml = "wellConfig.xml"
tree = ET.parse(configFile_xml)
root = tree.getroot()
secondRoot = root.find("DPI")
print(secondRoot.text)
secondRoot.text = str(DPI)
tree.write("wellConfig.xml")
print("DPI", DPI)
print(DPI)
# 将角点在图像上显示
cv2.drawChessboardCorners(img, (w, h), corners, corners is not None)
cv2.imshow('findCorners', img)
cv2.waitKey(1)
cv2.destroyAllWindows()
# 测试图片
# img = cv2.imread("circles/Snap_4.jpg",1)
# # img = cv2.imread("circles/sm02.jpg",1)
# # img = cv2.imread("circles/calb.jpg", 1)
# gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# image = cv2.resize(gray_img, (img.shape[1] * 2, img.shape[0] * 2))
#
# minThreshValue = 120
# _, binary = cv2.threshold(gray_img, minThreshValue, 255, cv2.THRESH_BINARY)
# cv2.imshow("image", binary)
#
# corners = find_corners(binary)
#
# if corners is not None:
# # print(corners)
#
# # 将角点在图像上显示
# cv2.drawChessboardCorners(img, (w, h), corners, corners is not None)
#
# # 计算距离
# dis_real = np.sqrt(np.power((w-1)*0.05, 2) + np.power((h-1)*0.05, 2))
# # DPI = dis_real/
# lr_points = corners[-1] - corners[0]
# dis_imge = np.sqrt(np.power(lr_points[0][0], 2) + np.power(lr_points[0][1], 2))
# DPI = dis_real/dis_imge
# print(DPI)
#
#
# cv2.imshow('findCorners', img)
# cv2.waitKey()
# cv2.destroyAllWindows()
# # 所有图片测试
# for i in range(15):
# fileName = "Snap_" + str(i) + ".jpg"
# # img = cv2.imread("circles/Snap_007.jpg",1)
# img = cv2.imread("circles/" + fileName,1)
# print(fileName)
#
# gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# image = cv2.resize(gray_img, (img.shape[1] * 2, img.shape[0] * 2))
#
# minThreshValue = 120
# _, binary = cv2.threshold(gray_img, minThreshValue, 255, cv2.THRESH_BINARY)
# cv2.imshow("image", binary)
#
# corners = find_corners(binary)
#
# if corners is not None:
# # print(corners)
#
# # 将角点在图像上显示
# cv2.drawChessboardCorners(img, (w, h), corners, corners is not None)
#
# # 计算距离
# dis_real = np.sqrt(np.power((w-1)*0.05, 2) + np.power((h-1)*0.05, 2))
# # DPI = dis_real/
# lr_points = corners[-1] - corners[0]
# dis_imge = np.sqrt(np.power(lr_points[0][0], 2) + np.power(lr_points[0][1], 2))
# DPI = dis_real/dis_imge
# print(DPI)
#
#
# cv2.imshow('findCorners', img)
# cv2.waitKey()
# cv2.destroyAllWindows()
wellConfig.xml
27
0.0016279952297576263
https://www.baidu.com/
Good