void vIdle() { if (TheCaptureFlag) { // capture image TheVideoCapturer.grab(); TheVideoCapturer.retrieve(TheInputImage); TheUndInputImage.create(TheInputImage.size(), CV_8UC3); // by deafult, opencv works in BGR, so we must convert to RGB because OpenGL in windows preffer cv::cvtColor(TheInputImage, TheInputImage, CV_BGR2RGB); // remove distorion in image cv::undistort(TheInputImage, TheUndInputImage, TheCameraParams.CameraMatrix, TheCameraParams.Distorsion); // detect markers MDetector.detect(TheUndInputImage, TheMarkers); // Detection of the board TheBoardDetected.second = TheBoardDetector.detect( TheMarkers, TheBoardConfig, TheBoardDetected.first, TheCameraParams, TheMarkerSize); // chekc the speed by calculating the mean speed of all iterations // resize the image to the size of the GL window cv::resize(TheUndInputImage, TheResizedImage, TheGlWindowSize); // create mask. It is a syntetic mask consisting of a simple rectangle, just to show a example of // opengl with mask TheMask = createSyntheticMask(TheResizedImage); // lets create with the same size of the resized // image, i.e. the size of the opengl window } glutPostRedisplay(); }
int main(int argc,char **argv) { try { if(argc<3) {cerr<<"Usage: image boardConfig.yml [cameraParams.yml] [markerSize] [outImage]"<<endl;exit(0);} aruco::CameraParameters CamParam; MarkerDetector MDetector; vector<aruco::Marker> Markers; float MarkerSize=-1; BoardConfiguration TheBoardConfig; BoardDetector TheBoardDetector; Board TheBoardDetected; cv::Mat InImage=cv::imread(argv[1]); TheBoardConfig.readFromFile(argv[2]); if (argc>=4) { CamParam.readFromXMLFile(argv[3]); //resizes the parameters to fit the size of the input image CamParam.resize( InImage.size()); } if (argc>=5) MarkerSize=atof(argv[4]); cv::namedWindow("in",1); MDetector.detect(InImage,Markers);//detect markers without computing R and T information //Detection of the board float probDetect=TheBoardDetector.detect( Markers, TheBoardConfig,TheBoardDetected, CamParam,MarkerSize); //for each marker, draw info and its boundaries in the image for(unsigned int i=0;i<Markers.size();i++){ cout<<Markers[i]<<endl; Markers[i].draw(InImage,Scalar(0,0,255),2); } //draw a 3d cube in each marker if there is 3d info if ( CamParam.isValid()){ for(unsigned int i=0;i<Markers.size();i++){ CvDrawingUtils::draw3dCube(InImage,Markers[i],CamParam); CvDrawingUtils::draw3dAxis(InImage,Markers[i],CamParam); } CvDrawingUtils::draw3dAxis(InImage,TheBoardDetected,CamParam); cout<<TheBoardDetected.Rvec<<" "<<TheBoardDetected.Tvec<<endl; } //draw board axis //show input with augmented information cv::imshow("in",InImage); cv::waitKey(0);//wait for key to be pressed if(argc>=6) cv::imwrite(argv[5],InImage); }catch(std::exception &ex) { cout<<"Exception :"<<ex.what()<<endl; } }
void cvTackBarEvents(int pos,void*) { if (iThresParam1<3) iThresParam1=3; if (iThresParam1%2!=1) iThresParam1++; if (ThresParam2<1) ThresParam2=1; ThresParam1=iThresParam1; ThresParam2=iThresParam2; TheBoardDetector.getMarkerDetector().setThresholdParams(ThresParam1,ThresParam2); //recompute //Detection of the board float probDetect=TheBoardDetector.detect( TheInputImage); if (TheCameraParameters.isValid() && probDetect>0.2) aruco::CvDrawingUtils::draw3dAxis(TheInputImageCopy,TheBoardDetector.getDetectedBoard(),TheCameraParameters); cv::imshow("in",TheInputImageCopy); cv::imshow("thres",TheBoardDetector.getMarkerDetector().getThresholdedImage()); }
void cvTackBarEvents(int pos,void*) { if (iThresParam1<3) iThresParam1=3; if (iThresParam1%2!=1) iThresParam1++; if (ThresParam2<1) ThresParam2=1; ThresParam1=iThresParam1; ThresParam2=iThresParam2; for (unsigned int i=0; i<TOTAL_METHODS; i++) MDetector[i].setThresholdParams(ThresParam1,ThresParam2); //recompute for (unsigned int i=0; i<TOTAL_METHODS; i++) MDetector[i].detect(TheInputImage,TheMarkers,TheCameraParameters ,TheMarkerSize); //Detection of the board float probDetect=TheBoardDetector.detect( TheMarkers, TheBoardConfig,TheBoardDetected, TheCameraParameters); if (TheCameraParameters.isValid() && probDetect>0.2) aruco::CvDrawingUtils::draw3dAxis(TheInputImageCopy,TheBoardDetected,TheCameraParameters); cv::imshow("in",TheInputImageCopy); cv::imshow("thres",MDetector[0].getThresholdedImage()); }
void vIdle() { if (TheCaptureFlag) { //capture image TheVideoCapturer.grab(); TheVideoCapturer.retrieve( TheInputImage); TheUndInputImage.create(TheInputImage.size(),CV_8UC3); //by deafult, opencv works in BGR, so we must convert to RGB because OpenGL in windows preffer cv::cvtColor(TheInputImage,TheInputImage,CV_BGR2RGB); //remove distorion in image cv::undistort(TheInputImage,TheUndInputImage, TheCameraParams.CameraMatrix,TheCameraParams.Distorsion); //detect markers MDetector.detect(TheUndInputImage,TheMarkers,TheCameraParams.CameraMatrix,Mat(),TheMarkerSize); //Detection of the board TheBoardDetected.second=TheBoardDetector.detect( TheMarkers, TheBoardConfig,TheBoardDetected.first, TheCameraParams,TheMarkerSize); //chekc the speed by calculating the mean speed of all iterations //resize the image to the size of the GL window cv::resize(TheUndInputImage,TheResizedImage,TheGlWindowSize); } glutPostRedisplay(); }
/*! * */ static void vIdle() { if (capture) { //capture image vCapturer >> inImg; assert(inImg.empty()==false); undImg.create(inImg.size(),CV_8UC3); //by default, opencv works in BGR, so we must convert to RGB because OpenGL in windows prefer // cv::cvtColor(inImg,inImg,CV_BGR2RGB); //remove distortion in image cv::undistort(inImg,undImg, camParams.getCamMatrix(),camParams.getDistor()); //detect markers mDetector.detect(undImg,markers,camParams.getCamMatrix(),Mat(),msiz->dval[0]); //Detection of the board board.second=bDetector.detect(markers, boardConfig,board.first, camParams,msiz->dval[0]); //check the speed by calculating the mean speed of all iterations //resize the image to the size of the GL window cv::resize(undImg, resImg, glSize); } glutPostRedisplay(); }
/**Static version (all in one) */ Board BoardDetector::detect(const cv::Mat &Image, const BoardConfiguration &bc, const CameraParameters &cp, float markerSizeMeters) { BoardDetector BD; BD.setParams(bc, cp, markerSizeMeters); BD.detect(Image); return BD.getDetectedBoard(); }
int main(int argc,char **argv) { try { if ( readArguments (argc,argv)==false) return 0; //parse arguments TheBoardConfig.readFromFile(TheBoardConfigFile); //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; //Open outputvideo if ( TheOutVideoFilePath!="") VWriter.open(TheOutVideoFilePath,CV_FOURCC('M','J','P','G'),15,TheInputImage.size()); //read camera parameters if passed if (TheIntrinsicFile!="") { TheCameraParameters.readFromXMLFile(TheIntrinsicFile); TheCameraParameters.resize(TheInputImage.size()); } //Create gui cv::namedWindow("thres",1); cv::namedWindow("in",1); TheBoardDetector.setParams(TheBoardConfig,TheCameraParameters,TheMarkerSize); TheBoardDetector.getMarkerDetector().getThresholdParams( ThresParam1,ThresParam2); TheBoardDetector.getMarkerDetector().enableErosion(true);//for chessboards 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); TheInputImage.copyTo(TheInputImageCopy); index++; //number of images captured double tick = (double)getTickCount();//for checking the speed //Detection of the board float probDetect=TheBoardDetector.detect(TheInputImage); //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 borders for (unsigned int i=0;i<TheBoardDetector.getDetectedMarkers().size();i++) TheBoardDetector.getDetectedMarkers()[i].draw(TheInputImageCopy,Scalar(0,0,255),1); //print board if (TheCameraParameters.isValid()) { if ( probDetect>0.2) { CvDrawingUtils::draw3dAxis( TheInputImageCopy,TheBoardDetector.getDetectedBoard(),TheCameraParameters); //draw3dBoardCube( TheInputImageCopy,TheBoardDetected,TheIntriscCameraMatrix,TheDistorsionCameraParams); } } //DONE! Easy, right? //show input with augmented information and the thresholded image cv::imshow("in",TheInputImageCopy); cv::imshow("thres",TheBoardDetector.getMarkerDetector().getThresholdedImage()); //write to video if required if ( TheOutVideoFilePath!="") { //create a beautiful compiosed image showing the thresholded //first create a small version of the thresholded image cv::Mat smallThres; cv::resize( TheBoardDetector.getMarkerDetector().getThresholdedImage(),smallThres,cvSize(TheInputImageCopy.cols/3,TheInputImageCopy.rows/3)); cv::Mat small3C; cv::cvtColor(smallThres,small3C,CV_GRAY2BGR); cv::Mat roi=TheInputImageCopy(cv::Rect(0,0,TheInputImageCopy.cols/3,TheInputImageCopy.rows/3)); small3C.copyTo(roi); VWriter<<TheInputImageCopy; // cv::imshow("TheInputImageCopy",TheInputImageCopy); } key=cv::waitKey(waitTime);//wait for key to be pressed processKey(key); } } catch (std::exception &ex) { cout<<"Exception :"<<ex.what()<<endl; } }
void image_callback(const sensor_msgs::ImageConstPtr& msg) { static tf::TransformBroadcaster br; if(!cam_info_received) return; cv_bridge::CvImagePtr cv_ptr; try { cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8); inImage = cv_ptr->image; resultImg = cv_ptr->image.clone(); //detection results will go into "markers" markers.clear(); //Ok, let's detect mDetector.detect(inImage, markers, camParam, marker_size, false); //Detection of the board float probDetect=the_board_detector.detect(markers, the_board_config, the_board_detected, camParam, marker_size); if (probDetect > 0.0) { foundBoard = true; //added/////////////////////// tf::Transform transform = ar_sys::getTf(the_board_detected.Rvec, the_board_detected.Tvec); tf::StampedTransform stampedTransform(transform, msg->header.stamp, msg->header.frame_id, board_frame); if (publish_tf) br.sendTransform(stampedTransform); geometry_msgs::PoseStamped poseMsg; tf::poseTFToMsg(transform, poseMsg.pose); poseMsg.header.frame_id = msg->header.frame_id; poseMsg.header.stamp = msg->header.stamp; pose_pub.publish(poseMsg); geometry_msgs::TransformStamped transformMsg; tf::transformStampedTFToMsg(stampedTransform, transformMsg); transform_pub.publish(transformMsg); geometry_msgs::Vector3Stamped positionMsg; positionMsg.header = transformMsg.header; positionMsg.vector = transformMsg.transform.translation; position_pub.publish(positionMsg); std_msgs::Float32 boardSizeMsg; boardSizeMsg.data=the_board_detected[0].ssize; boardSize_pub.publish(boardSizeMsg); } //ADDED//////////////////////////////////////////////////////////////////////////// if(rvec_pub.getNumSubscribers() > 0 && foundBoard) { cv_bridge::CvImage rvecMsg; rvecMsg.header.frame_id = msg->header.frame_id; rvecMsg.header.stamp = msg->header.stamp; rvecMsg.encoding = sensor_msgs::image_encodings::TYPE_32FC1; rvecMsg.image = the_board_detected[0].Rvec; rvec_pub.publish(rvecMsg.toImageMsg()); } if(tvec_pub.getNumSubscribers() > 0 && foundBoard) { cv_bridge::CvImage tvecMsg; tvecMsg.header.frame_id = msg->header.frame_id; tvecMsg.header.stamp = msg->header.stamp; tvecMsg.encoding = sensor_msgs::image_encodings::TYPE_32FC1; tvecMsg.image = the_board_detected[0].Tvec; tvec_pub.publish(tvecMsg.toImageMsg()); } /////////////////////////////////////////////////////////////////////////////// //for each marker, draw info and its boundaries in the image for(size_t i=0; draw_markers && i < markers.size(); ++i) { markers[i].draw(resultImg,cv::Scalar(0,0,255),2); } if(camParam.isValid() && marker_size != -1) { //draw a 3d cube in each marker if there is 3d info for(size_t i=0; i<markers.size(); ++i) { if (draw_markers_cube) CvDrawingUtils::draw3dCube(resultImg, markers[i], camParam); if (draw_markers_axis) CvDrawingUtils::draw3dAxis(resultImg, markers[i], camParam); } //draw board axis if (probDetect > 0.0) { CvDrawingUtils::draw3dAxis(resultImg, the_board_detected, camParam); } } if(image_pub.getNumSubscribers() > 0) { //show input with augmented information cv_bridge::CvImage out_msg; out_msg.header.frame_id = msg->header.frame_id; out_msg.header.stamp = msg->header.stamp; out_msg.encoding = sensor_msgs::image_encodings::RGB8; out_msg.image = resultImg; image_pub.publish(out_msg.toImageMsg()); } if(debug_pub.getNumSubscribers() > 0) { //show also the internal image resulting from the threshold operation cv_bridge::CvImage debug_msg; debug_msg.header.frame_id = msg->header.frame_id; debug_msg.header.stamp = msg->header.stamp; debug_msg.encoding = sensor_msgs::image_encodings::MONO8; debug_msg.image = mDetector.getThresholdedImage(); debug_pub.publish(debug_msg.toImageMsg()); } } catch (cv_bridge::Exception& e) { ROS_ERROR("cv_bridge exception: %s", e.what()); return; } }
void image_callback(const sensor_msgs::ImageConstPtr& msg) { if(!cam_info_received) return; cv_bridge::CvImagePtr cv_ptr; try { cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8); inImage = cv_ptr->image; resultImg = cv_ptr->image.clone(); //detection results will go into "markers" markers.clear(); //Ok, let's detect double min_size = boards[0].marker_size; for (int board_index = 1; board_index < boards.size(); board_index++) if (min_size > boards[board_index].marker_size) min_size = boards[board_index].marker_size; mDetector.detect(inImage, markers, camParam, min_size, false); for (int board_index = 0; board_index < boards.size(); board_index++) { Board board_detected; //Detection of the board float probDetect = the_board_detector.detect(markers, boards[board_index].config, board_detected, camParam, boards[board_index].marker_size); if (probDetect > 0.0) { tf::Transform transform = ar_sys::getTf(board_detected.Rvec, board_detected.Tvec); tf::StampedTransform stampedTransform(transform, ros::Time::now(), "apollon_cam_right", boards[board_index].name +"_right"); //_tfBraodcaster.sendTransform(stampedTransform); // from phillip /*geometry_msgs::PoseStamped poseMsg; tf::poseTFToMsg(transform, poseMsg.pose); poseMsg.header.frame_id = msg->header.frame_id; poseMsg.header.stamp = msg->header.stamp; pose_pub.publish(poseMsg);*/ geometry_msgs::TransformStamped transformMsg; tf::transformStampedTFToMsg(stampedTransform, transformMsg); transform_pub.publish(transformMsg); /*geometry_msgs::Vector3Stamped positionMsg; positionMsg.header = transformMsg.header; positionMsg.vector = transformMsg.transform.translation; position_pub.publish(positionMsg);*/ if(camParam.isValid()) { //draw board axis CvDrawingUtils::draw3dAxis(resultImg, board_detected, camParam); } } } //for each marker, draw info and its boundaries in the image for(size_t i=0; draw_markers && i < markers.size(); ++i) { markers[i].draw(resultImg,cv::Scalar(0,0,255),2); } if(camParam.isValid()) { //draw a 3d cube in each marker if there is 3d info for(size_t i=0; i<markers.size(); ++i) { if (draw_markers_cube) CvDrawingUtils::draw3dCube(resultImg, markers[i], camParam); if (draw_markers_axis) CvDrawingUtils::draw3dAxis(resultImg, markers[i], camParam); } } if(image_pub.getNumSubscribers() > 0) { //show input with augmented information cv_bridge::CvImage out_msg; out_msg.header.frame_id = msg->header.frame_id; out_msg.header.stamp = msg->header.stamp; out_msg.encoding = sensor_msgs::image_encodings::RGB8; out_msg.image = resultImg; image_pub.publish(out_msg.toImageMsg()); } if(debug_pub.getNumSubscribers() > 0) { //show also the internal image resulting from the threshold operation cv_bridge::CvImage debug_msg; debug_msg.header.frame_id = msg->header.frame_id; debug_msg.header.stamp = msg->header.stamp; debug_msg.encoding = sensor_msgs::image_encodings::MONO8; debug_msg.image = mDetector.getThresholdedImage(); debug_pub.publish(debug_msg.toImageMsg()); } } catch (cv_bridge::Exception& e) { ROS_ERROR("cv_bridge exception: %s", e.what()); return; } }
void MainWindow::on_pushButton_2_clicked() { std::string casen = "clutter"; bool saveimages = true; global::doDraw = true; std::string reportPath = "/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/"; //std::string filename = "Report_" + utils::currentDateTime(); //if (ui->UseDefaultglobal::image_imp->isChecked()){ if (!global::image_rgb.data){ //global::image_rgb = cv::imread("/Users/benedicte/Dropbox/kings/thesis/images/checkers7.jpg"); //global::image_rgb = cv::imread("/Users/benedicte/Dropbox/kings/thesis/report/boards/green2.jpg"); //global::image_rgb = cv::imread("/Users/benedicte/Dropbox/kings/thesis/report/pieces/red.jpg"); //global::image_rgb = cv::imread("/Users/benedicte/Dropbox/kings/thesis/report/casestudy/casestudy.jpg"); global::image_rgb = cv::imread("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/"+casen+".jpg"); if (global::image_rgb.data){ cvutils::rotate(global::image_rgb,global::image_rgb,-90); } else { throw std::invalid_argument("Image could not be read"); } } // Set up global image variables Preprocess prep; Settings::PreprocessSettings settings; settings.gaussianBlurSigma = 3; settings.gaussianBlurSize = cv::Size(3,3); // print image channels if (saveimages){ cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/image_r.png", global::image_r); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/image_g.png", global::image_g); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/image_b.png", global::image_b); } // chessboard detector Lines houghlines; Board board; // container for the detected board bool tryAgain = true; bool boardDetected = false; while (tryAgain){ prep.detectLines(settings); prep.getLines(houghlines); BoardDetector cbd = BoardDetector(houghlines); prep.showCanny(); prep.showHoughlines(); if (saveimages){ cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/rgb.png", global::image_rgb); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/gray.png", global::image_gray); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/normalized.png", global::image_norm); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/resized.png", global::image); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/canny.png", prep.getCanny()); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/hough.png", prep.getHough()); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/blurred.png", prep.getBlurred()); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/hough_mod.png", global::image_hough_mod); } try{ std::cout << "Trying to detect board with blur sigma " << settings.gaussianBlurSigma << std::endl; boardDetected = cbd.detect(board, &reportPath); } catch(std::exception &e){ } if (boardDetected){ tryAgain = false; } else { if (settings.gaussianBlurSigma > 1) settings.gaussianBlurSigma -= 1; if (settings.gaussianBlurSigma == 1){ settings.gaussianBlurSize = cv::Size(3,3); } if (settings.gaussianBlurSize == cv::Size(3,3) && settings.gaussianBlurSigma == 1){ settings.gaussianBlurSize = cv::Size(1,1); } if (settings.gaussianBlurSigma == 1 && settings.gaussianBlurSize == cv::Size(1,1) && settings.cannyLow > 4){ settings.cannyLow -= 4; } if (settings.gaussianBlurSigma == 1 && settings.gaussianBlurSize == cv::Size(1,1) && settings.cannyLow <= 4){ std::cout << "I give up" << std::endl; } } } // save images if (saveimages){ cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/rgb.png", global::image_rgb); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/gray.png", global::image_gray); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/normalized.png", global::image_norm); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/resized.png", global::image); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/canny.png", prep.getCanny()); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/hough.png", prep.getHough()); cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/blurred.png", prep.getBlurred()); } if (saveimages){ cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/hough_mod.png", global::image_hough_mod); board.write("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/final.png"); } board.draw(); cv::destroyAllWindows(); board.detectPieces(); if (global::doDraw) board.drawWithPieces(); board.writeImgWithPiecesToGlobal(); if (saveimages){ cv::imwrite("/Users/benedicte/Dropbox/kings/thesis/report/"+casen+"/pieces.png", global::image_pieces); } // Initial state to feed minimax cv::destroyAllWindows(); State state = board.initState(); std::cout << "FINISHED" << std::endl; // WRITE REPORTS }