Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I got wrong cv::cuda::Convoltion::convolution results.

I separated the color channels into BGR and convolved each color channels with the self-creation kernel in cv::cuda::Convolution and got the undesired results.

Below is my code for the kernel creation.

Matrix createGk(float sig, int row, int col)
{


    float r = 0.0;
    float sum = 0.0;
    int f_size, half; // h == kernel half size, f_size == kernel size

    f_size = (int)ceil(sig * 6) + 1;
    vector< vector<float> > gk;
    half = f_size / 2;

    for (int i = -half; i <= half; i++)
    {
        vector<float> row;
        for (int j = -half; j <= half; j++)
        {
            float value = 0.0;
            r = (i*i + j * j);
            //  gk[i + half][j + half] = exp((-(r * r)) / sig * sig);

            value = exp((-(r)) / sig * sig);
            //cout << value << endl;
            row.push_back(value);
            sum += value;
        }
        gk.push_back(row);
    }

    //For Normalization
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            gk[i][j] /= sum;
        }
    }

    return gk;
}

And this is the code for convolution in blue channel part.

     Matrix gk1, gk2, gk3;
    ///*
    gk1 = createGk(15);
    gk2 = createGk(80);
    gk3 = createGk(250);

    Mat gk1Mat((int)gk1.size(), (int)gk1[0].size(), CV_32F); // (int) ma pr yin possible data lost from type conversion so tr myo tway paw nay tl
    for (int i = 0; i < gk1.size(); i++)
    {
        for (int j = 0; j < gk1[i].size(); j++)
        {
            gk1Mat.at<float>(i, j) = gk1[i][j];
        }
    }

    Mat gk2Mat((int)gk2.size(), (int)gk2[0].size(), CV_32F); // (int) ma pr yin possible data lost from type conversion so tr myo tway paw nay tl
    for (int i = 0; i < gk2.size(); i++)
    {
        for (int j = 0; j < gk2[i].size(); j++)
        {
            gk2Mat.at<float>(i, j) = gk2[i][j];
        }
    }

    Mat gk3Mat((int)gk3.size(), (int)gk3[0].size(), CV_32F); // (int) ma pr yin possible data lost from type conversion so tr myo tway paw nay tl
    for (int i = 0; i < gk3.size(); i++)
    {
        for (int j = 0; j < gk3[i].size(); j++)
        {
            gk3Mat.at<float>(i, j) = gk3[i][j];
        }
    }


    cv::cuda::GpuMat d_gk1, d_gk2, d_gk3;
    d_gk1.upload(gk1Mat);
    d_gk2.upload(gk2Mat);
    d_gk3.upload(gk3Mat);

    Mat srcDouble;
    src.convertTo(srcDouble, CV_32FC3); // I have change float here
    vector<Mat> bgrCh;
    split(srcDouble, bgrCh);

    Mat h_bdouble, h_gdouble, h_rdouble;
    h_bdouble = bgrCh[0];
    h_gdouble = bgrCh[1];
    h_rdouble = bgrCh[2];
    //=======================================================================================

    int top1, bottom1, left1, right1;
    int top2, bottom2, left2, right2;
    int top3, bottom3, left3, right3;
    top1 = bottom1 = left1 = right1 = 45;
    top2 = bottom2 = left2 = right2 = 240;
    top3 = bottom3 = left3 = right3 = 750;


    //paddedImage
    Mat h_bdoublep1, h_bdoublep2, h_bdoublep3; 
    Mat h_gdoublep1, h_gdoublep2, h_gdoublep3;
    Mat h_rdoublep1, h_rdoublep2, h_rdoublep3;

    Scalar value(0, 0, 0);
    copyMakeBorder(h_bdouble, h_bdoublep1, top1, bottom1, left1, right1, BORDER_CONSTANT, value);
    copyMakeBorder(h_bdouble, h_bdoublep2, top2, bottom2, left2, right2, BORDER_CONSTANT, value);
    copyMakeBorder(h_bdouble, h_bdoublep3, top3, bottom3, left3, right3, BORDER_CONSTANT, value);

    cv::cuda::GpuMat d_bdoublep1,d_bdoublep2, d_bdoublep3;

    d_bdoublep1.upload(h_bdoublep1);
    d_bdoublep2.upload(h_bdoublep2);
    d_bdoublep3.upload(h_bdoublep3);

    cv::cuda::GpuMat d_ssrb1, d_ssrb2, d_ssrb3;

    cv::Ptr<cv::cuda::Convolution> convolver1 = cuda::createConvolution(Size(91, 91)); // Size(91, 91)
    cv::Ptr<cv::cuda::Convolution> convolver2 = cuda::createConvolution(Size(481, 481)); // Size(481, 481)
    cv::Ptr<cv::cuda::Convolution> convolver3 = cuda::createConvolution(Size(1501, 1501)); // Size(1501, 1501)

    convolver1->convolve(d_bdoublep1, d_gk1, d_ssrb1); 
    convolver2->convolve(d_bdoublep2, d_gk2, d_ssrb2); 
    convolver3->convolve(d_bdoublep3, d_gk3, d_ssrb3); 
    Mat b1, b2, b3;
    d_ssrb1.download(b1);
    d_ssrb2.download(b2);
    d_ssrb3.download(b3);

    namedWindow("d_ssrb1", WINDOW_FREERATIO);
    namedWindow("d_ssrb2", WINDOW_FREERATIO);
    namedWindow("d_ssrb3", WINDOW_FREERATIO);
    imshow("d_ssrb1", b1);
    imshow("d_ssrb3", b2);
    imshow("d_ssrb2", b3);