Object Distance Estimation
Implementation
Last updated
Last updated
/*Finds the distance of a detected object in real-world units
* camera_center_pixel_x = horizontal_resolution/2
* camera_center_pixel_y = vertical_resolution/2
* Focal_Length: focal length of the camera
* real_object_width: measured width of the game element (must be in the same units as the focal length)
* object_width_in_frame: width of the bounding box (in pixels)
*/
public double[] Distance_finder(
double Focal_Length,
double camera_center_pixel_x,
double camera_center_pixel_y,
double real_object_width,
double object_width_in_frame,
double object_x_pixel_coordinate,
double object_y_coordinate){
// how far in front the object is from the camera
distance_z = (real_object_width * Focal_Length)/object_width_in_frame;
// how far to the side the object is from the camera
distance_x = (distance_z / Focal_Length) *
(object_x_pixel_coordinate - camera_center_pixel_x);
// how far above the object is from the camera
distance_y = (distance_z / Focal_Length) *
(object_y_pixel_coordinate - camera_center_pixel_y);
return {distance, distance_x, distance_y}
}