$search
00001 #include "aruco/board.h" 00002 #include <fstream> 00003 using namespace std; 00004 using namespace cv; 00005 namespace aruco { 00006 00007 cv::Mat Board::createBoardImage( Size gridSize,int MarkerSize,int MarkerDistance,unsigned int FirstMarkerID, BoardConfiguration& TInfo ) throw (cv::Exception) 00008 { 00009 00010 vector<vector<int> > MarkersIds; 00011 // vector<vector<int> > *TheMarkersIds=NULL; 00012 00013 srand(time(NULL)); 00014 TInfo._markersId.create(gridSize,CV_32SC1); 00015 int nMarkers=gridSize.height*gridSize.width; 00016 if (FirstMarkerID+nMarkers>=1024) 00017 throw cv::Exception(9189,"Creation of board impies a marker with an impossible id:","aruco::createBoard",__FILE__,__LINE__); 00018 unsigned int idp=0; 00019 for ( int i=0;i<gridSize.height;i++) 00020 for ( int j=0;j<gridSize.width;j++,idp++) 00021 TInfo._markersId.at<int>(i,j)=FirstMarkerID+idp; //number in the range [0,1023] 00022 00023 00024 00025 00026 TInfo._markerSizePix=MarkerSize; 00027 TInfo._markerDistancePix=MarkerDistance; 00028 00029 int sizeY=gridSize.height*MarkerSize+(gridSize.height-1)*MarkerDistance; 00030 int sizeX=gridSize.width*MarkerSize+(gridSize.width-1)*MarkerDistance; 00031 00032 Mat tableImage(sizeY,sizeX,CV_8UC1); 00033 tableImage.setTo(Scalar(255)); 00034 for (int y=0;y<gridSize.height;y++) 00035 for (int x=0;x<gridSize.width;x++) { 00036 Mat subrect(tableImage,Rect( x*(MarkerDistance+MarkerSize),y*(MarkerDistance+MarkerSize),MarkerSize,MarkerSize)); 00037 Mat marker=Marker::createMarkerImage( TInfo._markersId.at<int>(y,x),MarkerSize); 00038 marker.copyTo(subrect); 00039 } 00040 00041 return tableImage; 00042 } 00047 BoardConfiguration::BoardConfiguration() 00048 { 00049 _markerSizePix=_markerDistancePix=-1; 00050 } 00055 BoardConfiguration::BoardConfiguration(const BoardConfiguration &T) 00056 { 00057 _markersId=T._markersId; 00058 _markerSizePix=T._markerSizePix; 00059 _markerDistancePix=T._markerDistancePix; 00060 00061 } 00066 void BoardConfiguration::saveToFile(string sfile)throw (cv::Exception) 00067 { 00068 ofstream file(sfile.c_str()); 00069 if (!file) throw cv::Exception(9190,"File could not be opened for writing :"+sfile,"BoardConfiguration::saveToFile",__FILE__,__LINE__); 00070 file<<"ArucoBoard 1.0"<<endl; 00071 file<<_markersId.size().height<<" "<<_markersId.size().width<<" "; 00072 for ( int i=0;i<_markersId.size().height;i++) { 00073 for ( int j=0;j<_markersId.size().width;j++) 00074 file<<_markersId.at<int>(i,j)<<" "; 00075 } 00076 file<<endl; 00077 file<< _markerSizePix<<" "<<_markerDistancePix<<" "<<endl; 00078 00079 } 00084 void BoardConfiguration::readFromFile(string sfile)throw (cv::Exception) 00085 { 00086 ifstream file(sfile.c_str()); 00087 if (!file) throw cv::Exception(9191,"File could not be opened fir reading :"+sfile,"BoardConfiguration::readFromFile",__FILE__,__LINE__); 00088 string sig,ver; 00089 file>>sig>>ver; 00090 if (sig!="ArucoBoard") throw cv::Exception(9191,"Invalid file type :"+sfile,"BoardConfiguration::readFromFile",__FILE__,__LINE__); 00091 if (ver!="1.0") throw cv::Exception(9191,"Invalid file version :"+sfile,"BoardConfiguration::readFromFile",__FILE__,__LINE__); 00092 Size size; 00093 file>>size.height>>size.width;//read size 00094 _markersId.create(size,CV_32SC1); 00095 for ( int i=0;i<size.height;i++) 00096 for ( int j=0;j<size.width;j++) 00097 file>>_markersId.at<int>(i,j); 00098 00099 file>> _markerSizePix>>_markerDistancePix; 00100 } 00101 00102 }