傅立叶变换中的掩蔽频率

时间:2010-06-28 20:29:29

标签: opencv fft

我正在搞乱OpenCV,我正在尝试做一些相同的东西信号处理我在MatLab中完成的事情。我想掩盖一些频率,所以我构建了一个矩阵来完成这个。问题是,OpenCV中的步骤似乎比Matlab中的步骤要多得多 在Matlab中,它很简单:

F = fft2(image);
smoothF = F .* mask;        // multiply FT by mask
smooth = ifft2(smoothF);    // do inverse FT

但是我在OpenCV中遇到同样的问题。 DFT给我留下了一个2通道图像,所以我将图像分割,乘以掩模,将其合并,然后执行逆DFT。但是,在我的最终图像中,我得到了一个奇怪的结果。我很确定我错过了什么......

CvMat* maskImage(CvMat* im, int maskWidth, int maskHeight)
{
    CvMat* mask = cvCreateMat(im->rows, im->cols, CV_64FC1);
    cvZero(mask);

    int cx, cy;
    cx = mask->cols/2;
    cy = mask->rows/2;

    int left_x = cx - maskWidth;
    int right_x = cx + maskWidth;

    int top_y = cy + maskHeight;
    int bottom_y = cy - maskHeight;

    //create mask
    for(int i = bottom_y; i < top_y; i++)
    {   
        for(int j = left_x; j < right_x; j++)
        {
            cvmSet(mask,i,j,1.0f); // Set M(i,j)
        }
    }

    cvShiftDFT(mask, mask);
    IplImage* maskImage, stub;
    maskImage = cvGetImage(mask, &stub);
    cvNamedWindow("mask", 0);
    cvShowImage("mask", maskImage);

    CvMat* real = cvCreateMat(im->rows, im->cols, CV_64FC1);
    CvMat* imag = cvCreateMat(im->rows, im->cols, CV_64FC1);

    cvSplit(im, imag, real, NULL, NULL);
    cvMul(real, mask, real);
    cvMul(imag, mask, imag);
    cvMerge(real, imag, NULL, NULL, im);

    IplImage* maskedImage;
    maskedImage = cvGetImage(imag, &stub);
    cvNamedWindow("masked", 0);
    cvShowImage("masked", maskedImage);


    return im;
}

1 个答案:

答案 0 :(得分:1)

您是否以相反的顺序合并实部和虚部?