如何在Matlab中按顺序处理图像 [英] How to process images in order in matlab

查看:939
本文介绍了如何在Matlab中按顺序处理图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含约1000张图像的目录.在我的matlab代码中,我必须按名称顺序处理这些图像.(img1.jpg,img2.jpg等).有没有一种方法可以直接从图像中读取图像,以便先处理img1.jpg,然后再处理img2.jpg,依此类推?预先谢谢你.

I have a directory that has about 1000 images. In my matlab code, I have to process these images in order by names.( img1.jpg , img2.jpg and so on). Is there a way to read from the images directrory in order so img1.jpg will be processed, then img2.jpg and so on? Thank you in advance.

imgFilesDir =dir('pics/*.jpg');

for n=1:length(imgFilesDir)
%read Target image and convert into single
   rgb2= im2single(imread(strcat('pics/',imgFilesDir(n).name)));
   I2 = rgb2gray(rgb2);
end

推荐答案

用正则表达式提取数字部分,从字符串转换为数字,进行数字排序,然后将该顺序应用于您的字符串:

Extract the numerical part with a regular expression, convert from strings to numbers, sort numerically, and apply that order to your strings:

imgFilesDirName = {imgFilesDir.name};
[~, ind] = sort(cellfun(@str2double, regexp(imgFilesDirName,'\d+(?=\..*)','match')));
imgFilesDirSorted = imgFilesDir(ind); %// sorted struct
imgFilesDirNameSorted = imgFilesDirName(ind); %// sorted names

假定数字部分是文件扩展名前的一位或多位数字.

The numerical part is assumed to be one or more digits right before the file extension.

例如,给定

imgFilesDirName = {'imag1.jpg', 'imag10.jpg', 'imag11.jpg', 'img2.jpg', 'img20.jpg'};

你得到

imgFilesDirNameSorted = 
    'imag1.jpg'    'img2.jpg'    'imag10.jpg'    'imag11.jpg'    'img20.jpg'

这篇关于如何在Matlab中按顺序处理图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆