KDvoid FileInputOutput ( KDint nIdx ) { string filename = "/data/outputfile.xml.gz"; string sOuput; // write { Mat R = Mat_<uchar>::eye(3, 3), T = Mat_<double>::zeros(3, 1); MyData m(1); FileStorage fs ( filename, FileStorage::WRITE ); fs << "iterationNr" << 100; fs << "strings" << "["; // text - string sequence fs << "image1.jpg" << "Awesomeness" << "baboon.jpg"; fs << "]"; // close sequence fs << "Mapping"; // text - mapping fs << "{" << "One" << 1; fs << "Two" << 2 << "}"; fs << "R" << R; // cv::Mat fs << "T" << T; fs << "MyData" << m; // your own data structures fs.release(); // explicit close } // read { sOuput += "Reading: \n"; FileStorage fs; fs.open ( filename, FileStorage::READ ); int itNr; itNr = (int) fs [ "iterationNr" ]; sOuput += itNr; if (!fs.isOpened()) { return; } FileNode n = fs["strings"]; // Read string sequence - Get node if (n.type() != FileNode::SEQ) { return; } n = fs["Mapping"]; // Read mappings from a sequence MyData m; Mat R, T; fs["R"] >> R; // Read cv::Mat fs["T"] >> T; fs["MyData"] >> m; // Read your own structure_ //cout << endl // << "R = " << R << endl; //cout << "T = " << T << endl << endl; //cout << "MyData = " << endl << m << endl << endl; //Show default behavior for non existing nodes //cout << "Attempt to read NonExisting (should initialize the data structure with its default)."; fs["NonExisting"] >> m; } g_pController->setMessage ( "See Data Folder" ); }
int main(int argc,char *argv[]) { path p (argv[1]); // p reads clearer than argv[1] in the following code // file.open("coordinates.txt"); if (exists(p)) // does p actually exist? { if (is_directory(p)) // is p a directory? { cout << p << " is a directory. Thank You!!\n"; vec v; copy(directory_iterator(p), directory_iterator(), back_inserter(v)); // cout << "hell yeah" << endl; vector<string> images_list; cvNamedWindow("AnnotateGT",CV_WINDOW_NORMAL); cvSetMouseCallback("AnnotateGT", mousehandler); // cout << images_list.size() << endl; /* int cnt = 0;*/ int dir_flag = decide_directory(p); string format_temp; if(dir_flag == 4) { cout << "Enter the barcode format: "; cin >> format_temp; } cout << dir_flag << endl; int flgg = 0; int index = 1; for (vec::const_iterator it (v.begin()); it != v.end(); ++it) { string temp = it->string(); // cout << temp << endl; // cout << basename(temp) << endl; if(temp.compare(temp.size()-4,4,".png")==0 || temp.compare(temp.size()-4,4,".jpg")==0 || temp.compare(temp.size()-4,4,".JPG")==0) { cout << index << " Out of " << v.size() << endl; index++; cout << temp << endl; string f_name; bool barcode_present; vector<string> barcode_format, decode_out; corner_points.clear(); temp_points.clear(); point_count = 0; if(dir_flag == 1) //blade/QVGA { f_name = basename(temp); decode_out.push_back(f_name.substr(0,f_name.size()-2)); f_name = "./gt/" + f_name + ".yml"; cout << "Filename is " << f_name << endl; cout << "The decode output is " << decode_out[0] << endl; barcode_present = true; barcode_format.push_back("UPC_A"); } else if(dir_flag == 2) //blade/VGA { f_name = basename(temp); decode_out.push_back(f_name.substr(0,f_name.size()-1)); f_name = "./gt/" + f_name + ".yml"; cout << "Filename is " << f_name << endl; cout << "The decode output is " << decode_out[0] << endl; barcode_present = true; barcode_format.push_back("UPC_A"); } else if(dir_flag == 3) //manduchi { f_name = basename(temp); decode_out.push_back(f_name.substr(0,f_name.find("_"))); f_name = "./gt/" + f_name + ".yml"; cout << "Filename is " << f_name << endl; cout << "The decode output is " << decode_out[0] << endl; barcode_present = true; barcode_format.push_back("UPC_A"); } else if(dir_flag == 4) //zxing { f_name = "./gt/" + basename(temp) + ".yml"; cout << "Filename is " << f_name << endl; string inStr = temp.substr(0,temp.size()-3) + "txt"; cout << inStr << endl; ifstream inFile; inFile.open(inStr.c_str()); string one_line; getline(inFile,one_line); cout << one_line << endl; inFile.close(); decode_out.push_back(one_line); barcode_format.push_back(format_temp); barcode_present = true; } else if(dir_flag == 5) { // if(flgg == 0) // { // if(basename(temp) == "Foto(507)") // flgg = 1; // continue; // } f_name = "./gt/" + basename(temp) + ".yml"; cout << "Output file name is " << f_name << endl; string inStr = temp + ".txt"; cout << "Input file name is " << inStr << endl; ifstream inFile; inFile.open(inStr.c_str()); string one_line; getline(inFile,one_line); cout << one_line << endl; inFile.close(); decode_out.push_back(one_line); barcode_present = true; barcode_format.push_back("ean13"); } else { cout << "NOT A DATASET DIRECTORY" << endl; return 0; } Mat img = imread(temp); imshow("AnnotateGT",img); cvWaitKey(0); fs.open(f_name,FileStorage::WRITE); fs << "barcode_present" << barcode_present; fs << "barcode_format" << "["; for(int i=0;i < barcode_format.size();i++) fs << "[:" << barcode_format[i] << "]"; fs << "]"; fs << "decode_out" << "[:"; for(int i=0;i < decode_out.size();i++) fs << "[:" << decode_out[i] << "]"; fs << "]"; cout << corner_points[0].size() << " ******************* " << corner_points[0].size() << endl; fs << "corner_points" << "["; for(int i=0;i < corner_points.size();i++) { fs << "[:"; for(int j=0;j < corner_points[i].size();j++) { // cout << corner_points[i][j]; fs << "[:" << corner_points[i][j].x << corner_points[i][j].y << "]"; } fs << "]"; } fs << "]"; fs.release(); // if(!parse_with_zxing(img)) // { // cout << "Zxing Failed in decoding" << endl; // } } } cvDestroyWindow("AnnotateGT"); } else
int main(int argc, char* argv[]) { SURF extractor; Mat train_descr; SurfFeatureDetector detector(700); BFMatcher match(NORM_L2); //Try to replace with FLANN vector<vector<DMatch> > matches; //namedWindow("Image", CV_WINDOW_AUTOSIZE ); vector<KeyPoint> keypoints; Mat descriptors; FileStorage train; train.open("Train.xml", FileStorage::READ); int correct = 0; float mean_error = 0; float min = 0; int m = 0; string name = argv[1]; for(int x = 2;x<argc;x++) { Mat img = imread(argv[x]); detector.detect(img, keypoints); //drawKeypoints(img,keypoints,img,255); //imshow("Image",img); //waitKey(0); extractor.compute(img, keypoints, descriptors); //Compute errors mean_error = 0; min = 0; m = 0; for(int n = 0; n <CLASSES; n++) { train[set[n]]>>train_descr; match.knnMatch(descriptors,train_descr,matches,NEIGHBOURS); for(int j=0;j<NEIGHBOURS;j++) for(int i=0;i<KEYPOINTS;i++) { mean_error+=matches.at(i).at(j).distance; } mean_error = mean_error/(NEIGHBOURS*KEYPOINTS); if(n==0) min = mean_error; if(min>mean_error) { min = mean_error; m = n; } } if(!name.compare(set[m])) correct+=1; } cout<<"Classification accuracy for "<<name<<" is:"<<(((float)correct)/(argc-2)) * 100<<endl; train.release(); return 0; }
int main( int argc, char** argv ) { int i, k; int flags = 0; Size boardSize, imageSize; float squareSize = 1.f, aspectRatio = 1.f; const char* outputFilename = "out_camera_data.yml"; const char* inputFilename = 0; vector<vector<Point2f> > imgpt[3]; vector<string> imageList; if(argc < 2) { help(); return 1; } for( i = 1; i < argc; i++ ) { const char* s = argv[i]; if( strcmp( s, "-w" ) == 0 ) { if( sscanf( argv[++i], "%u", &boardSize.width ) != 1 || boardSize.width <= 0 ) return fprintf( stderr, "Invalid board width\n" ), -1; } else if( strcmp( s, "-h" ) == 0 ) { if( sscanf( argv[++i], "%u", &boardSize.height ) != 1 || boardSize.height <= 0 ) return fprintf( stderr, "Invalid board height\n" ), -1; } else if( strcmp( s, "-s" ) == 0 ) { if( sscanf( argv[++i], "%f", &squareSize ) != 1 || squareSize <= 0 ) return fprintf( stderr, "Invalid board square width\n" ), -1; } else if( strcmp( s, "-a" ) == 0 ) { if( sscanf( argv[++i], "%f", &aspectRatio ) != 1 || aspectRatio <= 0 ) return printf("Invalid aspect ratio\n" ), -1; flags |= CALIB_FIX_ASPECT_RATIO; } else if( strcmp( s, "-zt" ) == 0 ) { flags |= CALIB_ZERO_TANGENT_DIST; } else if( strcmp( s, "-p" ) == 0 ) { flags |= CALIB_FIX_PRINCIPAL_POINT; } else if( strcmp( s, "-o" ) == 0 ) { outputFilename = argv[++i]; } else if( s[0] != '-' ) { inputFilename = s; } else return fprintf( stderr, "Unknown option %s", s ), -1; } if( !inputFilename || !readStringList(inputFilename, imageList) || imageList.size() == 0 || imageList.size() % 3 != 0 ) { printf("Error: the input image list is not specified, or can not be read, or the number of files is not divisible by 3\n"); return -1; } Mat view, viewGray; Mat cameraMatrix[3], distCoeffs[3], R[3], P[3], R12, T12; for( k = 0; k < 3; k++ ) { cameraMatrix[k] = Mat_<double>::eye(3,3); cameraMatrix[k].at<double>(0,0) = aspectRatio; cameraMatrix[k].at<double>(1,1) = 1; distCoeffs[k] = Mat_<double>::zeros(5,1); } Mat R13=Mat_<double>::eye(3,3), T13=Mat_<double>::zeros(3,1); FileStorage fs; namedWindow( "Image View", 0 ); for( k = 0; k < 3; k++ ) imgpt[k].resize(imageList.size()/3); for( i = 0; i < (int)(imageList.size()/3); i++ ) { for( k = 0; k < 3; k++ ) { int k1 = k == 0 ? 2 : k == 1 ? 0 : 1; printf("%s\n", imageList[i*3+k].c_str()); view = imread(imageList[i*3+k], 1); if(!view.empty()) { vector<Point2f> ptvec; imageSize = view.size(); cvtColor(view, viewGray, COLOR_BGR2GRAY); bool found = findChessboardCorners( view, boardSize, ptvec, CALIB_CB_ADAPTIVE_THRESH ); drawChessboardCorners( view, boardSize, Mat(ptvec), found ); if( found ) { imgpt[k1][i].resize(ptvec.size()); std::copy(ptvec.begin(), ptvec.end(), imgpt[k1][i].begin()); } //imshow("view", view); //int c = waitKey(0) & 255; //if( c == 27 || c == 'q' || c == 'Q' ) // return -1; } } } printf("Running calibration ...\n"); run3Calibration(imgpt[0], imgpt[1], imgpt[2], imageSize, boardSize, squareSize, aspectRatio, flags|CALIB_FIX_K4|CALIB_FIX_K5, cameraMatrix[0], distCoeffs[0], cameraMatrix[1], distCoeffs[1], cameraMatrix[2], distCoeffs[2], R12, T12, R13, T13); fs.open(outputFilename, FileStorage::WRITE); fs << "cameraMatrix1" << cameraMatrix[0]; fs << "cameraMatrix2" << cameraMatrix[1]; fs << "cameraMatrix3" << cameraMatrix[2]; fs << "distCoeffs1" << distCoeffs[0]; fs << "distCoeffs2" << distCoeffs[1]; fs << "distCoeffs3" << distCoeffs[2]; fs << "R12" << R12; fs << "T12" << T12; fs << "R13" << R13; fs << "T13" << T13; fs << "imageWidth" << imageSize.width; fs << "imageHeight" << imageSize.height; Mat Q; // step 3: find rectification transforms double ratio = rectify3Collinear(cameraMatrix[0], distCoeffs[0], cameraMatrix[1], distCoeffs[1], cameraMatrix[2], distCoeffs[2], imgpt[0], imgpt[2], imageSize, R12, T12, R13, T13, R[0], R[1], R[2], P[0], P[1], P[2], Q, -1., imageSize, 0, 0, CALIB_ZERO_DISPARITY); Mat map1[3], map2[3]; fs << "R1" << R[0]; fs << "R2" << R[1]; fs << "R3" << R[2]; fs << "P1" << P[0]; fs << "P2" << P[1]; fs << "P3" << P[2]; fs << "disparityRatio" << ratio; fs.release(); printf("Disparity ratio = %g\n", ratio); for( k = 0; k < 3; k++ ) initUndistortRectifyMap(cameraMatrix[k], distCoeffs[k], R[k], P[k], imageSize, CV_16SC2, map1[k], map2[k]); Mat canvas(imageSize.height, imageSize.width*3, CV_8UC3), small_canvas; destroyWindow("view"); canvas = Scalar::all(0); for( i = 0; i < (int)(imageList.size()/3); i++ ) { canvas = Scalar::all(0); for( k = 0; k < 3; k++ ) { int k1 = k == 0 ? 2 : k == 1 ? 0 : 1; int k2 = k == 0 ? 1 : k == 1 ? 0 : 2; view = imread(imageList[i*3+k], 1); if(view.empty()) continue; Mat rview = canvas.colRange(k2*imageSize.width, (k2+1)*imageSize.width); remap(view, rview, map1[k1], map2[k1], INTER_LINEAR); } printf("%s %s %s\n", imageList[i*3].c_str(), imageList[i*3+1].c_str(), imageList[i*3+2].c_str()); resize( canvas, small_canvas, Size(1500, 1500/3) ); for( k = 0; k < small_canvas.rows; k += 16 ) line(small_canvas, Point(0, k), Point(small_canvas.cols, k), Scalar(0,255,0), 1); imshow("rectified", small_canvas); int c = waitKey(0); if( c == 27 || c == 'q' || c == 'Q' ) break; } return 0; }
TEST_P(ML_ANN_METHOD, Test) { int methodType = get<0>(GetParam()); string methodName = get<1>(GetParam()); int N = get<2>(GetParam()); String folder = string(cvtest::TS::ptr()->get_data_path()); String original_path = folder + "waveform.data"; String dataname = folder + "waveform" + '_' + methodName; Ptr<TrainData> tdata2 = TrainData::loadFromCSV(original_path, 0); Mat samples = tdata2->getSamples()(Range(0, N), Range::all()); Mat responses(N, 3, CV_32FC1, Scalar(0)); for (int i = 0; i < N; i++) responses.at<float>(i, static_cast<int>(tdata2->getResponses().at<float>(i, 0))) = 1; Ptr<TrainData> tdata = TrainData::create(samples, ml::ROW_SAMPLE, responses); ASSERT_FALSE(tdata.empty()) << "Could not find test data file : " << original_path; RNG& rng = theRNG(); rng.state = 0; tdata->setTrainTestSplitRatio(0.8); Mat testSamples = tdata->getTestSamples(); #ifdef GENERATE_TESTDATA { Ptr<ml::ANN_MLP> xx = ml::ANN_MLP_ANNEAL::create(); Mat_<int> layerSizesXX(1, 4); layerSizesXX(0, 0) = tdata->getNVars(); layerSizesXX(0, 1) = 30; layerSizesXX(0, 2) = 30; layerSizesXX(0, 3) = tdata->getResponses().cols; xx->setLayerSizes(layerSizesXX); xx->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM); xx->setTrainMethod(ml::ANN_MLP::RPROP); xx->setTermCriteria(TermCriteria(TermCriteria::COUNT, 1, 0.01)); xx->train(tdata, ml::ANN_MLP::NO_OUTPUT_SCALE + ml::ANN_MLP::NO_INPUT_SCALE); FileStorage fs; fs.open(dataname + "_init_weight.yml.gz", FileStorage::WRITE + FileStorage::BASE64); xx->write(fs); fs.release(); } #endif { FileStorage fs; fs.open(dataname + "_init_weight.yml.gz", FileStorage::READ); Ptr<ml::ANN_MLP> x = ml::ANN_MLP_ANNEAL::create(); x->read(fs.root()); x->setTrainMethod(methodType); if (methodType == ml::ANN_MLP::ANNEAL) { x->setAnnealEnergyRNG(RNG(CV_BIG_INT(0xffffffff))); x->setAnnealInitialT(12); x->setAnnealFinalT(0.15); x->setAnnealCoolingRatio(0.96); x->setAnnealItePerStep(11); } x->setTermCriteria(TermCriteria(TermCriteria::COUNT, 100, 0.01)); x->train(tdata, ml::ANN_MLP::NO_OUTPUT_SCALE + ml::ANN_MLP::NO_INPUT_SCALE + ml::ANN_MLP::UPDATE_WEIGHTS); ASSERT_TRUE(x->isTrained()) << "Could not train networks with " << methodName; string filename = dataname + ".yml.gz"; Mat r_gold; #ifdef GENERATE_TESTDATA x->save(filename); x->predict(testSamples, r_gold); { FileStorage fs_response(dataname + "_response.yml.gz", FileStorage::WRITE + FileStorage::BASE64); fs_response << "response" << r_gold; } #else { FileStorage fs_response(dataname + "_response.yml.gz", FileStorage::READ); fs_response["response"] >> r_gold; } #endif ASSERT_FALSE(r_gold.empty()); Ptr<ml::ANN_MLP> y = Algorithm::load<ANN_MLP>(filename); ASSERT_TRUE(y != NULL) << "Could not load " << filename; Mat rx, ry; for (int j = 0; j < 4; j++) { rx = x->getWeights(j); ry = y->getWeights(j); double n = cvtest::norm(rx, ry, NORM_INF); EXPECT_LT(n, FLT_EPSILON) << "Weights are not equal for layer: " << j; } x->predict(testSamples, rx); y->predict(testSamples, ry); double n = cvtest::norm(ry, rx, NORM_INF); EXPECT_LT(n, FLT_EPSILON) << "Predict are not equal to result of the saved model"; n = cvtest::norm(r_gold, rx, NORM_INF); EXPECT_LT(n, FLT_EPSILON) << "Predict are not equal to 'gold' response"; } }