简体   繁体   English

不同方法的图像模糊

[英]Image blurring with different approaches

I am learning how to write down c++ program using open cv for Gaussian filtering for image blurring.我正在学习如何使用 open cv 编写 c++ 程序以进行高斯滤波以进行图像模糊。

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代码 1:来源

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]代码 2:来源[书籍:使用 OpenCV 和 CUDA 的动手 GPU 加速计算机视觉]

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?我的问题是,如果两个代码都使用高斯滤波器用于相同的模糊技术,为什么两个代码在语法和功能上有很多差异?

pyrDown() not only blurs the image by applying a Guassian filter, it also downsamples the resulting image to produce a smaller sized image. pyrDown()不仅通过应用高斯滤波器来模糊图像,它还对生成的图像进行下采样以生成更小尺寸的图像。 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.具体来说, pyrDown()默认情况下应用 5X5 高斯滤波器并通过选择行和列中的每隔一个像素进行下采样,以生成行和列大小为一半的图像。 So, you could try downsampling the result of applying the 5X5 filter likewise and compare.因此,您可以尝试对同样应用 5X5 过滤器的结果进行下采样并进行比较。

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.非常接近的警告是因为为了匹配这两种方法的结果,它们都必须(1)处理边界和(2)以相同的方式下采样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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