I'd like to calculate size of object in a photo which includes both a target object and a reference one.
I think what I'd like to do is what this software achieves (I don't know how precise this software is) https://itunes.apple.com/us/app/photo-meter-picture-measuring/id579961082?mt=8
I've already found, in general, it's called photogrammetry and seems active research field.
How would you find the height of objects given an image? https://physics.stackexchange.com/questions/151121/can-i-calculate-the-size-of-a-real-object-by-just-looking-at-the-picture-taken-b
But, I can not find
- what is basic way to measure a object in a photo with a reference object.
- a way to implement it or standard open source for it.
Update
- I can not utilize the distance of the object and reference from the camera.
- The reference and target are on the (approximately) same plane.
The most basic way is to simply determine the pixel dimensions of your reference object and use the metric dimensions to derive a mm / pixel factor. Then multiply the pixel dimensions of the object you want to measure with that factor. This only works if both objects are within the same plane parallel to your sensor.
For anything else you require distance information. You can either measure them or make some asumptions depending on how accurate you have to be.
The way to implement such measurements depends on your requirements.
Due to your assumption The reference and target are on the (approximately) same plane. you can apply the method "Algorithm 1: planar measurements" described in
Antonio Criminisi. "Single-View Metrology: Algorithms and Applications (Invited Paper)". In: Pattern Recognition. Ed. by Luc Van Gool. Vol. 2449. Lecture Notes in Computer Science. Springer Berlin Heidelberg, 2002, pp. 224-239.
The method allows you to measure the distance between two points that lie in the same plane.
Basically
where
p
is a point in your image expressed in homogeneous coordinates,P
is the corresponding point in the 3D world plane, also expressed in homogeneous coordinates, andH
is a 3x3 matrix called homography matrix and*
is the matrix-vector multiplication.The unit of measures of
p
are pixels, so for example ifp
is a point at rowr
and columnc
it will be expressed as[r,c,1]
. The unit of measure ofP
are your world units, for example meters, you can assume that your 3D world plane is the planeZ=0
and soP
is expressed as the homogeneous vector[X,Y,1]
.So a little modification to the "Algorithm 1: planar measurements." is the following:
Given an image of a planar surface estimate the image-to-world homography matrix H. Assume that the 9 elements of H are dimensionless.
In the image, select two points
p1=[r1,c1,1]
andp2=[r2,c2,1]
belonging to the reference object.Back-project each image point into the world plane via (1) to obtain the two world points
P1
andP2
. You do the matrix-vector multiplication and then you divide the resulting vector for its third component in order to get an homogeneous vector. For exampleP1=[X1,Y1,1]
isP1=[(c1*h_12 + h_11*r1 + h_13)/(c1*h_32 + h_31*r1 + h_33),(c1*h_22 + h_21*r1 + h_23)/(c1*h_32 + h_31*r1 + h_33),1]
. Assume for the moment that the nine elements ofH
are dimensionless, that means that the unit of measure ofX1
,Y1
,X2
,Y2
is pixel.Compute the distance
R
betweenP1
andP2
that isR=sqrt(pow(X1-X2,2)+pow(Y1-Y2,2)
,R
is still expressed in pixel. Now, sinceP1
andP2
are on the reference object it means that you know the distance between them in meters, let's call that distance, expressed in meters,M
.Compute the scale factor
s
ass=M/R
, the dimension ofs
is meter per pixel.Multiply each element of
H
bys
and callG
the new matrix you get. Now the elements ofG
are expressed in meter per pixel.Now, in the image select two points
p3
andp4
belonging to the target object.Back-project
p3
andp4
viaG
in order to getP3
andP4
.P3=G*p3
andP4=G*p4
. Again divide each vectors by its third element.P3=[X3,Y3,1]
andP4=[X4,Y4,1]
and nowX3
,Y3
,X4
andY4
are expressed in meters.Compute the desired target distance
D
betweenP3
andP4
that isD=sqrt(pow(X3-X4,2)+pow(Y3-Y4,2)
.D
is now expressed in meters.The appendix of the above mentioned paper explains how to compute
H
or for example you can use OpenCVcv::findHomography
: basically you need at least four correspondences between points in the real world and points in your image.Another source of information on how to estimate
H
is inJOHNSON, Micah K.; FARID, Hany. Metric measurements on a plane from a single image. Dept. Comput. Sci., Dartmouth College, Tech. Rep. TR2006-579, 2006.
If you need also to estimate the accuracy of your measurements you can find the details in
A. Criminisi. Accurate Visual Metrology from Single and Multiple Uncalibrated Images. Distinguished Dissertation Series. Springer-Verlag London Ltd., Sep 2001. ISBN: 1852334681.
An example in C++ with OpenCV:
Input image, in this case your reference object is the A4 sheet and the target object is the slide rule:
Input image with measures, the red crosses are used to estimate the homography:
The rectified image: