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.
댓글 수: 0
채택된 답변
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
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 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!