[Opencv learning] DFT Transformation

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

# Include "CV. H "# include" highgui. H "# include" cxcore. H "Void rotate (cvarr * src_arr, cvarr * rotate) {cvmat * TMP; cvmat q1stub, q2stub; cvmat rotate, q4stub; cvmat d1stub, d2stub; cvd3stub, d4stub; cvmat * Q1, * Q2, * Q3, * Q4; cvmat * D1, * D2, * D3, * D4; cvsize size = cvgetsize (src_arr ); cvsize dst_size = cvgetsize (dst_arr); int CX, Cy; If (dst_size.width! = Size. width) | (dst_size.height! = Size. height) {cverror (cv_stsunmatchedsizes, "cvshiftdft", "Source and Destination arrays must have the same sizes", _ file __,__ line __);} if (src_arr = dst_arr) {TMP = cvcreatemat (size. height/2, size. width/2, cvgetelemtype (src_arr);} Cx = size. width/2; // The Origin of the retrieved image. Cy = size. height/2; Q1 = cvgetsubrect (src_arr, & q1stub, cvrect (, CX, CY); // retrieve the First quadrant of the image, the Q1 Pointer Points to it q2 = cvgetsubrect (src_arr, & q2stub, cvrect (CX, 0, CX, CY); // the second quadrant of the image, directed by the Q2 pointer to it Q3 = cvgetsubrect (src_arr, & q3stub, cvrect (CX, Cy, CX, CY )); // retrieve the third quadrant of the image. The Q3 Pointer Points to it Q4 = cvgetsubrect (src_arr, & q4stub, cvrect (0, Cy, CX, CY )); // retrieve the Fourth quadrant of the image. The Q4 Pointer Points to it d1 = cvgetsubrect (src_arr, & d1stub, cvrect (, CX, CY); d2 = cvgetsubrect (src_arr, & d2stub, cvrect (CX, 0, CX, CY); D3 = cvgetsubrect (src_arr, & d3stub, cvrect (CY, Cy, CX, CY )); d4 = cvgetsubrect (src_arr, & d4stub, cvrect (0, Cy, CX, CY); If (src_arr! = Dst_arr) {If (! Cv_are_types_eq (Q1, D1) {cverror (cv_stsunmatchedsizes, "cvshiftdft", "Source and Destination arrays must have the same sizes", _ file __,__ line __);} // adjust the four quadrants of the Fourier Transform Image with the image center as the origin, // that is, the first and third quadrants are exchanged, and the second and fourth quadrants are exchanged with cvcopy (Q3, D1, 0 ); cvcopy (Q4, D2, 0); cvcopy (Q1, D3, 0); cvcopy (Q2, D4, 0 );} else {// if the source matrix is the same as the target matrix, cvcopy (Q3, TMP, 0), cvcopy (Q1, Q3, 0), and cvcopy (TMP, q1, 0); cvcopy (Q4, TMP, 0); cvcopy (Q2, Q4, 0); cvcopy (TMP, Q2, 0) ;}} int main (INT argc, char ** argv) {const char * filename = (argc> = 2? Argv [1]: "lena.jpg"); iplimage * im; iplimage * realinput, * imaginaryinput, * complexinput; iplimage * image_re, * image_im; int dft_m, dft_n; cvmat * callback; cvmat TMP; double M, M; Im = cvloadimage (filename, cv_load_image_grayscale); // load the image if (! Im) {return-1;} // allocate space realinput = cvcreateimage (cvgetsize (IM), ipl_depth_64f, 1); // single channel imaginaryinput = cvcreateimage (cvgetsize (IM), ipl_depth_64f, 1); // single channel complexinput = cvcreateimage (cvgetsize (IM), ipl_depth_64f, 2); // dual channel cvscale (IM, realinput, 1.0, 0.0 ); // # define cvscale cvconvertscale => readinput = Im cvzero (imaginaryinput); // clear the content of the image cvmerge (realinput, imaginaryinput, null, null, complexinput ); // mix the two images as two channels of complexinput/* obtain the optimal DFT size */dft_m = cvgetoptimaldftsize (im-> height-1 ); dft_n = direction (im-> width-1); dft_a = cvcreatemat (dft_m, dft_n, cv_64fc2); image_re = cvcreateimage (cvsize (dft_n, dft_m), direction, 1 ); // real-part image_im = cvcreateimage (cvsize (dft_n, dft_m), ipl_depth_64f, 1); // virtual cvgetsubrect (dft_a, & TMP, cvrect (, im-> width, im-> height); cvcopy (complexinput, & TMP, null); If (dft_a-> Cols> im-> width) // If the obtained optimal DFT size is larger than the source image in width, {cvgetsubrect (dft_a, & TMP, cvrect (im-> width, 0, dft_a-> Cols-im-> width, im-> height); cvzero (& TMP);} cvdft (dft_a, dft_a, cv_dxt_forward, complexinput-> height ); cvnamedwindow ("win", 0); cvnamedwindow ("magnhistory", 0); cvshowimage ("win", Im); // splits the real and virtual cvsplit (dft_a, image_re, image_im, 2.0); // calculate the power spectrum mag = SQRT (Re ^ 2 + Im ^ 2) cvpow (image_re, image_re, 2.0); cvpow (image_im, image_im ); cvadd (image_re, image_im, image_re, null); // image_re <= image_re + image_im cvpow (image_re, image_re, 0.5); // calculate log (1 + mag) cvadds (image_re, cvscalarall (1.0), image_re, null); cvlog (image_re, image_re); cvshiftdft (image_re, image_re); cvminmaxloc (image_re, & M, & M, null, null, null); cvscale (image_re, image_re, 1.0/(m-m), 1.0 * (-M)/(m-m); cvshowimage ("magnbench ", image_re); cvwaitkey (-1); Return 0 ;}

 

Author: gnuhpc

Source: http://www.cnblogs.com/gnuhpc/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.