int main(int argc, char** argv) {
    google::ParseCommandLineFlags(&argc, &argv, true);

    if (FLAGS_image_file == "") {
        cerr << "Image filename is required." << endl;
        return 1;
    }

    SlantEdgeMtf mtf_measurer;
    Mat image = imread(mats::ResolvePath(FLAGS_image_file), 0);
    if (!image.data) {
        cerr << "Could not read image file." << endl;
        return 1;
    }

    // Extract the slant edge ROI from the image.
    Mat roi;
    if (FLAGS_whole_image) {
        roi = image;
    } else {
        auto bounds = move(GetRoi(image));
        image(Range(bounds[1], bounds[1] + bounds[3]),
              Range(bounds[0], bounds[0] + bounds[2])).copyTo(roi);
    }

    SlantEdgeMtf mtf_analyzer;
    if (FLAGS_output_esf) {
        double edge[3];
        if (mtf_analyzer.DetectEdge(roi, edge)) {
            int samples = mtf_analyzer.GetSamplesPerPixel(image, edge);
            vector<double> esf, esf_stddevs;
            mtf_analyzer.GenerateEsf(roi, edge, samples, &esf, &esf_stddevs);
            mtf_analyzer.SmoothEsf(&esf);
            for (size_t i = 0; i < esf.size(); i++) {
                cout << i / double(samples) << "\t" << esf[i] << endl;
            }
        }
        return 0;
    }

    // Perform the slant edge analysis on the image.
    double orientation;
    vector<double> mtf;
    mtf_analyzer.Analyze(roi, &orientation, &mtf);

    // Create the plot data and print out the MTF.
    vector<pair<double, double>> mtf_data;
    for (size_t i = 0; i < mtf.size(); i++) {
        double freq = i / (2. * (mtf.size() - 1));
        mtf_data.emplace_back(freq, mtf[i]);
        cout << freq << "\t" << mtf[i] << endl;
    }

    if (FLAGS_quiet) return 0;

    Gnuplot gp;
    gp << "set xlabel \"Spatial Frequency [cyc/pixel]\"\n"
       << "set ylabel \"MTF\"\n";

    if (FLAGS_config_file == "") {
        gp << "unset key\n"
           << "plot " << gp.file1d(mtf_data) << " w l lw 3\n"
           << endl;
    } else {
        mats::SimulationConfig sim_config;
        mats::DetectorParameters detector_params;
        if (!mats::MatsInit(mats::ResolvePath(FLAGS_config_file),
                            &sim_config,
                            &detector_params)) {
            return 1;
        }

        // Initialize the simulation parameters.
        sim_config.set_array_size(512);

        int sim_index = mats::LookupSimulationId(sim_config, FLAGS_simulation_id);
        mats::Telescope telescope(sim_config, sim_index, detector_params);
        telescope.detector()->set_rows(512);
        telescope.detector()->set_cols(512);

        // Set up the spectral resolution of the simulation.
        vector<vector<double>> raw_weighting;
        mats_io::TextFileReader::Parse(
            sim_config.spectral_weighting_filename(),
            &raw_weighting);
        const vector<double>& wavelengths(raw_weighting[0]),
              spectral_weighting(raw_weighting[1]);

        double q = telescope.EffectiveQ(wavelengths, spectral_weighting);
        cerr << "F/#: " << telescope.FNumber() << endl;
        cerr << "Effective Q: " << q << endl;

        Mat_<complex<double>> theoretical_otf;
        telescope.EffectiveOtf(wavelengths, spectral_weighting, 0, 0,
                               &theoretical_otf);
        Mat theoretical_2d_mtf = magnitude(theoretical_otf);

        // Grab the radial profile of the OTF and convert to MTF.
        std::vector<double> theoretical_mtf;
        GetRadialProfile(FFTShift(theoretical_2d_mtf),
                         FLAGS_orientation * M_PI / 180,
                         &theoretical_mtf);

        vector<pair<double, double>> t_mtf_data;
        for (size_t i = 0; i < theoretical_mtf.size(); i++) {
            double pix_freq = i / (2. * (theoretical_mtf.size() - 1));  // [cyc/pixel]
            t_mtf_data.emplace_back(pix_freq, theoretical_mtf[i]);
        }

        gp << "plot " << gp.file1d(mtf_data) << " w l lw 3 t \"Measured\", "
           << gp.file1d(t_mtf_data) << " w l lw 3 t \"Theoretical\"\n"
           << endl;
    }

    return 0;
}
graphError::graphError(std::string _filename, int setGeneracion, int numberGeneration, int _nodos, int _noAnclas, double _max)
{
    nodos = _nodos;
    filename = _filename;
    noAnclas = _noAnclas;
    int margen = 3;

    if (_max>25) margen=6;

    //Cargar posiciones
    cargar_unknow();

    //Ejecucion de comandos en el shell
    std::string archivo2=filename+std::to_string(setGeneracion)+ext;
    std::string s="cp "+archivo2+" ContarLineas.sav";
    char *comando = strdup(s.c_str());
    system(comando);
    system("sed -i '1,2d' *.sav");

    for(int k=setGeneracion;k<numberGeneration+1;k+=setGeneracion){

        archivo=filename+std::to_string(k)+ext; //Define el nombre del archivo que guarda la poblacion
        filas = contador_filas(); //Se debe cambiar el nombre del archivo desde el cual se contaran las lineas
        cargar_poblacion(archivo);
        ordenar_matriz();
        mejor_individuo();

        //Graficar
        Gnuplot gp;
        std::vector<std::vector<std::pair<double, double> > > all_segments;
        std::vector<std::pair<double, double> > xy_pts_anclas;
        std::vector<std::pair<double, double> > xy_pts_unknow;
        //Grafica los segmentos (Error)
        int contador=0; //Contador para segmentos
        for(int j=0; j<nodos-noAnclas; j++) {
            std::vector<std::pair<double, double> > segment;
            for(int i=0; i<1; i++) {
                segment.push_back(std::make_pair(unknowX[contador], unknowY[contador]));
                segment.push_back(std::make_pair(individuoX[contador], individuoY[contador]));
                contador++;
            }
            all_segments.push_back(segment);
        }

        //Grafica los puntos
        for(int i=0; i<noAnclas; i++) xy_pts_anclas.push_back(std::make_pair(anclasX[i], anclasY[i]));

        for(int i=0; i<nodos-noAnclas; i++) xy_pts_unknow.push_back(std::make_pair(unknowX[i], unknowY[i]));

        gp << "set yrange [0:"<<_max+margen<<"]\n";
        //gp << "set autoscale\n";
        gp << "set title 'Generación "<<std::to_string(k)<<"' font 'Bookman,20'\n";
        gp << "set encoding iso_8859_1\n"; //Define los acentos para postscript
        gp << "set grid\n";
        gp << "set xlabel 'metros'\n";
        gp << "set ylabel 'metros'\n";
        gp << "set key font 'Helvetica,18'\n";//Modifica la leyenda
        gp << "set terminal postscript enhanced color font 'Helvetica,15'\n";//Cambia de terminal
        gp << "set output '| ps2pdf - Generacion_"<<std::to_string(k)<<".pdf'\n";//guarda en pdf
        gp << "set style line 1 lc rgb '#00DD00' pt 7 ps 1.5 \n";
        gp << "set style line 2 lc rgb 'blue' pt 6 ps 1.5 \n";
        gp << "plot '-' with line title 'Error', '-' with points ls 1 title 'Nodos Anclas',"
        << "'-' with points ls 2 title 'Posición Real'\n";
        //<< "'-' with vectors title 'pts_B'\n";
        gp.send2d(all_segments);
        gp.send1d(xy_pts_anclas);
        gp.send1d(xy_pts_unknow);
    }
}
Beispiel #3
0
int main(int argc, const char** argv)
{
    // don't forget to set LD_LIBRARY_PATH to directory where libOiVibrations.so libary resides
    // LD_LIBRARY_PATH=/home/john/sharedlibs
    // export LD_LIBRARY_PATH

    cout << "\n";
	if (argc < 2)
	{
		cout << "\n";
        cout << "Requires one or more command arguments.\n";
        cout << " Universal file name : FileName.uff \n";
		exit(1);
	}

    Oi::Proxy proxy;
    bool bSucceess = proxy.init(argc, argv);
    if (bSucceess)
        cout << "Initialization succeeded!\n";
    else
    {
        cout << "Initialization failed!\n";
        exit(1);
    }
    cout << "\n";

    int i(0);
    int nrows(0), ncols(0);
    const double* pnodes = proxy.getNodes(nrows, ncols); 
    
    vector<Point> nodesCollection;
    nodesCollection.reserve(nrows);

    Point pt1, pt2, pt3; 
    if (pnodes != NULL && ncols == 3)
    {
        for (i = 0; i < nrows; ++i)
        {
            Point pt; 
            pt.x = pnodes[0*nrows + i];
            pt.y = pnodes[1*nrows + i];             
            pt.z = pnodes[2*nrows + i];

            nodesCollection.push_back(pt);
        }
    }

    const unsigned int* plines = proxy.getLines(nrows, ncols);
    
    vector<Line> linesCollection;
    linesCollection.reserve(nrows);

    if (plines != NULL && ncols == 2 && !nodesCollection.empty())
    {
        unsigned int idx1(0), idx2(0);
        for (i = 0; i < nrows; ++i)
        {
            Line line;
            idx1 = plines[0*nrows + i];
            idx2 = plines[1*nrows + i];
            
            // number of node (point) starts from 1, where the indexes of vector<Point> 
            // starts from 0.
            line.point1 = nodesCollection[idx1-1]; 
            line.point2 = nodesCollection[idx2-1]; 

            linesCollection.push_back(line);
        }
    }
    
    const unsigned int* psurfaces = proxy.getSurfaces(nrows, ncols); 
    
    vector<Surface> surfacesCollection;
    surfacesCollection.reserve(nrows);

    if (psurfaces != NULL && ncols == 3)
    {
        unsigned int idx1(0), idx2(0), idx3(0);
        for (i = 0; i < nrows; ++i)
        {
            Surface surface;
            idx1 = psurfaces[0*nrows + i];
            idx2 = psurfaces[1*nrows + i];
            idx3 = psurfaces[2*nrows + i];
             
            surface.point1 = nodesCollection[idx1-1]; 
            surface.point2 = nodesCollection[idx2-1]; 
            surface.point3 = nodesCollection[idx3-1]; 

            surfacesCollection.push_back(surface);
        }
    }
    
/*
 *    std::cout << "------ Nodes ---------\n";
 *    std::ostream_iterator<Point> os_pit( std::cout, "\n" );
 *    std::copy(nodesCollection.begin(), nodesCollection.end(), os_pit);
 *    std::cout << std::endl;
 *
 *    std::cout << "------- Lines --------\n";
 *    std::ostream_iterator<Line> os_it( std::cout, "\n" );
 *    std::copy(linesCollection.begin(), linesCollection.end(), os_it); 
 *    std::cout << std::endl; 
 */
   
    vector<double> singularValues;

    int length(0);
    
    int numberOfMeasurements = proxy.getNumberOfMeasurements();    
    vector<const double*> pvalues(numberOfMeasurements);
    vector<const double*> pfreq(numberOfMeasurements);
    vector<Gnuplot*> plots;

    for (i = 0; i < numberOfMeasurements; ++i)
    {
        //pvalues[i] = proxy.getSingularValues(i, nrows, ncols);
        pvalues[i] = proxy.getSpectralDensity(i, nrows, ncols);

        pfreq[i] = proxy.getFrequencies(length);

        if (pvalues[i] != 0)
        {
            try 
            {
                std::stringstream ss;
                ss << "Measurement: " << (i+1); 
                Gnuplot* gplot = new Gnuplot(ss.str());
                gplot->set_title(ss.str());
                
                ss.str(""); ss.clear();
                
                gplot->set_grid();
                gplot->set_ylogscale();
                gplot->set_style("steps");
                for (int j = 0; j < ncols; ++j)
                {
                    ss << "singular values " << j; 
                    gplot->plot_xy(pfreq[i], pfreq[i]+length, pvalues[i]+j*nrows, pvalues[i]+(j+1)*nrows, ss.str() );                   
                    ss.str(""); ss.clear();
                }

                plots.push_back(gplot);
                
            }
            catch(GnuplotException& e)
            {
                std::cout << e.what() << "\n";
                std::for_each(plots.begin(), plots.end(), delete_plot);     
            }

        }

    }

    wait_for_key();
    std::for_each(plots.begin(), plots.end(), delete_plot); 
    

    /*
     *double natFreq = 33.8;
     *int nchannels(0), nsvd(0);
     *const complex<double>* pmodes = proxy.getModes(natFreq, 0, nchannels, nsvd);
     *
     *vector< complex<double> > modesList(nchannels*nsvd);
     *std::copy(pmodes, pmodes+nchannels*nsvd, &modesList[0]);
     */


	return 0;
}