图像处理的几种方法

来源:互联网 发布:wap商城源码 编辑:程序博客网 时间:2024/05/18 04:11

图像处理的几种方法

1.使用skimage

name details name details name details astronaut 宇航员图片 coffee 一杯咖啡图片 lena lena图片 camera 拿相机的人图片 coins 硬币 moon 月亮 checkerboard 棋盘 horse 马 page 一页书 chelsea 小猫图片 hubble_deep_field 星空 text 文字 clock 时钟 immunohistochemistry 结肠
import numpy as npimport scipyimport matplotlib.pyplot as pltfrom skimage import io,dataimg=data.chelsea()img=io.imread('d:/python/loli.jpg')#读取图片# img=io.imread('d:/python/loli.jpg',as_grey=True)#图片变换[m,n,l]=img.shapeimg1=skimage.transform.resize(img,(int(m*0.6),int(n*0.6),l),mode='reflect')#保存图片io.imsave('d:/python/loli1.jpg',img1)io.imshow(img1)plt.show()# 显示图片信息print (type(img))  #显示类型print (img.shape)  #显示尺寸print (img.shape[0])  #图片宽度print (img.shape[1])  #图片高度print (img.shape[2])  #图片通道数print (img.size)   #显示总像素个数print (img.max())  #最大像素值print (img.min())  #最小像素值print (img.mean()) #像素平均值
C:\ProgramData\Anaconda2\lib\site-packages\skimage\util\dtype.py:122: UserWarning: Possible precision loss when converting from float64 to uint8  .format(dtypeobj_in, dtypeobj_out))

这里写图片描述

<type 'numpy.ndarray'>(415L, 518L, 3L)41551836449102550111.793140128

2 使用scipy.misc(miscellaneous routines)不推荐,很多已经deprecated

Various utilities that don’t have another home.
Note that Pillow (https://python-pillow.org/) is not a dependency of SciPy, but the image manipulation functions indicated in the list below are not available without it.

Deprecated functions:

col 1 col 2 bytescale(*args, **kwds) bytescale is deprecated! fromimage(*args, **kwds) fromimage is deprecated! imfilter(*args, **kwds) imfilter is deprecated! imread(*args, **kwds) imread is deprecated! imresize(*args, **kwds) imresize is deprecated! imrotate(*args, **kwds) imrotate is deprecated! imsave(*args, **kwds) imsave is deprecated! imshow(*args, **kwds) imshow is deprecated! toimage(*args, **kwds) toimage is deprecated!
from scipy import miscimport matplotlib.pyplot as pltimport numpy as np# 读取图片img=misc.imread('d:/python/loli.jpg',mode='RGB')# 改变大小img1=scipy.misc.imresize(img,(111,80),interp='nearest')# 显示图片#misc.imshow(img) have been deprecatedplt.imshow(img)plt.show()misc.imsave('d:/python/loli1.jpg',img)

这里写图片描述

3 使用PIL做图像处理

3.1 Reading and Writing Images : open( infilename ) , save( outfilename )

3.2 Cutting and Pasting and Merging Images :

crop() : 从图像中提取出某个矩形大小的图像。它接收一个四元素的元组作为参数,各元素为(left, upper, right, lower),坐标系统的原点(0, 0)是左上角。
paste() :
merge()

3.3 几何变换

 box = (100, 100, 200, 200) region = im.crop(box) region.show() region = region.transpose(Image.ROTATE_180) region.show() im.paste(region, box) im.show()
#旋转一幅图片def roll(image, delta):    "Roll an image sideways"    xsize, ysize = image.size    delta = delta % xsize    if delta == 0: return image    part1 = image.crop((0, 0, delta, ysize))    part2 = image.crop((delta, 0, xsize, ysize))    image.paste(part2, (0, 0, xsize-delta, ysize))    image.paste(part1, (xsize-delta, 0, xsize, ysize))    return image
#简单的几何变换out = im.resize((128, 128))                     #out = im.rotate(45)                             #逆时针旋转 45 度角。out = im.transpose(Image.FLIP_LEFT_RIGHT)       #左右对换。out = im.transpose(Image.FLIP_TOP_BOTTOM)       #上下对换。out = im.transpose(Image.ROTATE_90)             #旋转 90 度角。out = im.transpose(Image.ROTATE_180)            #旋转 180 度角。out = im.transpose(Image.ROTATE_270)            #旋转 270 度角。
from PIL import Imageim=Image.open('d:/python/loli.jpg')print im.format,im.size,im.modeim.show()
JPEG (518, 415) RGB
box = (100, 100, 200, 200)region = im.crop(box)region.show()region = region.transpose(Image.ROTATE_180)region.show()im.paste(region, box)im.show()
out = im.resize((128, 128))                     #out = im.rotate(45)                             #逆时针旋转 45 度角。out = im.transpose(Image.FLIP_LEFT_RIGHT)       #左右对换。out = im.transpose(Image.FLIP_TOP_BOTTOM)       #上下对换。out = im.transpose(Image.ROTATE_90)             #旋转 90 度角。out = im.transpose(Image.ROTATE_180)            #旋转 180 度角。out = im.transpose(Image.ROTATE_270) out.show()
原创粉丝点击