那cv :: cuda :: Convoltion :: convolution的结果是否与普通的卷积相同?

时间:2019-05-21 01:08:56

标签: opencv

我正在用cv::cuda::Convolution::convolve对图像颜色通道进行卷积,而得到的结果与普通卷积不一样。我找不到我做错了的地方。还有一件事,我正在使用自创建内核进行卷积处理。

当我使用与图像大小相同的内核大小时,在某些图像中,除了图像左下方存在黑线外,没有任何错误。

下面是我创建内核的代码。

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

这是蓝色通道部分中卷积的代码。

 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);

预期结果:必须是没有黑线的平滑图像。

实际结果:

result images

0 个答案:

没有答案