skimage图像处理(五)

多数量图像处理(批量化处理)

多数量图片内读取指定图片

首先导入skimage.io模块并将其简称为io,导入skimage库中的data_dir模块,用于获取示例图像的目录路径

import skimage.io as io

from skimage import data_dir

构建一个字符串,表示要匹配的文件模式,这里假设data_dir包含示例图像

使用io.ImageCollection创建一个集合,包含data_dir目录下所有匹配str模式的图像

str=data_dir + '/*.png'

coll = io.ImageCollection(str)

打印集合中图像的数量,使用io.imshow显示集合中第三个图像

print(len(coll))

io.imshow(coll[2]) #集合中第一张图片为0

显示效果:

批量读取目录中的所有.jpg文件

批量读取目录中的所有.jpg文件,并将它们转换为灰度图像,使用io.imshow显示集合中第一个图像。

导入skimage.io模块,用于图像读取,导入skimage库中的color模块,用于颜色处

from skimage import data_dir, io, color

定义一个函数convert_gray,用于将图像转换为灰度图,使用io.imread读取图像,使用color.rgb2gray将RGB图像转换为灰度图像

def convert_gray(f):

rgb = io.imread(f)

return color.rgb2gray(rgb)

定义一个字符串,表示要匹配的文件模式,这里假设img包含图像文件路径

img = r"C:\Users\AAA\Desktop\新建文件夹"

使用io.ImageCollection创建一个集合,包含img目录下所有匹配str模式的图像,打印集合中图像的数量

str = img + '/*.jpg'

coll = io.ImageCollection(str, load_func=convert_gray)

print(len(coll))

使用io.imshow显示集合中第一个图像

io.imshow(coll[0])

显示效果:

图像批量处理在视频中引用

将SP.mp4这个视频中每隔10帧的图片读取出来,放在图片集合中(视频应用自己的视频时请改变路径其余代码不变)

导入cv2库用于视频处理,skimage.io用于图像读取,os库用于文件操作

import cv2

from skimage import io

import os

定义了一个名为AVILoader的类,该类用于从视频文件中加载帧,并将其转换为RGB格式。这个类实现了__call__方法,使其可以作为函数调用。

class AVILoader:

def init(self, video_file):

self.video_file = video_file

self.cap = cv2.VideoCapture(self.video_file)

def call(self, frame):

self.cap.set(cv2.CAP_PROP_POS_FRAMES, frame)

ret, frame = self.cap.read()

if ret:

return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

else:

return None

定义视频文件路径,创建一个AVILoader对象

video_file =r"C:\Users\AAA\Desktop\1\SP.mp4"

av_loader = AVILoader(video_file)

定义要提取的帧的范围,这里从0开始,每3帧提取一次,共提取4帧

frames = range(0, 12, 3)

定义输出文件夹路径

output_folder = 'frames'

创建输出文件夹,如果已存在则不报错

os.makedirs(output_folder, exist_ok=True)

保存每一帧为图像文件

for frame in frames:

img = av_loader(frame)

if img is not None:

filename = os.path.join(output_folder, f'frame_{frame}.jpg')

io.imsave(filename, img)

io.imshow(img) # 显示图像

io.show() # 显示图像窗口

创建图像集合

ic = io.ImageCollection(os.path.join(output_folder, '*.jpg'))

import cv2

from skimage import io

import os

创建AVILoader类

class AVILoader:

def init(self, video_file):

self.video_file = video_file

self.cap = cv2.VideoCapture(self.video_file)

def call(self, frame):

self.cap.set(cv2.CAP_PROP_POS_FRAMES, frame)

ret, frame = self.cap.read()

if ret:

return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

else:

return None

video_file =r"C:\Users\AAA\Desktop\1\SP.mp4"

av_loader = AVILoader(video_file)

frames = range(0, 12, 3)

output_folder = 'frames'

os.makedirs(output_folder, exist_ok=True)

#保存每一帧为图像文件

for frame in frames:

img = av_loader(frame)

if img is not None:

filename = os.path.join(output_folder, f'frame_{frame}.jpg')

io.imsave(filename, img)

io.imshow(img) # 显示图像

io.show() # 显示图像窗口

创建图像集合

ic = io.ImageCollection(os.path.join(output_folder, '*.jpg'))

显示效果:(可上下滑动显示)

依次读取rgb图片并转换为灰度图,将jpg形式转换为png形式保存在文件夹内

最后保存显示为所取图片的灰度化png形式图像

from skimage import data_dir,io,transform,color

import numpy as np

def convert_grey(f):

rgb=io.imread(f) #依次读取rgb图片

grey=color.rgb2grey(rgb) #将rgb图片转换成灰度图

dst=transform.resize(grey,(256,256)) #将灰度图片大小转换为256*256

return dst

img='C:/Users/AAA/Desktop/1'

str=img+'/*.jpg'

coll = io.ImageCollection(str,load_func=convert_gray)

for i in range(len(coll)):

循环保存图片

io.imsave('C:/Users/AAA/Desktop/1/'+np.str(i)+'.png',coll[i])

效果显示:

出现这种红色提示为正常现象

最后文件夹内显示效果如下

图像变形与缩放

改变图像尺寸

改变新图像尺寸为resize(200,200),函数格式如下skimage.transform.resize(image, output_shape)

image为需要改变尺寸的图片,output_shape为新的图片尺寸

导入transform模块用于图像大小调整,以及matplotlib的pyplot模块用于图像显示

from skimage import transform

import matplotlib.pyplot as plt

读取图像,使用transform.resize调整图像大小为200x200像素,创建一个新的图像窗口,命名为'resize'

img =io.imread('c.jpg')

dst=transform.resize(img, (200, 200))

plt.figure('resize')

在当前图像窗口中创建一个新的子图,位于1行2列的第1个位置,在当前子图上添加标题'before resize',使用imshow显示图像img,并使用plt.cm.gray灰度

plt.subplot(1,2,1)

plt.title('before resize')

plt.imshow(img,plt.cm.gray)

在当前图像窗口中创建一个新的子图,位于1行2列的第2个位置,在当前子图上添加标题'before resize',使用imshow显示图像img,并使用plt.cm.gray灰度

plt.subplot(1,2,2)

plt.title('before resize')

plt.imshow(dst,plt.cm.gray)

plt.show()

显示效果:

按比例缩放图像

from skimage import transform,data,io

img =io.imread('c.jpg')

print(img.shape) #图片原始大小

print(transform.rescale(img, 0.5).shape) #缩小为原来图片大小的0.5倍

print(transform.rescale(img, [0.2,0.25,0.2]).shape) #缩小为原来图片行数一半,列数四分之一,位置一半

print(transform.rescale(img, 3).shape) #放大为原来图片大小的3倍

io.imshow(img) #显示图像

显示效果:

图像旋转

函数格式skimage.transform.rotate(image, angle[, ...],resize=False)

from skimage import transform,data,io

import matplotlib.pyplot as plt

img = io.imread('c.jpg')

print(img.shape) #图片原始大小

img1=transform.rotate(img, 60) #旋转60度,不改变大小

print(img1.shape)

img2=transform.rotate(img, 30,resize=True) #旋转30度,同时改变大小

print(img2.shape)

plt.figure('resize')

plt.subplot(1,2,1)

plt.title('rotate 60')

plt.imshow(img1,plt.cm.gray)

plt.subplot(1,2,2)

plt.title('rotate 30')

plt.imshow(img2,plt.cm.gray)

plt.show()

显示效果:

相关推荐
jndingxin1 天前
OpenCV图像注册模块
人工智能·opencv·计算机视觉
R-G-B1 天前
【P14 3-6 】OpenCV Python——视频加载、摄像头调用、视频基本信息获取(宽、高、帧率、总帧数)
python·opencv·视频加载·摄像头调用·获取视频基本信息·获取视频帧率·获取视频帧数
荼蘼1 天前
OpenCv(三)——图像平滑处理
人工智能·opencv·计算机视觉
Monkey PilotX1 天前
机器人“ChatGPT 时刻”倒计时
人工智能·机器学习·计算机视觉·自动驾驶
程序猿小D2 天前
【完整源码+数据集+部署教程】孔洞检测系统源码和数据集:改进yolo11-RetBlock
yolo·计算机视觉·毕业设计·数据集·yolo11·孔洞检测
图灵学术计算机论文辅导2 天前
傅里叶变换+attention机制,深耕深度学习领域
人工智能·python·深度学习·计算机网络·考研·机器学习·计算机视觉
R-G-B2 天前
OpenCV Python——报错AttributeError: module ‘cv2‘ has no attribute ‘bgsegm‘,解决办法
人工智能·python·opencv·opencv python·attributeerror·module ‘cv2‘·no attribute
Struart_R2 天前
SpatialVLM和SpatialRGPT论文解读
计算机视觉·语言模型·transformer·大语言模型·vlm·视觉理解·空间推理
似乎很简单2 天前
【opencv-Python学习笔记(5):几何变换】
笔记·opencv·学习
老艾的AI世界2 天前
AI去、穿、换装软件下载,无内容限制,偷偷收藏
图像处理·人工智能·深度学习·神经网络·目标检测·机器学习·ai·换装·虚拟试衣·ai换装·一键换装