fanfuhan OpenCV 教學025 ~ opencv-025-圖像去除雜訊

fanfuhan OpenCV 教學025 ~ opencv-025-圖像去除雜訊

fanfuhan OpenCV 教學025 ~ opencv-025-圖像去除雜訊


資料來源: https://fanfuhan.github.io/

https://fanfuhan.github.io/2019/04/04/opencv-025/

GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV

均值去噪聲/雜訊
高斯模糊去噪聲/雜訊
非局部均值去噪聲/雜訊
絕對濾波去噪聲/雜訊
形態學去噪聲/雜訊

C++

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

using namespace std;
using namespace cv;

void add_gaussian_noise(Mat &image);

/*
 * 图像去噪
 */
int main() {
    Mat src = imread("../images/test.png");
    Mat src_clone = src.clone();
    if (src.empty()) {
        cout << "could not load image.." << endl;
    }
    imshow("input", src);

    // 产生高斯噪声
    add_gaussian_noise(src);

    Mat res1, res2, res3, res4;
    // 均值去噪
    blur(src, res1, Size(3, 3));
    imshow("mean_blur", res1);

    // 高斯去噪
    GaussianBlur(src, res2, Size(5, 5), 0);
    imshow("gaussian_blur", res2);

    // 中值去噪
    medianBlur(src, res3, 3);
    imshow("median_blur", res3);

    // 非局部均值去噪
    fastNlMeansDenoisingColored(src, res4, 15, 15, 10, 30);
    imshow("NLmeans_blur", res4);

    waitKey(0);
    return 0;
}

void add_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);
}

Python

import cv2 as cv
import numpy as np

def gaussian_noise(image):
    noise = np.zeros(image.shape, image.dtype)
    m = (15, 15, 15)
    s = (30, 30, 30)
    cv.randn(noise, m, s)
    dst = cv.add(image, noise)
    cv.imshow("gaussian noise", dst)
    return dst


src = cv.imread("D:\\code-workspace\\Clion-workspace\\learnOpencv\\images\\test.png")
cv.imshow("input", src)
h, w = src.shape[:2]
src = gaussian_noise(src)

result1 = cv.blur(src, (5, 5))
cv.imshow("mean_blur", result1)

result2 = cv.GaussianBlur(src, (5, 5), 0)
cv.imshow("gaussian_blur", result2)

result3 = cv.medianBlur(src, 5)
cv.imshow("median_blur", result3)

result4 = cv.fastNlMeansDenoisingColored(src, None, 15, 15, 10, 30)
cv.imshow("NLmeans_blur", result4)

cv.waitKey(0)
cv.destroyAllWindows()

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *