在MATLAB

时间:2016-06-24 21:41:41

标签: matlab image-processing

这可能非常简单,但我一直在寻找适当的答案。

我正在为灵长类动物开展视觉任务,并在MATLAB中使用PsychToolbox。我目前要做的是随机化主题必须决定的照片。有许多代码示例可以从目录中随机选择图像,但我使用的图像已经导入,因为它们以前是使用过的。

这里参考的是我用来从计算机上的目录中随机导入图像的代码:

% Get the image files for the experiment
imageFolder = [cd '/ALSAMultiracial/'];
imgList = dir(fullfile(imageFolder, '*.jpg'));
numImages = length(imgList);

numTrials  = numImages;

% Generate a random number between 1 and the number of images
randomNumber = randperm(numImages, 1);

% Get corresponding name of the image 
randomImage = imgList(randomNumber).name;

% Now load the image
theImage = imread([imageFolder randomImage]);

% Make the image into a texture
tex = Screen('MakeTexture', window, theImage);

% Draw the texture
Screen('DrawTexture', window, tex, [], [], 0);

在这个任务中,我在两个图像之间切换:theImage和theImage2。在我这样做之后,我想选择一个随机图像来显示:theImage或theImage2。我在想的是我可以创建一个列表或数组并执行类似于我之前所做的事情,但问题是我没有成功地创建这些图像的列表和数组以重复该过程,但它有没工作。作为参考,所讨论的所有图像都是jpg并且尺寸相同。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果您只是尝试将整个图片目录加载到数组中,我建议在imgList上使用for循环将所有图像读入数组。这样,当您生成随机数时,您只需引用数组索引。

要记住这一点。当调用imread时,它会返回一个3D数组(红绿蓝值)所以当你读入多个图像时,你将会有一个4D数组。

您可以执行以下操作:

    clear pictureData;
    clear imgList;
    for imgList = dir(fullfile(imageFolder, '*.jpg'))
        pictureData(i, :, :, :) = imread([imageFolder imgList]);
    end

阅读所有照片。然后,当您获取一个随机数时,您可以使用

获取所需的特定jpeg数据
pictureData(randomNumber,:,:,:)

如果有明确与否,请告诉我,如果您有具体问题,或者我完全错过了商标,我可以尝试进一步解释。

欢迎使用Stack Overflow。 :)