Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 维纳滤波器_C++_Opencv_Filter - Fatal编程技术网

C++ 维纳滤波器

C++ 维纳滤波器,c++,opencv,filter,C++,Opencv,Filter,基本上,我试图在灰度图像上实现一个非常基本的维纳滤波器版本,使用一个简化的维纳方程:(1/(SNR))*DFT(图像),然后取整个图像的IDFT。我的问题是,应该过滤的输出图像与输入图像完全相同,因此像素值似乎根本没有变化。谁能告诉我哪里出了问题?以下是我当前使用的代码: #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv/cv.hpp" #include "

基本上,我试图在灰度图像上实现一个非常基本的维纳滤波器版本,使用一个简化的维纳方程:(1/(SNR))*DFT(图像),然后取整个图像的IDFT。我的问题是,应该过滤的输出图像与输入图像完全相同,因此像素值似乎根本没有变化。谁能告诉我哪里出了问题?以下是我当前使用的代码:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.hpp"
#include "opencv/cxcore.hpp"
#include <iostream>


using namespace cv;
using namespace std;

void updateMag(Mat complex);
Mat updateResult(Mat complex);

Mat computeDFT(Mat image);
Mat DFT2(Mat I);
void shift(Mat magI);

int kernel_size = 0;

int main( int argc, char** argv )
{

    Mat  result;
    String file;
    file = " << SAMPLE FILE >>";

    Mat image = imread("/Users/John/Desktop/house.png", CV_LOAD_IMAGE_GRAYSCALE);
    namedWindow( "Orginal window", CV_WINDOW_AUTOSIZE  );// Create a window for display.
    imshow( "Orginal window", image );                   // Show our image inside it.

    float x = 1/0.001;
    Mat complex = computeDFT(image); // DFT of image

    updateMag(complex);         // compute magnitude of complex, switch to logarithmic scale and display...

    Mat fourierImage(complex.size(), complex.type());
    fourierImage = cv::Scalar::all(x);


    //cout<< "Fourier = " << endl << fourierImage << endl;
    //Mat complexFourier = computeDFT(fourierImage);
    //cout << "1" << endl << complexFourier.type() << endl << complexFourier.type() << endl;

    //complex = complex.mul(fourierImage);
    //mulSpectrums(complex, fourierImage, complex, DFT_ROWS);

    complex = complex.mul(x);
    result = updateResult(complex);      // do inverse transform and display the result image

    waitKey(0);

    return 0;
}



Mat updateResult(Mat complex)
{
    Mat work;
    //work.convertTo(work, CV_32F);
    idft(complex, work);
    //dft(complex, work, DFT_INVERSE + DFT_SCALE);
    Mat planes[] = {Mat::zeros(complex.size(), complex.type()), Mat::zeros(complex.size(), complex.type())};
    split(work, planes);                // planes[0] = Re(DFT(I)), planes[1] = Im(DFT(I))

    magnitude(planes[0], planes[1], work);    // === sqrt(Re(DFT(I))^2 + Im(DFT(I))^2)
    normalize(work, work, 1, 0, NORM_MINMAX);
    imshow("result", work);
    return work;
}

void updateMag(Mat complex )
{

    Mat magI;
    Mat planes[] = {Mat::zeros(complex.size(), CV_32F), Mat::zeros(complex.size(), CV_32F)};
    split(complex, planes);                // planes[0] = Re(DFT(I)), planes[1] = Im(DFT(I))

    magnitude(planes[0], planes[1], magI);    // sqrt(Re(DFT(I))^2 + Im(DFT(I))^2)

    // switch to logarithmic scale: log(1 + magnitude)
    magI += Scalar::all(1);
    log(magI, magI);

    shift(magI);
    normalize(magI, magI, 1, 0, NORM_INF); // Transform the matrix with float values into a
    // viewable image form (float between values 0 and 1).
    imshow("spectrum", magI);
}


Mat computeDFT(Mat image) {
    Mat padded;                            //expand input image to optimal size
    int m = getOptimalDFTSize( image.rows );
    int n = getOptimalDFTSize( image.cols ); // on the border add zero values
    copyMakeBorder(image, padded, 0, m - image.rows, 0, n - image.cols, BORDER_CONSTANT, Scalar::all(0));
    Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
    Mat complex;
    merge(planes, 2, complex);         // Add to the expanded another plane with zeros
    dft(complex, complex, DFT_COMPLEX_OUTPUT);  // furier transform
    return complex;
}



void shift(Mat magI) {

    // crop if it has an odd number of rows or columns
    magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));

    int cx = magI.cols/2;
    int cy = magI.rows/2;

    Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
    Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
    Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
    Mat q3(magI, 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);
}

Mat DFT2(Mat I)
{
    Mat padded;                            //expand input image to optimal size
    int m = getOptimalDFTSize( I.rows );
    int n = getOptimalDFTSize( I.cols ); // on the border add zero values
    copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));

    Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
    Mat complexI;
    merge(planes, 2, complexI);         // Add to the expanded another plane with zeros

    dft(complexI, complexI);            // 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(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
    magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
    Mat magI = planes[0];

    magI += Scalar::all(1);                    // switch to logarithmic scale
    log(magI, magI);

    // crop the spectrum, if it has an odd number of rows or columns
    magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));

    // rearrange the quadrants of Fourier image  so that the origin is at the image center
    int cx = magI.cols/2;
    int cy = magI.rows/2;

    Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
    Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
    Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
    Mat q3(magI, 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(magI, magI, 0, 1, CV_MINMAX); // Transform the matrix with float values into a
    // viewable image form (float between values 0 and 1).


    return complexI;
}
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv/cv.hpp”
#包括“opencv/cxcore.hpp”
#包括
使用名称空间cv;
使用名称空间std;
无效更新标记(Mat复合体);
Mat更新结果(Mat复合体);
Mat计算FT(Mat图像);
材料DFT2(材料I);
无效移位(Mat-magI);
int kernel_size=0;
int main(int argc,字符**argv)
{
Mat结果;
字符串文件;
file=“>”;
Mat image=imread(“/Users/John/Desktop/house.png”,CV\u LOAD\u image\u GRAYSCALE);
namedWindow(“原始窗口”,CV_window_AUTOSIZE);//创建一个显示窗口。
imshow(“原始窗口”,图像);//在其中显示我们的图像。
浮点数x=1/0.001;
Mat complex=computeDFT(图像);//图像的DFT
updateMag(复数);//计算复数的大小,切换到对数刻度并显示。。。
Mat fourierImage(complex.size(),complex.type());
fourierImage=cv::Scalar::all(x);

//如果您对python感兴趣,@JeruLuke感谢您的评论,但是我没有选择,因为这是一篇论文,我必须使用C++hmm…对我没有希望…但我会尽我所能。我对这个解决方案感兴趣too@JeruLuke我一直在四处寻找,我发现这篇文章非常有用-这是Thnx让我知道:)如果你对python感兴趣,@JeruLuke感谢你的评论,但是我没有选择,因为这是一篇论文,我必须使用C++hmm…对我没有希望…但我会尽我所能。我对这个解决方案感兴趣too@JeruLuke我一直在四处寻找,我发现这篇文章非常有用-H下面是让我知道的答案:)