[Solved] Hi, can anyone tell me code how to create histogram without the toolbox (imhist,hist) from matlab?


A bit more efficient than your code:

histogram = zeros(1,256);
for value = 1:256 % loop through possible values
    histogram(value) = histogram(value) + length(find(F(:)==value));
end

Note that this code makes a histogram for values from 1 to 256. For grayscale images you might need one from 0 to 255. But I’ll let you change this on your own for matters of practice.

edit:
Since you asked for a revision of your code, let me comment on it:

 [m n]=size(F);
 x=0;

No need to set x to zero, you are overwriting it later.

 H=0;

Instead of setting H to zero you should initialize it as an array, since you want to fill H(x) later. Something like H = zeros(1,256);

for z=1:256

I’m not sure what this loop over z is supposed to do. You never use z. You can remove it.

for i=1:m
    for j=1:n       
        x==F(i,j);

As I said below, this should be x=F(i,j); instead, as == is a test for equality.

        H(x+1)=H(x+1)+1

This works if all values in F are guaranteed to be between 0 and 255. Though I would say it’s not very good style.

Also, put a semicolon at the end of this line to suppress the outputs, otherwise your command line gets flooded (which is also slowing down things a lot).

    end
end
H;

The last line does nothing. Why is it there?

end

The outer loop over z is not needed (see above).

So, here is a “sanitized” version of your code:

[m,n] = size(F);
H = zeros(1,256);
for i=1:m
    for j=1:n
        x=F(i,j);
        H(x+1)=H(x+1)+1;
    end
end

Hope this helps.

4

solved Hi, can anyone tell me code how to create histogram without the toolbox (imhist,hist) from matlab?