我已经在opencv-python中编写了一个代码,用于计算对象的高度和宽度上的像素数。可以说,
height = 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转换为图片 ?如何计算scaling_factor?
您需要一种将以像素为单位的度量转换为以米为单位的方法。可能的方法是按照准确性的顺序。