首先,利用机器视觉来测定零件尺寸,我们能够直接得到的是图片的像素,要想获得尺寸大小,我们必须找到像素和实际尺寸间的关系。

我们在这里定义一个比例概念:每度量比的像素(pixels per metric ratio)
近似含义是每个单位指标中包含的像素数。例如,图表上的1厘米包含100张图像。

实际上,相当于引用对象的作用,例如已知地图上的引用材质,我们可以使用此引用对象将其转换为地图上其他对象的大小。

引用对象需要具有两个重要属性:

  1. 属性#1:我们应该以可测量的单位(例如毫米,英寸等)知道该对象的尺寸(在宽度或高度方面)。
  2. 属性#2:我们应该能够在图像中轻松找到这个参考对象,或者根据对象的 位置(例如参考对象总是放在图像的左上角),或者通过外观(如是独特的颜色或形状,独特且与图像中的所有其他物体不同)。在任何一种情况下,我们的参考应该 以某种方式唯一可识别。

这里我选择的参照对象是美国硬币(0.9in x 1.0in),单位为英寸,并且保证它一直在照片的最左侧。并使用它来定义 pixel_per_metric,我们定义为:

pixels_per_metric = object_width / know_width

现在,假设我们的 object_width(以像素为单位)计算为150像素宽(基于其关联的边界框)。

因此 pixels_per_metric是:

pixels_per_metric = 150px / 0.955in = 157px

因此暗示在我们的图像中每0.955英寸大约有157个像素。使用此比率,我们可以计算图像中对象的大小。

然而我们还遇到一个问题是:要想每张照片上都有一个参照对象——美国硬币,这对我们来说是很麻烦的一件事,因为我们在工厂处理大批量的零件时不可能在每个零件旁都放置一枚硬币。因此,我采用了图像混合方法来处理图像,将硬币图片人为混合在零件图片上。

引入各种需要的库,没有相应的库在写代码前及时安装。本人使用的Anaconda,可以直接在Anaconda Prompt中直接pip即可。

pip install imutils

确保是最新版本

pip install --upgrade imutils

若是Pycharm,可以在setting中编译器里面安装

from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
#import argparse
import imutils
import numpy as np
import cv2

图像混合处理

coin_img = cv2.imread("standard_object.png")
image = cv2.imread("example_03.png")
if not image.data:print("read image wrong!")
if not coin_img.data:print("read coin_img wrong")imageROI = np.ones((100, 88, 3))
imageROI =  coin_img[0:100, 0: 88]image[10:98, 10:98] = imageROI

对图像进行预处理
从磁盘加载我们的图像,将其转换为灰度,然后使用高斯滤波器对其进行平滑处理。然后,我们进行边缘检测以及扩张+侵蚀,以封闭边缘图中边缘之间的任何间隙
找到与我们的边缘图中的对象相对应的轮廓(即轮廓)
从左到右(允许我们提取参考对象)对这些轮廓进行排序 。
初始化 pixelPerMetric 值

# load the image, convert it to grayscale, and blur it slightlygray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)gray = cv2.GaussianBlur(gray, (7, 7), 0)# perform edge detection, then perform a dilation + erosion to# close gaps in between object edgesedged = cv2.Canny(gray, 50, 100)edged = cv2.dilate(edged, None, iterations=1)edged = cv2.erode(edged, None, iterations=1)# find contours in the edge mapcnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)cnts = imutils.grab_contours(cnts)# sort the contours from left-to-right and initialize the# 'pixels per metric' calibration variable(cnts, _) = contours.sort_contours(cnts)pixelsPerMetric = None
width = 3.5

检查每一个轮廓
我们开始在每个轮廓上循环。如果轮廓不够大,我们丢弃该区域,假设它是边缘检测过程遗留的噪声
如果轮廓区域足够大,我们计算图像的旋转边界框
然后,我们将旋转的边界框坐标排列在左上角,右上角,右下角和左下角
最后, 以绿色绘制对象的轮廓,然后以小的红色圆圈绘制边界框矩形的顶点。

# loop over the contours individuallyfor c in cnts:# if the contour is not sufficiently large, ignore itif cv2.contourArea(c) < 100:continue# compute the rotated bounding box of the contourorig = image.copy()box = cv2.minAreaRect(c)box = cv2.boxPoints(box)box = np.array(box, dtype="int")# order the points in the contour such that they appear# in top-left, top-right, bottom-right, and bottom-left# order, then draw the outline of the rotated bounding# boxbox = perspective.order_points(box)cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)# loop over the original points and draw themfor (x, y) in box:cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)

现在我们已经获得了边界框,我们可以计算出一系列中点:
打开我们获得的边界框,然后计算左上角和右上角之间的中点,然后是右下角之间的中点
我们还将分别计算左上角 + 左下角 和 右上角 + 右下角之间的中点。
在我们的图像上绘制蓝色中点 ,然后用紫色线连接中点 。

# unpack the ordered bounding box, then compute the midpoint# between the top-left and top-right coordinates, followed by# the midpoint between bottom-left and bottom-right coordinates(tl, tr, br, bl) = box(tltrX, tltrY) = midpoint(tl, tr)(blbrX, blbrY) = midpoint(bl, br)# compute the midpoint between the top-left and top-right points,# followed by the midpoint between the top-righ and bottom-right(tlblX, tlblY) = midpoint(tl, bl)(trbrX, trbrY) = midpoint(tr, br)# draw the midpoints on the imagecv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)# draw lines between the midpointscv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),(255, 0, 255), 2)cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),(255, 0, 255), 2)

接下来,我们需要通过调查我们的引用对象来初始化 pixelPerMetric变量:
首先,我们计算我们的中点集之间的欧几里德距离。该 DA 变量将包含高度距离(以像素为单位),而分贝将保存我们的宽度的距离。

然后,看看我们的 pixelsPerMetric 变量已经初始化,如果没有,我们把 分贝 由我们提供 - 宽度 ,从而使我们每英寸我们的(近似)的像素。

# compute the Euclidean distance between the midpointsdA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))# if the pixels per metric has not been initialized, then# compute it as the ratio of pixels to supplied metric# (in this case, inches)if pixelsPerMetric is None:pixelsPerMetric = dB / width

现在我们 已经定义了 pixelPerMetric变量,我们可以测量图像中对象的大小:
通过将相应的欧几里德距离除以pixelsPerMetric 值来计算对象的尺寸(以英寸为单位)
在图像上绘制对象的尺寸
显示输出结果

# compute the size of the objectdimA = dA / pixelsPerMetricdimB = dB / pixelsPerMetric# draw the object sizes on the imagecv2.putText(orig, "{:.1f}in".format(dimA),(int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,0.65, (255, 255, 255), 2)cv2.putText(orig, "{:.1f}in".format(dimB),(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,0.65, (255, 255, 255), 2)# show the output imagecv2.imshow("Image", orig)cv2.waitKey(0)

最后给上完整代码:

from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
#import argparse
import imutils
import numpy as np
import cv2def midpoint(A, B):return ((A[0] + B[0]) * 0.5, (A[1] + B[1]) * 0.5)coin_img = cv2.imread("standard_object.png")
image = cv2.imread("example_03.png")
if not image.data:print("read image wrong!")
if not coin_img.data:print("read coin_img wrong")imageROI = np.ones((100, 88, 3))
imageROI =  coin_img[0:100, 0: 88]image[10:98, 10:98] = imageROI
# load the image, convert it to grayscale, and blur it slightlygray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)gray = cv2.GaussianBlur(gray, (7, 7), 0)# perform edge detection, then perform a dilation + erosion to# close gaps in between object edgesedged = cv2.Canny(gray, 50, 100)edged = cv2.dilate(edged, None, iterations=1)edged = cv2.erode(edged, None, iterations=1)# find contours in the edge mapcnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)cnts = imutils.grab_contours(cnts)# sort the contours from left-to-right and initialize the# 'pixels per metric' calibration variable(cnts, _) = contours.sort_contours(cnts)pixelsPerMetric = None
width = 3.5# loop over the contours individuallyfor c in cnts:# if the contour is not sufficiently large, ignore itif cv2.contourArea(c) < 100:continue# compute the rotated bounding box of the contourorig = image.copy()box = cv2.minAreaRect(c)box = cv2.boxPoints(box)box = np.array(box, dtype="int")# order the points in the contour such that they appear# in top-left, top-right, bottom-right, and bottom-left# order, then draw the outline of the rotated bounding# boxbox = perspective.order_points(box)cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)# loop over the original points and draw themfor (x, y) in box:cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)# unpack the ordered bounding box, then compute the midpoint# between the top-left and top-right coordinates, followed by# the midpoint between bottom-left and bottom-right coordinates(tl, tr, br, bl) = box(tltrX, tltrY) = midpoint(tl, tr)(blbrX, blbrY) = midpoint(bl, br)# compute the midpoint between the top-left and top-right points,# followed by the midpoint between the top-righ and bottom-right(tlblX, tlblY) = midpoint(tl, bl)(trbrX, trbrY) = midpoint(tr, br)# draw the midpoints on the imagecv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)# draw lines between the midpointscv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2)cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2)# compute the Euclidean distance between the midpointsdA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))# if the pixels per metric has not been initialized, then# compute it as the ratio of pixels to supplied metric# (in this case, inches)if pixelsPerMetric is None:pixelsPerMetric = dB / width# compute the size of the objectdimA = dA / pixelsPerMetricdimB = dB / pixelsPerMetric# draw the object sizes on the imagecv2.putText(orig, "{:.1f}in".format(dimA),(int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2)cv2.putText(orig, "{:.1f}in".format(dimB),(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2)# show the output imagecv2.imshow("Image", orig)cv2.waitKey(0)


但是测量不一定是100%准确的。为什么物体测量不是100%准确?

原因有两方面:

首先,用手机拍了照片。角度肯定 不是物体上“俯视”(如鸟瞰图)的完美90度角。如果没有完美的90度视图(或尽可能接近它),对象的尺寸可能会出现扭曲。

其次,没有使用相机的内在和外在参数来校准手机。在不确定这些参数的情况下,照片可能容易发生径向和切向镜头失真。执行额外的校准步骤来查找这些参数可以“扭曲”我们的图像并导致更好的对象大小近似。

同时,在拍摄物体照片时尽量获得尽可能接近90度的视角 - 这有助于提高物体尺寸估计的准确性。

总结

在本文中,我们学习了如何通过使用python和OpenCV来测量图片中的物体的大小。

我们首先对图像进行了混合处理。

我们需要确定pixels per metric比率(单位尺寸像素数),即在给定的度量(如英寸、毫米、米等)下,像素的数量。

为了计算这个比率,我们需要一个参考物体,它需要两点重要的性质:

1、参考物体需要有含测量单位(英寸、毫米等等)的尺寸

2、无论从物体的位置还是形状,参考物体都需要容易被找到。

加入上面的性质都能满足,你可以使用参考物体计算pixels per metric比率,并根据这个计算图片中物体的大小。

参考:
[干货」如何使用OpenCV测量图像中物体的尺寸大小
[Python图像处理] 三.获取图像属性、兴趣ROI区域及通道处理

python-opencv尺寸测量相关推荐

  1. python opencv卡尺测量边缘距离

    opencv 卡尺法 测量边缘距离 参考来源 :https://github.com/crackwitz/metrology-demo 前言 一.测量方法 二.测量步骤 1.获取直线的像素 2.高斯滤 ...

  2. python+opencv实现机器视觉基础技术(2)(宽度测量,缺陷检测,医学检测

     本篇博客接着讲解机器视觉的有关技术和知识.包括宽度测量,缺陷检测,医学处理. 一:宽度测量   在传统的自动化生产中,对于尺寸的测量,典型的方法就是千分尺.游标卡尺.塞尺等.而这些测量手段测量精度低 ...

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

    实验四 尺寸测量 计算机视觉OpenCV基础实验合辑(实验1234+扩展) 资源下载地址: https://download.csdn.net/download/weixin_53403301 合辑: ...

  4. 用 Python 和 OpenCV 来测量相机到目标的距离

    用 Python 和 OpenCV 来测量相机到目标的距离 http://python.jobbole.com/84378/ 几天前,一个叫 Cameron 的 PyImageSearch 读者发来邮 ...

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

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

  6. Python OpenCV 批量修改文件夹内所有图片的尺寸

    Python OpenCV 批量修改文件夹内所有图片的尺寸 简述 插件 遍历所有文件(包括子文件) 缩放图片尺寸并保存图片 遍历并修改图片 简述 遍历文件夹下的所有子文件(包括自文件夹内的)的图片批量 ...

  7. python opencv 如何获取图像的尺寸(宽高)(分辨率)(大小)img.shape

    示例,获取文件夹中一张名为IMG_20200403_203742.jpg图片的分辨率 # -*- coding: utf-8 -*- """ @File : 如何获取图像 ...

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

    物体尺寸测量的思路是找一个确定尺寸的物体作为参照物,根据已知的计算未知物体尺寸. 如下图所示,绿色的板子尺寸为220*300(单位:毫米),通过程序计算白色纸片的长度. 目录 1.相关库 2.读图+图 ...

  9. Python+OpenCV实现自动扫雷,挑战扫雷世界记录!

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转载自知乎Artrix https://zhuanlan.zh ...

  10. Python+OpenCV实现自动扫雷,创造属于自己的世界记录!

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转载自知乎Artrix https://zhuanlan.zh ...

最新文章

  1. printf格式化输出几点注记
  2. c++ ifstream 文件不结束_C++核心编程 第十二节 文件操作
  3. httpclient工具使用(org.apache.httpcomponents.httpclient)
  4. 牛客网暑期ACM多校训练营(第五场)A-gap (二分答案)
  5. php分页功能乱码了怎么办,51、PHP文件内容分页操作,避免乱码
  6. 解决manjaro更新后pacman: error while loading shared libraries
  7. 如何将二维数组作为函数的参数传递
  8. html-表单初级验证
  9. 交换机跟计算机系统有关系,网速跟交换机有关系吗
  10. mysql 从库开启复制慢日志_Mysql数据库优化之开启慢查询日志
  11. Optional类的使用
  12. 软工网络15团队作业4-DAY2
  13. 我读《DOOM启世录》——成为一个真正厉害的人
  14. 基于PHP+MySQL的大学生健康管理系统
  15. 网页滚动截屏怎么截长图
  16. linux apache配置目录大小写,linux服务器centos系统apache路径不区分大小写的解决办法(适用WDCP面板)...
  17. 工作后,又想读个名校的计算机硕士,该怎么做?
  18. Percona监控数据库解决方案
  19. 【论文阅读】ICRA2021: VDB-EDT An Efficient Euclidean Distance Transform Algorithm Based on VDB Data Struct
  20. github国内镜像

热门文章

  1. 【3D目标检测】PV-RCNN:Point-Voxel Feature Set Abstraction for 3D Object Detection
  2. Ubiquitous Religions(并查集)
  3. webstorm最新版激活破解
  4. 第三周——小小大佬带飞队
  5. mac brew命令汇总
  6. linux下网速监控 linux及android分网卡网速监控
  7. pod 文件管理服务器,k8s中pod的状态管理
  8. C++中string类
  9. 美女时钟(shell)
  10. 同是匿名社交,国内外“秘密”大不同