Home > Net >  Image blurring with different approaches
Image blurring with different approaches

Time:09-21

I am learning how to write down c program using open cv for Gaussian filtering for image blurring.

After browsing lot of websites I found two different types of coding but both claim that those code can be used from image blurring and they applied Gauusian filter method for blurring.

Code 1: Source

int main(int argc, char** argv) {
    cv::Mat image;
    image = cv::imread(argv[1],cv::IMREAD_COLOR);

    cv::Mat dst;
    cv::Mat dst2;
    cv::pyrDown(image, dst);
    cv::pyrDown(dst,dst2);
    cv::imshow("original image", image);
    cv::imshow("1st downsample",dst);
    cv::imshow("2nd downsample",dst2);
    cv::waitKey();
}

Code 2: Source[Book: Hands-On GPU-Accelerated Computer Vision with OpenCV and CUDA]

int main    ()
{
        cv::Mat h_img1  =   cv::imread("images/cameraman.tif",0);
        cv::cuda::GpuMat    d_img1,d_result3x3,d_result5x5,d_result7x7;
        d_img1.upload(h_img1);
        cv::Ptr<cv::cuda::Filter>   filter3x3,filter5x5,filter7x7;
        filter3x3   =   cv::cuda::createGaussianFilter(CV_8UC1,CV_8UC1,cv::Size(3,3),1);
        filter3x3->apply(d_img1,    d_result3x3);
        filter5x5   =   cv::cuda::createGaussianFilter(CV_8UC1,CV_8UC1,cv::Size(5,5),1);
        filter5x5->apply(d_img1,    d_result5x5);
        filter7x7   =   cv::cuda::createGaussianFilter(CV_8UC1,CV_8UC1,cv::Size(7,7),1);
        filter7x7->apply(d_img1,    d_result7x7);
        cv::Mat h_result3x3,h_result5x5,h_result7x7;
        d_result3x3.download(h_result3x3);
        d_result5x5.download(h_result5x5);
        d_result7x7.download(h_result7x7);
        cv::imshow("Original    Image   ",  h_img1);
        cv::imshow("Blurred with    kernel  size    3x3",   h_result3x3);
        cv::imshow("Blurred with    kernel  size    5x5",   h_result5x5);
        cv::imshow("Blurred with    kernel  size    7x7",   h_result7x7);
        cv::waitKey();
        return  0;
}

My question is if both codes use for the same blurring technique using Gaussian filter why there are lot of syntactical and functional differences in two codes?

CodePudding user response:

pyrDown() not only blurs the image by applying a Guassian filter, it also downsamples the resulting image to produce a smaller sized image. It is producing an image pyramid by applying Gaussian blurring and downsampling. The reason for applying Gaussian blurring is that if you downsample directly, the resulting image will have a jagged appearance.

The second code snippet does only the Gaussian blurring. If you also appropriately downsample the resulting images then you will see results that are very close to the first code snippet. Specifically, pyrDown() by default applies the 5X5 Gaussian filter and downsamples by choosing every other pixel in both rows and columns to result in an image that is half the size in both rows and columns. So, you could try downsampling the result of applying the 5X5 filter likewise and compare.

The very close caveat is because in order to match the results of the two approaches, they both have to (1) deal with borders and (2) downsample in the same way.

  • Related