Are you sure you want to delete this access key?
comments | description | keywords |
---|---|---|
true | Learn how to calculate distances between objects using Ultralytics YOLO11 for accurate spatial positioning and scene understanding. | Ultralytics, YOLO11, distance calculation, computer vision, object tracking, spatial positioning |
Measuring the gap between two objects is known as distance calculation within a specified space. In the case of Ultralytics YOLO11, the bounding box centroid is employed to calculate the distance for bounding boxes highlighted by the user.
Watch: Distance Calculation using Ultralytics YOLO11
Distance Calculation using Ultralytics YOLO11 |
---|
![]() |
???+ tip "Distance Calculation"
- Click on any two bounding boxes with Left Mouse click for distance calculation
- Mouse Right Click will delete all drawn points
- Mouse Left Click can be used to draw points
???+ warning "Distance is Estimate"
Distance will be an estimate and may not be fully accurate, as it is calculated using 2-dimensional data,
which lacks information about the object's depth.
!!! example "Distance Calculation using Ultralytics YOLO"
=== "Python"
```python
import cv2
from ultralytics import solutions
cap = cv2.VideoCapture("path/to/video.mp4")
assert cap.isOpened(), "Error reading video file"
# Video writer
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
video_writer = cv2.VideoWriter("distance_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
# Initialize distance calculation object
distancecalculator = solutions.DistanceCalculation(
model="yolo11n.pt", # path to the YOLO11 model file.
show=True, # display the output
)
# Process video
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or processing is complete.")
break
results = distancecalculator(im0)
print(results) # access the output
video_writer.write(results.plot_im) # write the processed frame.
cap.release()
video_writer.release()
cv2.destroyAllWindows() # destroy all opened windows
```
DistanceCalculation()
ArgumentsHere's a table with the DistanceCalculation
arguments:
{% from "macros/solutions-args.md" import param_table %} {{ param_table(["model"]) }}
You can also make use of various track
arguments in the DistanceCalculation
solution.
{% from "macros/track-args.md" import param_table %} {{ param_table(["tracker", "conf", "iou", "classes", "verbose", "device"]) }}
Moreover, the following visualization arguments are available:
{% from "macros/visualization-args.md" import param_table %} {{ param_table(["show", "line_width", "show_conf", "show_labels"]) }}
The DistanceCalculation
class works by tracking objects across video frames and calculating the Euclidean distance between the centroids of selected bounding boxes. When you click on two objects, the solution:
The implementation uses the mouse_event_for_distance
method to handle mouse interactions, allowing users to select objects and clear selections as needed. The process
method handles the frame-by-frame processing, tracking objects, and calculating distances.
Distance calculation with YOLO11 has numerous practical applications:
To calculate distances between objects using Ultralytics YOLO11, you need to identify the bounding box centroids of the detected objects. This process involves initializing the DistanceCalculation
class from Ultralytics' solutions
module and using the model's tracking outputs to calculate the distances.
Using distance calculation with Ultralytics YOLO11 offers several advantages:
Yes, you can perform distance calculation in real-time video streams with Ultralytics YOLO11. The process involves capturing video frames using OpenCV, running YOLO11 object detection, and using the DistanceCalculation
class to calculate distances between objects in successive frames. For a detailed implementation, see the video stream example.
To delete points drawn during distance calculation with Ultralytics YOLO11, you can use a right mouse click. This action will clear all the points you have drawn. For more details, refer to the note section under the distance calculation example.
The key arguments for initializing the DistanceCalculation
class in Ultralytics YOLO11 include:
model
: Path to the YOLO11 model file.tracker
: Tracking algorithm to use (default is 'botsort.yaml').conf
: Confidence threshold for detections.show
: Flag to display the output.For an exhaustive list and default values, see the arguments of DistanceCalculation.
Press p or to see the previous file or, n or to see the next file
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?