author | layout | title | slug | date | categories | image | tags | description |
---|---|---|---|---|---|---|---|---|
pgaleone |
post |
Fixed camera setup for object localization and measurement |
Fixed camera setup for object localization and measurement |
2018-01-25 00:00:00 -0800 |
computer-vision |
images/computer-vision.jpg |
machine-learning |
Fixed camera setup for object localization and measurement |
A common task in Computer Vision is to use a camera for localize and measure certain objects in the scene. In the industry is common to use images of objects on a high contrast background and use Computer Vision algorithms to extract useful information.
There's a lot of literature about the computer vision algorithm that we can use to extract the information, but something that's usually neglected is how to correctly setup the camera in order to correctly address the problem. This post aim is to shed light on this subject.
The problem we aim to solve with Computer vision is to measure (in mm) objects of unknown shape, but with known thickness
The camera setup for this kind of problem consists in:
- Finding the correct working distance (distance between the object surface and the lenses)
- Choose the right focal length.
In the following I'm going to show a possible 3 steps approach that can be used to correctly setup the camera.
Without entering in the detail of camera calibration, all we need to know is that the calibration process allow to represent the camera intrinsic parameters as a
where
In case of a squared sensor
The theory of the camera resectioning gives us the relation between the estimated focal lengths (in px) and the real focal length (in mm).
Since we're considering
In short, the estimated focal length in pixel is the real focal length
This scaling factor is extremely important, because it measure the number of pixels in a millimeter of sensor.
There's a relation between the size of an object in the scene and the size of the object on the image plane. This relation comes from the thin lenses equiation.
Given
That in English it can be read as "the working distance in millimeters is the object real size in millimeter times the focal length in millimiters, divived by the object size on the image sensor".
Hence it's pretty easy to measure the size of the object in millimeters, when every other variable is know:
There are 2 constraints that have to be satisfied when designing an object measurement system:
- Being able to measure the whole object
- Minimum accuracy
The constraint on the ability of measure the whole object can be satisfied analyzing the Field of View (FOV) of the camera.
Let
Since the object can be in any possible orientation we can consider only the smaller FOV when finding the right distance for the camera (because this is the constrained one):
It's obvious that
The following images will make everything clear:
On the
This means that our working distance (noted as d in the picture) can be found exactly.
Please note that we're creating an object measurement application, hence we can exploit other information regard the object in order to improve the precision.
In fact, if we know in advance the set of thickness (in mm)
In practice, the real working distance (that's the one we're really interested) can be found as:
The offset term is an optional term, that usually can be found on the camera datasheet, that's the relative position of the sensor with respect to the measurement point (in the order of $$[0, 5]$$mm usually).
WARNING: The working distance computed in this way is a theoretical estimation of the real working distance since the camera model we're using is the pinhole using, hence we're using the thin lens equation as the foundation for our reasoning. In practice, the working distance to use in a real-world application must be computed using a software solution (exploiting the information about the size of a known object and the measured object in pixel) since the thin lens equations can't model complex lens system in a precise way. Hence, you can use all the content of this article to get a rough estimation of the working distance in order to properly setup the camera physically.
The constraint on the accuracy can be formalized as follow:
where
If, for instance, the requirement is to have an accuracy of 3mm, the inequality becomes:
From the relation of the object in the scene on the object on the sensor (now with the real working distance) we can measure the number of pixels per millimiter, in fact
$$X = \frac{\text{WD}_r \cdot \frac{x}{m}}{f} \Leftrightarrow x = \frac{X f m}{\text{WD}r} = \frac{Xf{xy}}{\text{WD}_r}$$
So, now is extremely easy to calculate the number of pixels per millimiter in the scene and check if the previous relation holds:
if the relation holds, we have correctly setup our system (but another safety margin can be to increase the number of pixels per accuracy required and hence change that 1 to something bigger).
Instead, if this relation does not hold we have to change the moving part of our system in order to satisfy every requirement:
- Check if the thickness of the object you're measuring can help you making the camera closed to the object
- Change the focal length (and repeat every calculation, but only after a new calibration!)
- Evaluate the usage of more cameras and stitch the images together
- Last resort: change the camera(s)
One last tip: the relation