简体   繁体   中英

OpenCV - Not getting desired output while applying Laplacian filter

I'm working in OpenCV C++. I have taken the classical lena image and applied gaussian noise of certain mean and variance noised_lena . I then applied average filter on this noised image and obtained a Averaged Image blurred image.

Now when I apply Laplacian filter it has to correctly detect the edges not correct output . But I'm not getting this desired output where the expected image should be where, one can detect the edges cleanly . The code is given below.

Thanks for the help in advance.

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

I performed image normalization on the blurred image you provided:

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

After I applied Laplacian filter I was able to obtain this :

在此处输入图片说明

Note: To perform image normalization make sure your image is of the type float

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM