-1

I am writing a simple code in matlab which has the purpose of creating the histogram of a grayscale image without using the function hist. I am stuck at the point in which mathlab displays the error "Subscript indices must either be real positive integers or logicals." Can you help me finding where is the wrong indices?

indirizzo='file.jpg';
immagine=imread(indirizzo);
immaginebn=rgb2gray(immagine);
n=zerps(0,255);
    for x=0:255;
        numeroennesimo=sum(sum(immaginebn==x));
        n(x)=numeroennesimo;
    end
plot(x,n)
Fabio96
  • 1
  • 1

1 Answers1

0

you cant use 0 as index. Either make n(x+1) or for x = 1:256 and substract the 1 in your comparison. And there is a typo, I guess it means zeros instead of zerps, which also doesnt work with a 0. And one more, your plot will also not work as the x has only a size of 1 while n is an array of 266 and for a histogram I would use a barplot instead.

indirizzo='file.jpg';
immagine=imread(indirizzo);
immaginebn=rgb2gray(immagine);
n=zeros(1,256);
    for x=0:255;
        numeroennesimo=sum(sum(immaginebn==x-1));
        n(x+1)=numeroennesimo;
    end
bar(0:255,n)

or

indirizzo='file.jpg';
immagine=imread(indirizzo);
immaginebn=rgb2gray(immagine);
n=zeros(1,256);
xplot=zeros(1,256);
    for x=1:256;
        numeroennesimo=sum(sum(immaginebn==x-1));
        n(x)=numeroennesimo;
        xplot(x) = x-1;
    end
plot(xplot,n)
horseshoe
  • 1,437
  • 14
  • 42