【OpenCV】标定后完成真实物理尺寸测量

1. 导包

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import math
import glob

2. 相机标定

pattern_rows = 10
pattern_cols = 6
pattern_size = (pattern_rows, pattern_cols)
square_size = 10  # mm
object_points = np.zeros((pattern_rows * pattern_cols, 3), np.float32)
object_points[:, :2] = np.mgrid[0:pattern_rows, 0:pattern_cols].T.reshape(-1, 2) * square_size
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)objpoints = []
imgpoints = []
images = glob.glob('./images/*.bmp')
for fname in images:gray = cv.imread(fname, cv.IMREAD_GRAYSCALE)height, width = gray.shapeflags = cv.CALIB_CB_ADAPTIVE_THRESH + cv.CALIB_CB_FILTER_QUADS + cv.CALIB_CB_FAST_CHECKret, corners = cv.findChessboardCorners(image=gray, patternSize=pattern_size, flags=flags)if ret == True:objpoints.append(object_points)corners = cv.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria)imgpoints.append(corners)ret, mtx, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, (width, height), None, None)

3. 参考图像

img_ref = cv.imread('./images./Image_20210907144725602.bmp', cv.IMREAD_GRAYSCALE)
plt.imshow(img_ref, cmap='gray')

在这里插入图片描述

4. 计算图像与参考平面的映射关系

trans, _ = cv.estimateAffine2D(corners, object_points[:,np.newaxis,:2])

5. 边缘点查找

def FindEdgePoint(src, roi, kernel_size = 2, edge_threshold = 30.0, edge_type = 1, edge_polarity = 0):# ROI 长宽取整且获取矩形顶点new_roi = (roi[0], (math.ceil(roi[1][0]), math.ceil(roi[1][1])), roi[2])roi_points = cv.boxPoints(new_roi)# ROI区域图像转正rect_point = np.array([[0, new_roi[1][1] - 1],[0, 0],[new_roi[1][0] - 1, 0],[new_roi[1][0] - 1, new_roi[1][1] - 1]], dtype=np.float32)trans_local = cv.getPerspectiveTransform(roi_points, rect_point)img_dst = cv.warpPerspective(src, trans_local, new_roi[1])# 垂直投影img_h2 = np.average(img_dst,axis=0)img_h = np.expand_dims(img_h2, 0)# 高斯滤波img_h = cv.GaussianBlur(img_h, (kernel_size * 2 + 1, 1), 0)# 索贝尔边缘提取img_s = cv.Sobel(src=img_h, ddepth=-1, dx=1, dy=0, ksize=1, scale = 1, delta = 0, borderType=cv.BORDER_DEFAULT)# 边缘提取img_s_abs = np.abs(img_s)indice = []values = []for index in range(img_s.shape[1]):if img_s_abs[0, index] > edge_threshold:if edge_type == 0:if edge_polarity == 0:values.append(img_s[0, index])indice.append(index)elif edge_polarity == 1 and img_s[0, index] > 0:values.append(img_s[0, index])indice.append(index)elif edge_polarity == 2 and img_s[0, index] < 0:values.append(img_s[0, index])indice.append(index)elif edge_type == 1:if edge_polarity == 0:indice.append(index)values.append(img_s[0, index])breakelif edge_polarity == 1 and img_s[0, index] > 0:indice.append(index)values.append(img_s[0, index])breakelif edge_polarity == 2 and img_s[0, index] < 0:values.append(img_s[0, index])indice.append(index)breakelif edge_threshold == 2:if edge_polarity == 0:if (len(indice) == 0):indice.append(index)values.append(img_s[0, index])else:indice[0] = indexvalues[1] = img_s[0, index]elif edge_polarity == 1 and img_s[0, index] > 0:if (len(indice) == 0):indice.append(index)values.append(img_s[0, index])else:indice[0] = indexvalues[1] = img_s[0, index]elif edge_polarity == 2 and img_s[0, index] < 0:if (len(indice) == 0):indice.append(index)values.append(img_s[0, index])else:indice[0] = indexvalues[1] = img_s[0, index]else:if edge_polarity == 0:if (len(indice) == 0):indice.append(index)values.append(img_s[0, index])else:if math.abs(values[1] < img_s_abs[0, index]):indice[0] = indexvalues[1] = img_s[0, index]elif edge_polarity == 1 and img_s[0, index] > 0:if (len(indice) == 0):indice.append(index)values.append(img_s[0, index])else:if math.abs(values[1] < img_s_abs[0, index]):indice[0] = indexvalues[1] = img_s[0, index]elif edge_polarity == 2 and img_s[0, index] < 0:if (len(indice) == 0):indice.append(index)values.append(img_s[0, index])else:if math.abs(values[1] < img_s_abs[0, index]):indice[0] = indexvalues[1] = img_s[0, index]# 反转坐标half_height = new_roi[1][1] * 0.5edge_point_h = []trans_inv = np.linalg.inv(trans_local)color_image = cv.cvtColor(src, cv.COLOR_GRAY2BGR)for i in range(len(values)):point_h = np.array([indice[0], half_height]).reshape(1,1,2)edge_point = cv.transform(point_h, trans_inv)cv.circle(color_image, (int(edge_point[0, 0, 0]), int(edge_point[0, 0, 1])), 20, (255, 0, 0), -1)plt.figure(figsize=(20,8))plt.imshow(color_image[...,::-1])return edge_point[0, 0, 0],edge_point[0, 0, 1]
x1,y1 = FindEdgePoint(img_ref, ((789.72, 1037.57),(150, 224.288), 0))

在这里插入图片描述

x2,y2 = FindEdgePoint(img_ref, ((3325.32, 1037.57),(150, 224.288), 0))

在这里插入图片描述

6. 将点映射到物理坐标

new_x1 = trans[0, 0] * x1 + trans[0, 1] * y1 + trans[0, 2]
new_y1 = trans[1, 0] * x1 + trans[1, 1] * y1 + trans[1, 2]
new_x2 = trans[0, 0] * x2 + trans[0, 1] * y2 + trans[0, 2]
new_y2 = trans[1, 0] * x2 + trans[1, 1] * y2 + trans[1, 2]dist = math.sqrt((new_x1 - new_x2) ** 2 + (new_y1 - new_y2) ** 2)
print(dist)

110.0605974101517

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/1618572.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

AI智能尺寸测量仪简介(图像尺寸测量仪)

AI智能一键式测量仪产品介绍 AI智能一键式测量仪基于自主研发的i-Vision AI视觉技术(CNN神经网络&MV机器视觉)实现识别物体、感兴趣区域(ROI)智能打光(AutoLight)与智能对焦功能&#xff0c;使得设备更智慧! AI智能一键式测量仪 PMS-MI2000-01让您的测量变得简单&#xff…

opencv-python实战---物体长度尺寸测量

本文的主要算法实现思路:找一个最小面积的矩形提供长宽由此推算出其他物体的长度 博主只写了测量物体最左最右的长度&#xff0c;只能测量矩形。 如图 最左边的为参照物&#xff0c;然后测量上下两个物体的最左和最右长度。 1&#xff0c;相关库 opencv-python 4.7.0.72 n…

python编程实现图片内多个物体尺寸测量

要实现图片内多个物体尺寸测量&#xff0c;你可以使用计算机视觉库&#xff0c;如 OpenCV 来实现。 首先&#xff0c;你需要读取图片&#xff0c;然后对图像进行预处理&#xff0c;以便更容易地检测到图像中的物体。例如&#xff0c;你可以使用边缘检测算法来提取边缘&#xf…

基于图像处理的尺寸测量系统

图像处理的尺寸测量较为重要&#xff0c;可以根据图像处理的基本知识&#xff0c;得到图像的基本信息&#xff0c;然后根据其研究的内容和具体的开发研究现状&#xff0c;得到最后的测量结果&#xff0c;后面将具体的介绍方法。 可以看出在上述的图上&#xff0c;将环形画出来 …

基于OpenCV的 桌面手机的尺寸测量

基于OpenCV的 桌面手机的尺寸测量 代码 见资源&#xff1a;基于OpenCV&#xff08;C&#xff09;的桌面手机的尺寸测量 网址&#xff1a;https://download.csdn.net/download/Aquamarine__/16689432 实验数据&#xff1a;https://download.csdn.net/download/Aquamarine__/16…

【计算机视觉OpenCV基础】实验四 尺寸测量

实验四 尺寸测量 计算机视觉OpenCV基础实验合辑&#xff08;实验1234扩展&#xff09; 资源下载地址&#xff1a; https://download.csdn.net/download/weixin_53403301 合辑&#xff1a;&#xff08;加在下载地址后面&#xff09; /87113581 讲义&#xff08;包括理论、图例、…

opencv 物体尺寸测量

1 原图&#xff0c;已知左上角正方形的实际大小为 2cm 2 转为灰度图 高斯模糊 gray cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.GaussianBlur(gray, (9, 9), 0) 3、边缘检测 edged cv2.Canny(blur, 50, 100) 开运算 4、根据左上角参考物体&#xff0c;计算单位长度…

物体尺寸测量-matlab

本程序可以实现物体尺寸测量,比如工件 、零件、 树叶等物体的尺寸测量 ,诸如周长和面积的测量。采用两种方法实现,一种是链码的方法,一种是图像处理的方法,具体如下 方法一 采取链码的方法 图 展示了以树叶为例子 得到可周长和面积的显示系统 在这里采取了链码的方法计算…

opencv实战---物体尺寸测量

物体尺寸测量的思路是找一个确定尺寸的物体作为参照物&#xff0c;根据已知的计算未知物体尺寸。 如下图所示&#xff0c;绿色的板子尺寸为220*300&#xff08;单位&#xff1a;毫米&#xff09;&#xff0c;通过程序计算白色纸片的长度。 目录 1、相关库 2、读图图片预处理…

python-opencv尺寸测量

原文&#xff1a;python-opencv尺寸测量_Krasjet_Yu.的博客-CSDN博客_python尺寸测量 首先&#xff0c;利用机器视觉来测定零件尺寸&#xff0c;我们能够直接得到的是图片的像素&#xff0c;要想获得尺寸大小&#xff0c;我们必须找到像素和实际尺寸间的关系。 我们在这里定义…

用Cmake build OpenCV后,在VS中查看OpenCV源码的方法(环境VS2022+openCV4.8.0) Part III

用Cmake build OpenCV后&#xff0c;在VS中查看OpenCV源码的方法(环境VS2022openCV4.8.0) Part III 用Cmake build OpenCV后&#xff0c;在VS中查看OpenCV源码的方法&#xff08;环境VS2022openCV4.8.0&#xff09; Part I_松下J27的博客-CSDN博客 用Cmake build OpenCV后&…

理解HTTPS/TLS/SSL(一)基础概念+配置本地自签名证书

文章目录 没有HTTPS时的样子场景模拟WireShark的Capture Filter和Display Filter设置Capture Filter启动程序设置Display Filter过滤抓到的包 结论 关于为什么加密更简洁有力的回答对称加密和非对称加密和CA证书密钥交换对称加密非对称加密CA机构和证书如何解决客户端和CA机构之…

顺序表链表OJ题(1)——【LeetCode】

W...Y的主页 &#x1f60a; 代码仓库分享 &#x1f495; 前言&#xff1a; 今天我们来回顾一下顺序表与链表&#xff0c;针对这一块我们也有许多OJ题目供大家参考。当我们学习完顺序表链表后避免不了一些习题的练习&#xff0c;这样才能巩固我们学习的内容。 话不多说&#xf…

PCI/PCIE总线的宏观理解

1、pcie总线协议实现的效果 (1)像访问内存一样去访问外设&#xff1b; (2)当建立好CPU地址空间到PCI/PCIE地址空间的映射关系后&#xff0c;程序访问CPU地址空间就可以达到访问PCI/PCIE地址空间的效果&#xff1b; 2、芯片地址空间 (1)32位的CPU寻址范围是4G&#xff0c;64位的…

MySQL执行更新的流程

一、加载缓存数据 引擎要执行更新语句的时候 &#xff0c;比如对“id10”这一行数据&#xff0c;他其实会先将“id10”这一行数据看看是否在缓冲池里&#xff0c;如果不在的话&#xff0c;那么会直接从磁盘里加载到缓冲池里来&#xff0c;而且接着还会对这行记录加独占锁。 二…

Spring中Bean及@Bean的理解与new对象的区别

一直在纠结一个问题&#xff1a;new创建对象和用Bean创建对象有什么区别吗&#xff1f;为什么在spring中要使用Bean&#xff1f;Bean有什么作用&#xff1f; 一、Bean是啥 1、Java面向对象&#xff0c;对象有方法和属性&#xff0c;那么就需要对象实例来调用方法和属性&#x…

Spring Bean到底是什么?有什么用?

Spring Bean是什么?有什么用&#xff1f; 一、Bean到底是什么&#xff1f;二.怎么使用bean&#xff1f;三.Bean配置四.Bean的作用域 Bean在Spring和SpringMVC中随处可见,将这个概念内化很重要&#xff0c;下面分享一下我的想法&#xff1a; 一、Bean到底是什么&#xff1f; …

spring bean是什么

原文链接&#xff1a;https://www.awaimai.com/2596.html 歪麦博客 Spring有跟多概念&#xff0c;其中最基本的一个就是bean&#xff0c;那到底spring bean是什么? Bean是Spring框架中最核心的两个概念之一&#xff08;另一个是面向切面编程AOP&#xff09;。 是否正确理解…

Spring Bean的作用域

在Spring中&#xff0c;bean作用域用于确定哪种类型的bean实例应该从Spring容器中返回给调用者。 目前Spring Bean的作用域或者说范围主要有五种。 作用域描述singleton在spring IoC容器仅存在一个Bean实例&#xff0c;Bean以单例方式存在&#xff0c;bean作用域范围的默认值…

Spring bean是什么?

Spring有跟多概念&#xff0c;其中最基本的一个就是bean&#xff0c;那到底spring bean是什么? Bean是Spring框架中最核心的两个概念之一&#xff08;另一个是面向切面编程AOP&#xff09;。 是否正确理解 Bean 对于掌握和高效使用 Spring 框架至关重要。 遗憾的是&#xff0…