There are many tutorials on how to calculate the distance between a camera and an object. Is it possible to calculate the approximate distance between a person detected and the camera using OpenCV?.
-
3What if you consider the person as an object then use those "many tutorials"? – hkchengrex Oct 03 '18 at 12:48
1 Answers
Yes it is possible. Like mentioned by @hkchengrex consider your face an object. There's plenty of methods. I'd recommend SIFT Feature Matching of the methods described following that link.
Here are roughly the required steps:
- Take a picture of the person and measure the distance manually.
- Crop this picture to only contain the person.
- Extract the image features (e.g. as sift descriptor)
- Take a second picture with the same person but unknown distance.
- Detect the person via sift matching (see link above)
- Compute a transformation between those two sift feature vectors
- Apply the transformation to the distance measured in 1.
Best start at the link provided and further SIFT tutorials in opencv. The required approach is a very simple one and will only work if the person in the picture that is being examined is very similar to the person of picture one. For more advanced approaches I'd refer to scientific papers. Search for "person detection".
In reply to the comments
TL;DR person with same height/width in reality but displayed smaller/larger in the image can be measured regarding distance.
The depicted approach works under the hood as follows. The person (=cropped image) captured at step 2 can be found in any future image as long as he/she appears very similar. In the new image it will give you the rectangular region where the person is located. As the dimensions of this rectangle are now smaller/larger you can take those changes to compute the transformation (which is basically intercept theorem) and thereby the new distance.
What does this mean for a general approach measuring ANY person?
- In case the person has the same width/height as the person from step 2 this process works flawlessly. In case they are of similar but but not identical height/width there will be calculation errors. But the results MAY still suffice for your use case. (You can define a generic human e.g. 1,8m of height and XX of width). Nevertheless SIFT might be a bit too specific here. Sorry I'd just refer you to google to see what works best.
- If your camera is fixated and the recorded scene doesn't change too much I'd just define a ground plane and manually annotate every pixel projected on this plane with a depth value. So you only have to detect the arbitrary person, see where their feet touch the ground plane and look up this pixel's defined depth value.
- If the use case has higher demands you'd have to measure depth in a more complex fashion. This can be done using a stereo-camera rig, a depth sensor or an image sequence via structure from motion.
So there is not the "one can do all" method in OpenCV. It always depends on the use case, the environment and a combination of quite elaborate methods.

- 269
- 1
- 9
-
Should i do this for every person prior determining the distance.Say, if a random person comes and i wanted to get the distance. will this method be possible?Thanks. – anu010291 Oct 05 '18 at 03:45
-
I think the answer'll be a bit too long for a comment. I'll update my reply in a minute to be more specific regarding the proposed approach. – TeddybearCrisis Oct 05 '18 at 11:04