forked from DragonFive/cv_nlp_deeplearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimtools.py
More file actions
56 lines (50 loc) · 1.63 KB
/
imtools.py
File metadata and controls
56 lines (50 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
#__author__ = 'ASUS'
import os
from PIL import Image
from numpy import *
def get_imlist(path):
"""
:param path: 路径
:return:返回以jpg结尾的文件名列表;
"""
return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]
def imresize(im,sz):
"""
使用PIL对象重新定义图像数组的大小
:param im: 图像对应的PIL数组
:param sz: 改变后的size
:return:返回改变后的图像的PIL数组
"""
image=Image.fromarray(uint8(im))
return array(image.resize(sz))
def histeq(im,nbr_bins=256):
"""
对一副灰度图像进行直方图均衡化
:param im:
:param nbr_bins:
:return:
"""
#计算图像的直方图 bins是imhist的长度+1
imhist,bins=histogram(im.flatten(),nbr_bins,normed=True)
cdf=imhist.cumsum()#累积分布函数 可以试一试求累加和;
cdf=255*cdf/cdf[-1];
#使用累积分布函数的线性插值,计算新的像素值;
im2=interp(im.flatten(),bins[:-1],cdf)
return im2.reshape(im.shape),cdf
def compute_average(imlist):
"""
计算图像列表的平均图像
:param imlist: 图像路径列表
:return:返回的是平均图像的数组
"""
#打开第一副图像把他保存在浮点型数组中
averageim=array(Image.open(imlist[0]),'f')
for imname in imlist[1:]:#从下标为1的位置开始 因为下标为0的已经取过了
try:
averageim+=array(Image.open(imname))
except:
print imname+'...skipped'
averageim/=len(imlist)
#返回uint8型的平均图像
return array(averageim,'uint8')