1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <iostream> #include <opencv2/opencv.hpp> #include <cmath>
using namespace cv; using namespace std;
void mydft(Mat img_input,Mat& out_img,Mat& transform_arr);
int main(){ Mat img,img_gray,img_out,img_transform; img = imread("/home/v/home.png"); if (img.empty()){ cout<<"can't open the image"<<endl; return -1; }
imshow("Img",img);
cvtColor(img,img_gray,COLOR_BGR2GRAY); imshow("Img_gray",img_gray);
mydft(img_gray,img_out,img_transform); imshow("img out",img_out); waitKey(0); return 0; }
void mydft(Mat img_input,Mat& out_img,Mat& transform_arr){
int m = getOptimalDFTSize(img_input.rows); int n = getOptimalDFTSize(img_input.cols); copyMakeBorder(img_input,img_input,0,m-img_input.rows,0,n-img_input.cols,BORDER_CONSTANT,Scalar::all(0));
Mat planes[] = {Mat_<float>(img_input),Mat::zeros(img_input.size(),CV_32F)};
merge(planes,2,transform_arr);
dft(transform_arr,transform_arr);
split(transform_arr,planes); magnitude(planes[0],planes[1],out_img);
out_img +=Scalar(1); log(out_img,out_img); normalize(out_img,out_img,0,1,NORM_MINMAX);
out_img = out_img(Rect(0,0,out_img.cols & -2,out_img.rows & -2));
int cx = out_img.cols/2; int cy = out_img.rows/2; Mat q0(out_img,Rect(0,0,cx,cy)); Mat q1(out_img,Rect(cx,0,cx,cy)); Mat q2(out_img,Rect(0,cy,cx,cy)); Mat q3(out_img,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); }
|