《python计算机视觉编程》读书笔记------7(Numpy篇)

来源:互联网 发布:数据筛选软件 编辑:程序博客网 时间:2024/05/21 19:21

图像平均:


# -*- coding: utf-8 -*-from PCV.tools.imtools import get_imlistfrom PIL import Imagefrom pylab import *from PCV.tools import imtoolsfrom matplotlib.font_manager import FontPropertiesfont = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)'''图像平均假设所有的图像具有相同的尺寸,我们可以对图像相同位置的像素相加取平均def compute_average(imlist):    #打开第一幅图,将其存储在浮点型数组中    averageim = array(Image.open(imlist[0]),'f')    for imname in imlist[1:]:        try:            averageim += array(Image.open(imname))        except:            print imname + '...skipped'        averageim /= len(imlist)    #返回uint8类型的平均图    return array(averageim,'uint8')'''#获取文件夹下的图片文件名(包括后缀名)filelist = get_imlist('C:/pytm/avg/') 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')show()

运行结果


0 0
原创粉丝点击