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
|
#include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace cv; using namespace std;
void convolution(cv::Mat src, cv::Mat kernel, cv::Mat& dst) { dst.create(abs(src.rows - kernel.rows)+1, abs(src.cols - kernel.cols)+1, src.type()); cv::Size dftSize; dftSize.width = getOptimalDFTSize(src.cols + kernel.cols - 1); dftSize.height = getOptimalDFTSize(src.rows + kernel.rows - 1); cv::Mat tempA(dftSize, src.type(), Scalar::all(0)); cv::Mat tempB(dftSize, kernel.type(), Scalar::all(0)); cv::Mat roiA(tempA, Rect(0,0,src.cols,src.rows)); src.copyTo(roiA); cv::Mat roiB(tempB, Rect(0,0,kernel.cols,kernel.rows)); kernel.copyTo(roiB); dft(tempA, tempA, 0, src.rows); dft(tempB, tempB, 0, kernel.rows); mulSpectrums(tempA, tempB, tempA, DFT_COMPLEX_OUTPUT); dft(tempA, tempA, DFT_INVERSE + DFT_SCALE, dst.rows); tempA(Rect(0, 0, dst.cols,dst.rows)).copyTo(dst); } int main() { cv::Mat srcImage = cv::imread("..\\images\\flower3.jpg", 0); if( srcImage.empty()) return -1; cv::Mat kernel = (cv::Mat_<float>(3,3) << 1, 1, 1, 1, 1, 1, 1, 1, 1); imshow("srcImage", srcImage); srcImage.convertTo(srcImage, CV_32F); cv::Mat resultImage; convolution(srcImage, kernel, resultImage); normalize(resultImage, resultImage, 0, 1, CV_MMX); cv::imshow("resultImage", resultImage); cv::waitKey(0); return 0; }
|