"Index exceeds matrix dimensions" error during performing RGB channel separation

조회 수: 1 (최근 30일)
The purpose of my code is -
  1. scan all image from a folder
  2. calculate entropy for each of RGB channel
  3. export data to a xlsx file
my code :
folder = 'F:\images\';
filePattern = fullfile(folder, '*.png');
myFiles = dir(filePattern); % the folder inwhich ur images exists
for k = 1 : length(myFiles)
fullFileName = fullfile(folder, myFiles(k).name);
I= fullFileName;
Red = I(:,:,1);
Green = I(:,:,2);
Blue = I(:,:,3);
%I = I(:); % Vectorization of RGB values
p = imhist(Red); % Histogram
p(p == 0) = [ ];% remove zero entries in p
p = p ./ numel(I); % normalize p so that sum(p) is one.
Er(k) = round(-sum(p.*log2(p)),3);
p = imhist(Blue); % Histogram
p(p == 0) = [ ];% remove zero entries in p
p = p ./ numel(I); % normalize p so that sum(p) is one.
Eb(k) = round(-sum(p.*log2(p)),3);
xlswrite('myfile.xlsx', [Er(k),Eb(k)], 'Sheet 1');
end
But i am getting following error:
Index exceeds matrix dimensions.
Error in test1 (line 9)
Green = I(:,:,2);
I couldn't solve this problem. Can anyone help me please?

채택된 답변

Simon Chan
Simon Chan 2021년 7월 25일
According to your code shown above, variable I is only a file name in the beginning of the for loop.
Have you do imread or some other command to read the images?
for k = 1 : length(myFiles)
fullFileName = fullfile(folder, myFiles(k).name);
I= imread(fullFileName); % I added imread
Red = I(:,:,1);
Green = I(:,:,2);
Blue = I(:,:,3);
  댓글 수: 4
rakib mostafiz
rakib mostafiz 2021년 7월 25일
All of the data are placed side by side column. Can it be done by two column only?
Simon Chan
Simon Chan 2021년 7월 25일
Doing transpose:
xlswrite('myfile.xlsx', [Er',Eb'], 'Sheet 1');
or
writematrix([Er',Eb'],'myfile.xlsx');

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Yongjian Feng
Yongjian Feng 2021년 7월 24일
Most likely your image is just a grey scale one. Only one channel.
  댓글 수: 1
rakib mostafiz
rakib mostafiz 2021년 7월 24일
Those image are colorful. Moreover, if i perform this operation on a single image rather than looping out all image from folder, it works just fine! But when i try to read each image and perform this operation, it showing that error. I really dont understand the problem here

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by