gpt4 book ai didi

python - 鉴于从相机(校准)到我的物体的距离是固定的,我如何测量物体的宽度?

转载 作者:太空宇宙 更新时间:2023-11-03 22:39:57 26 4
gpt4 key购买 nike

请原谅我对编码完全陌生。首先,出于该项目的目的,我正在使用 Python 绑定(bind)到 OpenCV 的库。

我的相机已针对显示鱼眼失真进行了校准。我分别获得了 K 和 D 的以下值,即固有相机矩阵和畸变矩阵:

K = [[438.76709 0.00000 338.13894]
[0.00000 440.79169 246.80081]
[0.00000 0.00000 1.00000]]

D = [-0.098034379506 0.054022224927 -0.046172648829 -0.009039512970]

Focal length: 2.8mm
Field of view: 145 degrees (from manual)

当我取消扭曲图像并显示它时,我获得了一个在拉伸(stretch)过远(预期)的区域具有黑色像素的图像。但是,这不会妨碍对象宽度的计算,因为对象并不大,只占图像的 20%。

我会将物体放置在距相机镜头 10 厘米 的位置。根据我在针孔相机模型上阅读的内容,我将需要控制 3D 到 2D 转换的外部参数。但是,我不确定我应该如何得出它。

假设我有 2 个点的像素坐标(每个点都沿着我想要测量距离的边缘),我如何使用这些派生矩阵找到这两个点之间的真实世界距离?

另外,如果我的矩形物体不平行于相机的主轴,即使在这种情况下,是否有计算宽度的算法?

最佳答案

鉴于您的相机和物体之间的距离是固定的,您可以做的是首先找出找到的角之间的像素距离,然后将其转换为毫米使用每毫米像素比/比例因子确定对象宽度。

使用的算法是Harris角点检测 Harris Corner Detection

捕获其中包含对象的帧

cap = cv2.VideoCapture(0)
while(True):
#Capture frame-by-frame
ret, frame = cap.read()
cv2.imshow('LIVE FRAME!', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#Save it to some location
cv2.imwrite('Your location', frame)

首先使用引用物体校准每毫米像素比。

 #Read Image
image = cv2.imread('Location of your previously saved frame with the object in it.')
object_width = input(int("Enter the width of your object: ")
object_height = input(int("Enter the height of your object: ")


#Find Corners
def find_centroids(dst):
ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
dst = np.uint8(dst)

# find centroids
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
# define the criteria to stop and refine the corners
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100,
0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids[1:]),(5,5),
(-1,-1),criteria)
return corners

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray, 5, 3, 0.04)
dst = cv2.dilate(dst, None)

# Get coordinates of the corners.
corners = find_centroids(dst)



for i in range(0, len(corners)):
print("Pixels found for this object are:",corners[i])
image[dst>0.1*dst.max()] = [0,0,255]
cv2.circle(image, (int(corners[i,0]), int(corners[i,1])), 7, (0,255,0), 2)

for corner in corners:
image[int(corner[1]), int(corner[0])] = [0, 0, 255]


a = len(corners)
print("Number of corners found:",a)

#List to store pixel difference.
distance_pixel = []

#List to store mm distance.
distance_mm = []


P1 = corners[0]
P2 = corners[1]
P3 = corners[2]
P4 = corners[3]

P1P2 = cv2.norm(P2-P1)
P1P3 = cv2.norm(P3-P1)
P2P4 = cv2.norm(P4-P2)
P3P4 = cv2.norm(P4-P3)

pixelsPerMetric_width1 = P1P2 / object_width
pixelsPerMetric_width2 = P3P4 / object_width
pixelsPerMetric_height1 = P1P3 / object_height
pixelsPerMetric_height2 = P2P4 / object_height



#Average of PixelsPerMetric
pixelsPerMetric_avg = pixelsPerMetric_width1 + pixelsPerMetric_width2 + pixelsPerMetric_height1 + pixelsPerMetric_height2

pixelsPerMetric = pixelsPerMetric_avg / 4
print(pixelsPerMetric)
P1P2_mm = P1P2 / pixelsPerMetric
P1P3_mm = P1P3 / pixelsPerMetric
P2P4_mm = P2P4 / pixelsPerMetric
P3P4_mm = P3P4 / pixelsPerMetric

distance_mm.append(P1P2_mm)
distance_mm.append(P1P3_mm)
distance_mm.append(P2P4_mm)
distance_mm.append(P3P4_mm)

distance_pixel.append(P1P2)
distance_pixel.append(P1P3)
distance_pixel.append(P2P4)
distance_pixel.append(P3P4)

以像素和毫米打印距离,即您的宽度和高度

print(distance_pixel)
print(distance_mm)

The pixelsPerMetric is your scale factor and gives the average number of pixels per mm. You can modify this code to work accordingly to your needs.

关于python - 鉴于从相机(校准)到我的物体的距离是固定的,我如何测量物体的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54912347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com