Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
C++
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;

/// Global variables

Mat src, src_gray;
Mat dst, detected_edges,padded;

int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";

/**
 * @function CannyThreshold
 * @brief Trackbar callback - Canny thresholds input with a ratio 1:3
 */
void CannyThreshold(int, void*)
{
  /// Reduce noise with a kernel 3x3
  blur( src_gray, detected_edges, Size(3,3) );

  /// Canny detector
  Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );

  /// Using Canny's output as a mask, we display our result
  dst = Scalar::all(0);

  src.copyTo( dst, detected_edges);
  imshow( window_name, dst );
  
  /// Wait until user exit program by pressing a key


    int m = getOptimalDFTSize( dst.rows );
    int n = getOptimalDFTSize( dst.cols ); // on the border add zero values
    copyMakeBorder(dst, padded, 0, m - dst.rows, 0, n - dst.cols, BORDER_CONSTANT, Scalar::all(0));

    Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
    Mat complexdst;
    merge(planes, 2, complexdst);         // Add to the expanded another plane with zeros

    dft(complexdst, complexdst);            // this way the result may fit in the source matrix

    // compute the magnitude and switch to logarithmic scale
    // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
    split(complexdst, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
    magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
    Mat dst = planes[0];

    dst += Scalar::all(1);                    // switch to logarithmic scale
    log(dst,dst);

    // crop the spectrum, if it has an odd number of rows or columns
    dst = dst(Rect(0, 0, dst.cols & -2, dst.rows & -2));

    // rearrange the quadrants of Fourier image  so that the origin is at the image center
    int cx = dst.cols/2;
    int cy = dst.rows/2;

    Mat q0(dst, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
    Mat q1(dst, Rect(cx, 0, cx, cy));  // Top-Right
    Mat q2(dst, Rect(0, cy, cx, cy));  // Bottom-Left
    Mat q3(dst, Rect(cx, cy, cx, cy)); // Bottom-Right

    Mat tmp;                           // swap quadrants (Top-Left with Bottom-Right)
    q0.copyTo(tmp);
    q3.copyTo(q0);
    tmp.copyTo(q3);

    q1.copyTo(tmp);                    // swap quadrant (Top-Right with Bottom-Left)
    q2.copyTo(q1);
    tmp.copyTo(q2);

    normalize(dst, dst, 0, 1, CV_MINMAX); // Transform the matrix with float values into a
                                            // viewable image form (float between values 0 and 1).
 imshow("spectrum magnitude", dst);
 }


/** @function main */
int main( int argc, char** argv )
	
{
  /// Load an image
  src = imread(".JPG");
	resize(src, src, Size(), 0.2, 0.2, INTER_LANCZOS4);

 
  /// Create a matrix of the same type and size as src (for dst)
  dst.create( src.size(), src.type() );

  /// Convert the image to grayscale
  cvtColor( src, src_gray, CV_BGR2GRAY );

  /// Create a window
  namedWindow( window_name, CV_WINDOW_AUTOSIZE );

  /// Create a Trackbar for user to enter threshold
  createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );

  /// Show the image
  CannyThreshold(0, 0);

 /*............... end of Fourier transfrom .......*/
  waitKey(0);

  return 0;
  }
Posted

1 solution

"The Program Not Run" is not a helpful error description - it's a bit like your car breaking down in the middle of nowhere and you calling the garage: "My car broke. It's a blue ford fiesta" and putting the phone down.
That tells the mechanics nothing useful - like where you are or what the problem might be. As a result, you would be sitting there waiting for a very long time before they turned up with the parts...

So what you need to do it start by using the debugger and finding out what it does do, and what it doesn't. When you know that, it is likely that the problem will be obvious to you.
Put a breakpoint at the top of the Main function, and step through your program: watch what it is doing, and work out what you think it should be doing. At some point the two will differ and that should hep you to focus on that area to find out why.

But we can't do that for you!
 
Share this answer
 
Comments
[no name] 15-Apr-14 18:57pm    
Thanks for your replay.I have already use debugger, when it's come this line Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)} shows Unhandled exception at 0x000007fefcfaaa7d in fourier transform.exe: Microsoft C++ exception: cv::Exception at memory location 0x0027e800..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900