300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Matlab按顺序读取文件夹内图片

Matlab按顺序读取文件夹内图片

时间:2019-07-20 11:14:17

相关推荐

Matlab按顺序读取文件夹内图片

问题描述

对于文件夹内命名为 1,2,3,10,11,21的图片;

matlab读取顺序为:1,10,11,2,21,3

我们希望的读取顺序为:1,2,3,10,11,21

在做图像处理时,我们希望按照固定顺序读取连续帧。针对此问题,需用到函数sort_nat,即可对文件夹内图片按自然顺序排序

本文使用方法:

按自然顺序读取图片,并将其依次重命名为000xx的形式,用于其他算法时即可按照正常顺序读取

主程序:

pt = 'E:\USPuncture_Project\Needle_detect\3_CT\CT_MATLAB_v0\CT_code_v0\1130\01_18G\';ext = '*.png';dis = dir([pt ext]);nms = sort_nat({dis.name});% 核心:排序for imgk = 1:length(nms)old_name = [pt nms{imgk}];%new_name = strcat(num2str(imgk),'.jpg'); new_name = sprintf('%05d.png',imgk);eval(['!rename' 32 old_name 32 new_name]);% windows下直接对文件重命名end

sort_nat函数:

function [cs,index] = sort_nat(c,mode)%sort_nat: Natural order sort of cell array of strings.% usage: [S,INDEX] = sort_nat(C)%% where,% C is a cell array (vector) of strings to be sorted.% S is C, sorted in natural order.% INDEX is the sort order such that S = C(INDEX);%% Natural order sorting sorts strings containing digits in a way such that% the numerical value of the digits is taken into account. It is% especially useful for sorting file names containing index numbers with% different numbers of digits. Often, people will use leading zeros to get% the right sort order, but with this function you don't have to do that.% For example, if C = {'file1.txt','file2.txt','file10.txt'}, a normal sort% will give you%% {'file1.txt' 'file10.txt' 'file2.txt'}%% whereas, sort_nat will give you%% {'file1.txt' 'file2.txt' 'file10.txt'}%% See also: sort% Version: 1.4, 22 January % Author: Douglas M. Schwarz% Email: dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu% Real_email = regexprep(Email,{'=','*'},{'@','.'})% Set default value for mode if necessary.if nargin < 2mode = 'ascend';end% Make sure mode is either 'ascend' or 'descend'.modes = strcmpi(mode,{'ascend','descend'});is_descend = modes(2);if ~any(modes)error('sort_nat:sortDirection',...'sorting direction must be ''ascend'' or ''descend''.')end% Replace runs of digits with '0'.c2 = regexprep(c,'\d+','0');% Compute char version of c2 and locations of zeros.s1 = char(c2);z = s1 == '0';% Extract the runs of digits and their start and end indices.[digruns,first,last] = regexp(c,'\d+','match','start','end');% Create matrix of numerical values of runs of digits and a matrix of the% number of digits in each run.num_str = length(c);max_len = size(s1,2);num_val = NaN(num_str,max_len);num_dig = NaN(num_str,max_len);for i = 1:num_strnum_val(i,z(i,:)) = sscanf(sprintf('%s ',digruns{i}{:}),'%f');num_dig(i,z(i,:)) = last{i} - first{i} + 1;end% Find columns that have at least one non-NaN. Make sure activecols is a% 1-by-n vector even if n = 0.activecols = reshape(find(~all(isnan(num_val))),1,[]);n = length(activecols);% Compute which columns in the composite matrix get the numbers.numcols = activecols + (1:2:2*n);% Compute which columns in the composite matrix get the number of digits.ndigcols = numcols + 1;% Compute which columns in the composite matrix get chars.charcols = true(1,max_len + 2*n);charcols(numcols) = false;charcols(ndigcols) = false;% Create and fill composite matrix, p = zeros(num_str,max_len + 2*n);comp(:,charcols) = double(s1);comp(:,numcols) = num_val(:,activecols);comp(:,ndigcols) = num_dig(:,activecols);% Sort rows of composite matrix and use index to sort c in ascending or% descending order, depending on mode.[unused,index] = sortrows(comp);if is_descendindex = index(end:-1:1);endindex = reshape(index,size(c));cs = c(index);

效果展示

顺序与原自然顺序相同

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。