How to find the histogram of multiple images loaded from a single folder
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Hi, I am using matlab R2010a. I have loaded 12 images from a folder . I am also able to display the images. But i find it difficult to further process the images. I have stored the images in a cell. How to find the histogram of the images ?
Accepted Answer
Image Analyst
on 19 Jan 2014
My attached demo (below in blue text) does exactly that, and produces nice figures for every image in the folder:

10 Comments
Monisha
on 19 Jan 2014
Thanks . The histogram is nicely shown .But i am working with grayscale images. Can the histogram be plotted in the same manner for a thresholded image also?
Image Analyst
on 19 Jan 2014
Of course. But a thresholded image will have only two values, 0 and 1, in the histogram.
I need to further process the images. I.e Getting the variance , entropy,relative smoothness of all the images. But when i do so, i dont get the values in the command window.
Here is the code:
for k = 1 : numberOfImagesProcessed
baseFileName = imageFiles(k).name;
[pixelCounts GLs] = imhist(redChannel, 256);
numberOfPixels(k) = sum(pixelCounts);
meanGL(k) = sum(GLs(k) .* pixelCounts) / numberOfPixels;
% Get the variance, which is the second central moment.
% variational element of the histogram of the image
varianceGL(k) = sum((GLs - meanGL) .^ 2 .* pixelCounts) / (numberOfPixels(k)-1);
% Get the standard deviation.
sd(k) = sqrt(varianceGL(k));
% Get the skew.- level of relative inclination of the image
% histogram
skew(k) = sum((GLs(k) - meanGL(k)) .^ 3 .* pixelCounts) / ((numberOfPixels(k) - 1) * sd(k)^3);
% Get the kurtosis. -
kurtosis(k) = sum((GLs(k) - meanGL(k)) .^ 4 .* pixelCounts) / ((numberOfPixels(k) - 1) * sd(k)^4);
%Get the relative smoothness
rs(k)=1-(1/(1+sd(k).^2));
fprintf(1, '%24s %6.2f, %6.2f, %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f \n', ...
baseFileName, redChannel_Mean(k), greenChannel_Mean(k), blueChannel_Mean(k),redChannel_Entropy(k),meanGL(k),varianceGL(k),sd(k),skew(k),kurtosis(k),rs(k));
Image Analyst
on 21 Jan 2014
Edited: Image Analyst
on 21 Jan 2014
Are you saying that fprintf() is not working? Step through it with the debugger. What happens when you hit fprintf()? Nothing at all???
Does it even get into the loop? What is the value of numberOfImagesProcessed? Note that my code had numberOfImagesToProcess in the for statement, not numberOfImagesProcessed like you changed it to. numberOfImagesProcessed is probably zero so it won't enter the loop. Why did you change it?
Monisha
on 21 Jan 2014
No its not with fprintf(). And also i have changed it to numberOfImagesToProcess from numberOfImagesProcessed .But still i get an error that "Matrix Dimensions must agree".
Image Analyst
on 21 Jan 2014
Edited: Image Analyst
on 21 Jan 2014
When you step through with the debugger and get to the line where it errors, what are the matrix dimensions of all matrices on that line? Please give ALL THE RED TEXT, not just a small snippet from it.
Monisha
on 22 Jan 2014
I have attached the m file of it. The error is shown on the following line: [pixelCounts GLs] = imhist(redChannel, 256);
Image Analyst
on 23 Jan 2014
You have probably figured it out by now, but the code you added after mine is not correct. Especially this line:
[pixelCounts GLs] = imhist(redChannel, 256);
You're never changing redChannel so it is the same at each iteration of your loop, which means pixelCounts is always the same, standard deviation is always the same, and so on.
Monisha
on 24 Jan 2014
Ya. So how can i find the pixelcount and grayscale value for every image in the loop. Should i change the loop condition?
Image Analyst
on 24 Jan 2014
Well, I do that, in my code don't I? You can see the histograms (pixel counts) of every color channel in every image. Just look what I did.
More Answers (0)
Categories
Find more on Histograms in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)