Calculate image size of an object from real size - opencv

I try to place a car(2D image of the car) on the road in the image, but I need to calculate a car size in pixels for the chosen horizontal position. I've found this
formula. It has object height(mm) which I need, but I don't have a distance to object(mm). Is there any way to calculate the distance from the camera to the horizontal line where car placed?
I knew camera matrix(fx,fy,cx,cy) and the real size of the car

If you know the size of the car, then it is possible to know the distance to it. However, you also need the information from the sensor size and focal position.
As depicted in this image , the distance D can be deduced from d, h and H by a simple conversion: d/D = h/H. Therefore, D=(H*d)/h. With h the size of the sensor in pixel, d the focal distance in mm and H the object size in pixels. D is the distance to the object in mm.

Related

It is possible to calculate distance between object to camera without object dimensions?

My goal is to calculate distance from an object that detected based on color.
When given that the camera and the object are horizontal, and also given and real relative height between them.
In the illustration of the problem, only h is known, and the horizontally condition.
Need to find D
Problem illustration

How to calculate the distance between object and camera, knowing the pixels occupied by the object in an image

By using the segmentation I am able to find the number of pixels occupied by an object in an image. Now I need to kind the distance by using the pixels occupied.
object real dimensions (H x W) = 11 x 5.5 cm.
The object is placed at 50 cm distance pixels occupied are = 42894
The object is placed at 60 cm distance pixels occupied are = 31269.
The total pixel in an image = 480 x 640 = 307200.
what is the distance if the image occupies 22323 pixels ???
The distance to the object is 67.7cm
Please read https://en.wikipedia.org/wiki/Pinhole_camera_model
Image size is inversely proportional to the distance. Repeat your experiement for a few distances and plot a size vs distance to see for yourself.
Of course this is a simplyfied model that only works for a fixed focal length.

how to get the real distance between two points in image?

HI I am using opencv to detect two objects in frame and calculate the distance between them in pixel but I need to calculate the distance between them in meter can you give the realtionship between the meter and pixel
This is generally impossible: From a single image the scale of reconstructed 3d points is unknown.
Your options are either to
Add more views, and calculate the depth using stereo vision algorithms.
Use knowledge about the size of the objects to determine the distance.
Edit
Given the depth z, camera calibration matrix K and image point x we can get the corresponding 3D-point Xas:
X = z * inv(K) * x
with x is in homogeneous coordinates.
When you have the two 3D points on the object, calculating the distance is trivial.
"thank you I try to use the size but I need the relationship between the real size in meter and the size in pixel with known distance between camera and the object"
What you need to do is have the object is question at a certain distance from the camera and measure the pixels. Then, move this object further or closer from/to the camera and measure the change pixels again.
From this you can ascertain the ratio of the change in pixels which is equivalent to the change in distance, thus the distance of the object from the camera. It's just a simple differential equation.
Assuming the size of the object in question is fixed or scaled to the object used to calculate the ratio, this approach should provide a rough estimate of the distance of the objects to the camera.
You then need to use this ratio to help with calculating the distance between the objects as it would be somewhat inverse proportional to the distance between the objects when their distance to the camera increases.
However this method is very messy and can be inefficient. A better approach would be using two different cameras and looking at the disparity between both as mentioned by Hannes

calculate the real distance between to point using image

I do some image processing task in 3D and I have a problem.
I use a simulator which provides me an special kind of cameras which can tell the distance between the position of camera and any arbitrary point, using the pixels of that point in the image of camera. For example I can get the distance between camera and the object which is placed in pixel 21:34.
Now I need to calculate the real distance between two arbitrary pixels in the image of camera.
It is easy when camera is vertical and placed on the above of the field and all objects are on the ground but when camera is horizontal the depth of objects in image is different.
So, how should I do?
Simple 3D reconstruction will accomplish this. The distance from camera to points in 3D is along optical axis that is Z, which you already have. You will need X, Y as well:
X = u*Z/f;
Y = v*Z/f,
where f is camera focal length in pixels, Z your distance in mm or meters and u,v is an image centered coordinates: u = column-width/2, v = height/2-row. Note the asymmetry due to the fact that rows go down while Y and v go up. As soon as you get your X, Y, Z the distance in 3D is given by Euclidean formula:
dist = sqrt((X1-X2)2+(Y1-Y2)2+(Z1-Z2)2)

How to find object height from an image when distance from an object is given but camera is stationary and tilted

I know this formula.
distance to object (mm) = focal length (mm) * real height of the object (mm) * image height (pixels) / object height (pixels) * sensor height (mm)
But it is applicable when sensor(camera) is placed parallel. What will be the modification in the formula if camera is tilted to an angle.
Note : The Angle and all the other values are known.
Re-define "object height" as "distance between two known 3D points A and B".
If your camera is calibrated and the distances da and db of A and B from the camera's centre are known, along with their pixel projection pA and pB, then it is simply a matter of expressing A and B in camera coordinate frame. You back-project pA and pB into two rays, find the points A and B on those rays at the known distances, then compute the length of the vector (A - B).

Resources