码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCv 025---图像去噪

时间:2019-11-12 11:10:43      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:iostream   tput   des   max   exp   gauss   png   ssi   代码   

1 前备知识

图像去噪声在OCR、机器人视觉与机器视觉领域应用开发中是重要的图像预处理手段之一,对图像二值化与二值分析很有帮助,OpenCV中常见的图像去噪声的方法有

- 均值去噪声

- 高斯模糊去噪声

- 非局部均值去噪声

- 双边滤波去噪声

- 形态学去噪声

2 所用到的主要OpenCv API

/** @brief Blurs an image using the normalized box filter.
@param src input image; it can have any number of channels, which are processed independently, but
the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
@param dst output image of the same size and type as src.
@param ksize blurring kernel size.
@param anchor anchor point; default value Point(-1,-1) means that the anchor is at the kernel
center.
@param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes
@sa  boxFilter, bilateralFilter, GaussianBlur, medianBlur
 */
CV_EXPORTS_W void blur( InputArray src, OutputArray dst,
                        Size ksize, Point anchor = Point(-1,-1),
                        int borderType = BORDER_DEFAULT );
/** @brief Blurs an image using a Gaussian filter.
@param src input image; the image can have any number of channels, which are processed
independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
@param dst output image of the same size and type as src.
@param ksize Gaussian kernel size. ksize.width and ksize.height can differ but they both must be
positive and odd. Or, they can be zero‘s and then they are computed from sigma.
@param sigmaX Gaussian kernel standard deviation in X direction.
@param sigmaY Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set to be
equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width and ksize.height,
respectively (see #getGaussianKernel for details); to fully control the result regardless of
possible future modifications of all this semantics, it is recommended to specify all of ksize,
sigmaX, and sigmaY.
@param borderType pixel extrapolation method, see #BorderTypes
@sa  sepFilter2D, filter2D, blur, boxFilter, bilateralFilter, medianBlur
 */
CV_EXPORTS_W void GaussianBlur( InputArray src, OutputArray dst, Size ksize,
                                double sigmaX, double sigmaY = 0,
                                int borderType = BORDER_DEFAULT );
/** @brief Blurs an image using the median filter.
@note The median filter uses #BORDER_REPLICATE internally to cope with border pixels, see #BorderTypes
@param src input 1-, 3-, or 4-channel image; when ksize is 3 or 5, the image depth should be
CV_8U, CV_16U, or CV_32F, for larger aperture sizes, it can only be CV_8U.
@param dst destination array of the same size and type as src.
@param ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...
@sa  bilateralFilter, blur, boxFilter, GaussianBlur
 */
CV_EXPORTS_W void medianBlur( InputArray src, OutputArray dst, int ksize );
/** @brief Modification of fastNlMeansDenoising function for colored images //彩色图像快速降噪功能的改进
@param src Input 8-bit 3-channel image.
@param dst Output image with the same size and type as src .
@param templateWindowSize Size in pixels of the template patch that is used to compute weights.
Should be odd. Recommended value 7 pixels
@param searchWindowSize Size in pixels of the window that is used to compute weighted average for
given pixel. Should be odd. Affect performance linearly: greater searchWindowsSize - greater
denoising time. Recommended value 21 pixels
@param h Parameter regulating filter strength for luminance component. Bigger h value perfectly
removes noise but also removes image details, smaller h value preserves details but also preserves
some noise
@param hColor The same as h but for color components. For most images value equals 10
will be enough to remove colored noise and do not distort colors

The function converts image to CIELAB colorspace and then separately denoise L and AB components
with given h parameters using fastNlMeansDenoising function.
 */
CV_EXPORTS_W void fastNlMeansDenoisingColored( InputArray src, OutputArray dst,
        float h = 3, float hColor = 3,
        int templateWindowSize = 7, int searchWindowSize = 21);

3 程序代码

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

void add_salt_pepper_noise(Mat &image);
void gaussian_noise(Mat &image);
int main(int artc, char** argv) {
    Mat src = imread("images/lena.png");
    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input", CV_WINDOW_AUTOSIZE);
    imshow("input", src);
    gaussian_noise(src);

    Mat result1, result2, result3, result4;
    blur(src, result1, Size(5, 5));
    imshow("result-blur", result1);

    GaussianBlur(src, result2, Size(5, 5), 0);
    imshow("result-gaussBlur", result2);

    medianBlur(src, result3, 5);
    imshow("result-medianBlur", result3);

    fastNlMeansDenoisingColored(src, result4, 15, 15, 10, 30);
    imshow("result-fastN1MDC", result4);

    waitKey(0);
    return 0;
}

void add_salt_pepper_noise(Mat &image) {
    RNG rng(12345);
    int h = image.rows;
    int w = image.cols;
    int nums = 10000;
    for (int i = 0; i < nums; i++) {
        int x = rng.uniform(0, w);
        int y = rng.uniform(0, h);
        if (i % 2 == 1) {
            image.at<Vec3b>(y, x) = Vec3b(255, 255, 255);
        }
        else {
            image.at<Vec3b>(y, x) = Vec3b(0, 0, 0);
        }
    }
    imshow("salt pepper", image);
}

void gaussian_noise(Mat &image) {
    Mat noise = Mat::zeros(image.size(), image.type());
    randn(noise, (15, 15, 15), (30, 30, 30));
    Mat dst;
    add(image, noise, dst);
    imshow("gaussian noise", dst);
    dst.copyTo(image);
}

4 运行结果

5 扩展及注意事项

6*目前只做大概了解,知道有这一算法接口,后续具体使用再做具体分析

OpenCv 025---图像去噪

标签:iostream   tput   des   max   exp   gauss   png   ssi   代码   

原文地址:https://www.cnblogs.com/Vince-Wu/p/11840162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!