Biian

一、图像直方图

1、基本原理

  直方图的定义:图像的直方图用来表示该图像像素值的分布情况。用一定数目的小区间(bin)来指定表征像素值的范围,每个小区间会得到落入该小区间表示范围的像素数目。

  绘制图像直方图:

  一幅数字图像在[0,G]的范围内总共有L个灰度级,其直方图定义为下列离散函数:h(rk) = nk

  式子中,rk 是区间[0,G]内的第k级灰度,nk为图像中出现rk 这种灰度级的像素数量。 对于uint8类图像,G的值为255;对于uint16类图像,G的值为65535;对于浮点图像,G的值为1.0。注意,对于uint8类和uint16类图像,G=L-1。

  图像的直方图可以使用 hist() 函数绘制。hist() 函数的第二个参数指定小区间的数目。需要注意的是,因为 hist() 只接受一维数组作为输入,所以我们在绘制图像直方图之前,必须先对图像进行压平处理。flatten() 方法将任意数组按照行优先准则转换成一维数组。

2、相关代码:(参考课本)

from array import array
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(\'../images/02.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()

3、运行结果:

 

二、高斯滤波

1、基本原理

  高斯滤波的定义:它是一种线性平滑滤波,能够有效的抑制高斯噪声,广泛应用于图像处理的减噪过程。 

  通俗的讲,高斯滤波就是对整幅图像进行加权的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。其滤波核的值由如下公式得到(用当前点与中心点的欧式距离的平方代替下面的(x2+y2):

          

  高斯滤波的具体操作是:用一个模板(或称卷积、掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值。  

  

  

  

  

   (图片资料参考自:https://blog.csdn.net/angelia_yue/article/details/105794344)

2、相关代码:(参考课本)

import cv2
import matplotlib.pyplot as plt

# 读取图片
img = cv2.imread(\'../images/02.jpg\')
source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# 高斯滤波,这里使用15*15的卷积核 0表示滤波器的标准差
result = cv2.GaussianBlur(source, (15, 15), 0)

# 显示图形
titles = [\'Source Image\', \'GaussianBlur Image\']
images = [source, result]
for i in range(2):
    plt.subplot(1, 2, i+1), plt.imshow(images[i], \'gray\')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()

3、运行结果:

 

 

三、直方图均衡化

1、基本原理

  直方图均衡化的基本思想原理:对图像中像素个数多的灰度级进行展宽,而对图像中像素个数少的灰度进行压缩,从而扩展像原取值的动态范围,提高了对比度和灰度色调的变化,使图像更加清晰。就是对图像进行非线性拉伸,重新分配图像像素值,使一定灰度范围内的像素数量大致相同,从而使给定图像的直方图分布改变成“均匀”分布直方图分布。

  下图为直方图均衡化的过程,体现了“均衡”的含义:(概率密度的均匀)

   

2、相关代码:(参考课本)

from array import array
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(\'../images/02.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, density=True, stacked=True)

subplot(2, 2, 4)
axis(\'off\')
title(u\'均衡化后的直方图\', fontproperties=font)
hist(im2.flatten(), 128, density=True, stacked=True)

show()

3、运行结果:

 

分类:

技术点:

相关文章:

  • 2021-04-21
  • 2019-12-26
  • 2022-02-09
  • 2022-01-23
  • 2022-12-23
  • 2021-09-03
  • 2022-12-23
猜你喜欢
  • 2021-12-23
  • 2021-08-26
  • 2021-06-25
  • 2021-12-23
  • 2021-11-26
  • 2021-05-07
  • 2019-08-09
相关资源
相似解决方案