Anonymous
Opencv: рассчитать расстояние между точками на разной высоте пола
Сообщение
Anonymous » 14 янв 2025, 09:10
Мы используем открытое резюме для автоматизации бота, играющего в футбол. На полу нам нужно определить расстояние между ботом и мячом.
Наш бот имеет высоту около 16 см, а диаметр мяча 6 см. Из-за этой разницы в высоте при движении бота к углам рассчитанное расстояние неточно.
Я попробовал выполнить калибровку камеры и попробовал метод матрицы гомографии.
Метод 1: гомография
Код: Выделить всё
tl = (61,438)
tr = (57,73)
bl = (540,426)
br = (538,55)
pts1 = np.float32([tl, bl, tr, br])
pts2 = np.float32([[0,0], [0, 2300], [1500, 0], [1500, 2300]])
# Compute the homography matrix
H, _ = cv2.findHomography(pts1, pts2)
def image_to_real(img_point):
img_point = np.array([[img_point]], dtype=np.float32)
real_point = cv2.perspectiveTransform(img_point, H)
return real_point[0][0]
real_point1 = image_to_real(bot_center)
real_point2 = image_to_real(ball_centers)
distanceNew = np.linalg.norm(real_point1 - real_point2)
Метод 2: метод, используемый для расчета расстояния после получения матрицы камеры и dist_coeffs
Код: Выделить всё
def calculate_real_world_distance(pt1, pt2, frame):
camera_matrix = np.array([[1.18093005e+03, 0, 4.88023267e+02],
[0, 9.10212133e+02, 3.77605255e+02],
[0, 0, 1]])
dist_coeffs = np.array([[-0.45134717, 1.1857564, 0.06250472, 0.09753945, -0.2519004]])
height_object1 = 0.18 # meters
height_object2 = 0.6 # meters
height_camera = 2.40
pt1 = (float(pt1[0]), float(pt1[1]))
pt2 = (float(pt2[0]), float(pt2[1]))
pt1 = np.array([pt1], dtype=np.float32)
pt2 = np.array([pt2], dtype=np.float32)
# Convert points to normalized image coordinates (undistorted)
pts_undistorted = cv2.undistortPoints(np.array([pt1, pt2]), camera_matrix, dist_coeffs)
# Extract normalized points
x1, y1 = pts_undistorted[0][0]
x2, y2 = pts_undistorted[1][0]
# Compute real-world scale factors from height difference (ceiling-mounted camera)
Z1 = height_camera - height_object1
Z2 = height_camera - height_object2
# Convert pixel points to real-world coordinates using height ratio
X1, Y1 = (x1 * Z1), (y1 * Z1)
X2, Y2 = (x2 * Z2), (y2 * Z2)
# Compute the Euclidean distance
distance = np.sqrt((X2 - X1)**2 + (Y2 - Y1)**2 + (Z2 - Z1)**2)
print(distance)
return distance * 100
Расстояние по-прежнему не во всех точках.
Подробнее здесь:
https://stackoverflow.com/questions/793 ... in-a-floor
1736835054
Anonymous
Мы используем открытое резюме для автоматизации бота, играющего в футбол. На полу нам нужно определить расстояние между ботом и мячом. Наш бот имеет высоту около 16 см, а диаметр мяча 6 см. Из-за этой разницы в высоте при движении бота к углам рассчитанное расстояние неточно. Я попробовал выполнить калибровку камеры и попробовал метод матрицы гомографии. Метод 1: гомография [code] tl = (61,438) tr = (57,73) bl = (540,426) br = (538,55) pts1 = np.float32([tl, bl, tr, br]) pts2 = np.float32([[0,0], [0, 2300], [1500, 0], [1500, 2300]]) # Compute the homography matrix H, _ = cv2.findHomography(pts1, pts2) def image_to_real(img_point): img_point = np.array([[img_point]], dtype=np.float32) real_point = cv2.perspectiveTransform(img_point, H) return real_point[0][0] real_point1 = image_to_real(bot_center) real_point2 = image_to_real(ball_centers) distanceNew = np.linalg.norm(real_point1 - real_point2) [/code] Метод 2: метод, используемый для расчета расстояния после получения матрицы камеры и dist_coeffs[code]def calculate_real_world_distance(pt1, pt2, frame): camera_matrix = np.array([[1.18093005e+03, 0, 4.88023267e+02], [0, 9.10212133e+02, 3.77605255e+02], [0, 0, 1]]) dist_coeffs = np.array([[-0.45134717, 1.1857564, 0.06250472, 0.09753945, -0.2519004]]) height_object1 = 0.18 # meters height_object2 = 0.6 # meters height_camera = 2.40 pt1 = (float(pt1[0]), float(pt1[1])) pt2 = (float(pt2[0]), float(pt2[1])) pt1 = np.array([pt1], dtype=np.float32) pt2 = np.array([pt2], dtype=np.float32) # Convert points to normalized image coordinates (undistorted) pts_undistorted = cv2.undistortPoints(np.array([pt1, pt2]), camera_matrix, dist_coeffs) # Extract normalized points x1, y1 = pts_undistorted[0][0] x2, y2 = pts_undistorted[1][0] # Compute real-world scale factors from height difference (ceiling-mounted camera) Z1 = height_camera - height_object1 Z2 = height_camera - height_object2 # Convert pixel points to real-world coordinates using height ratio X1, Y1 = (x1 * Z1), (y1 * Z1) X2, Y2 = (x2 * Z2), (y2 * Z2) # Compute the Euclidean distance distance = np.sqrt((X2 - X1)**2 + (Y2 - Y1)**2 + (Z2 - Z1)**2) print(distance) return distance * 100 [/code] Расстояние по-прежнему не во всех точках. Подробнее здесь: [url]https://stackoverflow.com/questions/79354083/open-cv-calculate-distance-between-points-at-different-heights-in-a-floor[/url]