OpenCV - 应用拉普拉斯滤波器时未获得所需的输出

时间:2017-01-23 06:15:51

标签: c++ opencv image-processing

我在OpenCV C ++工作。我采用classical lena image并应用了某些均值和方差noised_lena的高斯噪声。然后我在这个噪声图像上应用平均滤波器并获得Averaged Image模糊图像。

现在,当我应用拉普拉斯滤波器时,它必须正确检测边not correct output。但我没有得到这个所需的输出,其中预期的图像应该在哪里,人们可以干净地检测到边缘。代码如下。

提前感谢您的帮助。

int main(int argc, char *argv[])
{
    int i, dim, k, l, j;
    float res=0.0;
    Mat M = imread(argv[1],0); //input image in M
    Mat N(M.rows, M.cols, CV_8U);//Image obtained after applying average filter
    Mat Lap_N(M.rows, M.cols, CV_8U);//Padding the N with zeros based on the filter size given by the user
    Mat Lap(M.rows,M.cols,CV_8U);// image obtained after applying Laplacian filter
    cout << "enter the size of the filter u want odd number only"<<endl;
    cin >> dim;
    Mat Pad_M((M.rows+((dim-1))), (M.cols+((dim-1))), CV_8U);
    Mat Lap_M((M.rows+((dim-1))), (M.cols+((dim-1))), CV_8U);
    copyMakeBorder(M, Pad_M,dim/2,dim/2,dim/2,dim/2, BORDER_CONSTANT, 0);
    /*Average filter*/
    for(i=0;i<=Pad_M.rows-dim;i++)
    {
        for(j=0;j<=Pad_M.cols-dim;j++)
        {   res = 0.0;
            for(k=0;k<dim;k++)
            {   
                for(l=0;l<dim;l++)
                {   
                    res += Pad_M.at<uchar>(i+k,j+l);
                } 
            }
            res = res/(dim*dim);
            N.at<uchar>(i, j)=(uchar)((int)(res));
        }
    }
/* Laplacian Filter*/   
    copyMakeBorder(N, Lap_M,dim/2,dim/2,dim/2,dim/2, BORDER_CONSTANT, 0);
    res = 0;
    for(i=0;i<=Lap_M.rows-dim;i++)
    {
        for(j=0;j<=Lap_M.cols-dim;j++)
        {
            res = 0.0;
            for(k=0;k<dim;k++)
            {   
                for(l=0;l<dim;l++)
                {   
                    if(k==dim/2 && l==dim/2)
                    {
                        res =res - (((dim*dim)-1)*((float)Lap_M.at<uchar>(i+k,j+l)));
                    }
                    else
                    {
                        res += Lap_M.at<uchar>(i+k,j+l);
                    }
                } 
            }

            Lap_N.at<uchar>(i, j)=(uchar)((int)(res));
        }
    }

    imshow("original", M);
    imshow("padded", Pad_M);
    imshow("averaged", N);
    imshow("Laplacian", Lap_N);
    //imwrite("Lap2,500,5*5_lena.png",Lap);
    waitKey(0);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我对您提供的模糊图像执行了图像标准化:

cv2.normalize(gray, alpha=5, beta=0, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)

在我应用拉普拉斯滤波器后,我能够得到这个:

enter image description here

注意: 要执行图像规范化,请确保您的图片属于float

类型