python

import cv2 as  cv
import numpy as np
img = cv.imread("../mm.jpg")
cv.imshow("input", img)
def add_gaussian_noise(image):
    noise = np.zeros(image.shape, image.dtype)
    m = (15, 15, 15)#mean
    n = (30, 30, 30)#stdDev
    cv.randn(noise, m, n)
    dst = cv.add(image, noise)
    cv.imshow("gaussian_noise", noise)
    return dst
img_noise = add_gaussian_noise(img)
cv.imshow("gaussian_img", img_noise)
result = cv.fastNlMeansDenoisingColored(img_noise, None, 15, 15, 10, 30)
cv.imshow("result", result)
cv.waitKey(0)
cv.destroyAllWindows()

python中新知识点:

  • cv.fastNIMeansDenoisingColored()

c++

#include "all.h"
using namespace std;
using namespace cv;
void MyClass::day025() {
    Mat img = myRead("mm.jpg"), result, copy3, copy4;
    imshow("input", img);
    Mat copy1 = img.clone();
    Mat copy2 = img.clone();
    copy4 = Mat::zeros(img.size(), img.type());
    blur(img, result, Size(5, 5));
    imshow("blur", result);
    addGuassianNoise(copy1);
    addSaltPepperNoise(copy2);
    imshow("GuassianNoise", copy1);
    imshow("SaltPepperNoise", copy2);
    medianBlur(img, copy3, 5);
    imshow("median", copy3);
    //fastNlMeansDenoisingColored(copy1, copy4, 15, 15, 10, 30);
    //imshow("fastDenoising", copy4);
}
void MyClass::addSaltPepperNoise(Mat &img) {
    RNG rng(12345);
    int row = img.rows;
    int col = img.cols;
    int num = 10000;
    for (int i = 0; i < num; i++) {
        int x = rng.uniform(0, row);
        int y = rng.uniform(0, col);
        if (i % 2 == 1)
            img.at<Vec3b>(x, y) = Vec3b(255, 255, 255);
        else
            img.at<Vec3b>(x, y) = Vec3b(0, 0, 0);
    }
}
void MyClass::addGuassianNoise(Mat &img) {
    Mat noise = Mat::zeros(img.size(), img.type());
    randn(noise, (15, 15, 15), (30, 30, 30));
    Mat dst;
    add(img, noise, dst);
    dst.copyTo(img);
}

c++中新知识点:

  • cv::fastNIDenoisingColored()
  • randn()
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐