Splitting image channels error: Index in position 3 exceeds array bounds. Index must not exceed 1.

1 次查看(过去 30 天)
Hello,
I'm trying to write a code to import a series of tiff images for analysis. These raw images are overlayed and I'm trying to split them into their respective cfp and yfp channels prior to analysis. I'm able to extract the cfp channel, but I'm getting an error when it comes to the yfp channel about the size of the array. I'm not sure what I'm doing wrong. I'm not a pro at MATLAB and haven't used it in a while so any help would be super appreciated. Here's a copy of my code below.
Thanks!
for k = 1 : length(fileopen)
baseFileName = fileopen(k).name;
fretfiles = fullfile(fileopen(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fretfiles);
imageArray = imread(fretfiles);
%imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end
for k = 1:length(fretfiles)
cfp(:,:,:,k) = imread(fretfiles(:,:,1,:));
yfp(:,:,:,k) = imread(fretfiles(:,:,2,:));
end
Error: Index in position 3 exceeds array bounds. Index must not exceed 1.

采纳的回答

Walter Roberson
Walter Roberson 2023-11-12
编辑:Walter Roberson 2023-11-13
for k = 1 : length(fileopen)
baseFileName = fileopen(k).name;
fretfiles = fullfile(fileopen(k).folder, baseFileName);
So every iteration, fretfiles gets overwritten with a single file name, in the form of a character vector such as ['s', 'k', '0', 'a', '.', 'p', 'n', 'g'] (more commonly known as 'sk0a.png')
At the end of the for k loop, fretfiles is going to hold (just) the last filename that was assigned to it
for k = 1:length(fretfiles)
fretfiles is, as explored, a single file name as a character vector. length() of it will be the number of characters in the character vector.
cfp(:,:,:,k) = imread(fretfiles(:,:,1,:));
fretfiles is a character vector. It is 1 x something, and as usual with MATLAB there are implicit trailing dimensions that are all 1. So it is also 1 x something x 1 x 1 . Indexing it as fretfiles(:,:,1,:) will work to give you back the character vector. You then imread() that character vector, and that might possibly give you something that can fit into cfp(:,:,:,k) ... maybe.
yfp(:,:,:,k) = imread(fretfiles(:,:,2,:));
fretfiles is a character vector. It is 1 x something, and as usual with MATLAB there are implicit trailing dimensions that are all 1. So it is also 1 x something x 1 x 1 . Indexing it as fretfiles(:,:,2,:) will fail because the third dimension of the character vector is scalar.
  3 个评论
Walter Roberson
Walter Roberson 2023-11-12
I would suggest, though, that you merge those two loops. You probably do not even need to store all of the images.
numfiles = length(fileopen);
for k = numfiles : -1 : 1 %backwards !!
baseFileName = fileopen(k).name;
fretfiles = fullfile(fileopen(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fretfiles);
thisimg = imread(fretfiles);
imshow(thisimg(:,:,1,1)); % Display image.
drawnow; % Force display to update immediately.
cfp(:,:,:,k) = thisimg(:,:,1,:);
yfp(:,:,:,k) = thisimg(:,:,2,:);
end
Looping backwards like I do here saves on trying to preallocate the cfp and yfp to the appropriate size when we do not know how large the images are.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by