【问题标题】:MATLAB using 2D ArrayMATLAB 使用二维数组
【发布时间】:2013-09-22 03:07:10
【问题描述】:

我想用一个二维数组来存储img1、img2的所有值以及img1和img2的比较值, 我想实现喜欢的算法:

% read in the images from a folder one by one:
    somefolder = 'folder';
    filelist = dir([somefolder '/*.jpg']);
    s=numel(filelist);
    C = cell(length(filelist), 1);
    for k=1:s
       C{k}=imread([somefolder filelist(k).name]); 
    end
%choose any of the two images to compare
    for t=1:(s-1)
        for r=(t+1):s
           img1=C{r};
           img2=C{t};
           ssim_value[num][1]=img1;   % first img
           ssim_value[num][2]=img2;   % second img
           ssim_value[num][3]=mssim;  % ssim value of these two images

        end 
    end

所以,我使用的二维数组(ssim_value)有错误,初始化它的正确方法是什么,如何达到保存我要存储的值的目的。

谁能帮帮我。提前致谢。

【问题讨论】:

  • 你有python背景吗?试试不带方括号的ssim_value。尝试类似:ssim_value{num,1}
  • 谢谢,我试过你的方法,应该可以了,我需要加载整个文件夹的图片看看效果如何,tks。
  • 使用fullfile 命令为文件/文件夹名称创建字符串:fullfile( somefolder, '*.jpg' ) 优于[somefolder '/*.jpg']

标签: arrays matlab image-processing


【解决方案1】:

我假设“num”是您将提供的数字,例如 5 或其他数字。不能像在 Python 中那样在数组中混合类型。此外,正如@Schorsch 指出的那样,您在 Matlab 中使用括号来索引数组。

您尝试形成的二维数组必须是二维元胞数组。例如:

a = {{"a",3},{"two",[1,2,3]};

在这种情况下,a{1,2} = 3,a{2,1} = “二”。

您可能事先不知道目录中有多少文件,因此可能无法提前对元胞数组进行预初始化。在任何情况下,Matlab 数组只需要出于性能原因进行预初始化,您可以在 Matlab 中轻松找到有关初始化数组的信息。

鉴于此,我很确定您想要完成的是:

%choose any of the two images to compare
    ssim_value = {};
    for t=1:(s-1)
        for r=(t+1):s
           img1=C{r};
           img2=C{t};
           ssim_value{num,1}=img1;   % first img
           ssim_value{num,2}=img2;   % second img
           ssim_value{num,3}=mssim;  % ssim value of these two images

        end 
    end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多