python的数字图像处理学习(1)

导入原有的测试图片,测试图片路径,和一些方法,显示出测试图像,测试图像路径。

from skimage import io,data,data_dir
img_rgb=data.chelsea()
io.imshow(img_rgb)
data_dir

使用打开方式的方案,将图像转为灰度图,并显示。保存图像。

img_gray=io.imread(data_dir+'/chelsea.png',as_grey=True)
io.imshow(img_gray)
io.imsave('chelsea.png',img_gray)

 显示出数字图像的各种信息:

print(type(img_gray))  #显示类型
print(img_gray.shape)  #显示尺寸
print(img_gray.shape[0])  #图片宽度
print(img_gray.shape[1])  #图片高度
print(img_gray.size)   #显示总像素个数
print(img_gray.max())  #最大像素值
print(img_gray.min())  #最小像素值
print(img_gray.mean()) #像素平均值

提取图像的红色通道值,显示。

img_rgb_r=img_rgb[:,:,0]#选取像素值,红色单通道
io.imshow(img_rgb_r)

导入深度复制图像的方法,将图像备份,建立椒盐噪声图像。

import numpy as np
import copy as cp
img_rgb_noise = cp.deepcopy(img_rgb)
for i in range(1000):
    x = np.random.randint(0,img_rgb_noise.shape[0])
    y = np.random.randint(0,img_rgb_noise.shape[1])
    img_rgb_noise[x,y,:] = 255
io.imshow(img_rgb_noise)

将图像切片提取,显示出部分图像。

img_rgb_cut = img_rgb[0:int(img_rgb.shape[0]/2),0:int(img_rgb.shape[1]/2),:]
io.imshow(img_rgb_cut)

将灰度图像二值化的一种方法:

import copy as cp
img_gray_binaryzation = cp.deepcopy(img_gray)
for i in range(img_gray.shape[0]):
    for j in range(img_gray.shape[1]):
        if img_gray_binaryzation[i,j] > 0.5 :
            img_gray_binaryzation[i,j] = 1
        else :
            img_gray_binaryzation[i,j] = 0
io.imshow(img_gray_binaryzation)

通过判断,提取出部分符合条件的数据,将符合条件的数据进行修改,,,一个示例

import copy as cp
img_rgb_blue = cp.deepcopy(img_rgb)
reddish = img_rgb[:, :, 0] >100#建立红色通道值大于100的二维布尔数组
img_rgb_blue[reddish,0] = [0]#根据筛选条件,筛选出并将红色通道赋值为0
reddish = img_rgb[:, :, 2] >100
img_rgb_blue[reddish,2] = [0]#根据筛选条件,筛选出并将蓝色通道赋值为0
reddish.shape
io.imshow(img_rgb_blue)

 图像的数据类型查看,float数据类型为0-1

img_rgb.dtype.name#查看图像的数据类型
img_gray.dtype.name
img_gray.max()

图像的数据类型转化:

from skimage import img_as_float,img_as_ubyte
arrays = img_as_ubyte(img_gray)
arrays.max()

使用color方法,完成图像的形式转化:

from skimage import color
io.imshow(color.rgb2gray(img_rgb))
io.imshow(color.convert_colorspace(img_rgb,'RGB','hsv'))

使用matplotlib中的pyplot下的imshow显示图像。

import matplotlib.pyplot as plt
dst = plt.imshow(img_rgb)#有一个AxesImage对象显示出来
plt.axis('off')
print(type(dst))

图像分割,设置标题,灰度图显示,不显示坐标的方法示例

from skimage import data
import matplotlib.pyplot as plt
img=data.astronaut()
plt.figure(num='astronaut',figsize=(8,8))  #创建一个名为astronaut的窗口,并设置大小 

plt.subplot(2,2,1)     #将窗口分为两行两列四个子图,则可显示四幅图片
plt.title('origin image')   #第一幅图片标题
plt.imshow(img)      #绘制第一幅图片

plt.subplot(2,2,2)     #第二个子图
plt.title('R channel')   #第二幅图片标题
plt.imshow(img[:,:,0],plt.cm.gray)      #绘制第二幅图片,且为灰度图
plt.axis('off')     #不显示坐标尺寸

plt.show()   #显示窗口

读取某目录下的所有图像的方法,多选图像格式和路径等

import skimage.io as io
from skimage import data_dir
str=data_dir + '/*.png'
str+=';'+data_dir + '/*.jpg'
coll = io.ImageCollection(str)
print(len(coll))
io.imshow(coll[0])

 io.ImageCollection()这个函数省略第二个参数,就是批量读取。将批量读取修改为其它批量操作,如批量转换为灰度图,修改图像大小

然后批量保存图像:

from skimage import data_dir,io,transform,color
import numpy as np
def convert_gray(f):
     rgb=io.imread(f)    #依次读取rgb图片
     gray=color.rgb2gray(rgb)   #将rgb图片转换成灰度图
     dst=transform.resize(gray,(256,256))  #将灰度图片大小转换为256*256
     return dst  
str=data_dir+'/*.png'
str+=';'+data_dir + '/*.jpg'
coll = io.ImageCollection(str,load_func=convert_gray)#修改默认批量操作为转化为灰度图
for i in range(len(coll)):
    io.imsave('./data/'+np.str(i)+'.jpg',coll[i])  #循环保存图片

猜你喜欢

转载自www.cnblogs.com/bai2018/p/10487016.html