Visual Servoing Platform
version 3.5.0 under development (2022-02-15)
|
ViSP is interfaced with OpenCV third party. In this tutorial we explain how to convert data such as camera parameters or images from ViSP to OpenCV or vice versa.
ViSP camera parameters are implemented in vpCameraParameters class. If you want to calibrate a camera with ViSP tools follow Tutorial: Camera intrinsic calibration.
Let us recall the pinhole camera model implemented in ViSP. In this model, a scene view is formed by projecting 3D points into the image plane using a perspective transformation.
where:
When , the previous equation is equivalent to the following:
Real lenses usually have some radial distortion. So, the above model is extended as:
where is the first order radial distortion. Higher order distortion coefficients are not considered in ViSP.
Even if OpenCV notations are different, this model is exactly the same then the one used in OpenCV and described here where higher order OpenCV distortion parameters are turned to 0.
The following table gives the correspondences between ViSP and OpenCV parameters:
From a coding point of view, let us consider the following code also available in tutorial-bridge-opencv-camera-param.cpp where we initialize camera parameters using ViSP:
These parameters could be used to initialize OpenCV camera parameters:
ViSP image is implemented in vpImage class, while OpenCV images in cv::Mat
class. All the functions that allow image conversion from ViSP to OpenCV or vice versa are implemented in vpImageConvert.
Some image conversion examples could be found in tutorial-bridge-opencv-camera-param.cpp. There is also tutorial-bridge-opencv-image.cpp from where the following snippets are extracted.
Color image conversion
The following code allows to read a color image with ViSP:
and then convert the image in OpenCV using:
before saving the converted color image
Grey scale image conversion
The following code allows to read a grey scale image with ViSP. Note here that if the input image (ie. monkey.jpeg
) is a color image, vpImageIo::read() converts implicitly the color image in a grey scale image:
and then convert the image in OpenCV using:
before saving the converted grey image
Color image conversion
The following code allows to read a color image with OpenCV:
and then convert the image in ViSP using:
before saving the converted color image
Grey scale image conversion
The following code allows to read a grey scale image with OpenCV:
and then convert the image in ViSP using:
before saving the converted grey image
ViSP matrices are implemented in different classes:
while OpenCV matrices are always implemented as a cv::Mat
.
Up to now, there is no specific function that does the conversion between ViSP and OpenCv matrices.
Note that each element of a ViSP matrix is a double and that ViSP matrices are row-major.
In the following subsections we illustrate how to convert a vpMatrix to a cv::Mat
, but the code could be easily adapted to other ViSP specific matrices as long as the corresponding OpenCV matrix contains double elements and that it has the same size as the ViSP specific matrix.
Deep copy conversion
To convert the previous matrix in a vpMatrix using a deep copy, you may use:
The content of the matrices is now the following:
M_cv: [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12] M: 1 2 3 4 5 6 7 8 9 10 11 12
Deep copy conversion
To convert the previous matrix in a cv::Mat
using a deep copy, you may use:
The content of the matrices is now the following:
M: 1 2 3 4 5 6 7 8 9 10 11 12 M_cv_deep: [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12]
Pointer assignment, without deep copy conversion
For performance issues, there is also the possibility to just copy the data pointer. To this end, you may use:
The content of the matrices is now the following:
M: 1 2 3 4 5 6 7 8 9 10 11 12 M_cv: [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12]
As a side effect, you may know that modifying one of the matrices (ie. M
like below or M_cv
) will affect the content of both matrices. Let us illustrate this behavior, where the diagonal of M
is set to 1:
As expected, the content of M_cv
matrix is also changed:
Set M = eye M: 1 0 0 0 0 1 0 0 0 0 1 0 M_cv: [1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 0]