您的位置:首页 > 编程语言 > Python开发

Python的基础图像处理

2019-03-06 19:29 134 查看

介绍

最近开始安装了Python+Opencv,准备开始学习计算机视觉啦,下面是一些简单的基础图像处理,安装软件就不在描述了,顺便会提到安装三方库的方法,大部分可以直接用pip命令安装,少部分pip不通的可以通过Github下载安装。下面介绍基础图像处理的一部分内容:

  1. 图像的灰度化、转换图像格式
  2. 图像轮廓和直方图
  3. 直方图均衡化
  4. 高斯滤波

1. 图像灰度化、转换格式

图像处理的基础之一是读取图像,所以这里我们要先导入一个PIL库。PIL (Python Imaging Library)图像库提供了很多常用的图像处理及很多有用的图像基本操作,比如图像缩放、裁剪、颜色转换等。PIL是免费的,可以通过easy_install Pillow命令进行安装,如果失败了,可以点击以下链接下载。
PIL下载地址
下载完之后保存在python根目录下的\Lib\site-packages下就可以了。
我们来试一下图像的灰度化吧:

图像灰度化代码:
from PIL import Image
from pylab import *

from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)
figure()

pil_im = Image.open('pic/cat.png')
gray()
subplot(121)
title(u'原图',fontproperties=font)
axis('off')
imshow(pil_im)

pil_im = Image.open('pic/cat.png').convert('L')
subplot(122)
title(u'灰度图',fontproperties=font)
axis('off')
imshow(pil_im)

show()
效果图:


当然以上只是最简单的操作,在平时的工作里,可能会出现需要转换大批量的图片格式的情况,如果动手一张一张改实在会令人崩溃,如果通过代码,我们可以很轻松地实现图片格式转换,在转换之前,我们还需要带入一个PCV库,本来想通过pip明显进行快速下载安装,没想到显示无法找到资源,后来上网搜了一圈,找到了一个Github上别人分享的资源:

PCV下载地址

下载完之后在该目录下使用cmd命令,运行语句python setup.py install 。安装之后试一下import PCV ,没有报错的话就说明安装成功了。切回主题,我们来试一下转换图片格式
代码如下:

图片转化格式代码:
from PCV.tools.imtools import get_imlist  # 导入原书的PCV模块
from PIL import Image
import os
import pickle

filelist = get_imlist('pic/')  # 获取convert_images_format_test文件夹下的图片文件名(包括后缀名)
imlist = open('imlist.txt', mode='wb')  # 将获取的图片文件列表保存到imlist.txt中
pickle.dump(filelist, imlist)  # 序列化
imlist.close()

for infile in filelist:
outfile = os.path.splitext(infile)[0] + ".png"  # 分离文件名与扩展名
if infile != outfile:
try:
Image.open(infile).save(outfile)
except IOError:
print ("cannot convert", infile)
效果图:

2. 图像轮廓和直方图

当在处理数学及绘图或在图像上描点、画直线、曲线时,Matplotlib是一个很好的绘图库,它比PIL库提供了更有力的特性。Pylab 是 matplotlib 面向对象绘图库的一个接口,所以安装好Matplotlib库足够了。Matplotlib库可以通过下面的pip语句进行安装:

python -m pip install matplotlib

如果提示失败了,可能是pip版本过低,可以先用以下命令对pip进行版本升级:

python -m pip install -U pip setuptools

升级之后再进行下载就可以了。接下来我们来试一下显示图像的轮廓和直方图吧:

图像轮廓和直方图代码:
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('pic/cat.png').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()
效果图:

3. 直方图均衡化

图像均衡化作为预处理操作,在归一化图像强度时是一个很好的方式,并且通过直方图均衡化可以增加图像对比度。下面是对图像直方图进行均衡化处理的例子:

直方图均衡化代码:
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('pic/cat.png').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()
效果图:

4. 高斯滤波

图像滤波就是在尽量保留图像细节特征的条件下,对目标图像的噪声进行抑制,是图像预处理中不可缺少的操作,其处理效果的好坏将直接影响到后续图像处理和分析的有效性和可靠性。我们采用Rudin-Osher-Fatemi de-noising(ROF)模型。

高斯滤波代码:
from PIL import Image
from pylab import *
from numpy import *
from numpy import random
from scipy.ndimage import filters
from scipy.misc import imsave
from PCV.tools import rof

""" This is the de-noising example using ROF in Section 1.5. """

# 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)

im = array(Image.open('man.png').convert('L'))

U,T = rof.denoise(im,im)
G = filters.gaussian_filter(im,10)

# plot
figure()
gray()

subplot(1,3,1)
imshow(im)
#axis('equal')
axis('off')
title(u'原噪声图像', fontproperties=font)

subplot(1,3,2)
imshow(G)
#axis('equal')
axis('off')
title(u'高斯模糊后的图像', fontproperties=font)

subplot(1,3,3)
imshow(U)
#axis('equal')
axis('off')
title(u'ROF降噪后的图像', fontproperties=font)

show()
效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: