【问题标题】:How to calculate dimension of image by knowing pixels and distance between camera and object?如何通过知道像素和相机与物体之间的距离来计算图像的尺寸?
【发布时间】:2019-11-26 06:10:30
【问题描述】:

我在 opencv-python 中编写了一个代码,用于计算对象高度和宽度的像素数。 可以说,

高度 = 567 像素 宽度 = 324 像素 我还有 KNOWN_FOCAL_LENGTH 的相机和 原始物体和相机之间的距离

# Getting the biggest contour
cnt = max(contours, key = cv2.contourArea)
cv2.drawContours(image, cnt, -1, (0, 255, 0), 3)

peri = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.005* peri, True)

# get the bounding rect
x, y, w, h = cv2.boundingRect(approx)

print("Width in pixels :  {},  Height in pixels :  {}".format(w,h))


# draw a green rectangle to visualize the bounding rect
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
WidthData = "Width :  {} mm".format(round(w/scaling_factor,2))
HeightData = "Height :  {} mm".format(round(h/scaling_factor,2))
textData = WidthData + ", " + HeightData
cv2.putText(image, textData, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 2)

print(WidthData)
print(HeightData)

cv2.imshow('Result', image)

如何计算可以将 number_of_pixels 转换为 original_length of 图片 ? 如何计算scaling_factor

【问题讨论】:

    标签: opencv camera-calibration object-recognition


    【解决方案1】:

    您需要一种将像素单位转换为米单位的方法。可能的方法是按准确度排序。

    • 查看摄像头传感器的物理尺寸,例如来自相机的规格表,或者,如果幸运的话,来自存储在图像中的元数据以及像素(EXIF 标头)
    • 在场景中放置一个已知大小的对象。
    • 直接将焦距从像素缩放到镜头给定的毫米。

    【讨论】:

      猜你喜欢
      • 2020-11-17
      • 2020-11-17
      • 2012-04-13
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      相关资源
      最近更新 更多