欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

matlab学习总结--文件夹下的所有图片按顺序读取、处理和保存

程序员文章站 2024-03-24 18:23:58
...

matlab对文件夹下的所有图片按顺序读取、处理和保存

I0 = imread('F:\lystudy\matlabstudy\a图像分割获取运动区域\opticalflow\mask\固定mask\左右分离.jpg');
%使用dir将路径下.jpg格式的图片取出。img_dir为结构体类型
img_dir = dir('F:\lystudy\matlabstudy\a图像分割获取运动区域\opticalflow\static_video\3run\02\*.jpg');     
img_dir1=sort_nat({img_dir.name});      %img_dir.name取出文件名,sort_nat 文件名排序
[m n]=size(img_dir1); 
for i=1:n
    name=strcat('F:\lystudy\matlabstudy\a图像分割获取运动区域\opticalflow\static_video\3run\02\',img_dir1{1,i});
    I=imread(name);
    V = bitand(I0,I);
    %figure(1);imshow(V);title('第i帧按位与结果')            %避免显示过多,注释掉
    %循环保存图片,利用path和imwrite实现
    path =[ 'F:\lystudy\matlabstudy\a图像分割获取运动区域\opticalflow\static_video\3run\全部帧按位与结果\',num2str(i),'.jpg'];
    imwrite(V,path,'jpg');
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 2011
% 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 < 2
	mode = '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_str
	num_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, comp.
comp = 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_descend
	index = index(end:-1:1);
end
index = reshape(index,size(c));
cs = c(index);

处理完结果如下图
matlab学习总结--文件夹下的所有图片按顺序读取、处理和保存
references

https://blog.csdn.net/paoxungan5156/article/details/107006982?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-4.control

相关标签: 学习总结 matlab