文章目录
一、直方图均衡化
1.1 直方图均衡化原理
直方图均衡化处理的“中心思想”是把原始图像的灰度直方图从比较集中的某个灰度区间变成在全部灰度范围内的均匀分布。直方图均衡化就是对图像进行非线性拉伸,重新分配图像像素值,使一定灰度范围内的像素数量大致相同。直方图均衡化就是把给定图像的直方图分布改变成“均匀”分布直方图分布
1.2 直方图均衡化实现代码
(1)用PIL中的convert()方法将图像转换为灰度图像
(2)hist()函数绘制直方图:hist(im.flatten(),128)
hist()第二个参数指定小区间的数目由于hist()函数只接受一维数组作为输入,所以在绘制直方图之前,必须对图像进行压平处理。flatten()方法将任意数组按照行优先原则转换成一维数组。
# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
from PCV.tools import imtools
# 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)
im = array(Image.open('C:\image\photo.jpg').convert('L')) # 打开图像,并转成灰度图像
#im = array(Image.open('../data/AquaTermi_lowcontrast.JPG').convert('L'))
im2, cdf = imtools.histeq(im)
figure()
subplot(2, 2, 1)
axis('off')
gray()
title(u'原始图像', fontproperties=font)
imshow(im)
subplot(2, 2, 2)
axis('off')
title(u'直方图均衡化后的图像', fontproperties=font)
imshow(im2)
subplot(2, 2, 3)
axis('off')
title(u'原始直方图', fontproperties=font)
#hist(im.flatten(), 128, cumulative=True, normed=True)
hist(im.flatten(), 128, normed=True)
subplot(2, 2, 4)
axis('off')
title(u'均衡化后的直方图', fontproperties=font)
#hist(im2.flatten(), 128, cumulative=True, normed=True)
hist(im2.flatten(), 128, normed=True)
show()
1.3 直方图均衡化结果
分析:原图像像素大多集中在直方图中间的强度上,在均衡化操作之后的这幅图片显然对比度更强,并且图像的像素在整个强度范围内均衡分布.
二、高斯滤波(高斯模糊)
2.1 高斯滤波原理
现实中的数字图像在数字化和传输过程中常受到成像设备与外部环境噪声干扰等影响,减少数字图像中噪声的过程称为图像降噪
高斯滤波(高斯模糊) 是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像去噪。 可以简单地理解为,高斯滤波去噪就是对整幅图像像素值进行加权平均,针对每一个像素点的值,都由其本身值和邻域内的其他像素值经过加权平均后得到。
2.2 高斯滤波实现代码
(1)用噪声合成图像
# create synthetic image with noise
im = zeros((500,500))
im[100:400,100:400] = 128
im[200:300,200:300] = 255
im = im + 30*random.standard_normal((500,500))
(2)rof.denoise():基于ROF的图像去噪模型
filters.gaussian_filter:多维高斯滤波器
filters.gaussian_filter(input, sigma, order=0, output=None, mode=‘reflect’, cval=0.0, truncate=4.0)
input:输入到函数的是矩阵
sigma: 标量或标量序列。就是高斯函数里面的
# -*- coding: utf-8 -*-
from pylab import *
from numpy import *
from numpy import random
from scipy.ndimage import filters
from scipy.misc import imsave
from PCV.tools import rof
""" This is the de-noising example using ROF in Section 1.5. """
# 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)
T = rof.denoise(im,im)
G = filters.gaussian_filter(im,10)
# save the result
#imsave('synth_original.pdf',im)
#imsave('synth_rof.pdf',U)
#imsave('synth_gaussian.pdf',G)
# plot
figure()
gray()
subplot(1,3,1)
imshow(im)
#axis('equal')
axis('off')
title(u'原噪声图像', fontproperties=font)
subplot(1,3,2)
imshow(G)
#axis('equal')
axis('off')
title(u'高斯模糊后的图像', fontproperties=font)
subplot(1,3,3)
imshow(U)
#axis('equal')
axis('off')
title(u'ROF降噪后的图像', fontproperties=font)
show()
2.3 高斯滤波实现结果
第一幅图为原噪声图像,第二幅用高斯标准差为10进行模糊,最后一幅为ROF降噪后图像,ROF可以保护并同时增强图像边缘和细节进行降噪。
三、直方图和图像轮廓
3.1 直方图、图像轮廓原理
图像直方图是用来表现图像中亮度分布的直方图,给出的是图像中某个亮度或者某个范围亮度下共有几个像素,即统计一幅图某个亮度像素数量。
图像轮廓可以简单认为成连续的点(连着边界)连在一起的曲线,具有相同的颜色或者灰度。轮廓在形状分析和物体的检测和识别中很有用。
3.2 直方图、图像轮廓实现代码
(1)用PIL的convert()方法将图像转换为灰度图像
(2)contour(Z) 是根据矩阵Z画出等高线,Z是以x,y为平面的高度。Z必须是一个至少二维的矩阵。等高线的数量和水平线的值将根据Z值的最小值和最大值自动选择。x,y轴的范围是[1:n]和[1:m],
# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
# 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)
im = array(Image.open('C:\image\photo.jpg').convert('L')) # 打开图像,并转成灰度图像
figure()
subplot(121)
gray()
contour(im, origin='image')
axis('equal')
axis('off')
title(u'图像轮廓', fontproperties=font)
subplot(122)
hist(im.flatten(), 128)
title(u'图像直方图', fontproperties=font)
plt.xlim([0,260])
plt.ylim([0,11000])
show()