計算機視覺--使用pycharm進行基礎圖像處理

前言

這是第一次使用python所以也是第一次安裝與python相關的軟件,前後裝了3天時間,其中艱辛會寫在另外一篇博客中。

https://blog.csdn.net/lzydelyc/article/details/104461773

1.繪製圖像輪廓以及圖像(灰度)直方圖

1.1基本原理

繪製圖像輪廓首先將圖像灰度化,然後對每個座標[x,y]像素值施加同一閾值
調用的方法(部分)
convert():將圖像轉爲灰度圖像
contour(Z) 創建一個包含矩陣Z的等值線的等高線圖
axis(‘equal’) :表示x軸和y軸的單位長度相同
axis(‘off’) :關閉所有座標軸線、刻度標記和標籤
flatten():在繪製圖像直方圖之前對圖像進行過壓平處理
hist():繪製灰度圖像直方圖

1.2代碼實現

# encoding:utf-8

from PIL import Image
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']


im = array(Image.open('C:\Python\Pictrue\head.jpg').convert('L'))

figure()
subplot(121)
gray()
contour(im, origin='image')
axis('equal')
axis('off')
title(u'灰度圖像')

subplot(122)
hist(im.flatten(), 128)#一維數組
title(u'圖像直方圖')
plt.xlim([0, 260])
plt.ylim([0, 11000])

show()

1.3結果展示

在這裏插入圖片描述

2.高斯濾波

2.1基本原理

圖像模糊就是將(灰度)圖像 I 和一個高斯核進行卷積操作。
高斯模糊通常是其他圖像處理操作的一部分,在這裏用來作爲使用ROF模型去噪的操作步驟之一。
高斯濾波中sigma(高斯核的標準偏差)值越大,圖像越模糊(見下圖紅框數字)
在這裏插入圖片描述
在高斯濾波中,多維濾波器被實現爲一維卷積濾波器的序列。中間數組以與輸出相同的數據類型存儲。因此,對於精度有限的輸出類型,結果可能不精確,因爲中間結果可能存儲的精度不足。
ROF模型的優點在於去除噪聲的同時較好的保持了邊緣,同時,ROF的峯值信噪比較高。

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

from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)

im = array(Image.open('C:\Python\Pictrue\head.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.直方圖均衡化

3.1基本原理

直方圖均衡化是將一幅圖像的灰度直方圖扁平,使變換後的圖像中的每個灰度值的分佈概率相同。直方圖均衡化可以增強圖像的對比度。
histeq():用於增強灰度值動態範圍偏小的圖像的對比度。基本思想是把原始圖像的灰度統計直方圖變換成爲均勻分佈的形式,增加了像素灰度值的動態範圍,從而達到增強圖像整體對比度的效果。

3.2代碼實現

在繪製matplotlib圖的時候,出現了中文亂碼的情況,在查找後發現需要添加以下兩句語句

from matplotlib.font_manager import FontProperties  
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)

並在title()中添加以下語句

title(u'原始圖像',fontproperties=font)

以下是完整代碼段


```javascript
# encoding: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:\Python\Pictrue\head.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結果展示

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章