tags:

views:

135

answers:

1

I'm messing around with OpenCV, and am trying to do some of the same stuff signal processing stuff I've done in MatLab. I'm looking to mask out some frequencies, so I have constructed a matrix which will do this. The problem is that there seem to be a few more steps in OpenCV than in Matlab to accomplish this.
In Matlab, it's simple enough:

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

But I'm having trouble doing the same in OpenCV. The DFT leaves me with a 2 channel image, so I've split the image, multiplied by the mask, merged it back, and then perform the inverse DFT. However, I got a weird result in my final image. I'm pretty sure I'm missing something...

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;
}
A: 

Any reason you are merging the real and imaginary components in the reverse order?

Sanjay Manohar
No, sorry that's a typo
Simonw