I wrote a function for constructing histogram for an arbitrary picture. It reads only R (red) and G(green) channel.

Code:

function H = myhistogram2RG(img1, bins)
%Here you make eqaul distribution for the pixels
img1 = floor(double(img1)*(bins) / 255 ) + 1 ;
%Matrice bins * bins all of zeroes
H = zeros(bins, bins);
for i=1:size(img1, 1)
for j = 1:size(img1, 2)
R = img1(i,j,1);
G = img1(i,j,2);
%When the pixel is the right colour add one to corresponding
%element in the matrice
H(R,G) = H(R,G) + 1;
end
end
%Normalization
H = H / sum(sum(H));
end

When I read a picture with imread command, and I call a function with myhistogram2RG(image,10) I get an error.

Code:

Attempted to access H(11,8); index out of bounds because
size(H)=[10,10].

I don't understand why isn't working. Thank you for your help.