用于尺寸测量的相机图像校准 - 查找每像素值的毫米

问题描述

背景:我正在尝试使用来自相机的视频输入实时测量对象的尺寸。我使用的是通用 USB 显微镜相机 (link to camera)。视频帧的分辨率为 1280 x 720。我现在正在处理视频的单个帧/图像。

为了测量实际(物理)尺寸,我想首先校准相机测量值,即获得每像素的毫米值。为此,我用尺子替换了要测量的原始对象。由于相机是倾斜安装的,为了获得俯视图或鸟瞰图,我使用透视变换 4 点方法对图像进行了校正。

View as seen from camera

Image after corrected for perspective warp distortion

为了获得上面的透视校正图像,我创建了以下代码

import cv2
import numpy as np
import matplotlib.pylot as plt

# To open matplotlib in interactive mode
%matplotlib qt5
 
# Load the image
img = cv2.imread('Extracted Images/Color/colorframe50.jpg') 

print('Width: {0}'.format(img.shape[1]))
print('Height: {0}'.format(img.shape[0]))
print('Channel: {0}'.format(img.shape[2]))
 
# Create a copy of the image
img_copy = np.copy(img)
 
# Convert to RGB so as to display via matplotlib
# Using Matplotlib we can easily find the coordinates of the 4 points that is essential for finding then transformation matrix
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img_rgb = cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB)
 
#plt.imshow(img_copy)

scale_width = round(11.7348*100) # mm Actual ruler width measured using calipers
scale_height = round(6.35*100)   # mm Height based on selected 4 points for perspective transform 

# to calculate the transformation matrix
input_pts = np.float32([[192.30,343.00],[1079.0,379.80],[153.50,571.90],[1107.10,611.70]])
output_pts = np.float32([[0,0],[scale_width-1,[0,scale_height-1],scale_height-1]])

# Compute the perspective transform M
M = cv2.getPerspectiveTransform(input_pts,output_pts)
print(M.shape)
print(M)
 
# Apply the perspective transformation to the image
imgPersp = cv2.warpPerspective(img_rgb,M,(scale_width,scale_height)) #,flags=cv2.INTER_LINEAR)
imgGrayPersp = cv2.cvtColor(out,cv2.COLOR_BGR2GRAY)

# save image 
#cv2.imwrite('scalePerspCorrrected.jpg',imgGrayPersp)

# visulaize corners using cv2 circles
for x in range (0,4):
    cv2.circle(img_rgb,(input_pts[x][0],input_pts[x][1]),5,(255,0),cv2.FILLED)

# Plot results
plt.figure()    

titles = ['Original Image','4-point Selection','Perspective Warp Correction','Grayscale Perspective Warp Correction']
images = [img,img_rgb,imgPersp,imgGrayPersp]

for i in range(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray') # use gray argument to correctly show grayscale images. Doesnt affect rgb images
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])

plt.show()
  1. 在为透视变换选择 4 个点时,我使用 matplotlib 图中的鼠标指针观察 x,y 坐标。是否有使用阈值和边缘检测获得 4 个点的准确方法?我猜这会影响毫米/像素值

  2. 我想测量标尺上刻度之间的像素数(例如,在上面的图片中,标记 8 与右侧任何相邻刻度之间的像素数)。因为我已经知道标尺刻度之间的实际距离,所以我可以得到每像素的毫米值。获得标尺上任意两个刻度之间的距离的最佳操作顺序是什么?我该怎么做?有什么建议吗?

一旦我有了每像素的毫米值,我计划使用相同的 4 个点和转换矩阵 M 来转换包含要测量的对象的图像,以获得未失真的图像,并将毫米/像素值与计算的对象距离相乘(以像素为单位)以找到以毫米为单位的真实尺寸。

  1. 我想测量物体的尺寸,公差至少为 0.020 毫米或更低。我要测量的物体的典型尺寸在 0.65 到 1.20 毫米之间。为了实现这一目标,我应该考虑哪些因素?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)