Compute Histogram of an image using loops
Show older comments
I'm trying to computer a histogram of an image by using loops. I cannot use the imhist function. How do I do this?
3 Comments
Matt J
on 22 Jan 2018
We can help you with your homework once we see an attempt on your part, and specific questions about where you're stuck.
Guillaume
on 22 Jan 2018
As per Matt's comment, we're not going to do your homework, particularly if you show no effort.
There's not even enough details to answer the question properly. What is the class of the image (double, uint8, uint16, all allowed?). How many bins for the histogram? Is the image colour or greyscale?
Note that the uint8, 256 bins, greyscale image is trivially done in four lines of code including the loop.
Davidson Bock
on 22 Jan 2018
Edited: Davidson Bock
on 22 Jan 2018
Answers (1)
Image Analyst
on 22 Jan 2018
Here's a hint. As you said, you need a loop (one or two depending on how you do it) So you'll have something like this for the 2 loop way:
[rows, columns, numberOfColorChannels] = size(yourImage);
% Preallocate histogram.
h = zeros(1, 256);
for row = 1 : rows
for col = 1 : columns
% Now get the gray level
grayLevel = yourImage(row, col); % Assumes gray scale image.
% Compute histogram, h. The index is the gray level for uint8 and the gray level/256 for uint16. Add 1 to the existing value.
if isa(class(yourImage), 'uint16'........
index = ........
else
end
h(index) = ............you finish........
end
end
I didn't do much other than make the for loops, which you already said you wanted to use. Now you should fill in the inside of the for loop to increment h every time you encounter a pixel of that gray level.
6 Comments
Davidson Bock
on 23 Jan 2018
Image Analyst
on 23 Jan 2018
Edited: Image Analyst
on 23 Jan 2018
No, that's NOT the way to do it with one loop. That inefficient way would require creating a new binary image and then summing it. If you had a megapixel image, that would require a million comparisons and a million sums per gray level. It's far easier to just scan the image and process a pixel at a time:
h = zeros(1, 256); % Assuming 8 bit image
for k = 1 : numel(grayImage)
thisGrayLevel = grayImage(k);
h(thisGrayLevel + 1) = you finish....
end
Also, don't call your array histogram since that's already the name of a built-in function that you do not want to destroy.
Davidson Bock
on 23 Jan 2018
Edited: Davidson Bock
on 23 Jan 2018
I'm guessing that Image Analyst's
h(thisGrayLevel + 1) = ...
was a mistake. The +1 goes on the right hand side of the = and is not added to thisGrayLevel
But frankly, just spending two minutes thinking how to build an histogram by scanning the pixels of an image, or just doing it manually would give you the algorithm. It's extremely simple.
Image Analyst
on 23 Jan 2018
Why do you think it was a mistake? For a gray level of zero, you can't have the zeroeth index, so 1 is added. The first element of h will hold the count for the number of pixels with gray level 0. There will also be a +1 on the right hand side of the equation.
Guillaume
on 24 Jan 2018
Do'h! Of course, you're right. It needs the graylevel+1.
The rest of my comment still stand.
Categories
Find more on Histograms in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!