Python+OpenCV:图像去噪(Image Denoising)

理论

We have seen many image smoothing techniques like Gaussian Blurring, Median Blurring etc and they were good to some extent in removing small quantities of noise.

In those techniques, we took a small neighbourhood around a pixel and did some operations like gaussian weighted average, median of the values etc to replace the central element.

In short, noise removal at a pixel was local to its neighbourhood.

There is a property of noise.

Noise is generally considered to be a random variable with zero mean.

Consider a noisy pixel,  where  is the true value of pixel and n is the noise in that pixel.

You can take large number of same pixels (say N) from different images and computes their average.

Ideally, you should get  since mean of noise is zero.

You can verify it yourself by a simple setup.

Hold a static camera to a certain location for a couple of seconds.

This will give you plenty of frames, or a lot of images of the same scene.

Then write a piece of code to find the average of all the frames in the video (This should be too simple for you now ).

Compare the final result and first frame. You can see reduction in noise.

Unfortunately this simple method is not robust to camera and scene motions. Also often there is only one noisy image available.

So idea is simple, we need a set of similar images to average out the noise.

Consider a small window (say 5x5 window) in the image.

Chance is large that the same patch may be somewhere else in the image.

Sometimes in a small neighbourhood around it.

What about using these similar patches together and find their average? For that particular window, that is fine.

See an example image below:

The blue patches in the image looks the similar. Green patches looks similar.

So we take a pixel, take small window around it, search for similar windows in the image, average all the windows and replace the pixel with the result we got.

This method is Non-Local Means Denoising. It takes more time compared to blurring techniques we saw earlier, but its result is very good.

More details and online demo can be found at link: http://www.ipol.im/pub/art/2011/bcm_nlm/ (It has the details, online demo etc. Highly recommended to visit. Our test image is generated from this link).

For color images, image is converted to CIELAB colorspace and then it separately denoise L and AB components.

Image Denoising in OpenCV

OpenCV provides four variations of this technique.

  1. cv.fastNlMeansDenoising() - works with a single grayscale images.
  2. cv.fastNlMeansDenoisingColored() - works with a color image.
  3. cv.fastNlMeansDenoisingMulti() - works with image sequence captured in short period of time (grayscale images).
  4. cv.fastNlMeansDenoisingColoredMulti() - same as above, but for color images.

Common arguments are:

  • h : parameter deciding filter strength. Higher h value removes noise better, but removes details of image also. (10 is ok).
  • hForColorComponents : same as h, but for color images only. (normally same as h).
  • templateWindowSize : should be odd. (recommended 7).
  • searchWindowSize : should be odd. (recommended 21).

Please visit first link in additional resources for more details on these parameters.

We will demonstrate 2 and 3 here. Rest is left for you.

1. cv.fastNlMeansDenoisingColored()

As mentioned above it is used to remove noise from color images. (Noise is expected to be gaussian).

See the example below:

####################################################################################################
# 图像去噪(Image Denoising)
def lmc_cv_image_denoising(method):"""函数功能: 图像去噪(Image Denoising)."""#  0: fastNlMeansDenoisingColored(): used to remove noise from color images.if 0 == method:image = lmc_cv.imread('D:/99-Research/TestData/image/cartoon6.jpg')image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)denoising_image = lmc_cv.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)# 显示结果pyplot.subplot(121)pyplot.imshow(image)pyplot.xticks([])pyplot.yticks([])pyplot.subplot(122)pyplot.imshow(denoising_image)pyplot.xticks([])pyplot.yticks([])pyplot.show()

2. cv.fastNlMeansDenoisingMulti()

Now we will apply the same method to a video.

The first argument is the list of noisy frames.

Second argument imgToDenoiseIndex specifies which frame we need to denoise, for that we pass the index of frame in our input list.

Third is the temporalWindowSize which specifies the number of nearby frames to be used for denoising.

It should be odd. In that case, a total of temporalWindowSize frames are used where central frame is the frame to be denoised.

For example, you passed a list of 5 frames as input. Let imgToDenoiseIndex = 2 and temporalWindowSize = 3. Then frame-1, frame-2 and frame-3 are used to denoise frame-2.

Let's see an example.

####################################################################################################
# 图像去噪(Image Denoising)
def lmc_cv_image_denoising(method):"""函数功能: 图像去噪(Image Denoising)."""# 1: remove noise from color video.if 1 == method:# 读取视频parser = argparse.ArgumentParser(description='This sample demonstrates remove noise from color video.')parser.add_argument('--input', type=str, help='Path to a video or a sequence of image.',default='D:/99-Research/TestData/image/slow_traffic_small.mp4')args = parser.parse_args()cap = lmc_cv.VideoCapture(lmc_cv.samples.findFileOrKeep(args.input))# create a list of first 5 framesimage = [cap.read()[1] for i in range(5)]# convert all to grayscalegray = [lmc_cv.cvtColor(i, lmc_cv.COLOR_BGR2GRAY) for i in image]# convert all to float64gray = [np.float64(i) for i in gray]# create a noise of variance 25noise = np.random.randn(*gray[1].shape) * 10# Add this noise to imagesnoisy = [i + noise for i in gray]# Convert back to uint8noisy = [np.uint8(np.clip(i, 0, 255)) for i in noisy]# Denoise 3rd frame considering all the 5 framesdenoising_image = lmc_cv.fastNlMeansDenoisingMulti(noisy, 2, 5, None, 4, 7, 35)# 显示结果pyplot.subplot(131)pyplot.imshow(gray[2], 'gray')pyplot.xticks([])pyplot.yticks([])pyplot.subplot(132)pyplot.imshow(noisy[2], 'gray')pyplot.xticks([])pyplot.yticks([])pyplot.subplot(133)pyplot.imshow(denoising_image, 'gray')pyplot.xticks([])pyplot.yticks([])pyplot.show()

It takes considerable amount of time for computation. In the result, first image is the original frame, second is the noisy one, third is the denoised image.

Python+OpenCV:图像去噪(Image Denoising)相关推荐

  1. 《OpenCv视觉之眼》Python图像处理五 :Opencv图像去噪处理之均值滤波、方框滤波、中值滤波和高斯滤波

    本专栏主要介绍如果通过OpenCv-Python进行图像处理,通过原理理解OpenCv-Python的函数处理原型,在具体情况中,针对不同的图像进行不同等级的.不同方法的处理,以达到对图像进行去噪.锐 ...

  2. python opencv 彩色图非局部平均去噪

    python opencv 彩色图非局部平均去噪 代码: import cv2 import numpy as np # 灰度图像去噪 def MeansDenoising(img,N,h,templ ...

  3. python opencv 灰度图非局部平均去噪

    python opencv 灰度图非局部平均去噪 代码: import cv2 import numpy as np # 灰度图像去噪 def MeansDenoising(img,h,templat ...

  4. Python OpenCV GrabCut进行前景分割和提取

    Python OpenCV GrabCut进行前景分割和提取 1. 效果图 1.1 边界框GrabCut效果图 1.2 Mask GrabCut效果图 2. GrabCut原理 2.1 GrabCut ...

  5. 解决Python OpenCV 读取视频并抽帧出现error while decoding的问题

    解决Python OpenCV 读取视频抽帧出现error while decoding的问题 1. 问题 2. 解决 3. 源代码 参考 1. 问题 读取H264视频,抽帧视频并保存,报错如下: [ ...

  6. Python OpenCV应用K均值聚类进行颜色量化

    Python OpenCV应用K均值聚类进行颜色量化 1. 效果图 2. 颜色量化是什么? 3. MiniBatchKMeans & KMeans 4. 源码 参考 在这篇博客文章中,我将向您 ...

  7. 使用Python,OpenCV在视频中进行实时条形码检测

    使用Python,OpenCV在视频中进行实时条形码检测 1. 步骤 2. 适用场景及优化 3. 总结 4. 源码 参考 上一篇博客介绍了如何检测和查找图像中的条形码.这篇博客将进行一些优化以检测实时 ...

  8. Python+OpenCV图像处理实验

    目录 1.灰度化功能 2.反转功能 3.通道分离功能 4.噪音.滤波功能 5.高斯双边滤波功能 6.均值偏移滤波功能 7.图像二值化功能 8.Canny边缘检测功能 9.直线检测功能 10.圆形检测功 ...

  9. Python+OpenCV创建级联文件(Windows7/10环境)

    目录 1.搭建环境 2.准备数据集 3.训练级联文件 之前使用Python+OpenCV实现交通路标识别,具体实现步骤及心得如下: OpenCV训练属于自己的xml文件,需以下几个步骤: 1.首先下载 ...

最新文章

  1. Isolation Forest
  2. python numpy中astype使用不当导致图像出现artifact
  3. iteritems()与items()
  4. ant中的table和pagination表格分页结合使用 手写分页
  5. 11-散列1 电话聊天狂人
  6. python 把多个list合并为一个并去重内容_110道Python面试题(上)
  7. qml 不刷新 放大还原_【显示器选择详解】你的电脑能否带动高分辨率,高刷新率显示器?...
  8. 于Eclipse传导C/C++配置方法开发(20140721新)
  9. ubuntu mysql双主热备配置_mysql学习:mysql双主热备+lvs+keepalived配置
  10. CAD(2007)学习笔记
  11. 【Redis】笔记(尚硅谷、黑马整合)
  12. 2014年黑金FPGA原创教程规划发布
  13. 计算机乘法原理 移位,原码乘法,原码乘法原理详解
  14. maya2018英文翻译_Maya 软件所有中英文翻译
  15. 解决vscode打开中文乱码,用记事本打开却无乱码
  16. 软件生命周期中的测试概念,IT软件生命周期中的重要概念
  17. 10. 爬虫训练场,分页爬虫案例前端页面制作
  18. 性格测试分析软件,4种性格测试系统
  19. xbox360链接pc_如何将PC游戏从Windows 10免费流式传输到Xbox One
  20. 【观察】纯公有云+全场景SaaS服务,用友YonSuite领跑背后的“制胜秘诀”

热门文章

  1. ORA-01092解决一例
  2. 2018-2019-2 网络对抗技术 20165322 Exp8 Web基础
  3. SSM商城系统开发笔记-配置01-web.xml
  4. struts2 action 中autowired 不能注入
  5. Linux下mySql的安装和使用
  6. 可用的免费公开的DNS
  7. WCF并发连接数的问题
  8. django学习第一天
  9. Android之使用枚举利弊及替代方案
  10. CAD转换图片的小窍门