#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;
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";
void CannyThreshold(int, void*)
{
blur( src_gray, detected_edges, Size(3,3) );
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
dst = Scalar::all(0);
src.copyTo( dst, detected_edges);
imshow( window_name, dst );
int m = getOptimalDFTSize( dst.rows );
int n = getOptimalDFTSize( dst.cols ); 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);
dft(complexdst, complexdst);
split(complexdst, planes); magnitude(planes[0], planes[1], planes[0]); Mat dst = planes[0];
dst += Scalar::all(1); log(dst,dst);
dst = dst(Rect(0, 0, dst.cols & -2, dst.rows & -2));
int cx = dst.cols/2;
int cy = dst.rows/2;
Mat q0(dst, Rect(0, 0, cx, cy)); Mat q1(dst, Rect(cx, 0, cx, cy)); Mat q2(dst, Rect(0, cy, cx, cy)); Mat q3(dst, Rect(cx, cy, cx, cy));
Mat tmp; q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp); q2.copyTo(q1);
tmp.copyTo(q2);
normalize(dst, dst, 0, 1, CV_MINMAX); imshow("spectrum magnitude", dst);
}
int main( int argc, char** argv )
{
src = imread(".JPG");
resize(src, src, Size(), 0.2, 0.2, INTER_LANCZOS4);
dst.create( src.size(), src.type() );
cvtColor( src, src_gray, CV_BGR2GRAY );
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
CannyThreshold(0, 0);
waitKey(0);
return 0;
}