python图像处理2

来源:互联网 发布:淘宝如何解绑手机号 编辑:程序博客网 时间:2024/06/07 15:01

运行环境Ubuntu14.04   python2.7
在图像上绘制点和线:

from PIL import Imagefrom 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 Imagefrom 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 Imagefrom 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 valuefor i in range(100):    im[i+300,:] = im[i,:]im2 = 255 - imfigure()imshow(im2)im3 = 2.55 * im + 100figure()imshow(im)pil_im = Image.fromarray(im2)  #将array数据类型转会Imagepil_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)
2 0
原创粉丝点击