1 #include <visp3/rbt/vpRBInitializationHelper.h>
3 #include <visp3/vision/vpPose.h>
4 #include <visp3/core/vpDisplay.h>
5 #include <visp3/core/vpImage.h>
6 #include <visp3/io/vpImageIo.h>
7 #include <visp3/core/vpIoTools.h>
9 #ifdef VISP_HAVE_MODULE_GUI
10 #include <visp3/gui/vpDisplayFactory.h>
20 while (!fileId.fail() && (c ==
'#')) {
21 fileId.ignore(std::numeric_limits<std::streamsize>::max(), fileId.widen(
'\n'));
33 std::fstream finitpos;
34 finitpos.open(filename.c_str(), std::ios::out);
41 #ifdef VISP_HAVE_MODULE_GUI
44 void vpRBInitializationHelper::initClick(
const vpImage<T> &I,
const std::string &initFile,
bool displayHelp)
46 std::cout <<
"Starting init click!" << std::endl;
52 std::string ext =
".init";
53 std::string str_pose =
"";
54 size_t pos = initFile.rfind(ext);
57 std::fstream finitpos;
60 std::string poseSavingFilename;
61 if (poseSavingFilename.empty()) {
62 if (pos != std::string::npos)
63 str_pose = initFile.substr(0, pos) +
".0.pos";
65 str_pose = initFile +
".0.pos";
67 finitpos.open(str_pose.c_str(), std::ios::in);
71 finitpos.open(poseSavingFilename.c_str(), std::ios::in);
72 ss << poseSavingFilename;
74 if (finitpos.fail()) {
75 std::cout <<
"Cannot read " << ss.str() << std::endl <<
"cMo set to identity" << std::endl;
79 for (
unsigned int i = 0; i < 6; i += 1) {
80 finitpos >> init_pos[i];
86 std::cout <<
"Tracker initial pose read from " << ss.str() <<
": " << std::endl << last_cMo << std::endl;
93 std::cout <<
"No modification : left click " << std::endl;
94 std::cout <<
"Modify initial pose : right click " << std::endl;
120 ss.str(std::string());
126 if (pos != std::string::npos) {
134 std::cout <<
"Load 3D points from: " << ss.str() << std::endl;
135 #if (VISP_CXX_STANDARD > VISP_CXX_STANDARD_98)
136 finit.open(ss.str());
138 finit.open(ss.str().c_str());
141 std::cout <<
"Cannot read " << ss.str() << std::endl;
145 #ifdef VISP_HAVE_MODULE_IO
149 const std::string imgExtVec[] = {
".ppm",
".pgm",
".jpg",
".jpeg",
".png" };
151 bool foundHelpImg =
false;
152 if (pos != std::string::npos) {
153 for (
size_t i = 0; i < 5 && !foundHelpImg; i++) {
154 dispF = initFile.substr(0, pos) + imgExtVec[i];
159 for (
size_t i = 0; i < 5 && !foundHelpImg; i++) {
160 dispF = initFile + imgExtVec[i];
166 std::cout <<
"Load image to help initialization: " << dispF << std::endl;
172 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
176 d_help->init(Iref, winXPos + (
int)width + 80, winYPos,
"Where to initialize...");
184 if (d_help !=
nullptr) {
197 finit.ignore(256,
'\n');
198 std::cout <<
"Number of 3D points " << n3d << std::endl;
204 std::vector<vpPoint> P(n3d);
205 for (
unsigned int i = 0; i < n3d; i++) {
213 finit.ignore(256,
'\n');
216 std::cout <<
"Point " << i + 1 <<
" with 3D coordinates: " << pt_3d_tf[0] <<
" " << pt_3d_tf[1] <<
" "
217 << pt_3d_tf[2] << std::endl;
219 P[i].setWorldCoordinates(pt_3d_tf[0], pt_3d_tf[1], pt_3d_tf[2]);
224 bool isWellInit =
false;
225 while (!isWellInit) {
226 std::vector<vpImagePoint> mem_ip;
227 for (
unsigned int i = 0; i < n3d; i++) {
228 std::ostringstream text;
229 text <<
"Click on point " << i + 1;
233 for (
unsigned int k = 0; k < mem_ip.size(); k++) {
238 std::cout <<
"Click on point " << i + 1 <<
" ";
242 mem_ip.push_back(ip);
249 std::cout <<
"with 2D coordinates: " << ip << std::endl;
256 std::cout <<
"Before optim: " << m_cam << std::endl;
258 std::cout <<
"Pose computation from points failed!" << std::endl;
259 for (
unsigned int i = 0; i < n3d; ++i) {
260 std::cout <<
"Point " << i <<
": " << std::endl;
261 std::cout <<
" 3D: " << pose.
getPoints()[i].get_oP().t() << std::endl;
262 std::cout <<
"2D: " << pose.
getPoints()[i].get_x() <<
", " << pose.
getPoints()[i].get_y() << std::endl;
265 std::cout <<
"POSE after optim: " << m_cMo << std::endl;
288 if (poseSavingFilename.empty())
293 if (d_help !=
nullptr) {
300 template void vpRBInitializationHelper::initClick<unsigned char>(
const vpImage<unsigned char> &I,
const std::string &initFile,
bool displayHelp);
301 template void vpRBInitializationHelper::initClick<vpRGBa>(
const vpImage<vpRGBa> &I,
const std::string &initFile,
bool displayHelp);
Implementation of column vector and the associated operations.
static const vpColor green
Class that defines generic functionalities for display.
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)
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
void addPoint(const vpPoint &P)
@ DEMENTHON_LAGRANGE_VIRTUAL_VS
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, FuncCheckValidityPose func=nullptr)
std::vector< vpPoint > getPoints() const
void savePose(const std::string &filename) const
void removeComment(std::ifstream &fileId)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT void sleepMs(double t)