bool readParameters(int argc, char** argv) { if (argc<4) { usage(); return false; } TheInputVideo=argv[1]; /* // read input video if (TheInputVideo=="live") TheVideoCapturer.open(0); else TheVideoCapturer.open(argv[1]); if (!TheVideoCapturer.isOpened()) { cerr<<"Could not open video"<<endl; return false; }*/ //read from camera if (TheInputVideo=="live") TheVideoCapturer.open(0); else TheVideoCapturer.open(TheInputVideo); if (!TheVideoCapturer.isOpened()) { cerr<<"Could not open video"<<endl; return -1; } // read intrinsic file try { CameraParams.readFromXMLFile(argv[2]); } catch (std::exception &ex) { cout<<ex.what()<<endl; return false; } // read board file try { TheBoardConfig.readFromFile(argv[3]); } catch (std::exception &ex) { cout<<ex.what()<<endl; return false; } if(argc>4) TheMarkerSize=atof(argv[4]); else TheMarkerSize=1.; return true; }
/*! * */ int main(int argc,char **argv) { readArguments(argc,argv); aruco::BoardConfiguration boardConf; boardConf.readFromFile(boC->sval[0]); cv::VideoCapture vcapture; //read from camera or from file if (strcmp(inp->sval[0], "live")==0) { vcapture.open(0); vcapture.set(CV_CAP_PROP_FRAME_WIDTH, wid->ival[0]); vcapture.set(CV_CAP_PROP_FRAME_HEIGHT, hei->ival[0]); int val = CV_FOURCC('M', 'P', 'E', 'G'); vcapture.set(CV_CAP_PROP_FOURCC, val); } else vcapture.open(inp->sval[0]); //check video is open if (!vcapture.isOpened()) { std::cerr<<"Could not open video"<<std::endl; return -1; } //read first image to get the dimensions vcapture>>inpImg; //Open outputvideo cv::VideoWriter vWriter; if (out->count) vWriter.open(out->sval[0], CV_FOURCC('M','J','P','G'), fps->ival[0], inpImg.size()); //read camera parameters if passed if (intr->count) { params.readFromXMLFile(intr->sval[0]); params.resize(inpImg.size()); } //Create gui cv::namedWindow("thres", CV_GUI_NORMAL | CV_WINDOW_AUTOSIZE); cv::namedWindow("in", CV_GUI_NORMAL | CV_WINDOW_AUTOSIZE); cvMoveWindow("thres", 0, 0); cvMoveWindow("in", inpImg.cols + 5, 0); boardDetector.setParams(boardConf,params,msiz->dval[0]); boardDetector.getMarkerDetector().getThresholdParams( dThresParam1,dThresParam2); // boardDetector.getMarkerDetector().enableErosion(true);//for chessboards iThresParam1=dThresParam1; iThresParam2=dThresParam2; cv::createTrackbar("ThresParam1", "in",&iThresParam1, 13, cvTrackBarEvents); cv::createTrackbar("ThresParam2", "in",&iThresParam2, 13, cvTrackBarEvents); int index=0; /////////////// // Main Lopp // /////////////// while(appRunnin) { inpImg.copyTo(inpImgCpy); index++; //number of images captured double tick = static_cast<double>(cv::getTickCount());//for checking the speed float probDetect=boardDetector.detect(inpImg); //Detection of the board tick=(static_cast<double>(cv::getTickCount())-tick)/cv::getTickFrequency(); std::cout<<"Time detection="<<1000*tick<<" milliseconds"<<std::endl; //print marker borders for (unsigned int i=0; i<boardDetector.getDetectedMarkers().size(); i++) boardDetector.getDetectedMarkers()[i].draw(inpImgCpy,cv::Scalar(0,0,255),1); //print board if (params.isValid()) { if ( probDetect>thre->dval[0]) { aruco::CvDrawingUtils::draw3dAxis( inpImgCpy,boardDetector.getDetectedBoard(),params); } } //DONE! Easy, right? //show input with augmented information and the thresholded image cv::imshow("in",inpImgCpy); cv::imshow("thres",boardDetector.getMarkerDetector().getThresholdedImage()); // write to video if desired if (out->count) { //create a beautiful composed image showing the thresholded //first create a small version of the thresholded image cv::Mat smallThres; cv::resize(boardDetector.getMarkerDetector().getThresholdedImage(),smallThres, cv::Size(inpImgCpy.cols/3,inpImgCpy.rows/3)); cv::Mat small3C; cv::cvtColor(smallThres,small3C,CV_GRAY2BGR); cv::Mat roi=inpImgCpy(cv::Rect(0,0,inpImgCpy.cols/3,inpImgCpy.rows/3)); small3C.copyTo(roi); vWriter<<inpImgCpy; } processKey(cv::waitKey(waitTime));//wait for key to be pressed appRunnin &= vcapture.grab(); vcapture.retrieve(inpImg); } arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return EXIT_SUCCESS; }
int main() { // 打开摄像头 video_capture.open(0); if (!video_capture.isOpened()){ std::cerr << "Could not open video" << endl; return -1; } // 获取第一张图像,用于这设置参数 video_capture >> input_image; // 读取摄像机参数 camera_params.readFromXMLFile("camera.yml"); camera_params.resize(input_image.size()); // 注册窗口 cv::namedWindow("thres",1); cv::namedWindow("in",1); marker_dector.getThresholdParams(threshold_param1, threshold_param2); i_threshold_param1 = threshold_param1; i_threshold_param2 = threshold_param2; cv::createTrackbar("ThresParam1", "in",&i_threshold_param1, 13, cvTackBarEvents); cv::createTrackbar("ThresParam2", "in",&i_threshold_param2, 13, cvTackBarEvents); char key=0; int index=0; //capture until press ESC or until the end of the video while( key!=27 && video_capture.grab()) { //copy image video_capture.retrieve(input_image); //number of images captured index++; //for checking the speed double tick = (double)cv::getTickCount(); //Detection of markers in the image passed marker_dector.detect(input_image, markers, camera_params, marker_size); //chekc the speed by calculating the mean speed of all iterations average_time.first += ((double)cv::getTickCount() - tick) / cv::getTickFrequency(); average_time.second++; std::cout << "Time detection=" << 1000 * average_time.first / average_time.second << " milliseconds" << endl; //print marker info and draw the markers in image input_image.copyTo(input_image_copy); for(unsigned int i = 0; i < markers.size(); i++){ std::cout << markers[i] << std::endl; markers[i].draw(input_image, Scalar(0, 0, 255), 2); } //draw a 3d cube in each marker if there is 3d info if ( camera_params.isValid()) for(unsigned int i = 0; i < markers.size(); i++){ aruco::CvDrawingUtils::draw3dCube(input_image, markers[i], camera_params); aruco::CvDrawingUtils::draw3dAxis(input_image, markers[i], camera_params); } //DONE! Easy, right? cout << endl << endl << endl; //show input with augmented information and the thresholded image cv::imshow("in", input_image); cv::imshow("thres", marker_dector.getThresholdedImage()); key=cv::waitKey(0);//wait for key to be pressed } return 0; }