int main(int argc,char **argv) { try { if (readArguments (argc,argv)==false) { return 0; } //parse arguments ; //read from camera or from file if (TheInputVideo=="live") { TheVideoCapturer.open(0); waitTime=10; } else TheVideoCapturer.open(TheInputVideo); //check video is open if (!TheVideoCapturer.isOpened()) { cerr<<"Could not open video"<<endl; return -1; } //read first image to get the dimensions TheVideoCapturer>>TheInputImage; //read camera parameters if passed if (TheIntrinsicFile!="") { TheCameraParameters.readFromXMLFile(TheIntrinsicFile); TheCameraParameters.resize(TheInputImage.size()); } //Configure other parameters if (ThePyrDownLevel>0) MDetector.pyrDown(ThePyrDownLevel); //begin copy-paste from http://stackoverflow.com/questions/11550021/converting-a-mat-file-from-matlab-into-cvmat-matrix-in-opencv Mat oneVect; Mat useVecLat; Mat someVects; Mat zeroYzero; string demoFile = "demo.yml"; FileStorage fsDemo( demoFile, FileStorage::READ); fsDemo["oneVect"] >> oneVect; fsDemo["oneVect"] >> useVecLat; fsDemo["oneVect"] >> zeroYzero; fsDemo["someVects"] >> someVects; cout << "Print the contents of oneVect:" << endl; cout << oneVect << endl; fsDemo.release(); //close the file // Declare what you need // FileStorage fileOutt("reading_positions.yml", FileStorage::WRITE); //end copy-paste from http://stackoverflow.com/questions/11550021/converting-a-mat-file-from-matlab-into-cvmat-matrix-in-opencv cout << "an element oneVect:" << endl; cout << oneVect.at<float>(0,1) << endl; // to access the 42 in this YAML: //oneVect: !!opencv-matrix // rows: 1 // cols: 3 // dt: f // data: [ 4, 3, 42, 55] // do oneVect.at<float>(0,2) //end data input //Create gui cv::namedWindow("thres",1); cv::namedWindow("in",1); MDetector.getThresholdParams( ThresParam1,ThresParam2); MDetector.setCornerRefinementMethod(MarkerDetector::LINES); iThresParam1=ThresParam1; iThresParam2=ThresParam2; cv::createTrackbar("ThresParam1", "in",&iThresParam1, 13, cvTackBarEvents); cv::createTrackbar("ThresParam2", "in",&iThresParam2, 13, cvTackBarEvents); char key=0; int index=0; //capture until press ESC or until the end of the video while ( key!=27 && TheVideoCapturer.grab()) { TheVideoCapturer.retrieve( TheInputImage); //copy image index++; //number of images captured double tick = (double)getTickCount();//for checking the speed //Detection of markers in the image passed cout << "q" ; MDetector.detect(TheInputImage,TheMarkers,TheCameraParameters,TheMarkerSize); //note that this function outputs the marker info, for some reason. //chekc the speed by calculating the mean speed of all iterations AvrgTime.first+=((double)getTickCount()-tick)/getTickFrequency(); AvrgTime.second++; //cout<<"Time detection="<<1000*AvrgTime.first/AvrgTime.second<<" milliseconds"<<endl; //print marker info and draw the markers in image TheInputImage.copyTo(TheInputImageCopy); for (unsigned int i=0;i<TheMarkers.size();i++) { cout<<TheMarkers[i].Tvec.at<float>(0,0)<<","<< TheMarkers[i].Tvec.at<float>(0,1)<<","<< TheMarkers[i].Tvec.at<float>(0,2)<<","; // cout<<TheMarkers[i]<<endl; if (TheMarkers[i].id == 605 && 1 == 0) { // THIS WILL NEVER HAPPEN!!! 1 is not zero. Mat R33; cv::Rodrigues(TheMarkers[i].Rvec,R33); cout << R33 << endl; cout << TheMarkers[i].id << endl; for (unsigned int maytr=0;maytr<TheMarkers.size();maytr++) { //take 0 , 1 , 0, inverse transform first, then transform. if (TheMarkers[maytr].id == 500) { zeroYzero.at<float>(0,0) = 0; zeroYzero.at<float>(0,1) = 2; zeroYzero.at<float>(0,2) = 0; zeroYzero.at<float>(0,3) = 0; zeroYzero.at<float>(0,4) = 0; zeroYzero.at<float>(0,5) = 0; cout << zeroYzero << endl; Mat R33for500; cout << TheMarkers[maytr].id<< "food"<< endl; cv::Rodrigues(TheMarkers[maytr].Rvec,R33for500); cout << R33for500 << endl; //cout << TheMarkers[i].id << endl; //R33for500 * R33.t() * zeroYzero; //transpose method //Mat afterDouble; //cout << TheMarkers[maytr].id<< "water"<< endl; //afterDouble = R33for500 * (R33.inv() * zeroYzero); //inversion method //R33for500 * (R33.inv() * zeroYzero); //inversion method //cout << "shelll"<< endl; //cout << afterDouble << endl; //useVecLat.at<float>(0,0) = 0; //useVecLat.at<float>(0,1) = 0; //useVecLat.at<float>(0,2) = 0; //useVecLat.at<float>(0,3) = afterDouble.at<float>(0,0); //useVecLat.at<float>(0,4) = afterDouble.at<float>(0,1); //useVecLat.at<float>(0,5) = afterDouble.at<float>(0,2); //drawVecAtPos(TheInputImageCopy,TheMarkers[maytr],TheCameraParameters,afterDouble); //oneVect); } } } TheMarkers[i].draw(TheInputImageCopy,Scalar(0,0,255),1); //time date stuff std::time_t result = std::time(NULL); //nullptr); std::cout // << std::asctime(std::localtime(&result)) << result; // << " seconds since the Epoch\n"; // fileOutt << "time" << result ; //<< endl; cout<<endl; // <<endl<<endl; } //print other rectangles that contains no valid markers /** for (unsigned int i=0;i<MDetector.getCandidates().size();i++) { aruco::Marker m( MDetector.getCandidates()[i],999); m.draw(TheInputImageCopy,cv::Scalar(255,0,0)); }*/ //draw a 3d cube in each marker if there is 3d info if ( TheCameraParameters.isValid()) for (unsigned int i=0;i<TheMarkers.size();i++) { CvDrawingUtils::draw3dCube(TheInputImageCopy,TheMarkers[i],TheCameraParameters); //Never use this; just reference // CvDrawingUtils::draw3dAxis(TheInputImageCopy,TheMarkers[i],TheCameraParameters); draw3dAxisj(TheInputImageCopy,TheMarkers[i],TheCameraParameters); drawVecAtPos(TheInputImageCopy,TheMarkers[i],TheCameraParameters,oneVect); drawVecsAtPosTesting(TheInputImageCopy,TheMarkers[i],TheCameraParameters,someVects); } //DONE! Easy, right? //show input with augmented information and the thresholded image cv::imshow("in",TheInputImageCopy); cv::imshow("thres",MDetector.getThresholdedImage()); key=cv::waitKey(waitTime);//wait for key to be pressed } // fileOutt.release(); } catch (std::exception &ex) { cout<<"Exception :"<<ex.what()<<endl; } }
JNIEXPORT void JNICALL Java_wrapper_MarkerDetector_JpyrDown(JNIEnv * env, jobject obj, jint level){ MarkerDetector *inst = getHandle<MarkerDetector>(env, obj); inst->pyrDown((int)level); }
int main(int argc,char **argv) { try { if (readArguments (argc,argv)==false) { return 0; } //parse arguments ; //read from camera or from file if (TheInputVideo=="live") { TheVideoCapturer.open(0); waitTime=10; } else TheVideoCapturer.open(TheInputVideo); //check video is open if (!TheVideoCapturer.isOpened()) { cerr<<"Could not open video"<<endl; return -1; } //read first image to get the dimensions TheVideoCapturer>>TheInputImage; //read camera parameters if passed if (TheIntrinsicFile!="") { TheCameraParameters.readFromXMLFile(TheIntrinsicFile); TheCameraParameters.resize(TheInputImage.size()); } //Configure other parameters if (ThePyrDownLevel>0) MDetector.pyrDown(ThePyrDownLevel); //Create gui MDetector.getThresholdParams( ThresParam1,ThresParam2); MDetector.setCornerRefinementMethod(MarkerDetector::LINES); /* cv::namedWindow("thres",1); cv::namedWindow("in",1); iThresParam1=ThresParam1; iThresParam2=ThresParam2; cv::createTrackbar("ThresParam1", "in",&iThresParam1, 13, cvTackBarEvents); cv::createTrackbar("ThresParam2", "in",&iThresParam2, 13, cvTackBarEvents); */ char key=0; int index=0; //capture until press ESC or until the end of the video while ( key!=27 && TheVideoCapturer.grab() ) // && index <= 50) { TheVideoCapturer.retrieve( TheInputImage); //copy image index++; //number of images captured double tick = (double)getTickCount();//for checking the speed //Detection of markers in the image passed MDetector.detect(TheInputImage,TheMarkers,TheCameraParameters,TheMarkerSize); //chekc the speed by calculating the mean speed of all iterations AvrgTime.first+=((double)getTickCount()-tick)/getTickFrequency(); AvrgTime.second++; //cout<<"Time detection="<<1000*AvrgTime.first/AvrgTime.second<<" milliseconds"<<endl; //print marker info and draw the markers in image TheInputImage.copyTo(TheInputImageCopy); for (unsigned int i=0;i<TheMarkers.size();i++) { if (AllMarkers.count( TheMarkers[i].id ) == 0) AllMarkers[TheMarkers[i].id] = map<int,Marker>(); AllMarkers[TheMarkers[i].id][index] = TheMarkers[i]; cout<<index<<endl; cout<<TheMarkers[i]<<endl; TheMarkers[i].draw(TheInputImageCopy,Scalar(0,0,255),1); } //print other rectangles that contains no valid markers /** for (unsigned int i=0;i<MDetector.getCandidates().size();i++) { aruco::Marker m( MDetector.getCandidates()[i],999); m.draw(TheInputImageCopy,cv::Scalar(255,0,0)); }*/ //draw a 3d cube in each marker if there is 3d info if ( TheCameraParameters.isValid()) for (unsigned int i=0;i<TheMarkers.size();i++) { CvDrawingUtils::draw3dCube(TheInputImageCopy,TheMarkers[i],TheCameraParameters); CvDrawingUtils::draw3dAxis(TheInputImageCopy,TheMarkers[i],TheCameraParameters); } //DONE! Easy, right? cout<<endl<<endl<<endl; //show input with augmented information and the thresholded image //cv::imshow("in",TheInputImageCopy); //cv::imshow("thres",MDetector.getThresholdedImage()); //key=cv::waitKey(waitTime);//wait for key to be pressed } lastFrame = index; } catch (std::exception &ex) { cout<<"Exception :"<<ex.what()<<endl; } cout << "All done."<< endl; map<int, Markers>::const_iterator i; for( i = AllMarkers.begin(); i != AllMarkers.end(); ++i ) { int markerId = (*i).first; map<int, Marker> markers = (*i).second; int frameCount = markers.size(); cout << "frameCount = " << frameCount << endl; std::vector<double> x(frameCount); std::vector<double> m0x(frameCount); std::vector<double> m0y(frameCount); std::vector<double> m1x(frameCount); std::vector<double> m1y(frameCount); std::vector<double> m2x(frameCount); std::vector<double> m2y(frameCount); std::vector<double> m3x(frameCount); std::vector<double> m3y(frameCount); std::vector<double> tx(frameCount); std::vector<double> ty(frameCount); std::vector<double> tz(frameCount); std::vector<double> rx(frameCount); std::vector<double> ry(frameCount); std::vector<double> rz(frameCount); map<int, Marker>::const_iterator j; int index = 0; for( j = markers.begin(); j != markers.end(); ++j, index++ ) { int frameIndex = (*j).first; Marker marker = (*j).second; x[index] = frameIndex; m0x[index] = marker[0].x; m0y[index] = marker[0].y; m1x[index] = marker[1].x; m1y[index] = marker[1].y; m2x[index] = marker[2].x; m2y[index] = marker[2].y; m3x[index] = marker[3].x; m3y[index] = marker[3].y; tx[index] = marker.Tvec.ptr<float>(0)[0]; ty[index] = marker.Tvec.ptr<float>(0)[1]; tz[index] = marker.Tvec.ptr<float>(0)[2]; rx[index] = marker.Rvec.ptr<float>(0)[0]; ry[index] = marker.Rvec.ptr<float>(0)[1]; rz[index] = marker.Rvec.ptr<float>(0)[2]; cout << frameIndex << endl; } #define SPLINE(VAR) gsl_spline *spline_ ## VAR = gsl_spline_alloc (gsl_interp_cspline, frameCount); gsl_spline_init (spline_ ## VAR, &x[0], &VAR[0], frameCount) SPLINE(m0x); SPLINE(m0y); SPLINE(m1x); SPLINE(m1y); SPLINE(m2x); SPLINE(m2y); SPLINE(m3x); SPLINE(m3y); SPLINE(tx); SPLINE(ty); SPLINE(tz); SPLINE(rx); SPLINE(ry); SPLINE(rz); for( index = 0; index < lastFrame; index++ ) { double m0x = gsl_spline_eval (spline_m0x, index, NULL); double m0y = gsl_spline_eval (spline_m0y, index, NULL); double m1x = gsl_spline_eval (spline_m1x, index, NULL); double m1y = gsl_spline_eval (spline_m1y, index, NULL); double m2x = gsl_spline_eval (spline_m2x, index, NULL); double m2y = gsl_spline_eval (spline_m2y, index, NULL); double m3x = gsl_spline_eval (spline_m3x, index, NULL); double m3y = gsl_spline_eval (spline_m3y, index, NULL); double tx = gsl_spline_eval (spline_tx, index, NULL); double ty = gsl_spline_eval (spline_ty, index, NULL); double tz = gsl_spline_eval (spline_tz, index, NULL); double rx = gsl_spline_eval (spline_rx, index, NULL); double ry = gsl_spline_eval (spline_ry, index, NULL); double rz = gsl_spline_eval (spline_rz, index, NULL); cv::Point2f m0 = cv::Point2f(m0x,m0y); cv::Point2f m1 = cv::Point2f(m1x,m1y); cv::Point2f m2 = cv::Point2f(m2x,m2y); cv::Point2f m3 = cv::Point2f(m3x,m3y); std::vector<cv::Point2f> corners(4); corners[0] = m0; corners[1] = m1; corners[2] = m2; corners[3] = m3; Marker interpolated = Marker(corners, markerId); interpolated.Rvec.create(3,1,CV_32FC1); interpolated.Tvec.create(3,1,CV_32FC1); interpolated.Tvec.at<float>(0,0) = tx; interpolated.Tvec.at<float>(1,0) = ty; interpolated.Tvec.at<float>(2,0) = tz; interpolated.Rvec.at<float>(0,0) = rx; interpolated.Rvec.at<float>(1,0) = ry; interpolated.Rvec.at<float>(2,0) = rz; cout << index << endl; cout << interpolated << endl; } gsl_spline_free (spline_m0x); gsl_spline_free (spline_m0y); gsl_spline_free (spline_m1x); gsl_spline_free (spline_m1y); gsl_spline_free (spline_m2x); gsl_spline_free (spline_m2y); gsl_spline_free (spline_m3x); gsl_spline_free (spline_m3y); gsl_spline_free (spline_tx); gsl_spline_free (spline_ty); gsl_spline_free (spline_tz); gsl_spline_free (spline_rx); gsl_spline_free (spline_ry); gsl_spline_free (spline_rz); //map<int, Marker>::const_iterator j; //cout << "id = " << markerId << endl; } //cout << TheFrames << endl; }
int main(int argc, char **argv) { try { if (readArguments(argc, argv) == false) { return 0; } // parse arguments // read from camera or from file if (TheInputVideo.find("live") != string::npos) { int vIdx = 0; // check if the :idx is here char cad[100]; if (TheInputVideo.find(":") != string::npos) { std::replace(TheInputVideo.begin(), TheInputVideo.end(), ':', ' '); sscanf(TheInputVideo.c_str(), "%s %d", cad, &vIdx); } cout << "Opening camera index " << vIdx << endl; TheVideoCapturer.open(vIdx); waitTime = 10; } else TheVideoCapturer.open(TheInputVideo); // check video is open if (!TheVideoCapturer.isOpened()) { cerr << "Could not open video" << endl; return -1; } bool isVideoFile = false; if (TheInputVideo.find(".avi") != std::string::npos || TheInputVideo.find("live") != string::npos) isVideoFile = true; // read first image to get the dimensions TheVideoCapturer >> TheInputImage; // read camera parameters if passed if (TheIntrinsicFile != "") { TheCameraParameters.readFromXMLFile(TheIntrinsicFile); TheCameraParameters.resize(TheInputImage.size()); } // Configure other parameters if (ThePyrDownLevel > 0) MDetector.pyrDown(ThePyrDownLevel); // Create gui cv::namedWindow("thres", 1); cv::namedWindow("in", 1); MDetector.setThresholdParams(7, 7); MDetector.setThresholdParamRange(2, 0); // MDetector.enableLockedCornersMethod(true); // MDetector.setCornerRefinementMethod ( MarkerDetector::SUBPIX ); MDetector.getThresholdParams(ThresParam1, ThresParam2); iThresParam1 = ThresParam1; iThresParam2 = ThresParam2; //cv::createTrackbar("ThresParam1", "in", &iThresParam1, 25, cvTackBarEvents); //cv::createTrackbar("ThresParam2", "in", &iThresParam2, 13, cvTackBarEvents); char key = 0; int index = 0; // capture until press ESC or until the end of the video TheVideoCapturer.retrieve(TheInputImage); cv::Size sz = TheInputImage.size(); MDetector.createCudaBuffers(sz.width, sz.height); do { // copy image index++; // number of images captured double tick = (double)getTickCount(); // for checking the speed // Detection of markers in the image passed MDetector.detect(TheInputImage, TheMarkers, TheCameraParameters, TheMarkerSize); // chekc the speed by calculating the mean speed of all iterations AvrgTime.first += ((double)getTickCount() - tick) / getTickFrequency(); AvrgTime.second++; cout << "\rTime detection=" << 1000 * AvrgTime.first / AvrgTime.second << " milliseconds nmarkers=" << TheMarkers.size() << std::flush; // print marker info and draw the markers in image TheInputImage.copyTo(TheInputImageCopy); for (unsigned int i = 0; i < TheMarkers.size(); i++) { cout << endl << TheMarkers[i]; TheMarkers[i].draw(TheInputImageCopy, Scalar(0, 0, 255), 1); } if (TheMarkers.size() != 0) cout << endl; // print other rectangles that contains no valid markers /** for (unsigned int i=0;i<MDetector.getCandidates().size();i++) { aruco::Marker m( MDetector.getCandidates()[i],999); m.draw(TheInputImageCopy,cv::Scalar(255,0,0)); }*/ // draw a 3d cube in each marker if there is 3d info if (TheCameraParameters.isValid()) for (unsigned int i = 0; i < TheMarkers.size(); i++) { CvDrawingUtils::draw3dCube(TheInputImageCopy, TheMarkers[i], TheCameraParameters); CvDrawingUtils::draw3dAxis(TheInputImageCopy, TheMarkers[i], TheCameraParameters); } // DONE! Easy, right? // show input with augmented information and the thresholded image cv::imshow("in", TheInputImageCopy); cv::imshow("thres", MDetector.getThresholdedImage()); //cv::imshow("thres_gpu", MDetector.getThresholdedImageGPU()); key = cv::waitKey(waitTime); // wait for key to be pressed if (isVideoFile) TheVideoCapturer.retrieve(TheInputImage); } while (key != 27 && (TheVideoCapturer.grab() || !isVideoFile)); } catch (std::exception &ex) { cout << "Exception :" << ex.what() << endl; } }
int main(int argc,char **argv) { try { if (readArguments (argc,argv)==false) { return 0; } //parse arguments ; //read from camera or from file if (TheInputVideo=="live") { TheVideoCapturer.open(0); waitTime=10; } else TheVideoCapturer.open(TheInputVideo); //check video is open if (!TheVideoCapturer.isOpened()) { cerr<<"Could not open video"<<endl; return -1; } //read first image to get the dimensions TheVideoCapturer>>TheInputImage; //read camera parameters if passed if (TheIntrinsicFile!="") { TheCameraParameters.readFromXMLFile(TheIntrinsicFile); TheCameraParameters.resize(TheInputImage.size()); } //Configure other parameters if (ThePyrDownLevel>0) MDetector.pyrDown(ThePyrDownLevel); //Create gui cv::namedWindow("thres",1); cv::namedWindow("in",1); MDetector.getThresholdParams( ThresParam1,ThresParam2); MDetector.setCornerRefinementMethod(MarkerDetector::LINES); iThresParam1=ThresParam1; iThresParam2=ThresParam2; cv::createTrackbar("ThresParam1", "in",&iThresParam1, 13, cvTackBarEvents); cv::createTrackbar("ThresParam2", "in",&iThresParam2, 13, cvTackBarEvents); char key=0; int index=0; //capture until press ESC or until the end of the video while ( key!=27 && TheVideoCapturer.grab()) { TheVideoCapturer.retrieve( TheInputImage); //copy image index++; //number of images captured double tick = (double)getTickCount();//for checking the speed //Detection of markers in the image passed MDetector.detect(TheInputImage,TheMarkers,TheCameraParameters,TheMarkerSize); //chekc the speed by calculating the mean speed of all iterations AvrgTime.first+=((double)getTickCount()-tick)/getTickFrequency(); AvrgTime.second++; cout<<"Time detection="<<1000*AvrgTime.first/AvrgTime.second<<" milliseconds"<<endl; //print marker info and draw the markers in image TheInputImage.copyTo(TheInputImageCopy); for (unsigned int i=0;i<TheMarkers.size();i++) { cout<<TheMarkers[i]<<endl; TheMarkers[i].draw(TheInputImageCopy,Scalar(0,0,255),1); } //print other rectangles that contains no valid markers /** for (unsigned int i=0;i<MDetector.getCandidates().size();i++) { aruco::Marker m( MDetector.getCandidates()[i],999); m.draw(TheInputImageCopy,cv::Scalar(255,0,0)); }*/ //draw a 3d cube in each marker if there is 3d info if ( TheCameraParameters.isValid()) for (unsigned int i=0;i<TheMarkers.size();i++) { CvDrawingUtils::draw3dCube(TheInputImageCopy,TheMarkers[i],TheCameraParameters); CvDrawingUtils::draw3dAxis(TheInputImageCopy,TheMarkers[i],TheCameraParameters); } //DONE! Easy, right? cout<<endl<<endl<<endl; //show input with augmented information and the thresholded image cv::imshow("in",TheInputImageCopy); cv::imshow("thres",MDetector.getThresholdedImage()); key=cv::waitKey(waitTime);//wait for key to be pressed } } catch (std::exception &ex) { cout<<"Exception :"<<ex.what()<<endl; } }