0
 clc;
rgb = imread('apple.jpg');
rgb = imresize(rgb,[200,200]);
mlab = makecform('srgb2lab');
lab = applycform(rgb,mlab);
k=1;
 for i=1:k*3:200
     for j=1:k*3:200
       m = region(i,j,lab);
        [a,b,c] = size(m);
         m1 = region(1,1,m);
         m2 = region((a-3),1,m);
         m3 = region(1,(b-3),m);
         m4 = region((a-3),(b-3),m);
         Amean = (m1(:,:,2) + m2(:,:,2) + m3(:,:,2) + m4(:,:,2))/4;
         Bmean = (m1(:,:,3) + m2(:,:,3) + m3(:,:,3) + m4(:,:,3))/4;
         Lmean = (m1(:,:,1) + m2(:,:,1) + m3(:,:,1) + m4(:,:,1))/4;
 end;
end

in above code i am facing problem of index exceeds matrix value while i am calculating Amean and Bmean value in between for loop.

but when i am calculating only Lmean then it's run perfectly.

So please give me solution of this problem.

region.m is

function [median,c] = region(a,b,IM)
IM = imresize(IM,[200,200]);
        c1 = imcrop(IM,[a b 3 3]);
        m1 = mean(mean(c1));
        median = m1;
        c = c1;

end
hardik
  • 378
  • 5
  • 18
  • This is occurring because `m1` is the only one out of those four variables that has 3 dimensions. `m2`, `m3`, and `m4` are all constants, so trying to get those indices from them results in an error. All of this seems to stem from the `region` function. I have no idea what it does, but it returns `m` as a `1x1x3` matrix. Therefore, a=1, b=1, and c=3. Therefore, in `m2`, for example, `a-3` results in a negative number, which causes `m2` to be constant. – needarubberduck Aug 06 '15 at 05:03
  • 1
    Pretty sure this is happening because `m1` is a greyscale image (i.e. not RGB). In Matlab RGB images are 3D matrices whereas greyscale images are 2D. Access the first "slice" of the third dimension in a 2D image is allowed by matlab (i.e. `m1(:,:,1)` is fine) but you can't access any other slices (e.g. `m1(:,:,2)`) because they don't exist. – Dan Aug 06 '15 at 06:20
  • thanks for your answer it was very help full to me....thank you again – hardik Aug 06 '15 at 19:11

0 Answers0