这里博客是自己做的个系统整理,主要说的是python在数字图像处理方面的应用,主要用到的库有PIL和skimage
PIL库
from PIl import Image
img = Image.open('路径')
img.show()
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('路径')
plt.figure('')
plt.axis('off')
plt.imshow(img)
plt.show()
print(img.size) #图像的尺寸(width,height)
print(img.mode) #图像的模式(RGB或其他)
print(img.format) #图像的格式(png,jpg等)
img.save('路径') #图像的保存
gray = img.convert('L') #彩色图像转灰度图
plt.imshow(gray,cmap='gray')
r,g,b = img.split() #通道分离
pic = Image.merge('RGB',(r,g,b)) #合并三通道
box = (left,upper,right,lower)
roi = img.crop(box) #图像裁剪
几何变换:
resize(),rotate(),transpose()
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
img = np.array(Image.open('路径'))
print(img.shape) #图像尺寸(行,列,通道数)(i,j,k)
print(img.dtype) #图像类型(unit8等)
print(img.size) #图像尺寸i*j*k
print(type(img)) #<class 'numpy.ndarray>
#一维数组变二维数组用reshape(),二维数组变一维数组用flatten()
# 对图像求直方图,先把图像矩阵变为一维数组,然后在进行统计
img = np.array(Image.open('').convert('L'))
arr = img.flatten()
n,bins,patches = plt.hist(arr,bins=256,normed=1,facecolor='',alpha=)
bins:直方图的柱数
normed:是否将得到的直方图向量归一化,默认是0
facecolor:直方图颜色
alpha:透明度
返回值:
n:直方图向量,是否归一化由参数设定
bins:返回各个bin的区间范围
patches:返回bin里面包含的数据,是一个list
skimage库
scikit-image 是基于scipy的图像处理包,将图片作为numpy数组处理,读取的图像格式是(height,width,channel)
感谢原博主的贡献,自己在学习python的图像处理的时候主要按照这个教程来进行学习的,感觉非常有用。
这个主要是对python的skimage库进行的一些讲述,其实现在opencv库也有了python接口,但是自己还没有用过,之前大致看了下《opencv官方教程中文版for python》,发现其实大部分接口和skimage的很像,可能在调用方面存在些差异,具体opencv不是很了解。