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

3 ビュー (過去 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 Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by