本教程的目的是解释如何使用vpImageSimulator类在给定摄影机位置投影平面场景的图像。例如,该功能可在视觉伺服模拟过程中使用,如教程:基于图像的视觉伺服中所述,以引入图像处理。
给定平面20cm×20cm正方形目标的图像作为下一幅图像中显示的图像,我们将在这里演示如何在给定相机位置投影该图像,以及如何获得结果图像。
#include <visp/vpDisplayX.h>
#include <visp/vpDisplayGDI.h>
#include <visp/vpImageIo.h>
#include <visp/vpImageSimulator.h>
int main()
{
try {
vpImage<unsigned char> target;
vpImageIo::read(target, "./target_square.pgm");
vpColVector X[4];
for (int i = 0; i < 4; i++) X[i].resize(3);
// Top left Top right Bottom right Bottom left
X[0][0] = -0.1; X[1][0] = 0.1; X[2][0] = 0.1; X[3][0] = -0.1;
X[0][1] = -0.1; X[1][1] = -0.1; X[2][1] = 0.1; X[3][1] = 0.1;
X[0][2] = 0; X[1][2] = 0; X[2][2] = 0; X[3][2] = 0;
vpImage<unsigned char> I(480, 640);
vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
vpHomogeneousMatrix cMo(0, 0, 0.35, 0, vpMath::rad(30), vpMath::rad(15));
vpImageSimulator sim;
sim.setInterpolationType(vpImageSimulator::BILINEAR_INTERPOLATION);
sim.init(target, X);
// Get the new image of the projected planar image target
sim.setCleanPreviousImage(true);
sim.setCameraPosition(cMo);
sim.getImage(I, cam);
try {
vpImageIo::write(I, "./rendered_image.jpg");
}
catch(...) {
std::cout << "Unsupported image format" << std::endl;
}
#if defined(VISP_HAVE_X11)
vpDisplayX d(I);
#elif defined(VISP_HAVE_GDI)
vpDisplayGDI d(I);
#else
std::cout << "No image viewer is available..." << std::endl;
#endif
vpDisplay::setTitle(I, "Planar image projection");
vpDisplay::display(I);
vpDisplay::flush(I);
std::cout << "A click to quit..." << std::endl;
vpDisplay::getClick(I);
}
catch(vpException e) {
std::cout << "Catch an exception: " << e << std::endl;
}
}
该程序的结果显示在下图中。