How to use bar() function in MATLAB to display histogram count as a bar graph?

We have an original image which I took it from my mobile device, so I used the subplot function to change the color image to 5 images with different brightness(already converted the color image to a gray-level image), and I need to use to bar function to make each gray-level image has their own histogram, so I will get 5 histograms with different brightness level gray-level image, but I don't know how to create a different histogram with the bar function and imhist function, anyone would like to contribute your ideas? thank you.

Answers (2)

Try this
edge = 0:256;
% Image 1
[counts, grayLevels] = histcounts(image1, edges);
subplot(2, 3, 1);
bar(grayLevels(1:end-1), counts);
title('Histogram of Image1');
% Image 2
[counts, grayLevels] = histcounts(image2, edges);
subplot(2, 3, 2);
bar(grayLevels(1:end-1), counts);
title('Histogram of Image2');
Repeat for images 3-5, changing the last argument in subplot to 3, 4, and 5.
Is there a particular reason we're avoiding imhist()?
inpict = imread('cameraman.tif');
% you could use histcounts() and bar()
subplot(3, 1, 1);
edges = 0:256;
[counts, grayLevels] = histcounts(inpict, edges);
bar(grayLevels(1:end-1), counts);
% or you could use imhist() and bar()
subplot(3, 1, 2);
[counts edges] = imhist(inpict);
bar(edges, counts);
% or you could just use imhist()
subplot(3, 1, 3);
imhist(inpict);

Products

Release

R2022b

Asked:

on 25 Feb 2023

Edited:

on 26 Feb 2023

Community Treasure Hunt

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

Start Hunting!