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

python图像处理2

2016-03-09 19:22 716 查看
运行环境Ubuntu14.04   python2.7

在图像上绘制点和线:

from PIL import Image
from pylab import *

im = array(Image.open('/home/chengk/图片/aaa.jpg'))
imshow(im)

x = [100,100,400,400]
y = [200,500,200,500]

plot(x,y,'r*')
plot(x[:2],y[:2],'go-')
show()




一些Pylab的绘图命令

‘b’ 蓝色

‘g’ 绿色

‘r’ 红色

‘c’ 青色

‘m’ 品红

‘y’ 黄色

‘k’ 黑色

‘w’ 白色

‘-’ 实线

‘–’ 虚线

‘:’ 点线

标记:

‘.’ 点

‘o’ 圆圈

‘s’ 正方形

‘*’ 星

‘+’ 加号

‘x’ 叉

读取图像到数组并显示

from PIL import Image
from pylab import *
aa = Image.open('/home/chengk/图片/aaa.jpg').convert('L')
aa.show()
im = array(aa)
imshow(im,cmap=cm.get_cmap('gray'))
show()


因为我这里把图片转化为了灰度图,所以在imshow那里会加上后一个参数,如果不加,显示的会有问题。一般情况使用imshow()

imshow(im)


绘制图像轮廓,在上面的代码的show()前添加:

figure()
gray()
contour(im,origin='image')
axis('equal')
axis('off')


新建一个图表并绘制

然后是绘制直方图,继续在show()前添加:

figure()
hist(im.flatten(),128)




可以直接对图像的像素进行操作:

# -*- coding: UTF-8 -*-

from PIL import Image
from pylab import *
aa = Image.open('/home/chengk/图片/aaa.jpg').convert('L')
aa.show()
im = array(aa)
imshow(im)

print im.shape   #输出图像大小和通道数
value = im[100,100]
print value

for i in range(100):
im[i+300,:] = im[i,:]

im2 = 255 - im
figure()
imshow(im2)

im3 = 2.55 * im + 100
figure()
imshow(im)

pil_im = Image.fromarray(im2)  #将array数据类型转会Image
pil_im.show()

show()


直方图均衡化,这里写成了一个函数的形式:

def histeq(im,nbr_bins=256):
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: