find the histogram of the image(cameraman)without using the matlab built-in functions for histogram calculations.compare your result with the with those obtained using the functions imhist().display the input image,the two histograms-(yours and matla
Mostra commenti meno recenti
if true
% code
end
Risposta accettata
Più risposte (3)
Image Analyst
il 17 Mar 2017
The code you posted won't do it obviously. You need to use a for loop over all rows and all columns. The gray level will be the index of a "counts" array that you'll build up. Hint:
[rows, columns, numberOfColorChannels] = size(grayImage);
counts = zeros(1, 256);
for col = 1 : columns
for row = 1 : rows
7 Commenti
Marwa Ahmeid
il 18 Mar 2017
Image Analyst
il 18 Mar 2017
To plot, you can use bar() or plot().
Marwa Ahmeid
il 20 Mar 2017
Image Analyst
il 21 Mar 2017
Marwa, it looks like you figured out how to complete my code, though you changed the nice descriptive variable names I had to make it more cryptic and hard to read. This is how I would have completed it:
function [counts, grayLevels] = MyHistogram(grayImage)
[rows, columns, numberOfColorChannels] = size(grayImage);
counts = zeros(1, 256);
for col = 1 : columns
for row = 1 : rows
% Get the gray level.
grayLevel = grayImage(row, col);
% Add 1 because graylevel zero goes into index 1 and so on.
counts(grayLevel) = counts(grayLevel) + 1;
end
end
% Plot the histogram.
grayLevels = 0 : 255;
bar(grayLevels, counts, 'BarWidth', 1, 'FaceColor', 'b');
xlabel('Gray Level', 'FontSize', 20);
ylabel('Pixel Count', 'FontSize', 20);
title('Histogram', 'FontSize', 20);
grid on;
end
Marwa Ahmeid
il 23 Mar 2017
Modificato: Marwa Ahmeid
il 23 Mar 2017
Saravana Kumar Balakrishnan Janaki
il 9 Apr 2020
your code throws this error 'Array indices must be positive integers or logical values.'
Image Analyst
il 9 Apr 2020
Looks like I said to add 1 in the comment but didn't in the code. Thanks for catching that. Here is corrected code:
function [counts, grayLevels] = MyHistogram(grayImage)
[rows, columns, numberOfColorChannels] = size(grayImage);
counts = zeros(1, 256);
for col = 1 : columns
for row = 1 : rows
% Get the gray level.
grayLevel = grayImage(row, col);
% Add 1 because graylevel zero goes into index 1 and so on.
counts(grayLevel+ 1) = counts(grayLevel+1) + 1;
end
end
% Plot the histogram.
grayLevels = 0 : 255;
bar(grayLevels, counts, 'BarWidth', 1, 'FaceColor', 'b');
xlabel('Gray Level', 'FontSize', 20);
ylabel('Pixel Count', 'FontSize', 20);
title('Histogram', 'FontSize', 20);
grid on;
end
T G
il 11 Dic 2018
0 voti
Thank u so much Image Analyst for helping..
Ankan Banerjee
il 19 Set 2021
0 voti
Can histogram be calculated using find() command?
1 Commento
Image Analyst
il 19 Set 2021
Yes, if you used a loop to find all the values that fell in a certain range and then sum them up into the appropriate bin, but why would you want to do it that more complicated way?
Categorie
Scopri di più su Histograms in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!