第一章 基本的图像操作和处理

(一)图像直方图

图像的直方图用来表征该图像像素值得分布情况。

代码:

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(‘X.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()

python和matlab之间的区别。在matlab中使用imshow就可以直接将图片显示出来,而在python中,需要写一个函数show来显示图片

运行结果如下:

计算机视觉--基本的图像操作和处理

(二)直方图均衡化

直方图均衡化是指将一幅图像的灰度直方图变平,使变换后的图像中每个灰度值的分布概率都相同。

代码:

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(‘X.jpg’).convert(‘L’)) # 打开图像,并转成灰度图像
#im = array(Image.open(‘X.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()

运行结果如下:
计算机视觉--基本的图像操作和处理

(三)高斯模糊

图像的高斯模糊是一个非常经典的图像卷积例子。

代码:

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. “”"

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

im = array(Image.open(‘X.jpg’).convert(‘L’))

U,T = rof.denoise(im,im)
G = filters.gaussian_filter(im,10)

#imsave(‘synth_original.pdf’,im)
#imsave(‘synth_rof.pdf’,U)
#imsave(‘synth_gaussian.pdf’,G)

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()

运行结果如下:
计算机视觉--基本的图像操作和处理

相关文章:

  • 2021-04-11
  • 2021-07-17
  • 2021-12-03
  • 2021-08-10
  • 2021-12-06
  • 2021-11-29
  • 2021-08-10
  • 2021-06-03
猜你喜欢
  • 2021-12-20
  • 2021-05-17
  • 2021-11-27
  • 2022-12-23
  • 2021-09-02
  • 2021-05-14
  • 2021-05-09
相关资源
相似解决方案