1#include <visp3/rbt/vpRBInitializationHelper.h>
2#include <visp3/rbt/vpRBTracker.h>
4#include <visp3/vision/vpPose.h>
5#include <visp3/core/vpDisplay.h>
6#include <visp3/core/vpImage.h>
7#include <visp3/io/vpImageIo.h>
8#include <visp3/core/vpIoTools.h>
10#ifdef VISP_HAVE_MODULE_GUI
11#include <visp3/gui/vpDisplayFactory.h>
25 while (!fileId.fail() && ((c ==
'#') || (c ==
'\n') || (c ==
'\r'))) {
27 fileId.ignore(std::numeric_limits<std::streamsize>::max(), fileId.widen(
'\n'));
40 std::fstream finitpos;
41 finitpos.open(filename.c_str(), std::ios::out);
48#ifdef VISP_HAVE_MODULE_GUI
51void vpRBInitializationHelper::initClick(
const vpImage<T> &I,
const std::string &initFile,
bool displayHelp,
vpRBTracker &tracker)
53 std::cout <<
"Starting init click!" << std::endl;
59 std::string ext =
".init";
60 std::string str_pose =
"";
61 size_t pos = initFile.rfind(ext);
64 std::fstream finitpos;
67 std::string poseSavingFilename;
68 if (poseSavingFilename.empty()) {
69 if (pos != std::string::npos)
70 str_pose = initFile.substr(0, pos) +
".0.pos";
72 str_pose = initFile +
".0.pos";
74 finitpos.open(str_pose.c_str(), std::ios::in);
78 finitpos.open(poseSavingFilename.c_str(), std::ios::in);
79 ss << poseSavingFilename;
81 if (finitpos.fail()) {
82 std::cout <<
"Cannot read " << ss.str() << std::endl <<
"cMo set to identity" << std::endl;
86 for (
unsigned int i = 0;
i < 6;
i += 1) {
87 finitpos >> init_pos[
i];
93 std::cout <<
"Tracker initial pose read from " << ss.str() <<
": " << std::endl << last_cMo << std::endl;
100 std::cout <<
"No modification : left click " << std::endl;
101 std::cout <<
"Modify initial pose : right click " << std::endl;
117 vpDisplay *d_help =
nullptr;
127 ss.str(std::string());
133 if (pos != std::string::npos) {
141 std::cout <<
"Load 3D points from: " << ss.str() << std::endl;
142#if (VISP_CXX_STANDARD > VISP_CXX_STANDARD_98)
143 finit.open(ss.str());
145 finit.open(ss.str().c_str());
148 std::cout <<
"Cannot read " << ss.str() << std::endl;
149 throw vpException(
vpException::ioError,
"Cannot open model-based tracker init file %s", ss.str().c_str());
152#ifdef VISP_HAVE_MODULE_IO
156 const std::string imgExtVec[] = {
".ppm",
".pgm",
".jpg",
".jpeg",
".png" };
158 bool foundHelpImg =
false;
159 if (pos != std::string::npos) {
160 for (
size_t i = 0;
i < 5 && !foundHelpImg;
i++) {
161 dispF = initFile.substr(0, pos) + imgExtVec[
i];
166 for (
size_t i = 0;
i < 5 && !foundHelpImg;
i++) {
167 dispF = initFile + imgExtVec[
i];
173 std::cout <<
"Load image to help initialization: " << dispF << std::endl;
177 vpImage<vpRGBa> Iref;
179#if defined(VISP_HAVE_DISPLAY)
183 d_help->init(Iref, winXPos +
static_cast<int>(width) + 80, winYPos,
"Where to initialize...");
191 if (d_help !=
nullptr) {
204 finit.ignore(256,
'\n');
205 std::cout <<
"Number of 3D points " << n3d << std::endl;
207 throw vpException(
vpException::badValue,
"In %s file, the number of 3D points exceed the max allowed",
211 std::vector<vpPoint> P(n3d);
212 for (
unsigned int i = 0;
i < n3d;
i++) {
216 vpColVector pt_3d(4, 1.0);
220 finit.ignore(256,
'\n');
222 vpColVector pt_3d_tf = pt_3d;
223 std::cout <<
"Point " <<
i + 1 <<
" with 3D coordinates: " << pt_3d_tf[0] <<
" " << pt_3d_tf[1] <<
" "
224 << pt_3d_tf[2] << std::endl;
226 P[
i].setWorldCoordinates(pt_3d_tf[0], pt_3d_tf[1], pt_3d_tf[2]);
231 bool isWellInit =
false;
232 while (!isWellInit) {
233 std::vector<vpImagePoint> mem_ip;
234 for (
unsigned int i = 0;
i < n3d;
i++) {
235 std::ostringstream text;
236 text <<
"Click on point " <<
i + 1;
240 for (
unsigned int k = 0; k < mem_ip.size(); k++) {
245 std::cout <<
"Click on point " <<
i + 1 <<
" ";
249 mem_ip.push_back(ip);
256 std::cout <<
"with 2D coordinates: " << ip << std::endl;
263 std::cout <<
"Before optim: " << m_cam << std::endl;
265 std::cout <<
"Pose computation from points failed!" << std::endl;
266 for (
unsigned int i = 0;
i < n3d; ++
i) {
267 std::cout <<
"Point " <<
i <<
": " << std::endl;
268 std::cout <<
" 3D: " << pose.
getPoints()[
i].get_oP().t() << std::endl;
269 std::cout <<
"2D: " << pose.
getPoints()[
i].get_x() <<
", " << pose.
getPoints()[
i].get_y() << std::endl;
273 vpRBFeatureTrackerInput frame;
274 tracker.updateRender(frame, m_cMo);
275 tracker.displaySilhouette(I, frame);
298 if (poseSavingFilename.empty())
303 if (d_help !=
nullptr) {
310template VISP_EXPORT
void vpRBInitializationHelper::initClick<unsigned char>(
const vpImage<unsigned char> &I,
const std::string &initFile,
bool displayHelp,
vpRBTracker &tracker);
311template VISP_EXPORT
void vpRBInitializationHelper::initClick<vpRGBa>(
const vpImage<vpRGBa> &I,
const std::string &initFile,
bool displayHelp,
vpRBTracker &tracker);
static const vpColor green
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
int getWindowXPosition() const
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
int getWindowYPosition() const
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
@ badValue
Used to indicate that a value is not in the allowed range.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
unsigned int getWidth() const
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Implementation of a pose vector and operations on poses.
vpPoseVector & buildFrom(const double &tx, const double &ty, const double &tz, const double &tux, const double &tuy, const double &tuz)
std::vector< vpPoint > getPoints() const
void addPoint(const vpPoint &P)
@ DEMENTHON_LAGRANGE_VIRTUAL_VS
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, FuncCheckValidityPose func=nullptr)
void removeCommentsAndEmptyLines(std::ifstream &fileId)
void savePose(const std::string &filename) const
Class implementing the Render-Based Tracker (RBT).
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT void sleepMs(double t)