【Python图像处理】图片读取/提取直方图
来源:互联网 发布:陈伟霆 知乎 编辑:程序博客网 时间:2024/05/18 09:19
PIL(Pillow)模块对图片进行简单操作以及matplotlib绘制图像直方图
环境:MacOS + Python2.7
1. 模块介绍
- PIL(Pillow)
PIL(Python Imaging Library)库,Python平台的图像处理标准库,但这个库现在已经停止更新了,所以使用Pillow, 它是由PIL fork而来的库。
安装方式(homebrew),在terminal输入
brew install Homebrew/science/pillow#根据brew提示输入下面两行mkdir -p /Users/Lychee/Library/Python/2.7/lib/python/site-packagesecho 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/Lychee/Library/Python/2.7/lib/python/site-packages/homebrew.pth
在python环境测试,无报错则成功
from PIL import Image
2. 对图片的基本操作
图片打开与显示
有两种方法:
1)利用操作系统自带图片浏览器打开from PIL import Imageimg = Image.open('./my_pictures/img.jpg')img.show()
2) 结合matplotlib绘制图片
from PIL import Image import matplotlib.pyplot as plt img = Image.open('./my_pictures/img.jpg') plt.figure("flower") plt.imshow(img) plt.show()
注意:在运行这段代码时可能会报错:
File “/usr/local/lib/python2.7/site-packages/PIL/Image.py”, line 709, in tostring
raise NotImplementedError(“tostring() has been removed. ”
NotImplementedError: tostring() has been removed. Please call tobytes() instead.是由于更新后的PIL中tostring()已经被舍弃,解决方式是直接打开出错的代码:Image.py,可以找到报错位置
将这段代码修改为:
然后保存即可。
再次运行上面的代码段,显示结果为:
这样显示的好处在于可以自定义名称,绘制坐标系等等。提取图片直方图
提取直方图的思路是:统计图像中像素点为某个值的个数,RGB彩色图像的提取就是将R\G\B三种颜色分别的0-255像素统计出来,然后绘制直方图。
对图像做的处理是:现将图像的R\G\B分离,然后将图像矩阵转换为一维(flatten函数)矩阵,接下来再开始统计。
# -*- coding: utf-8 -*-from PIL import Imageimport numpy as npimport matplotlib.pyplot as pltsrc = Image.open('./my_pictures/img.jpg')r,g,b = src.split() #分离R\G\Bplt.figure("flowerHist")#要对图像求直方图,就需要先把图像矩阵进行flatten操作,使之变为一维数组,然后再进行统计#分别提取R\G\B统计值,叠加绘图ar = np.array(r).flatten() plt.hist(ar,bins = 256, normed = 1, facecolor = 'red', edgecolor = 'red', hold = 1)ag = np.array(g).flatten()plt.hist(ag,bins = 256, normed = 1, facecolor = 'green', edgecolor = 'green', hold = 1)ab = np.array(b).flatten()plt.hist(ab,bins = 256, normed = 1, facecolor = 'blue', edgecolor = 'blue', hold = 1)plt.show()
参考博客:感谢~
http://blog.csdn.net/fuwenyan/article/details/53676153
http://www.linuxdiyf.com/linux/30432.html
- 【Python图像处理】图片读取/提取直方图
- python 图像直方图处理
- 用python简单处理图片(5):图像直方图
- 用python简单处理图片(5):图像直方图
- 用python简单处理图片(5):图像直方图
- 【python图像处理】python绘制灰度直方图
- 直方图均衡化处理图像(python)
- Python图像处理(5):直方图
- OpenCV提取图像颜色直方图
- 图像处理之相似图片识别(直方图应用篇)
- 图像处理之相似图片识别(直方图应用篇)
- 图像处理------相似图片识别(直方图应用篇)
- 图像处理之相似图片识别(直方图应用篇)
- 图像处理之相似图片识别(直方图应用篇)
- 图像处理之相似图片识别(直方图应用篇)
- 图像处理之相似图片识别(直方图应用篇)
- 图像处理之相似图片识别(直方图应用篇)
- 【图像处理】直方图变换
- Naive Bayes贝叶斯
- 蓝桥杯 算法训练 字串统计 JAVA
- 【递推】洛谷 P1176 路径计数2
- PLSQL实验
- 【排序】洛谷 P1177 【模板】快速排序
- 【Python图像处理】图片读取/提取直方图
- wamp 安装laravel
- js中回调函数----结合微信小程序
- 使用localhost访问远程tensorboard
- 面向web前端及node开发人员的vim配置
- 导入时如何定制spring-boot依赖项的版本
- Boost.Aiso教程 4 使用成员函数作为一个句柄
- 【模拟】洛谷 P1179 数字统计
- Android databinding xml中处理数据与控件绑定