/*!
	Main MATLAB interface

	@param[in,out] nlhs		Number of left hand (output) matrices.
	@param[in,out] plhs		Pointers to left hand (output) matrices.
	@param[in] nrhs			Number of right hand (input) matrices.
	@param[in] prhs			Pointers to right hand (input) matrices.
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	//persistence and data variables
	Persistence1D p;
	std::vector<float> data; 

	//Variables to store persistence results
	std::vector<int> minIndices; 
	std::vector<int> maxIndices; 
	std::vector<TPairedExtrema> pairs;
	float gminValue;
	int gminIndex;
	
	//Assumption: Vector data lives in phrs[0]
	//Any deviation from this is not tolerated.

	//prhs - right hand - input - MATLAB standard naming convention
	//plhs - left hand - output
	if (!CheckInput(nlhs, plhs, nrhs, prhs)) return;

#ifdef _DEBUG	
    mexPrintf("Copying input data...\n");
#endif
	WriteInputToVector(prhs, data);
	
#ifdef _DEBUG
	WriteVectorToMexOutput(data);
#endif
	
#ifdef _DEBUG
    mexPrintf("Running Persistence1D...\n");
#endif
	p.RunPersistence(data);

#ifdef _DEBUG
    mexPrintf("Done.\n");
	mexPrintf("Getting results...\n");
#endif

    p.GetPairedExtrema(pairs, NO_FILTERING , MATLAB_INDEXING);
    gminIndex = p.GetGlobalMinimumIndex(MATLAB_INDEXING);
	gminValue = p.GetGlobalMinimumValue();
	p.GetExtremaIndices(minIndices, maxIndices, NO_FILTERING, MATLAB_INDEXING);

#ifdef _DEBUG
	mexPrintf("Done.\n");
	mexPrintf("Writing results to MATLAB...\n");
#endif
    	
	plhs[0] = VectorToMxSingleArray(minIndices);
	plhs[1] = VectorToMxSingleArray(maxIndices);
	plhs[2] = VectorToMxSingleArray(pairs);
	plhs[3] = ScalarToMxSingleArray(gminIndex);
	plhs[4] = ScalarToMxSingleArray(gminValue);

#ifdef _DEBUG	
	mexPrintf("Done.\n");
#endif
	return;
}
int main()
{
	//Create some data
	vector< float > data;
	data.push_back(2.0);   data.push_back(5.0);   data.push_back(7.0);
	data.push_back(-12.0); data.push_back(-13.0); data.push_back(-7.0);
	data.push_back(10.0);  data.push_back(18.0);  data.push_back(6.0);
	data.push_back(8.0);   data.push_back(7.0);   data.push_back(4.0);

	//Run persistence on data - this is the main call.
	Persistence1D p;
	p.RunPersistence(data);

	//Get all extrema with a persistence larger than 10.
	vector< TPairedExtrema > Extrema;
	p.GetPairedExtrema(Extrema, 10);

	//Print all found pairs - pairs are sorted ascending wrt. persistence.
	for(vector< TPairedExtrema >::iterator it = Extrema.begin(); it != Extrema.end(); it++)
	{
		cout << "Persistence: " << (*it).Persistence
			 << " minimum index: " << (*it).MinIndex
			 << " maximum index: " << (*it).MaxIndex
			 << std::endl;
	}

	//Also, print the global minimum.
	cout << "Global minimum index: " << p.GetGlobalMinimumIndex() 
		 << " Global minimum value: " << p.GetGlobalMinimumValue() << endl;

	/*
		Note that filtering and printing can also be done with one single function call:
		p.PrintResults(10);
	*/

	return 0;
}
Пример #3
0
void Predspracovanie::frekvencieLinii(Mat normaliz,Mat smer,int velkost_bloku){ //Odhad frekvencnej mapy
    int riadky = this->orientation.rows/velkost_bloku;
    int stlpce = this->orientation.cols/velkost_bloku;
    double uhol;
    Point2i stred;
    this->frekvencnaMat = Mat(orientation.rows,orientation.cols,CV_64F);
    this->sigma= Mat(orientation.rows,orientation.cols,CV_64F);
    this->zobrazFrekvencnu = Mat(orientation.rows,orientation.cols,CV_8UC1);
    Mat O_submat(255,750,CV_8UC1,0.0),M,crop,zrotovana; //pouziva sa iba ak chceme vykreslit sinusovy priebeh
    for(int x=0;x<riadky;x++){
        for(int y=0;y<stlpce;y++){
            stred = Point(y*velkost_bloku+velkost_bloku/2, x*velkost_bloku+velkost_bloku/2); //zistime stred bloku
            uhol = (smer.at<double>(x,y)/*+PI/2*/)*180/PI; //prevedieme uhol zo smerovej mapy do stupnov

            RotatedRect rRect(stred, Size(3*velkost_bloku,2*velkost_bloku), uhol); //vytvory sa rotovany obdlznik s vypocitanym stredom

            M = getRotationMatrix2D(rRect.center, uhol, 1.0); //obratenie aby nebol rotovany
                    // prerotuje obraz na 0 stupnov
                    warpAffine(normaliz, zrotovana, M, normaliz.size(), INTER_CUBIC);
                    // vystrihne iba ROI
                    getRectSubPix(zrotovana, rRect.size, rRect.center, crop);

                                vector<float> xSignature; //vektor Xsignatury

                                for (int k = 0; k < crop.cols; k++)
                                {
                                    int sum = 0;

                                    for (int d = 0; d < crop.rows; d++)
                                    {

                                        sum = sum + (int)crop.at<uchar>(d, k);

                                    }


                                    xSignature.push_back(sum/velkost_bloku); //vlozim hodnotu

                                }

                                vector<float> xSignature2; //xSignatura pre vypocet sigmy v Gaborovom filtri
                                for(int bla=0;bla<xSignature.size();bla++){

                                     xSignature2.push_back(abs(xSignature[bla]-255));
                                     }




                               Persistence1D p;                     //Vyhladanie lokalnych maxim v xSignature
                               p.RunPersistence(xSignature2);
                               vector< TPairedExtrema > Extrema;
                               p.GetPairedExtrema(Extrema,10);
                               vector<double> maxima;
                               vector<double> minima;
                               //ZISTIM SI MAXIMA
                               for(vector< TPairedExtrema >::iterator it = Extrema.begin(); it != Extrema.end(); it++)
                                  {
                                     maxima.push_back((*it).MaxIndex);
                                     minima.push_back((*it).MinIndex);

                                  }
                               sort(maxima.begin(),maxima.end());//SORT OD NAJNIZSIEHO PO NAJVYSSI

                               //AZ TU PREBIEHA VYPOCET
                               double sum = 0;
                               for(int i = 0; i<maxima.size();i++){//priemerny pocet pixlov medzi dvoma maximami v xSignature
                                    if(i != (maxima.size()-1)){
                                        sum += maxima[i+1] - maxima[i] - 1;
                                    }
                               }
                               double vysledok = sum/maxima.size(); //zmen na double
                               if(vysledok<0){
                                   vysledok = 0;
                               }
                                sum = 0;
                                sort(minima.begin(),minima.end()); //priemerny pocet pixlov medzi dvoma minimami v xSignature
                               for(int i = 0; i<minima.size();i++){
                                    if(i != (minima.size()-1)){
                                        sum += minima[i+1] - minima[i] - 1;
                                    }
                               }

                               double vysledok_min = sum/minima.size(); //zmen na double
                               if(vysledok_min<0){
                                   vysledok_min = 0;
                               }
                               for(int i = 0; i<velkost_bloku;i++){
                                   for(int j = 0; j<velkost_bloku;j++){
                                      this->sigma.at<double>(x*velkost_bloku+i,y*velkost_bloku+j) = vysledok_min; // hodnota urcena pre sigma v Gabore
                                      this->frekvencnaMat.at<double>(x*velkost_bloku+i,y*velkost_bloku+j) = 2*vysledok; //frekvencncia
                                     // this->zobrazFrekvencnu.at<uchar>(x*velkost_bloku+i,y*velkost_bloku+j) = (10*vysledok);//toto je pre zobrazenie v GUI
                                   }
                               }

        }
    }


}