diff --git a/modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp b/modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp index 1321ce423..8f15e8fad 100644 --- a/modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp +++ b/modules/cnn_3dobj/samples/sphereview_3dobj_demo.cpp @@ -64,6 +64,84 @@ Point3d getCenter(string plymodel) data.z = data.z/numPoint; return data; }; +void createHeader(int num_item, int rows, int cols, const char* headerPath) +{ + char* a0 = new char; + strcpy(a0, headerPath); + char a1[] = "image"; + char a2[] = "label"; + char* headerPathimg = new char; + strcpy(headerPathimg, a0); + strcat(headerPathimg, a1); + char* headerPathlab = new char; + strcpy(headerPathlab, a0); + strcat(headerPathlab, a2); + std::ofstream headerImg(headerPathimg, ios::out|ios::binary); + std::ofstream headerLabel(headerPathlab, ios::out|ios::binary); + int headerimg[4] = {2051,num_item,rows,cols}; + int headerlabel[2] = {2049,num_item}; + headerImg.write(reinterpret_cast(headerimg), sizeof(int)*4); + headerImg.close(); + headerLabel.write(reinterpret_cast(headerlabel), sizeof(int)*2); + headerLabel.close(); +}; +void writeBinaryfile(string filename, const char* binaryPath, const char* headerPath, int num_item) +{ + int isrgb = 0; + cv::Mat ImgforBin = cv::imread(filename, isrgb); + char* A0 = new char; + strcpy(A0, binaryPath); + char A1[] = "image"; + char A2[] = "label"; + char* binPathimg = new char; + strcpy(binPathimg, A0); + strcat(binPathimg, A1); + char* binPathlab = new char; + strcpy(binPathlab, A0); + strcat(binPathlab, A2); + fstream img_file, lab_file; + img_file.open(binPathimg,ios::in); + lab_file.open(binPathlab,ios::in); + if(!img_file) + { + cout << "Creating the training data at: " << binaryPath << ". " << endl; + char* a0 = new char; + strcpy(a0, headerPath); + char a1[] = "image"; + char a2[] = "label"; + char* headerPathimg = new char; + strcpy(headerPathimg, a0); + strcat(headerPathimg,a1); + char* headerPathlab = new char; + strcpy(headerPathlab, a0); + strcat(headerPathlab,a2); + createHeader(num_item, 250, 250, binaryPath); + ofstream img_file(binPathimg,ios::out|ios::binary|ios::app); + ofstream lab_file(binPathlab,ios::out|ios::binary|ios::app); + for (int r = 0; r < ImgforBin.rows; r++) + { + img_file.write(reinterpret_cast(ImgforBin.ptr(r)), ImgforBin.cols*ImgforBin.elemSize()); + } + unsigned char templab = 0; + lab_file << templab; + } + else + { + img_file.close(); + lab_file.close(); + img_file.open(binPathimg,ios::out|ios::binary|ios::app); + lab_file.open(binPathlab,ios::out|ios::binary|ios::app); + cout <<"Concatenating the training data at: " << binaryPath << ". " << endl; + for (int r = 0; r < ImgforBin.rows; r++) + { + img_file.write(reinterpret_cast(ImgforBin.ptr(r)), ImgforBin.cols*ImgforBin.elemSize()); + } + unsigned char templab = 0; + lab_file << templab; + } + img_file.close(); + lab_file.close(); +}; int main(int argc, char *argv[]){ const String keys = "{help | | demo :$ ./sphereview_test -radius=250 -ite_depth=1 -plymodel=../ape.ply -imagedir=../data/images_ape/ -labeldir=../data/label_ape.txt, then press 'q' to run the demo for images generation when you see the gray background and a coordinate.}" "{radius | 250 | Distanse from camera to object, used for adjust view for the reason that differet scale of .ply model.}" @@ -93,6 +171,7 @@ int main(int argc, char *argv[]){ bool camera_pov = (true); /// Create a window viz::Viz3d myWindow("Coordinate Frame"); + myWindow.setWindowSize(Size(250,250)); /// Add coordinate axes myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem()); myWindow.setBackgroundColor(viz::Color::gray()); @@ -101,6 +180,10 @@ int main(int argc, char *argv[]){ /// Let's assume camera has the following properties Point3d cam_focal_point = getCenter(plymodel); Point3d cam_y_dir(0.0f,0.0f,1.0f); + int num_obj = 1; + const char* headerPath = "./header_for_"; + const char* binaryPath = "./binary_"; + createHeader((int)campos.size(), 250, 250, headerPath); for(int pose = 0; pose < (int)campos.size(); pose++){ imglabel << campos.at(pose).x << ' ' << campos.at(pose).y << ' ' << campos.at(pose).z << endl; /// We can get the pose of the cam using makeCameraPoses @@ -142,6 +225,7 @@ int main(int argc, char *argv[]){ filename = imagedir + filename; filename += ".png"; myWindow.saveScreenshot(filename); + writeBinaryfile(filename, binaryPath, headerPath,(int)campos.size()*num_obj); } return 1; };