measure the distance between two object

Hello everyone;
I detect two different objects in Yolo v4 and I want to measure the distance between them, what is the method to measure the distance
can anyone help me...

7 Comments

Possibly. Doesn't YOLO give you the bounding boxes around the two objects? So you have the coordinates so you can compute the distance. Not sure of the problem. Try explaining better.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
thank you for your explaining, the two objects have boundary box and i can extract the coordinate, but the problem how can the measure the distance between the two boundary and between the boundary and object
Expplain what you'd want. Do you want the distance between closest edges? Do you want the distance between centers of the boxes? Do you want the Hausdorf distance?
If the box boundaries were properly chosen then the distance between the boundary and the object would be 0.
weam
weam on 25 Mar 2023
Edited: weam on 25 Mar 2023
Mr. Image Analyst and Mr. Walter Roberson , I want to measure the distance between the two objects.
Firstly, apply the traditional image processing (gray, threshold, edge detection), but this method fails because of a lot of the noise in the background sothat, recognize the objects in YOLO.
In addition, if the distance between the boundary box and the object would be 0, is that the distance between the boundary box and the object affect the accuracy when transform into the real world
I want to measure the distance between the two objects.
There is more than one way of measuring "distance" . We need you to say which meaning of "distance" you are using.
Are you trying to measure the shortest euclidean measure between the objects -- between the two pixels that are the shortest straight line between the two objects? If so then I already described finding the boundaries and using pdist2.
If you are having difficulty distinguishing the boundary edges of the objects, then you should probably step back and ask about that, posting original images and then also marked-up versions showing where the boundary should be and describing why it should be there.
meaning the distance (x, y). attachment the original image to see what I mean
and I will trying the pdist2.
thank you so much for your answer.

Sign in to comment.

Answers (2)

If you want the "real-world" distance, then you will either need to know the resolution of the pixels, or else you will need to have some kind of reference object in the image that can be compared against.
Bounding boxes will not work properly. If you trace the borders of the objects, and one object is not inside the other, then you can use pdist2() to find the distance from each point along one boundary to each point along the other boundary, and take the minimum distance as being the distance between the two.
If you take the bounding box that includes both objects, and construct a binary image that is true inside the bounding box only in places where neither object exists, then you might possibly be able to do something with the feret distance; https://blogs.mathworks.com/steve/2017/09/29/feret-diameter-introduction/?s_tid=srchtitle_feret%20distance_3

1 Comment

thank you so much for your answer, and I try with your advices

Sign in to comment.

Image Analyst
Image Analyst on 26 Mar 2023
Edited: Image Analyst on 26 Mar 2023
I have no idea why you decided to use YOLO for this task:
Maybe some data scientist told you to do that. They seem to think neural networks/deep learning are the solution for everything. Wrong. Kind of like how most novice image processing people think all algorithms begin with edge detection as step one. Again, wrong. Most "data scientists" come from a math, programming, or statistics background and they are sadly unaware of optics, lighting, imaging, color science, and traditional image processing methods like an imaging person would know, because they've never been trained in those topics. And it's not just me saying that. Imaging professors say that too. One said that most data scientists have only one tool (deep learning) in their toolbox : a hammer. If all they see if nails, they're fine but when they encounter a screw, they're in trouble. But an imaging scientist will have both hammers and screwdrivers in their toolbox and know which tool to use when.
This is a simple traditional task. No need to over complicate it with training a network.
This looks like a machine vision application where you are simply making caliper-type distance measurements in some kind of inspection rig. So in that case, your task is made simpler by the fact that the camera is bolted down, the lens is bolted down, the lighting is consistent, etc. So in that case you can do there steps:
  1. Rotate the image so that the black rod is horizontal. It will be a fixed, known angle for all your images.
  2. Crop your image to the known location of the black rod and light colored metal pieces on each end.
  3. Threshold to find the largest dark blob.
  4. Call regionprops to get it's bounding box. The left-most x value is the first element of the bounding box.
  5. Subtract the known, fixed value of the metal edge on the left from the x value of the black rod to get the distance. Multiply by the spatial calibration factor to get the distance in mm.
  6. Find the middle y value of the bounding box. That is the center of the black rod.
  7. Subtract the y location of wherever the top of the blue line is from the center of the black rod to get the vertical distance you've shown in blue. Multiply by the spatial calibration factor to get the distance in mm.
See attached spatial calibration demo.

3 Comments

Thank you so much for everything, for taking your time, for your better explanation, and for the attachment. then I will try to solve this problem with your advice.
Hi @Image Analyst, I see this approach is dependant on having a known distance, my question is how do I know the objects size without a reference, I know my camera's Intrinsics and Extrinsics, but not a known object or the distance from the camera to the object,
any Idea how this might be achieved?
Thanks
Well you could start by using the thin lens equation https://en.wikipedia.org/wiki/Thin_lens however if you don't know the distance from the object to the lens there is no way to know it's lateral size. Your image of the moon could be a 50 cm poster of the moon, or it could be the real moon with a diameter of thousands of kilometers.

Sign in to comment.

Asked:

on 24 Mar 2023

Commented:

on 26 Sep 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!