Compute Histogram of an image using loops

8 ビュー (過去 30 日間)
Davidson Bock
Davidson Bock 2018 年 1 月 22 日
コメント済み: Guillaume 2018 年 1 月 24 日
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 件のコメント
Guillaume
Guillaume 2018 年 1 月 22 日
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
Davidson Bock 2018 年 1 月 22 日
編集済み: Davidson Bock 2018 年 1 月 22 日
I'm very new to matlab, just started it. The image is supposed to be of type int16 of size 256 x 1. I've done the other 4 part for this homework. I am supposed to use a for loop?
This is proof of my attempt on the homework by completing the other questions

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2018 年 1 月 22 日
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 件のコメント
Image Analyst
Image Analyst 2018 年 1 月 23 日
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
Guillaume 2018 年 1 月 24 日
Do'h! Of course, you're right. It needs the graylevel+1.
The rest of my comment still stand.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by