-8

can anyone tell me how to create histogram from an grayscale image without the function from the matlab.

Thanks

  • 1
    ...and you have tried ? – Moudiz Jan 12 '17 at 12:12
  • 1
    Can you show us what you tried ? – Thomas Rollet Jan 12 '17 at 12:14
  • 5
    Hello and welcome to StackOverflow! Stackoverflow is not a free code writing service, nor a free debugging service. We help *you* write good code. What that means is you need to make an attempt, show us the code and ask a specific question about that code that will help you progress it. Please read [how and what kind of questions](http://stackoverflow.com/help/how-to-ask) you can ask here, and how to write a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). – Rody Oldenhuis Jan 12 '17 at 12:44
  • You don't need a toolbox to make a histogram – sco1 Jan 12 '17 at 13:20
  • [m n]=size(F); x=0; H=0; for z=1:256 for i=1:m for j=1:n x==F(i,j); H(x+1)=H(x+1)+1 end end H; end bar(H) – Candra Dewa Jan 12 '17 at 13:54
  • i try that way.. and got nothing.. – Candra Dewa Jan 12 '17 at 13:54

1 Answers1

0

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.

Florian
  • 1,804
  • 2
  • 9
  • 19