c# - EMGU/OpenCV FFT of image not yielding expected results -
i'm trying visualize fft of image emgu. here's image i'm processing:
here's expected result:
here's get:
here's code:
image<gray, float> image = new image<gray, float>(@"c:\users\me\desktop\sample3.jpg"); intptr compleximage = cvinvoke.cvcreateimage(image.size, emgu.cv.cvenum.ipl_depth.ipl_depth_32f, 2); cvinvoke.cvsetzero(compleximage); cvinvoke.cvsetimagecoi(compleximage, 1); cvinvoke.cvcopy(image, compleximage, intptr.zero); cvinvoke.cvsetimagecoi(compleximage, 0); matrix<float> dft = new matrix<float>(image.rows, image.cols, 2); cvinvoke.cvdft(compleximage, dft, emgu.cv.cvenum.cv_dxt.cv_dxt_forward, 0); matrix<float> outreal = new matrix<float>(image.size); matrix<float> outim = new matrix<float>(image.size); cvinvoke.cvsplit(dft, outreal, outim, intptr.zero, intptr.zero); image<gray, float> fftimage = new image<gray, float>(outreal.size); cvinvoke.cvcopy(outreal, fftimage, intptr.zero); picturebox1.image = image.tobitmap(); picturebox2.image = fftimage.log().tobitmap();
what mistake making here?
update: per roger rowland's suggestion here's updated code. result looks better i'm not 100% sure it's correct. here's result:
image<gray, float> image = new image<gray, float>(@"c:\users\yytov\desktop\sample3.jpg"); intptr compleximage = cvinvoke.cvcreateimage(image.size, emgu.cv.cvenum.ipl_depth.ipl_depth_32f, 2); cvinvoke.cvsetzero(compleximage); // initialize elements 0 cvinvoke.cvsetimagecoi(compleximage, 1); cvinvoke.cvcopy(image, compleximage, intptr.zero); cvinvoke.cvsetimagecoi(compleximage, 0); matrix<float> dft = new matrix<float>(image.rows, image.cols, 2); cvinvoke.cvdft(compleximage, dft, emgu.cv.cvenum.cv_dxt.cv_dxt_forward, 0); //the real part of fourier transform matrix<float> outreal = new matrix<float>(image.size); //the imaginary part of fourier transform matrix<float> outim = new matrix<float>(image.size); cvinvoke.cvsplit(dft, outreal, outim, intptr.zero, intptr.zero); cvinvoke.cvpow(outreal, outreal, 2.0); cvinvoke.cvpow(outim, outim, 2.0); cvinvoke.cvadd(outreal, outim, outreal, intptr.zero); cvinvoke.cvpow(outreal, outreal, 0.5); cvinvoke.cvadds(outreal, new mcvscalar(1.0), outreal, intptr.zero); // 1 + mag cvinvoke.cvlog(outreal, outreal); // log(1 + mag) // swap quadrants int cx = outreal.cols / 2; int cy = outreal.rows / 2; matrix<float> q0 = outreal.getsubrect(new rectangle(0, 0, cx, cy)); matrix<float> q1 = outreal.getsubrect(new rectangle(cx, 0, cx, cy)); matrix<float> q2 = outreal.getsubrect(new rectangle(0, cy, cx, cy)); matrix<float> q3 = outreal.getsubrect(new rectangle(cx, cy, cx, cy)); matrix<float> tmp = new matrix<float>(q0.size); q0.copyto(tmp); q3.copyto(q0); tmp.copyto(q3); q1.copyto(tmp); q2.copyto(q1); tmp.copyto(q2); cvinvoke.cvnormalize(outreal, outreal, 0.0, 255.0, emgu.cv.cvenum.norm_type.cv_minmax, intptr.zero); image<gray, float> fftimage = new image<gray, float>(outreal.size); cvinvoke.cvcopy(outreal, fftimage, intptr.zero); picturebox1.image = image.tobitmap(); picturebox2.image = fftimage.tobitmap();
i cannot comment on magnitude/intensity of resulting image, can give tip spatial distribution of points in image.
opencv doesn't rearrange quadrants put origin [0,0] center of image. have rearrange quadrants manually.
look @ step 6 @ following page: http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html
it's official doc opencv, it's in c++, principle holds.
Comments
Post a Comment