python - 基于空间维度去除图像噪声

标签 python image opencv noise

我想使用 Python 从 RGB 图像的主要特征周围去除噪声“五彩纸屑”。理想情况下,此过程将使示例图像中心的大特征( Blob )保持不变。即是否可以仅在其面积低于给定值时去除噪声?

我曾尝试在示例图像上使用 OpenCV 的 fastNlMeansDenoisingColored 函数(见下文),但这会从图像的其余部分移除重要信号。

这是示例图片:

example.png

也可以是downloaded here .

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('example.png')

dst = cv2.fastNlMeansDenoisingColored(img,None,10,7,21)

# Original
plt.imshow(img)
plt.show()
print(np.nanmin(img),np.nanmax(img))
# denoised
plt.imshow(dst)
print(np.nanmin(dst),np.nanmax(dst))
plt.show()
# difference 
plt.imshow(img-dst)
plt.show()

Result from code

最佳答案

如果您只想要中央的 Blob ,您可以选择寻找轮廓并选择面积最大的那个。

代码:

#--- convert image to grayscale ---
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

#--- Perform Otsu threshold ---
ret2, th2 = cv2.threshold(imgray,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imshow('Threshold', th2)

它产生一个二值图像:

enter image description here

#--- Finding contours using the binary image ---
 _, contours, hierarchy =    cv2.findContours(th2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

#--- finding the contour with the maximum area ---
big_contour = 0
max_area = 0

for cnt in contours:
    if (cv2.contourArea(cnt) > max_area):
        max_area = cv2.contourArea(cnt)
        big_contour = cnt

#--- creating a mask containing only the biggest contour ---
mask = np.zeros(imgray.shape)
cv2.drawContours(mask, [big_contour], 0, (255,255,255), -1)
cv2.imshow('Mask', mask)

enter image description here

#--- masking the image above with a copy of the original image ---
im2 = im.copy()
fin = cv2.bitwise_and(im2, im2, mask = mask.astype(np.uint8))
cv2.imshow('Final result', fin)

enter image description here

关于python - 基于空间维度去除图像噪声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51329338/

相关文章:

jQuery - 文件上传 - 缩略图

php - 按位运算 rune 件函数 php

python - gurobi 6.0.2/setPWLObj 的分段线性目标崩溃

python - 元组索引超出范围,Tensorflow

python - pyparsing:嵌套计数数组?

javascript - 如何在此脚本中从 JSON 数组加载图像?

opencv - 在 OpenCV 的复杂背景中使用 Tesseract 检测文本

c++ - 编译opencv程序导致gcc -I/usr/local/lib test.cpp test.cpp :1:10: fatal error: opencv2/core. hpp: No such file or directory

c++ - 未找到 opencv .dll 文件

python - [sklearn][standardscaler] 我可以反转模型输出的 standardscaler 吗?