python 计算机视觉学习笔记(1)--对图像进行基本处理

python computer vision  learning notes(1)

1.PIL-Python图像库

PIL:Python Imaging Library是一个强大的图像处理库。但因为年代久远未进行不断的维护,所以PIL只支持到python2.7。如果你的python的版本是3.xx,可以使用Pillow代替。Pillow是由一些志愿者的兼容版,并且具有一些新的特性。

下面为一个简单实例,显示一张图片的灰度图

python 计算机视觉学习笔记(1)--对图像进行基本处理_第1张图片

 

# -*- coding: utf-8 -*-
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(r'C:\Users\noob\Desktop\a.jpg')
gray()
subplot(121)
title(u'原图',fontproperties=font)
axis('off')
imshow(pil_im)

pil_im = Image.open(r'C:\Users\noob\Desktop\a.jpg').convert('L')
subplot(122)
title(u'灰度图',fontproperties=font)
axis('off')
imshow(pil_im)

show()

 

 

2 对图片进行格式转换

利用PIL的open函数创建PIL的图像对象,并利用save函数对图像格式进行转换。其中的imlist.txt用来存储文件夹下所有图片名称和路径。

下面例子把图像从.jpg格式转化为.png格式。

python 计算机视觉学习笔记(1)--对图像进行基本处理_第2张图片

# -*- coding: utf-8 -*-
from PCV.tools.imtools import get_imlist #导入PCV模块
from PIL import Image
import os
import pickle

filelist = get_imlist('C:/Users/noob/Desktop/test/') #获取test文件夹下的图片文件名(包括后缀名)
imlist = file('C:/Users/noob/Desktop/test/imlist.txt','w') #将获取的图片文件列表保存到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

 

3.生成图像的灰度图,缩略图和图像拷贝粘贴区域,并图像进行尺寸旋转变换

图2调用gray函数生成灰度图。

图3调用crop函数生成拷贝区域图。参数box为要拷贝图像的四个边与坐标交点的值顺序为(左,上,右,下)。坐标系原点为图片左上角为(0,0)。还可以使用transpose函数对图像倒置并利用paste()把生成的拷贝图放到原来的图中。

不清楚的话可以参考http://goingmyway.cn/oldblog/?p=786

图4调用thumnail函数生成缩略图。函数里面的参数size为长度和宽度,单位像素点。

图5调用resize函数调整图片尺寸。

图6调用rotate函数对图片进行旋转。

python 计算机视觉学习笔记(1)--对图像进行基本处理_第3张图片

# -*- coding: utf-8 -*-
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('C:/Users/noob/Desktop/test/a.jpg')
print pil_im.mode, pil_im.size, pil_im.format
subplot(231)
title(u'原图', fontproperties=font)
axis('off')
imshow(pil_im)

# 显示灰度图
pil_im = Image.open('C:/Users/noob/Desktop/test/a.jpg').convert('L')
gray()
subplot(232)
title(u'灰度图', fontproperties=font)
axis('off')
imshow(pil_im)

#拷贝粘贴区域
pil_im = Image.open('C:/Users/noob/Desktop/test/a.jpg')
box = (100,100,400,400)
region = pil_im.crop(box)
region = region.transpose(Image.ROTATE_180)
pil_im.paste(region,box)
subplot(233)
title(u'拷贝粘贴区域', fontproperties=font)
axis('off')
imshow(pil_im)

# 缩略图
pil_im = Image.open('C:/Users/noob/Desktop/test/a.jpg')
size = 128, 128
pil_im.thumbnail(size)
print pil_im.size
subplot(234)
title(u'缩略图', fontproperties=font)
axis('off')
imshow(pil_im)
pil_im.save('C:/Users/noob/Desktop/test/b.jpg') #保存缩略图

# 调整图像尺寸
pil_im = Image.open('C:/Users/noob/Desktop/test/a.jpg')
pil_im = pil_im.resize(size)
print pil_im.size
subplot(235)
title(u'调整尺寸后的图像', fontproperties=font)
axis('off')
imshow(pil_im)

# 旋转图像45°
pil_im = Image.open('C:/Users/noob/Desktop/test/a.jpg')
pil_im = pil_im.rotate(45)
subplot(236)
title(u'旋转45°后的图像', fontproperties=font)
axis('off')
imshow(pil_im)

show()

4.图像轮廓和直方图

在得到图像轮廓图之前,要先把图像转换成灰度图。通过调用flatten函数得到直方图。

python 计算机视觉学习笔记(1)--对图像进行基本处理_第4张图片

# -*- coding: utf-8 -*-
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('C:/Users/noob/Desktop/test/a.jpg').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()

5 直方图均衡化 

直方图均衡是一种图像预操作。通过对图像强度归一化操作(即让各个像素值的区域所含的点的个数相近),可以增强图像的对比度。通过调用histeq函数可以实现其操作。

python 计算机视觉学习笔记(1)--对图像进行基本处理_第5张图片

# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
from PCV.tools import imtools

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

im = array(Image.open('C:/Users/noob/Desktop/test/a.jpg').convert('L'))  # 打开图像,并转成灰度图像

im2, cdf = imtools.histeq(im)

figure()
subplot(2, 2, 1)
axis('off')
gray()
title(u'原始图像', fontproperties=font)
imshow(im)

subplot(2, 2, 2)
axis('off')
title(u'直方图均衡化后的图像', fontproperties=font)
imshow(im2)

subplot(2, 2, 3)
axis('off')
title(u'原始直方图', fontproperties=font)

hist(im.flatten(), 128, normed=True)

subplot(2, 2, 4)
axis('off')
title(u'均衡化后的直方图', fontproperties=font)
#hist(im2.flatten(), 128, cumulative=True, normed=True)
hist(im2.flatten(), 128, normed=True)

show()

6.图像降噪

高斯模糊就是一种低通滤波器,也就是一种卷积。我们日常生活中拍近处物体为了想使拍的照片更有层次感,经常想突出前景,虚化它的背景。就会利用手机去对焦,这时你就可以看到图像的对焦的图像是清晰地 ,但它后面的背景是模糊的。这就是用到高斯滤波。

如果大家想了解原理的话,可以参考https://www.cnblogs.com/evennl/p/3894438.html

https://blog.csdn.net/qq_37385726/article/details/82020214

 

中间一幅图示用标准差为10进行高斯模糊后的结果,最右边一幅图是用ROF降噪后的图像。

 

python 计算机视觉学习笔记(1)--对图像进行基本处理_第6张图片

# -*- coding: utf-8 -*-
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('C:/Users/noob/Desktop/test/a.jpg').convert('L'))

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



figure()
gray()

subplot(1,3,1)
imshow(im)

axis('off')
title(u'原噪声图像', fontproperties=font)

subplot(1,3,2)
imshow(G)

axis('off')
title(u'高斯模糊后的图像', fontproperties=font)

subplot(1,3,3)
imshow(U)

axis('off')
title(u'ROF降噪后的图像', fontproperties=font)

show()

 

你可能感兴趣的:(python 计算机视觉学习笔记(1)--对图像进行基本处理)