本文介绍了如何使用傅里叶变换计算图像的方向角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <opencv2\opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <stdint.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
/// Global variables
Mat src, src_gray,canny_image,padded,good_f_t, threshold_image,lowerRect,imageROI,dst, detected_edges;
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 );
/*-----------------------fourier transform--------------------------*/
Mat padded; //expand input image to optimal size
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 magdst = planes[0];
magdst += Scalar::all(1); // switch to logarithmic scale
log(magdst, magdst);
// crop the spectrum, if it has an odd number of rows or columns
magdst = magdst(Rect(0, 0, magdst.cols & -2, magdst.rows & -2));
// rearrange the quadrants of Fourier image so that the origin is at the image center
int cx = magdst.cols/2;
int cy = magdst.rows/2;
Mat q0(magdst, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
Mat q1(magdst, Rect(cx, 0, cx, cy)); // Top-Right
Mat q2(magdst, Rect(0, cy, cx, cy)); // Bottom-Left
Mat q3(magdst, 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(magdst, magdst, 0, 1, CV_MINMAX); // Transform the matrix with float values into a
// viewable image form (float between values 0 and 1).
imshow("spectrum magnitude", magdst);
/*-----------------------end of fourier transform--------------------------*/
}
/** @function main */
int main( int argc, char** argv )
{
/// Load an image
src = imread("C:/Users/Saha/Desktop/DSCF0563.JPG");
resize(src, src, Size(), 0.2, 0.2, INTER_LANCZOS4);
if( !src.data )
{ return -1; }
/// 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);
/// Wait until user exit program by pressing a key
waitKey(0);
return 0;
}
推荐答案
这篇关于如何使用傅里叶变换计算图像的方向角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!