python的数字图像处理学习(1),,导入原有的测试图片,
python的数字图像处理学习(1),,导入原有的测试图片,
导入原有的测试图片,测试图片路径,和一些方法,显示出测试图像,测试图像路径。
from skimage import io,data,data_dirimg_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 npimport copy as cpimg_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,:] = 255io.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 cpimg_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] = 0io.imshow(img_gray_binaryzation)
通过判断,提取出部分符合条件的数据,将符合条件的数据进行修改,,,一个示例
import copy as cpimg_rgb_blue = cp.deepcopy(img_rgb)reddish = img_rgb[:, :, 0] >100#建立红色通道值大于100的二维布尔数组img_rgb_blue[reddish,0] = [0]#根据筛选条件,筛选出并将红色通道赋值为0reddish = img_rgb[:, :, 2] >100img_rgb_blue[reddish,2] = [0]#根据筛选条件,筛选出并将蓝色通道赋值为0reddish.shapeio.imshow(img_rgb_blue)
图像的数据类型查看,float数据类型为0-1
img_rgb.dtype.name#查看图像的数据类型img_gray.dtype.nameimg_gray.max()
图像的数据类型转化:
from skimage import img_as_float,img_as_ubytearrays = img_as_ubyte(img_gray)arrays.max()
使用color方法,完成图像的形式转化:
from skimage import colorio.imshow(color.rgb2gray(img_rgb))
io.imshow(color.convert_colorspace(img_rgb,‘RGB‘,‘hsv‘))
使用matplotlib中的pyplot下的imshow显示图像。
import matplotlib.pyplot as pltdst = plt.imshow(img_rgb)#有一个AxesImage对象显示出来plt.axis(‘off‘)print(type(dst))
图像分割,设置标题,灰度图显示,不显示坐标的方法示例
from skimage import dataimport matplotlib.pyplot as pltimg=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 iofrom skimage import data_dirstr=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,colorimport numpy as npdef 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]) #循环保存图片
python的数字图像处理学习(1)
相关内容
- 推荐几本高质量的Python书籍--附github下载路径,github怎
- Python安装以及虚拟环境的使用,安装虚拟环境,作用虚拟
- Python绘制五星红旗,什么的五星红旗,# -*- codi
- 如何让你的Python代码更加pythonic ?,怎样让代码高亮显
- 小白学 Python(2):基础数据类型(上),,人生苦短,
- python语句结构(if判断语句),,一、python语句
- 三、python之文件的处理,python能处理什么文件,1、文件
- python 序列化,python序列化对象的函数,序列化 1、
- Python学习-range的用法,,range() 函数
- Python3 对文件操作,对文件,计算机文件在计算机系
评论关闭