// Returns a (WH)xd matrix, i.e. a matrix where each row is a feature vector. // The input image type is supposed to be CV_8UC3. Mat HueFeatureExtractor::computeFeatureSet(const Mat& img, const Mat& mask) { // Compute the number of pixels to be considered int num_features = 0; for(int i=0; i<mask.rows; i++) { for(int j=0; j<mask.cols; j++) { if(mask.at<bool>(i,j)) num_features++; } } // Create feature matrix; each row represents a feature vector Mat feature_matrix(num_features, featureNumber(), CV_64F); // Counter for the next feature vector to write int feature_counter = 0; // Convert to HSV Mat img_hsv; cvtColor(img, img_hsv, CV_BGR2HSV); // Compute features: hue for(int i=0; i<img_hsv.rows; i++) { // Set the feature vector to the pixel's RGB value for(int j=0; j<img_hsv.cols; j++) { if(mask.at<bool>(i,j)) { // Get the (i,j) pixel Vec3b pixel = img_hsv.at<Vec3b>(i, j); // Set the feature matrix values, normalizing in [0,1] feature_matrix.at<double>(feature_counter, 0) = ((double)pixel[0])/10.0; // Increment the number of feature vectors feature_counter++; } } } // Return feature matrix return feature_matrix; }
int main (int argc, char *argv[]) { document *data; double *alpha; double **beta; FILE *ap, *bp; // for alpha, beta char c; int nlex, dlenmax; int nclass = CLASS_DEFAULT; // default in lda.h int emmax = EMMAX_DEFAULT; // default in lda.h int demmax = DEMMAX_DEFAULT; // default in lda.h double epsilon = EPSILON_DEFAULT; // default in lda.h while ((c = getopt(argc, argv, "N:I:D:E:h")) != -1) { switch (c) { case 'N': nclass = atoi(optarg); break; case 'I': emmax = atoi(optarg); break; case 'D': demmax = atoi(optarg); break; case 'E': epsilon = atof(optarg); break; case 'h': usage (); break; default : usage (); break; } } if (!(argc - optind == 2)) usage (); /* open data */ if ((data = feature_matrix(argv[optind], &nlex, &dlenmax)) == NULL) { fprintf(stderr, "lda:: cannot open training data.\n"); exit(1); } /* allocate parameters */ if ((alpha = (double *)calloc(nclass, sizeof(double))) == NULL) { fprintf(stderr, "lda:: cannot allocate alpha.\n"); exit(1); } if ((beta = dmatrix(nlex, nclass)) == NULL) { fprintf(stderr, "lda:: cannot allocate beta.\n"); exit(1); } /* open model outputs */ if (((ap = fopen(strconcat(argv[optind + 1], ".alpha"), "w")) == NULL) || ((bp = fopen(strconcat(argv[optind + 1], ".beta"), "w")) == NULL)) { fprintf(stderr, "lda:: cannot open model outputs.\n"); exit(1); } lda_learn (data, alpha, beta, nclass, nlex, dlenmax, emmax, demmax, epsilon); lda_write (ap, bp, alpha, beta, nclass, nlex); free_feature_matrix(data); free_dmatrix(beta, nlex); free(alpha); fclose(ap); fclose(bp); exit(0); }
int main(int argc, char **argv) { std::cout<<"Usage: SampleLocalStatistics image.vtk mesh.vtp label_array_name output_features.csv output_label.csv"<<std::endl; // const char* inputImageFileName = "/home/costa/Dropbox/code/HOG3D/bin/01D-LGE-320.vtk"; // const char* inputMeshFileName = "/home/costa/Dropbox/code/HOG3D/bin/01D_320_autolabels.vtp"; // const char* outputHogFileName = "/home/costa/Dropbox/code/HOG3D/bin/01D_320_hog.csv"; // const char* outputLabelsFileName = "/home/costa/Dropbox/code/HOG3D/bin/01D_320_lbl.csv"; // const char* labelArrayName = "autolabels"; if(argc<6) return EXIT_FAILURE; int c=1; const char* inputImageFileName = argv[c++]; const char* inputMeshFileName = argv[c++]; const char* labelArrayName = argv[c++]; const char* outputFeaturesFileName = argv[c++]; const char* outputLabelsFileName = argv[c++]; //float radius = atof(argv[c++]); //define some region of interest around the point arma::vec radii; //does not support more than 1 value for now radii << 0.3 << 0.7 <<1.0 <<1.6<<3.5<<5.0; //mm //---------------------------------------------------- // // // read the image // // typedef unsigned int PixelType; typedef itk::Image< PixelType, 3 > ImageType; typedef itk::ImageFileReader< ImageType > ImageReaderType; typename ImageReaderType::Pointer imageReader = ImageReaderType::New(); imageReader->SetFileName(inputImageFileName); try { imageReader->Update(); } catch (itk::ExceptionObject& e) { std::cerr << e.what() << std::endl; return EXIT_FAILURE; } // // //---------------------------------------------------- //---------------------------------------------------- // // read the mesh // // // vtkSmartPointer<vtkXMLPolyDataReader> rd = vtkSmartPointer<vtkXMLPolyDataReader>::New(); rd->SetFileName(inputMeshFileName); rd->Update(); vtkPolyData* mesh = rd->GetOutput(); // // //---------------------------------------------------- //---------------------------------------------------- // // save the labels // // // std::cout<<"Saving the labels"<<std::endl; vtkDataArray* mesh_labels = mesh->GetPointData()->GetArray(labelArrayName); arma::uvec labels(mesh_labels->GetNumberOfTuples()); for(int i=0; i<mesh_labels->GetNumberOfTuples(); i++) { labels(i) = mesh_labels->GetTuple1(i); } labels.save(outputLabelsFileName, arma::raw_ascii); // // //---------------------------------------------------- //---------------------------------------------------- // // for each point of the mesh, take normal and // for each scale (radius+sigma) get the HOG // // // //create the matrix to store everything //every row - HOGS of one point std::cout<<"Calculating Features"<<std::endl; //sample something to know the size of the neighborhood double pt[3]; mesh->GetPoint(0, pt); arma::vec features; const int nfeatures = 4; //4 statistical features arma::mat feature_matrix(mesh->GetNumberOfPoints(), nfeatures*radii.size()); // for(int j=0; j<radii.size(); j++) { std::cout<<"Radius "<<radii[j]<<std::endl; for(int i=0; i<mesh->GetNumberOfPoints(); i++) { if(i%1000==0) std::cout<<"Point "<<i<<"/"<<mesh->GetNumberOfPoints()<<"\r"<<std::flush; double pt[3]; mesh->GetPoint(i, pt); SampleStatistics<ImageType>(pt, radii[j], imageReader->GetOutput(), features); if(nfeatures!=features.size()) std::cout<<std::endl<<"Nonconstant number of samples (vertex "<<i<<"): "<<nfeatures<<" vs "<<features.size()<<std::endl<<std::flush; //copy the histogram into the matrix for(int k=0; k<features.size(); k++) { feature_matrix(i,k+j*nfeatures) = features(k); } } std::cout<<std::endl; } // // //---------------------------------------------------- std::cout<<std::endl<<"Saving"<<std::endl; //save the matrix feature_matrix.save(outputFeaturesFileName, arma::csv_ascii); // return EXIT_SUCCESS; }