forked from oreillymedia/Learning-OpenCV-3_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_12-01.cpp
58 lines (40 loc) · 1.66 KB
/
example_12-01.cpp
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
// Using cv::dft() and cv::idft() to accelerate the computation of
// convolutions
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
if(argc != 2) {
cout << "Fourier Transform\nUsage: " <<argv[0] <<" <imagename>" << endl;
return -1;
}
cv::Mat A = cv::imread(argv[1],0);
if( A.empty() ) { cout << "Cannot load " << argv[1] << endl; return -1; }
cv::Size patchSize( 100, 100 );
cv::Point topleft( A.cols/2, A.rows/2 );
cv::Rect roi( topleft.x, topleft.y, patchSize.width, patchSize.height );
cv::Mat B = A( roi );
int dft_M = cv::getOptimalDFTSize( A.rows+B.rows-1 );
int dft_N = cv::getOptimalDFTSize( A.cols+B.cols-1 );
cv::Mat dft_A = cv::Mat::zeros( dft_M, dft_N, CV_32F );
cv::Mat dft_B = cv::Mat::zeros( dft_M, dft_N, CV_32F );
cv::Mat dft_A_part = dft_A( cv::Rect(0, 0, A.cols,A.rows) );
cv::Mat dft_B_part = dft_B( cv::Rect(0, 0, B.cols,B.rows) );
A.convertTo( dft_A_part, dft_A_part.type(), 1, -mean(A)[0] );
B.convertTo( dft_B_part, dft_B_part.type(), 1, -mean(B)[0] );
cv::dft( dft_A, dft_A, 0, A.rows );
cv::dft( dft_B, dft_B, 0, B.rows );
// set the last parameter to false to compute convolution instead of correlation
//
cv::mulSpectrums( dft_A, dft_B, dft_A, 0, true );
cv::idft( dft_A, dft_A, cv::DFT_SCALE, A.rows + B.rows - 1 );
cv::Mat corr = dft_A( cv::Rect(0, 0, A.cols + B.cols - 1, A.rows + B.rows - 1) );
cv::normalize( corr, corr, 0, 1, cv::NORM_MINMAX, corr.type() );
cv::pow( corr, 3., corr );
B ^= cv::Scalar::all( 255 );
cv::imshow( "Image", A );
cv::imshow( "ROI", B );
cv::imshow( "Correlation", corr );
cv::waitKey();
return 0;
}