select3dobj.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. *
  3. * select3obj.cpp With a calibration chessboard on a table, mark an object in a 3D box and
  4. * track that object in all subsequent frames as long as the camera can see
  5. * the chessboard. Also segments the object using the box projection. This
  6. * program is useful for collecting large datasets of many views of an object
  7. * on a table.
  8. *
  9. */
  10. #include "opencv2/core.hpp"
  11. #include <opencv2/core/utility.hpp>
  12. #include "opencv2/imgproc.hpp"
  13. #include "opencv2/calib3d.hpp"
  14. #include "opencv2/imgcodecs.hpp"
  15. #include "opencv2/videoio.hpp"
  16. #include "opencv2/highgui.hpp"
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string>
  21. using namespace std;
  22. using namespace cv;
  23. static string helphelp(char** argv)
  24. {
  25. return string("\nThis program's purpose is to collect data sets of an object and its segmentation mask.\n")
  26. + "\n"
  27. "It shows how to use a calibrated camera together with a calibration pattern to\n"
  28. "compute the homography of the plane the calibration pattern is on. It also shows grabCut\n"
  29. "segmentation etc.\n"
  30. "\n"
  31. + argv[0]
  32. + " -w=<board_width> -h=<board_height> [-s=<square_size>]\n"
  33. " -i=<camera_intrinsics_filename> -o=<output_prefix>\n"
  34. "\n"
  35. " -w=<board_width> Number of chessboard corners wide\n"
  36. " -h=<board_height> Number of chessboard corners width\n"
  37. " [-s=<square_size>] Optional measure of chessboard squares in meters\n"
  38. " -i=<camera_intrinsics_filename> Camera matrix .yml file from calibration.cpp\n"
  39. " -o=<output_prefix> Prefix the output segmentation images with this\n"
  40. " [video_filename/cameraId] If present, read from that video file or that ID\n"
  41. "\n"
  42. "Using a camera's intrinsics (from calibrating a camera -- see calibration.cpp) and an\n"
  43. "image of the object sitting on a planar surface with a calibration pattern of\n"
  44. "(board_width x board_height) on the surface, we draw a 3D box around the object. From\n"
  45. "then on, we can move a camera and as long as it sees the chessboard calibration pattern,\n"
  46. "it will store a mask of where the object is. We get successive images using <output_prefix>\n"
  47. "of the segmentation mask containing the object. This makes creating training sets easy.\n"
  48. "It is best if the chessboard is odd x even in dimensions to avoid ambiguous poses.\n"
  49. "\n"
  50. "The actions one can use while the program is running are:\n"
  51. "\n"
  52. " Select object as 3D box with the mouse.\n"
  53. " First draw one line on the plane to outline the projection of that object on the plane\n"
  54. " Then extend that line into a box to encompass the projection of that object onto the plane\n"
  55. " The use the mouse again to extend the box upwards from the plane to encase the object.\n"
  56. " Then use the following commands\n"
  57. " ESC - Reset the selection\n"
  58. " SPACE - Skip the frame; move to the next frame (not in video mode)\n"
  59. " ENTER - Confirm the selection. Grab next object in video mode.\n"
  60. " q - Exit the program\n"
  61. "\n\n";
  62. }
  63. // static void help()
  64. // {
  65. // puts(helphelp);
  66. // }
  67. struct MouseEvent
  68. {
  69. MouseEvent() { event = -1; buttonState = 0; }
  70. Point pt;
  71. int event;
  72. int buttonState;
  73. };
  74. static void onMouse(int event, int x, int y, int flags, void* userdata)
  75. {
  76. MouseEvent* data = (MouseEvent*)userdata;
  77. data->event = event;
  78. data->pt = Point(x,y);
  79. data->buttonState = flags;
  80. }
  81. static bool readCameraMatrix(const string& filename,
  82. Mat& cameraMatrix, Mat& distCoeffs,
  83. Size& calibratedImageSize )
  84. {
  85. FileStorage fs(filename, FileStorage::READ);
  86. fs["image_width"] >> calibratedImageSize.width;
  87. fs["image_height"] >> calibratedImageSize.height;
  88. fs["distortion_coefficients"] >> distCoeffs;
  89. fs["camera_matrix"] >> cameraMatrix;
  90. if( distCoeffs.type() != CV_64F )
  91. distCoeffs = Mat_<double>(distCoeffs);
  92. if( cameraMatrix.type() != CV_64F )
  93. cameraMatrix = Mat_<double>(cameraMatrix);
  94. return true;
  95. }
  96. static void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners)
  97. {
  98. corners.resize(0);
  99. for( int i = 0; i < boardSize.height; i++ )
  100. for( int j = 0; j < boardSize.width; j++ )
  101. corners.push_back(Point3f(float(j*squareSize),
  102. float(i*squareSize), 0));
  103. }
  104. static Point3f image2plane(Point2f imgpt, const Mat& R, const Mat& tvec,
  105. const Mat& cameraMatrix, double Z)
  106. {
  107. Mat R1 = R.clone();
  108. R1.col(2) = R1.col(2)*Z + tvec;
  109. Mat_<double> v = (cameraMatrix*R1).inv()*(Mat_<double>(3,1) << imgpt.x, imgpt.y, 1);
  110. double iw = fabs(v(2,0)) > DBL_EPSILON ? 1./v(2,0) : 0;
  111. return Point3f((float)(v(0,0)*iw), (float)(v(1,0)*iw), (float)Z);
  112. }
  113. static Rect extract3DBox(const Mat& frame, Mat& shownFrame, Mat& selectedObjFrame,
  114. const Mat& cameraMatrix, const Mat& rvec, const Mat& tvec,
  115. const vector<Point3f>& box, int nobjpt, bool runExtraSegmentation)
  116. {
  117. selectedObjFrame = Mat::zeros(frame.size(), frame.type());
  118. if( nobjpt == 0 )
  119. return Rect();
  120. vector<Point3f> objpt;
  121. vector<Point2f> imgpt;
  122. objpt.push_back(box[0]);
  123. if( nobjpt > 1 )
  124. objpt.push_back(box[1]);
  125. if( nobjpt > 2 )
  126. {
  127. objpt.push_back(box[2]);
  128. objpt.push_back(objpt[2] - objpt[1] + objpt[0]);
  129. }
  130. if( nobjpt > 3 )
  131. for( int i = 0; i < 4; i++ )
  132. objpt.push_back(Point3f(objpt[i].x, objpt[i].y, box[3].z));
  133. projectPoints(Mat(objpt), rvec, tvec, cameraMatrix, Mat(), imgpt);
  134. if( !shownFrame.empty() )
  135. {
  136. if( nobjpt == 1 )
  137. circle(shownFrame, imgpt[0], 3, Scalar(0,255,0), -1, LINE_AA);
  138. else if( nobjpt == 2 )
  139. {
  140. circle(shownFrame, imgpt[0], 3, Scalar(0,255,0), -1, LINE_AA);
  141. circle(shownFrame, imgpt[1], 3, Scalar(0,255,0), -1, LINE_AA);
  142. line(shownFrame, imgpt[0], imgpt[1], Scalar(0,255,0), 3, LINE_AA);
  143. }
  144. else if( nobjpt == 3 )
  145. for( int i = 0; i < 4; i++ )
  146. {
  147. circle(shownFrame, imgpt[i], 3, Scalar(0,255,0), -1, LINE_AA);
  148. line(shownFrame, imgpt[i], imgpt[(i+1)%4], Scalar(0,255,0), 3, LINE_AA);
  149. }
  150. else
  151. for( int i = 0; i < 8; i++ )
  152. {
  153. circle(shownFrame, imgpt[i], 3, Scalar(0,255,0), -1, LINE_AA);
  154. line(shownFrame, imgpt[i], imgpt[(i+1)%4 + (i/4)*4], Scalar(0,255,0), 3, LINE_AA);
  155. line(shownFrame, imgpt[i], imgpt[i%4], Scalar(0,255,0), 3, LINE_AA);
  156. }
  157. }
  158. if( nobjpt <= 2 )
  159. return Rect();
  160. vector<Point> hull;
  161. convexHull(Mat_<Point>(Mat(imgpt)), hull);
  162. Mat selectedObjMask = Mat::zeros(frame.size(), CV_8U);
  163. fillConvexPoly(selectedObjMask, &hull[0], (int)hull.size(), Scalar::all(255), 8, 0);
  164. Rect roi = boundingRect(Mat(hull)) & Rect(Point(), frame.size());
  165. if( runExtraSegmentation )
  166. {
  167. selectedObjMask = Scalar::all(GC_BGD);
  168. fillConvexPoly(selectedObjMask, &hull[0], (int)hull.size(), Scalar::all(GC_PR_FGD), 8, 0);
  169. Mat bgdModel, fgdModel;
  170. grabCut(frame, selectedObjMask, roi, bgdModel, fgdModel,
  171. 3, GC_INIT_WITH_RECT + GC_INIT_WITH_MASK);
  172. bitwise_and(selectedObjMask, Scalar::all(1), selectedObjMask);
  173. }
  174. frame.copyTo(selectedObjFrame, selectedObjMask);
  175. return roi;
  176. }
  177. static int select3DBox(const string& windowname, const string& selWinName, const Mat& frame,
  178. const Mat& cameraMatrix, const Mat& rvec, const Mat& tvec,
  179. vector<Point3f>& box)
  180. {
  181. const float eps = 1e-3f;
  182. MouseEvent mouse;
  183. setMouseCallback(windowname, onMouse, &mouse);
  184. vector<Point3f> tempobj(8);
  185. vector<Point2f> imgpt(4), tempimg(8);
  186. vector<Point> temphull;
  187. int nobjpt = 0;
  188. Mat R, selectedObjMask, selectedObjFrame, shownFrame;
  189. Rodrigues(rvec, R);
  190. box.resize(4);
  191. for(;;)
  192. {
  193. float Z = 0.f;
  194. bool dragging = (mouse.buttonState & EVENT_FLAG_LBUTTON) != 0;
  195. int npt = nobjpt;
  196. if( (mouse.event == EVENT_LBUTTONDOWN ||
  197. mouse.event == EVENT_LBUTTONUP ||
  198. dragging) && nobjpt < 4 )
  199. {
  200. Point2f m = mouse.pt;
  201. if( nobjpt < 2 )
  202. imgpt[npt] = m;
  203. else
  204. {
  205. tempobj.resize(1);
  206. int nearestIdx = npt-1;
  207. if( nobjpt == 3 )
  208. {
  209. nearestIdx = 0;
  210. for( int i = 1; i < npt; i++ )
  211. if( norm(m - imgpt[i]) < norm(m - imgpt[nearestIdx]) )
  212. nearestIdx = i;
  213. }
  214. if( npt == 2 )
  215. {
  216. float dx = box[1].x - box[0].x, dy = box[1].y - box[0].y;
  217. float len = 1.f/std::sqrt(dx*dx+dy*dy);
  218. tempobj[0] = Point3f(dy*len + box[nearestIdx].x,
  219. -dx*len + box[nearestIdx].y, 0.f);
  220. }
  221. else
  222. tempobj[0] = Point3f(box[nearestIdx].x, box[nearestIdx].y, 1.f);
  223. projectPoints(Mat(tempobj), rvec, tvec, cameraMatrix, Mat(), tempimg);
  224. Point2f a = imgpt[nearestIdx], b = tempimg[0], d1 = b - a, d2 = m - a;
  225. float n1 = (float)norm(d1), n2 = (float)norm(d2);
  226. if( n1*n2 < eps )
  227. imgpt[npt] = a;
  228. else
  229. {
  230. Z = d1.dot(d2)/(n1*n1);
  231. imgpt[npt] = d1*Z + a;
  232. }
  233. }
  234. box[npt] = image2plane(imgpt[npt], R, tvec, cameraMatrix, npt<3 ? 0 : Z);
  235. if( (npt == 0 && mouse.event == EVENT_LBUTTONDOWN) ||
  236. (npt > 0 && norm(box[npt] - box[npt-1]) > eps &&
  237. mouse.event == EVENT_LBUTTONUP) )
  238. {
  239. nobjpt++;
  240. if( nobjpt < 4 )
  241. {
  242. imgpt[nobjpt] = imgpt[nobjpt-1];
  243. box[nobjpt] = box[nobjpt-1];
  244. }
  245. }
  246. // reset the event
  247. mouse.event = -1;
  248. //mouse.buttonState = 0;
  249. npt++;
  250. }
  251. frame.copyTo(shownFrame);
  252. extract3DBox(frame, shownFrame, selectedObjFrame,
  253. cameraMatrix, rvec, tvec, box, npt, false);
  254. imshow(windowname, shownFrame);
  255. imshow(selWinName, selectedObjFrame);
  256. char c = (char)waitKey(30);
  257. if( c == 27 )
  258. {
  259. nobjpt = 0;
  260. }
  261. if( c == 'q' || c == 'Q' || c == ' ' )
  262. {
  263. box.clear();
  264. return c == ' ' ? -1 : -100;
  265. }
  266. if( (c == '\r' || c == '\n') && nobjpt == 4 && box[3].z != 0 )
  267. return 1;
  268. }
  269. }
  270. static bool readModelViews( const string& filename, vector<Point3f>& box,
  271. vector<string>& imagelist,
  272. vector<Rect>& roiList, vector<Vec6f>& poseList )
  273. {
  274. imagelist.resize(0);
  275. roiList.resize(0);
  276. poseList.resize(0);
  277. box.resize(0);
  278. FileStorage fs(filename, FileStorage::READ);
  279. if( !fs.isOpened() )
  280. return false;
  281. fs["box"] >> box;
  282. FileNode all = fs["views"];
  283. if( all.type() != FileNode::SEQ )
  284. return false;
  285. FileNodeIterator it = all.begin(), it_end = all.end();
  286. for(; it != it_end; ++it)
  287. {
  288. FileNode n = *it;
  289. imagelist.push_back((string)n["image"]);
  290. FileNode nr = n["rect"];
  291. roiList.push_back(Rect((int)nr[0], (int)nr[1], (int)nr[2], (int)nr[3]));
  292. FileNode np = n["pose"];
  293. poseList.push_back(Vec6f((float)np[0], (float)np[1], (float)np[2],
  294. (float)np[3], (float)np[4], (float)np[5]));
  295. }
  296. return true;
  297. }
  298. static bool writeModelViews(const string& filename, const vector<Point3f>& box,
  299. const vector<string>& imagelist,
  300. const vector<Rect>& roiList,
  301. const vector<Vec6f>& poseList)
  302. {
  303. FileStorage fs(filename, FileStorage::WRITE);
  304. if( !fs.isOpened() )
  305. return false;
  306. fs << "box" << "[:";
  307. fs << box << "]" << "views" << "[";
  308. size_t i, nviews = imagelist.size();
  309. CV_Assert( nviews == roiList.size() && nviews == poseList.size() );
  310. for( i = 0; i < nviews; i++ )
  311. {
  312. Rect r = roiList[i];
  313. Vec6f p = poseList[i];
  314. fs << "{" << "image" << imagelist[i] <<
  315. "roi" << "[:" << r.x << r.y << r.width << r.height << "]" <<
  316. "pose" << "[:" << p[0] << p[1] << p[2] << p[3] << p[4] << p[5] << "]" << "}";
  317. }
  318. fs << "]";
  319. return true;
  320. }
  321. static bool readStringList( const string& filename, vector<string>& l )
  322. {
  323. l.resize(0);
  324. FileStorage fs(filename, FileStorage::READ);
  325. if( !fs.isOpened() )
  326. return false;
  327. FileNode n = fs.getFirstTopLevelNode();
  328. if( n.type() != FileNode::SEQ )
  329. return false;
  330. FileNodeIterator it = n.begin(), it_end = n.end();
  331. for( ; it != it_end; ++it )
  332. l.push_back((string)*it);
  333. return true;
  334. }
  335. int main(int argc, char** argv)
  336. {
  337. string help = string("Usage: ") + argv[0] + " -w=<board_width> -h=<board_height> [-s=<square_size>]\n" +
  338. "\t-i=<intrinsics_filename> -o=<output_prefix> [video_filename/cameraId]\n";
  339. const char* screen_help =
  340. "Actions: \n"
  341. "\tSelect object as 3D box with the mouse. That's it\n"
  342. "\tESC - Reset the selection\n"
  343. "\tSPACE - Skip the frame; move to the next frame (not in video mode)\n"
  344. "\tENTER - Confirm the selection. Grab next object in video mode.\n"
  345. "\tq - Exit the program\n";
  346. cv::CommandLineParser parser(argc, argv, "{help h||}{w||}{h||}{s|1|}{i||}{o||}{@input|0|}");
  347. if (parser.has("help"))
  348. {
  349. puts(helphelp(argv).c_str());
  350. puts(help.c_str());
  351. return 0;
  352. }
  353. string intrinsicsFilename;
  354. string outprefix = "";
  355. string inputName = "";
  356. int cameraId = 0;
  357. Size boardSize;
  358. double squareSize;
  359. vector<string> imageList;
  360. intrinsicsFilename = parser.get<string>("i");
  361. outprefix = parser.get<string>("o");
  362. boardSize.width = parser.get<int>("w");
  363. boardSize.height = parser.get<int>("h");
  364. squareSize = parser.get<double>("s");
  365. if ( parser.get<string>("@input").size() == 1 && isdigit(parser.get<string>("@input")[0]) )
  366. cameraId = parser.get<int>("@input");
  367. else
  368. inputName = samples::findFileOrKeep(parser.get<string>("@input"));
  369. if (!parser.check())
  370. {
  371. puts(help.c_str());
  372. parser.printErrors();
  373. return 0;
  374. }
  375. if ( boardSize.width <= 0 )
  376. {
  377. printf("Incorrect -w parameter (must be a positive integer)\n");
  378. puts(help.c_str());
  379. return 0;
  380. }
  381. if ( boardSize.height <= 0 )
  382. {
  383. printf("Incorrect -h parameter (must be a positive integer)\n");
  384. puts(help.c_str());
  385. return 0;
  386. }
  387. if ( squareSize <= 0 )
  388. {
  389. printf("Incorrect -s parameter (must be a positive real number)\n");
  390. puts(help.c_str());
  391. return 0;
  392. }
  393. Mat cameraMatrix, distCoeffs;
  394. Size calibratedImageSize;
  395. readCameraMatrix(intrinsicsFilename, cameraMatrix, distCoeffs, calibratedImageSize );
  396. VideoCapture capture;
  397. if( !inputName.empty() )
  398. {
  399. if( !readStringList(inputName, imageList) &&
  400. !capture.open(inputName))
  401. {
  402. fprintf( stderr, "The input file could not be opened\n" );
  403. return -1;
  404. }
  405. }
  406. else
  407. capture.open(cameraId);
  408. if( !capture.isOpened() && imageList.empty() )
  409. return fprintf( stderr, "Could not initialize video capture\n" ), -2;
  410. const char* outbarename = 0;
  411. {
  412. outbarename = strrchr(outprefix.c_str(), '/');
  413. const char* tmp = strrchr(outprefix.c_str(), '\\');
  414. char cmd[1000];
  415. snprintf(cmd, sizeof(cmd), "mkdir %s", outprefix.c_str());
  416. if( tmp && tmp > outbarename )
  417. outbarename = tmp;
  418. if( outbarename )
  419. {
  420. cmd[6 + outbarename - outprefix.c_str()] = '\0';
  421. int result = system(cmd);
  422. CV_Assert(result == 0);
  423. outbarename++;
  424. }
  425. else
  426. outbarename = outprefix.c_str();
  427. }
  428. Mat frame, shownFrame, selectedObjFrame, mapxy;
  429. namedWindow("View", 1);
  430. namedWindow("Selected Object", 1);
  431. setMouseCallback("View", onMouse, 0);
  432. bool boardFound = false;
  433. string indexFilename = cv::format("%s_index.yml", outprefix.c_str());
  434. vector<string> capturedImgList;
  435. vector<Rect> roiList;
  436. vector<Vec6f> poseList;
  437. vector<Point3f> box, boardPoints;
  438. readModelViews(indexFilename, box, capturedImgList, roiList, poseList);
  439. calcChessboardCorners(boardSize, (float)squareSize, boardPoints);
  440. int frameIdx = 0;
  441. bool grabNext = !imageList.empty();
  442. puts(screen_help);
  443. for(int i = 0;;i++)
  444. {
  445. Mat frame0;
  446. if( !imageList.empty() )
  447. {
  448. if( i < (int)imageList.size() )
  449. frame0 = imread(string(imageList[i]), 1);
  450. }
  451. else
  452. capture >> frame0;
  453. if( frame0.empty() )
  454. break;
  455. if( frame.empty() )
  456. {
  457. if( frame0.size() != calibratedImageSize )
  458. {
  459. double sx = (double)frame0.cols/calibratedImageSize.width;
  460. double sy = (double)frame0.rows/calibratedImageSize.height;
  461. // adjust the camera matrix for the new resolution
  462. cameraMatrix.at<double>(0,0) *= sx;
  463. cameraMatrix.at<double>(0,2) *= sx;
  464. cameraMatrix.at<double>(1,1) *= sy;
  465. cameraMatrix.at<double>(1,2) *= sy;
  466. }
  467. Mat dummy;
  468. initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
  469. cameraMatrix, frame0.size(),
  470. CV_32FC2, mapxy, dummy );
  471. distCoeffs = Mat::zeros(5, 1, CV_64F);
  472. }
  473. remap(frame0, frame, mapxy, Mat(), INTER_LINEAR);
  474. vector<Point2f> foundBoardCorners;
  475. boardFound = findChessboardCorners(frame, boardSize, foundBoardCorners);
  476. Mat rvec, tvec;
  477. if( boardFound )
  478. solvePnP(Mat(boardPoints), Mat(foundBoardCorners), cameraMatrix,
  479. distCoeffs, rvec, tvec, false);
  480. frame.copyTo(shownFrame);
  481. drawChessboardCorners(shownFrame, boardSize, Mat(foundBoardCorners), boardFound);
  482. selectedObjFrame = Mat::zeros(frame.size(), frame.type());
  483. if( boardFound && grabNext )
  484. {
  485. if( box.empty() )
  486. {
  487. int code = select3DBox("View", "Selected Object", frame,
  488. cameraMatrix, rvec, tvec, box);
  489. if( code == -100 )
  490. break;
  491. }
  492. if( !box.empty() )
  493. {
  494. Rect r = extract3DBox(frame, shownFrame, selectedObjFrame,
  495. cameraMatrix, rvec, tvec, box, 4, true);
  496. if( !r.empty() )
  497. {
  498. const int maxFrameIdx = 10000;
  499. char path[1000];
  500. for(;frameIdx < maxFrameIdx;frameIdx++)
  501. {
  502. snprintf(path, sizeof(path), "%s%04d.jpg", outprefix.c_str(), frameIdx);
  503. FILE* f = fopen(path, "rb");
  504. if( !f )
  505. break;
  506. fclose(f);
  507. }
  508. if( frameIdx == maxFrameIdx )
  509. {
  510. printf("Can not save the image as %s<...>.jpg", outprefix.c_str());
  511. break;
  512. }
  513. imwrite(path, selectedObjFrame(r));
  514. capturedImgList.push_back(string(path));
  515. roiList.push_back(r);
  516. float p[6];
  517. Mat RV(3, 1, CV_32F, p), TV(3, 1, CV_32F, p+3);
  518. rvec.convertTo(RV, RV.type());
  519. tvec.convertTo(TV, TV.type());
  520. poseList.push_back(Vec6f(p[0], p[1], p[2], p[3], p[4], p[5]));
  521. }
  522. }
  523. grabNext = !imageList.empty();
  524. }
  525. imshow("View", shownFrame);
  526. imshow("Selected Object", selectedObjFrame);
  527. char c = (char)waitKey(imageList.empty() && !box.empty() ? 30 : 300);
  528. if( c == 'q' || c == 'Q' )
  529. break;
  530. if( c == '\r' || c == '\n' )
  531. grabNext = true;
  532. }
  533. writeModelViews(indexFilename, box, capturedImgList, roiList, poseList);
  534. return 0;
  535. }