Pregunta

im using the following code to calculate convolution of an image with a specified kernel(in my case gaussian). Everytime I get a different result and the result image is not even close to the one i obtained by convolution in the Spatial domain. First I thought the problem is with the datatype of the images. I changed them to 32 and 64 but still the same results. Can anyone tell me what could be wrong?

http://opencv.willowgarage.com/documentation/cpp/core_operations_on_arrays.html#dft this function above is giving me a black image. I have input in GRAYSCALE.

void convol_fft(const Mat& A,const vector<vector<float>>& kernel2d,Mat& result)
{

    Mat B = Mat(3,3,CV_64F);
    for (int row = 0; row < kernel2d.size(); row++)
        for (int col = 0; col < kernel2d[row].size(); col++){
            B.at<uchar>(row,col) = (uchar)kernel2d[row][col];
        }

    int dft_M = getOptimalDFTSize( A.rows+B.rows-1 );
    int dft_N = getOptimalDFTSize( A.cols+B.cols-1 );
    Mat dft_A = Mat::zeros(dft_M, dft_N, CV_64F);
    Mat dft_B = Mat::zeros(dft_M, dft_N, CV_64F);

    Mat dft_A_part = dft_A(Rect(0, 0, A.cols,A.rows));
    A.convertTo(dft_A_part, dft_A_part.type(), 1, -mean(A)[0]);
    Mat dft_B_part = dft_B(Rect(0, 0, B.cols,B.rows));
    B.convertTo(dft_B_part, dft_B_part.type(), 1, -mean(B)[0]);

    dft(dft_A, dft_A, 0, A.rows);
    dft(dft_B, dft_B, 0, B.rows);

    // set the last parameter to false to compute convolution instead of correlation
    mulSpectrums( dft_A, dft_B, dft_A, 0, false );
    idft(dft_A, dft_A, DFT_SCALE, A.rows + B.rows - 1 );

    result = dft_A(Rect(0, 0, A.cols + B.cols - 1, A.rows + B.rows - 1));
    normalize(result, result, 0, 1, NORM_MINMAX, result.type());
    pow(result, 3., result);

  //  B ^= Scalar::all(255);

}
¿Fue útil?

Solución 2

I am not sure about OpenCV...but this looks suspicious.

for (int row = 0; row < kernel2d.size(); row++)
    for (int col = 0; col < kernel2d[row].size(); col++){
        B.at<uchar>(row,col) = (uchar)kernel2d[row][col];
 }

If you are filling up the B kernel then the row should be kernel2d[col].size(). It looks like you are overrunning the B kernel. What is value of kernel2d.size() ?

Why not just load the values directly? Saving all the function calls.

For gaussian kernel it should look something like {1,2,1,2,3,2,1,2,1}.

Otros consejos

The following code based on openCV's phaseCorrelateRes() will do correlation in 2 dimensions.

static void fftShift(InputOutputArray _out)
{
    Mat out = _out.getMat();

    if(out.rows == 1 && out.cols == 1)
    {
        // trivially shifted.
        return;
    }

    vector<Mat> planes;
    split(out, planes);

    int xMid = out.cols >> 1;
    int yMid = out.rows >> 1;

    bool is_1d = xMid == 0 || yMid == 0;

    if(is_1d)
    {
        xMid = xMid + yMid;

        for(size_t i = 0; i < planes.size(); i++)
        {
            Mat tmp;
            Mat half0(planes[i], Rect(0, 0, xMid, 1));
            Mat half1(planes[i], Rect(xMid, 0, xMid, 1));

            half0.copyTo(tmp);
            half1.copyTo(half0);
            tmp.copyTo(half1);
        }
    }
    else
    {
        for(size_t i = 0; i < planes.size(); i++)
        {
            // perform quadrant swaps...
            Mat tmp;
            Mat q0(planes[i], Rect(0,    0,    xMid, yMid));
            Mat q1(planes[i], Rect(xMid, 0,    xMid, yMid));
            Mat q2(planes[i], Rect(0,    yMid, xMid, yMid));
            Mat q3(planes[i], Rect(xMid, yMid, xMid, yMid));

            q0.copyTo(tmp);
            q3.copyTo(q0);
            tmp.copyTo(q3);

            q1.copyTo(tmp);
            q2.copyTo(q1);
            tmp.copyTo(q2);
        }
    }

    merge(planes, out);
}

void Correlate2d(
    const cv::Mat& src1, 
    const cv::Mat& src2, 
    cv::Mat& dst,
    double* response)
{

    CV_Assert( src1.type() == src2.type());
    CV_Assert( src1.type() == CV_32FC1 || src1.type() == CV_64FC1 );
    CV_Assert( src1.size == src2.size);

    int M = getOptimalDFTSize(src1.rows);
    int N = getOptimalDFTSize(src1.cols);

    Mat padded1, padded2, paddedWin;

    if(M != src1.rows || N != src1.cols)
    {
        copyMakeBorder(src1, padded1, 0, M - src1.rows, 0, N - src1.cols, BORDER_CONSTANT, Scalar::all(0));
        copyMakeBorder(src2, padded2, 0, M - src2.rows, 0, N - src2.cols, BORDER_CONSTANT, Scalar::all(0));
    }
    else
    {
        padded1 = src1;
        padded2 = src2;
    }

    Mat FFT1, FFT2, P, Pm, C;

    // correlation equation
    // Reference: http://en.wikipedia.org/wiki/Phase_correlation
    dft(padded1, FFT1, DFT_REAL_OUTPUT);
    dft(padded2, FFT2, DFT_REAL_OUTPUT);

    mulSpectrums(FFT1, FFT2, dst, 0, true);
    idft(dst, dst, DFT_SCALE); // gives us the correlation result...
    fftShift(dst); // shift the energy to the center of the frame.

    // locate the highest peak
    Point peakLoc;
    minMaxLoc(dst, NULL, NULL, NULL, &peakLoc);

    // max response is scaled
    if( response )
        *response = dst.at<float>(peakLoc);
}

You can find the code in \opencv\sources\modules\imgproc\src\phasecorr.cpp

In order to change the code to convolution simply change this line:

mulSpectrums(FFT1, FFT2, dst, 0, true);

to

mulSpectrums(FFT1, FFT2, dst, 0, false);

This is equivalent to doing in matlab:

dst = fftshift(ifft2(fft2(src1).*conj(fft2(src2))))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top