Python 计算机视觉 第一章 图像处理基础

第一章 图像处理基础

1.1 灰度图

PIL (Python Imaging Library)图像库提供了很多常用的图像处理及很多有用的图像基本操作。PIL库下载地址www.pythonware.com/products/pil/

# -*- 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)
figure()

pil_im = Image.open('D:/05.jpg')
gray()
subplot(121)
title(u'原图',fontproperties=font)
axis('off')
imshow(pil_im)

pil_im = Image.open('D:/05.jpg').convert('L')
subplot(122)
title(u'灰度图',fontproperties=font)
axis('off')
imshow(pil_im)

show()

运行上述代码可以得到这两幅图像,如下:
在这里插入图片描述

1.2直方图
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('D:/05.jpg').convert('L'))

figure()
hist(im.flatten(), 128)
title(u'图像直方图', fontproperties=font)
plt.xlim([0,260])
plt.ylim([0,11000])

show()

运行上述代码可以得到直方图,如下:
在这里插入图片描述

1.3直方图均衡化
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('../data/empire.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()

出现报错:ModuleNotFoundError: No module named ‘PCV’
需要安装PCV,参照这个进行下载:https://blog.youkuaiyun.com/jiaoyangwm/article/details/79451757
但是安装不成功,出现如下报错:
在这里插入图片描述
下载失败了要怎么办呢,我先pip list看了一下发现我的PCV已经安装上去了,说明其实不是PCV的问题,所以我进行了下面的操作:
在这里插入图片描述
这是python2和python3的差别,python3在使用print的时候要在后面加上括号,所以我们需要打开C:\Users\XCX\AppData\Local\Programs\Python\Python36\lib\site-packages\PCV\tools\imtools.py 在line27报错的地方加上括号,就可以解决问题了。
运行结果如下:
在这里插入图片描述

1.4高斯滤波
from PIL import Image
from pylab import *
from scipy.ndimage import filters

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

im = array(Image.open('D:/05.jpg').convert('L'))

figure()
gray()
axis('off')
subplot(1, 4, 1)
axis('off')
title(u'原图', fontproperties=font)
imshow(im)

for bi, blur in enumerate([2, 5, 10]):
  im2 = zeros(im.shape)
  im2 = filters.gaussian_filter(im, blur)
  im2 = np.uint8(im2)
  imNum=str(blur)
  subplot(1, 4, 2 + bi)
  axis('off')
  title(u'标准差为'+imNum, fontproperties=font)
  imshow(im2)
  
  show()

运行上述代码可以得到高斯滤波,如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值