histogram by not using the default imhist

I1 = imread('RDL.jpg');
raw = im2double(I1(:,:,1));
m=max(max(raw));
m1=min(min(raw));
binsize=(m-m1)/10;
for i=1:1:size(raw,1)
for j=1:1:size(raw,2)
value=raw(i,j); %read input image level
if value >= min(min(raw)) && value <= max(max(raw))
for i=1:1:10
if value <= i*binsize+min(min(raw))+ value > (i-1)*binsize+min(min(raw))
% original histogram in pixels
imhist(i)=imhist(i)+1;
% normalized histogram pdf
%InputIm_normalized_histogram(i)=InputIm_histogram(i)/resolution;
end
end
end
end
end
imhist(i)
I have used the code to plot a histogram from the image below. I want the graph similar to the attached image. But I am not able to get it. Kindly let me know where have I made the mistake.

3 Kommentare

Jan
Jan am 13 Mär. 2021
You forgot to mention, why you assume, that there is a mistake.
Vaswati Biswas
Vaswati Biswas am 13 Mär. 2021
Yeah I forgot to mention. Sorry for that. I was not getting any output graph from that. But no error is also shown
Jan
Jan am 13 Mär. 2021
imhist(i) is a scalar, because you have defined it as a vector. Your code does not contain a command to dispaly a graph.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 13 Mär. 2021
Bearbeitet: Jan am 13 Mär. 2021

0 Stimmen

Initially imhist is a command. Calling it as imhist(i) + 1 should cause an error. Do you get a corresponding message?
Use a different name and initialize your counter:
history = zeros(1, 10);
This line is meaningless:
if value >= min(min(raw)) && value <= max(max(raw))
because all values are >= the minimum and <= the maximum.
if value <= i*binsize+min(min(raw))+ value > (i-1)*binsize+min(min(raw))
% ^
Here you do not want a +, but an & to get a logical AND. You have evaluated min(min(raw)) already, so it is more efficient and easier to read, if you use m1 here.
A hint: "m" as maximum and "m1" as minimum are not easy to remember. maxV and minV might be better.
A leaner version of your code:
Img = imread('RDL.jpg');
R = im2double(Img(:,:,1)); % The red channel
maxR = max(max(R));
minR = min(min(R));
nBin = 10;
BinEdge = linspace(minR, maxR, nBin + 1);
BinEdge(end) = Inf; % any value > maxR to include it in last bin
hist = zeros(1, nBin);
for iBin = 1:nBin
hist(iBin) = sum(BinEdge(iBin) <= R(:) & R(:) < BinEdge(iBin + 1));
end
Instead of loops over the pixels, sum() is used to count the values of R matching into each bin.

6 Kommentare

Vaswati Biswas
Vaswati Biswas am 13 Mär. 2021
Thanks for your help but using this code also I am getting the same thing. No error is there still I am not getting any output. I want a graph like this:
Your code does not conatin any command to produce a graph. Which detail of the show graph is required? What about
bar(hist)
Vaswati Biswas
Vaswati Biswas am 14 Mär. 2021
Thanks. Now I am able to get the graph but the details that I want that is missing. I am not able to get how can I get that? Maybe I am missing something. My output should have a peak at the value of highest intensity. But I am not getting that
Image Analyst
Image Analyst am 14 Mär. 2021
The histogram has it's peak at the MODE (most common) intensity, not the highest intensity. You will not have a peak at the highest intensity unless the highest intensity is also the most common intensity.
Vaswati Biswas
Vaswati Biswas am 14 Mär. 2021
@Image Analyst Thanks for clearing my doubt. Yeah I was making a mistake.
Vaswati Biswas
Vaswati Biswas am 14 Mär. 2021
@Jan Thanks for your help it gives me the correct result now

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 14 Mär. 2021

1 Stimme

Then why not simply use histogram() or histcounts()? Why do you want to write (buggy) code yourself?

3 Kommentare

Vaswati Biswas
Vaswati Biswas am 14 Mär. 2021
Bearbeitet: Vaswati Biswas am 14 Mär. 2021
I was using imhist but it didn'y give me the result that I wanted. For example:the peak has a value at 2.2*10^4 in y axis. So my histogram should also have a peak in that value but using imhist I am not able to get it.
Image Analyst
Image Analyst am 14 Mär. 2021
That doesn't explain why you don't use the built-in histogram functions.
Vaswati Biswas
Vaswati Biswas am 15 Mär. 2021
Bearbeitet: Vaswati Biswas am 15 Mär. 2021
I didn't try built-in histogram function earlier. But later I checked that too it works fine. Thanks for your suggestion.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 13 Mär. 2021

Bearbeitet:

am 15 Mär. 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by