python 标定相机 检测圆点 对称或非对称02

import cv2
import numpy as np
import xml.etree.ElementTree as ET


# DPI 0.0016378020443511103
w = 7
h = 6
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 0.001)


def find_corners(img):
    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_SYMMETRIC_GRID, blobDetector, None)

    if ret:
        cv2.cornerSubPix(img, corners, (w, h), (1, 1), criteria)
        return corners
    return None




if __name__ == "__main__":

    # 打开相机测试
    capture = cv2.VideoCapture(0)

    mean_dpi = []

    while True:
        ret, img = capture.read()
        # cv2.imshow("video", img)
        key = cv2.waitKey(1)
        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

            mean_dpi.append(DPI)

            if len(mean_dpi) >= 3:
                # 如果运行到这儿表示标定成功
                DPI = np.mean(mean_dpi)
                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)
                mean_dpi = []
                # 将角点在图像上显示
            cv2.drawChessboardCorners(img, (w, h), corners, corners is not None)

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

    cv2.destroyAllWindows()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值