void ExtractDescriptorHelper::extractFourierDescriptors(CvSeq* contour) { int x=contour->total; CvPoint *pt=new CvPoint[contour->total]; for(int i=0; i<contour->total; i++) { pt[i] = *(CvPoint*)cvGetSeqElem(contour, i); //cout<<"x:"<< pt[i].x<<" y:"<<pt[i].y<<endl; } //cout<<"Koordinatlar yazildi"<<endl; vector<ComplexNumber> contoursComplex; //Complex Number vectoru olusturduk int r1, i1; for(int j=0; j<contour->total; j++) // contour kordinatlarýný vectorde karmaþýk olarak tuttuk 1+2j gibi { ComplexNumber temp(pt[j].x, pt[j].y); //temp.print(); contoursComplex.push_back(temp); // copy constructor ve assignment operator implemente et çünkü push_back bunlarý kullanýyor. Ve destructor. } //contoursComplex ile chainCode u hesapla... //<Shifting the contour coordinates to center> ComplexNumber shiftC; ComplexNumber total(0,0); for(int x=0; x<contour->total; x++) { total=total.add(contoursComplex[x]); } ComplexNumber divisor(contour->total, 0); shiftC=total.div(divisor); vector<ComplexNumber> T; for(int x=0; x<contour->total; x++) { ComplexNumber tmp; tmp=contoursComplex[x].sub(shiftC); T.push_back(tmp); //T.at(x)=tmp; } //</Shifting the contour coordinates to center> //BURAYA KADAR TRANSITION OLAYI HALLOLDU, BÜTÜN NOKTALAR SANAL BÝR ORJÝNE GÖRE ALINDI. sortComplexVector(T);// açýlara gore sýralamayý yapýyor. Ufak tefek sapmalar var, elle yazmak yerine quicksort falan yap. //FFT KISMINI BURADA YAPIYORUM, MATEMATIKSEL ISLEMLERE GORE. DFT nin MATEMATIKSEL TANIMINA BAKILARAK BURADAKI ISLEM ANLASILABILIR ComplexNumber tempSum; vector<ComplexNumber> fourierDesc; ComplexNumber div(T.size(), 0); for(int u=0; u<T.size(); u++) { tempSum.setReal(0); tempSum.setImag(0); for(int k=0; k<T.size(); k++) { ComplexNumber tempComplex(cos((2*360*u*k*PI)/(180*T.size())), (-1)*(sin((2*360*u*k*PI)/(T.size()*180)))); tempSum=tempSum.add((T.at(k)).mult(tempComplex)); } fourierDesc.push_back((tempSum.div(div))); } //FFT KISMI BURADA BITIYOR // NORMALIZATION KISMI YAPILIYOR BURADA, SCALE ,TRANSITION VE ROTATION INVARIANT OLUYOR BOYLECE vector<ComplexNumber> tmpFourierDesc; for(int i=0; i<fourierDesc.size(); i++) tmpFourierDesc.push_back(fourierDesc.at(i)); ComplexNumber zero(0,0); //Translation Invariance: tmpFourierDesc.at(0)=zero; //si=abs(T(1)) sayisini tutuyorum //Scale Invariance: ComplexNumber myVarSiDiv(sqrt(pow(tmpFourierDesc.at(1).getReal(),2) + pow(tmpFourierDesc.at(1).getImag(),2)), 0); //T(i)=T(i)/si iþlemi yapýlýyor. for(int m=0; m<tmpFourierDesc.size(); m++) tmpFourierDesc.at(m).div(myVarSiDiv); //Son olarak tum elemanlarý double vectorunde tutuyorum. Boylece fourier desc lar cýktý. //vector<double> finalFourierDesc; //Burada da T=abs(T) yi fourier descriptor olarak tuttum, böylece ROTATION AND CHANGES IN STARTING POINT oldu. //myFourierDescriptors vectoru, class içinde tuttuðum en sonki fourier descriptorlarý içeren vectordur. for(int n=0; n<tmpFourierDesc.size(); n++) { myFourierDescriptors.push_back(sqrt(pow(tmpFourierDesc.at(n).getReal(),2) + pow(tmpFourierDesc.at(n).getImag(),2))); //cout<<n<<". element: "<<myFourierDescriptors.at(n)<<endl; } /* if(tmpFourierDesc.size()<fourierSize) { int substruct=fourierSize-tmpFourierDesc.size(); for(int j=0; j<substruct; j++) myFourierDescriptors.push_back(0); }*/ };
ComplexNumber ComplexNumber::add(const ComplexNumber& c1,const ComplexNumber& c2){ //std::cout << __PRETTY_FUNCTION__ << std::endl; ComplexNumber sum = c1; sum.add(c2); return sum; }