python-opencv尺寸测量

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

我们在这里定义一个比例概念:每度量比的像素(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 slightly


gray = 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 edges

edged = cv2.Canny(gray, 50, 100)

edged = cv2.dilate(edged, None, iterations=1)

edged = cv2.erode(edged, None, iterations=1)

 

# find contours in the edge map

cnts = 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 individually

for c in cnts:

	 # if the contour is not sufficiently large, ignore it
	
	 if cv2.contourArea(c) < 100:
	
	 continue
	
	 
	
	 # compute the rotated bounding box of the contour
	
	 orig = 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
	
	 # box
	
	 box = perspective.order_points(box)
	
	 cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
	
	 
	
	 # loop over the original points and draw them
	
	 for (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 image

 cv2.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 midpoints

 cv2.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 midpoints

 dA = 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 object

 dimA = dA / pixelsPerMetric

 dimB = dB / pixelsPerMetric

 

 # draw the object sizes on the image

 cv2.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 image

 cv2.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 cv2

def 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 slightly

gray = 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 edges

edged = cv2.Canny(gray, 50, 100)

edged = cv2.dilate(edged, None, iterations=1)

edged = cv2.erode(edged, None, iterations=1)

# find contours in the edge map

cnts = 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 individually

for c in cnts:

	# if the contour is not sufficiently large, ignore it

    if cv2.contourArea(c) < 100:

 		continue

	# compute the rotated bounding box of the contour

	orig = 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
	
	# box
	
	box = perspective.order_points(box)
	
	cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)

	# loop over the original points and draw them

	for (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 image
	
	cv2.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 midpoints
	
	cv2.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 midpoints

	dA = 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 object
	dimA = dA / pixelsPerMetric
	
	dimB = dB / pixelsPerMetric
	
	# draw the object sizes on the image
	
	cv2.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 image
	
	cv2.imshow("Image", orig)
	
	cv2.waitKey(0)

在这里插入图片描述
但是测量不一定是100%准确的。为什么物体测量不是100%准确?

原因有两方面:

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

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

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

总结

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

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

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

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

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

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

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

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

  • 23
    点赞
  • 321
    收藏
    觉得还不错? 一键收藏
  • 37
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值