首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在matlab中按顺序处理图像

如何在matlab中按顺序处理图像
EN

Stack Overflow用户
提问于 2015-03-18 06:18:13
回答 3查看 293关注 0票数 1

我有一个目录,里面有大约1000张图片。在我的matlab代码中,我必须按名称处理这些图像( img1.jpg,img2.jpg等等)。是否有一种方法可以按顺序读取图像的指向性,以便处理img1.jpg,然后处理img2.jpg等等?提前谢谢你。

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-03-18 09:53:03

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

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

所述数字部分假定为文件扩展名之前的一个或多个数字。

例如,给定

代码语言:javascript
复制
imgFilesDirName = {'imag1.jpg', 'imag10.jpg', 'imag11.jpg', 'img2.jpg', 'img20.jpg'};

你会得到

代码语言:javascript
复制
imgFilesDirNameSorted = 
    'imag1.jpg'    'img2.jpg'    'imag10.jpg'    'imag11.jpg'    'img20.jpg'
票数 2
EN

Stack Overflow用户

发布于 2015-03-18 08:50:46

在这种情况下,您可以使用[~, intVecSort] = sort(arrayfun(@(x) x.name, r, 'Uniformoutput', false));

按日期排序甚至更短一些,[~, intVecSort] = sort(arrayfun(@(x) x.name, r);

如果您希望使用img3作为第三个名称,则此操作names = arrayfun(@(x) x.name, r, 'Uniformoutput', false); names(:, 2) = cellfun(@(x) {length(x)}, names(:, 1)); [~, intVecSort] = sortrows(names, [2, 1]);

票数 1
EN

Stack Overflow用户

发布于 2016-04-06 09:06:44

文件交换提交也能派上用场-

代码语言:javascript
复制
sort_nat

它可以获得文件名的输入字符串,并按自然顺序排序。

代码语言:javascript
复制
>> imgFilesDirName = {'imag1.jpg', 'imag10.jpg', 'imag11.jpg', 'img2.jpg', 'img20.jpg'};
>> sort_nat(imgFilesDirName)

ans = 

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

免责声明:我直接从Matlab文件交换中复制了文本,它绝不是我的。

代码语言:javascript
复制
function [cs,index] = sort_nat(c,mode)
    % 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);
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29115354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档