因为之前有装pycharm,当时装的python也是3.7版本的,就没有再重装了,虽然老师建议是装3.0以下的版本。
本次实验需要用到的库有:
1.PIL (Python Imaging Library)图像库:提供了很多常用的图像处理及很多有用的图像基本操作。PIL库下载地址[www.pythonware.com/products/pil/]。
2.Matplotlib库:当在处理数学及绘图或在图像上描点、画直线、曲线时,Matplotlib是一个很好的绘图库,它比PIL库提供了更有力的特性。Matplotlib是开源的,可以在[matplotlib.sourceforge.net]上下载,并且它还提供了详细的文档及教程。这里,会展示一些我们在本书后面会用到的函数的一些实例。
3.NumPy库:NumPy是Python一个流行的用于科学计算包。它包含了很多诸如矢量、矩阵、图像等其他非常有用的对象和线性代数函数。在本书中几乎所有的例子都用到了NumPy数组对象。NumPy可以在scipy.org/Download]下载,在线文档包含了很多常见问题的答案。
4.导入PCV:参考博客:link
得以解决。
5.SciPy库:SciPy是一个开源的数学工具包,它是建立在NumPy的基础上的。它提供了很多有效的常规操作,包括数值综合、最优化、统计、信号处理以及图像处理。正如接下来所展示的,SciPy库包含了很多有用的模块。SciPy库可以再[http://scipy.org/Download]下载。
由于要引入的库很多,所以在刚开始尝试实验时就遇到了不少问题,不过百度一下所有的问题都有解决方法:
win+r打开cmd;//其实没有相关的库的话,可以先alt+shift+enter看看系统能不能自己安装下载试错
都装好之后就没有报错啦!
不过图片的路径又出了问题
调了一下格式就解决了:
运行结果如下:
两个例子:图像轮廓线和图线等高线。在画图像轮廓前需要转换为灰度图像,因为轮廓需要获取每个坐标[x,y]位置的像素值。下面是画图像轮廓和直方图的代码:
#-*- 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//Garfield//Desktop//1.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()
在读入图像到NumPy数组后,就可以对它进行任何我们想要的操作了。对图像进行灰度变换便是一个简单的例子。这里给出一些进行灰度变换的例子:
代码:
#-*- coding: utf-8 -*-
import numpy
from PIL import Image
from pylab import *
im = array(Image.open('C://Users//Garfield//Desktop//1.jpg').convert('L'))
print(int(im.min()), int(im.max()))
im2 = 255 - im # invert image
print(int(im2.min()), int(im2.max()))
im3 = (100.0/255) * im + 100 # clamp to interval 100...200
print(int(im3.min()), int(im3.max()))
im4 = 255.0 * (im/255.0)**2 # squared
print(int(im4.min()), int(im4.max()))
figure()
gray()
subplot(1, 3, 1)
imshow(im2)
axis('off')
title(r'$f(x)=255-x$')
subplot(1, 3, 2)
imshow(im3)
axis('off')
title(r'$f(x)=\frac{100}{255}x+100$')
subplot(1, 3, 3)
imshow(im4)
axis('off')
title(r'$f(x)=255(\frac{x}{255})^2$')
show()
上面左边灰度变换函数采用的是f(x)=255-x,中间采用的是f(x)=(100/255)x+100,右边采用的是变换函数是f(x)=255(x/255)^2。运行上面代码,可以得到下图中的结果:
命令
print int(im.min()), int(im.max())
用以检查每幅图像的最小值和最大值;
本次实验中对每幅图像用到了打印最小像素值和最大像素值,显示结果如下:
NumPy数组将成为我们对图像及数据进行处理的最主要工具,但是调整矩阵大小并没有一种简单的方法。我们可以用PIL图像对象转换写一个简单的图像尺寸调整函数:
def imresize(im,sz):
""" Resize an image array using PIL. """
pil_im = Image.fromarray(uint8(im))
return array(pil_im.resize(sz))
上面定义的调整函数,在imtools.py中可以找到。
一个极其有用的例子是灰度变换后进行直方图均衡化。图像均衡化作为预处理操作,在归一化图像强度时是一个很好的方式,并且通过直方图均衡化可以增加图像对比度。下面是对图像直方图进行均衡化处理的例子:
代码:
#-*- 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//Garfield//Desktop//1.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()
对图像取平均是一种图像降噪的简单方法,经常用于产生艺术效果。假设所有的图像具有相同的尺寸,我们可以对图像相同位置的像素相加取平均,下面是一个演示对图像取平均的例子:
代码:
#-*- coding: utf-8 -*-
from PCV.tools.imtools import get_imlist
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)
filelist = get_imlist('C://Users//Garfield//Desktop//testpic//') #获取convert_images_format_test文件夹下的图片文件名(包括后缀名)
avg = imtools.compute_average(filelist)
for impath in filelist:
im1 = array(Image.open(impath))
subplot(2, 2, filelist.index(impath)+1)
imshow(im1)
imNum=str(filelist.index(impath)+1)
title(u'待平均图像'+imNum, fontproperties=font)
axis('off')
subplot(2, 2, 4)
imshow(avg)
title(u'平均后的图像', fontproperties=font)
axis('off')
一个经典的并且十分有用的图像卷积例子是对图像进行高斯模糊。高斯模糊可以用于定义图像尺度、计算兴趣点以及很多其他的应用场合。下面是对图像进行模糊显示:
代码:
#-*- coding: utf-8 -*-
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('board.jpeg'))
im = array(Image.open('../data/empire.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)
#如果是彩色图像,则分别对三个通道进行模糊
#for bi, blur in enumerate([2, 5, 10]):
#im2 = zeros(im.shape)
#for i in range(3):
#im2[:, :, i] = filters.gaussian_filter(im[:, :, i], blur)
#im2 = np.uint8(im2)
#subplot(1, 4, 2 + bi)
#axis('off')
#imshow(im2)
show()
运行结果截图:
上面第一幅图为待模糊图像,第二幅用高斯标准差为2进行模糊,第三幅用高斯标准差为5进行模糊,最后一幅用高斯标准差为10进行模糊。
本次实验对于用python语言实现对图像的基本操作有了一个初步的认识,感觉很方便,很有实用性。