一、直方圖
1.1原理
直方圖是數值數據分布的精確圖形表示。 這是一個連續變量的概率分布的估計,是一種條形圖。為了構建直方圖,第一步是將值的范圍分段,即將整個值的范圍分成一系列間隔,然后計算每個間隔中有多少值。 這些值通常被指定為連續的,不重疊的變量間隔。 間隔必須相鄰,並且通常是(但不是必須的)相等的大小。在畫圖像輪廓前需要將原圖像轉換為灰度圖像,因為輪廓需要獲取每個坐標[x,y]位置的像素值。
1.2代碼展示
# -*- 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:/Users/TWZ/Desktop/kobe.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()
1.3實驗結果
二、高斯濾波及降噪
2.1原理
首先,高斯濾波器是一種線性濾波器,能夠有效的抑制噪聲,平滑圖像。其作用原理和均值濾波器類似,都是取濾波器窗口內的像素的均值作為輸出。其窗口模板的系數和均值濾波器不同,均值濾波器的模板系數都是相同的為1;而高斯濾波器的模板系數,則隨着距離模板中心的增大而系數減小。圖像降噪是一個在盡可能保持圖像細節和結構信息時去除噪聲的過程。
2.2代碼展示
# -*- coding: utf-8 -*- from PIL import Image 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) im = array(Image.open('C:/Users/TWZ/Desktop/kobe.jpg').convert('L')) U,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實驗結果
三、直方圖均衡化
3.1原理
直方圖均衡化實質上是對圖像進行非線性拉伸,重新分配圖像象元值,使一定灰度范圍內象元值的數量大致相等。這樣,原來直方圖中間的峰頂部分對比度得到增強,而兩側的谷底部分對比度降低,輸出圖像的直方圖是一個較平的分段直方圖;如果輸出數據分段值較小的話,會產生粗略分類的視覺效果。一個非常有用的例子是灰度變換后進行直方圖均衡化,通過直方圖均衡化可以增加圖像對比度。
3.2代碼展示
# -*- 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:/Users/TWZ/Desktop/kobe.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()
3.3實驗結果