How can I measure the area covered by a Bar Chart Plot?

I want to calculate the area covered by this histogram plot (considering a boundary all over the figure and calculating the whole area) . How can I do that?

댓글 수: 3

What do you have to work with?
  • All the data that went into making the histogram?
  • The positions and heights of the bars?
  • Only the image itself?
I need to consider the whole bar chart as a complete area and calculate the percentage of pixel they hold compared to the background, filling the spaces in between. Something like this. Is there any way to do that?
> I need to consider the whole bar chart as a complete area and calculate the percentage of pixel they hold compared to the background, filling the spaces in between.
I don't think there's nearly enough information provided to understand what the question is.
  1. What defines the background? The x-axis in the first image you shared doesn't even show the axis limit values so the background is not defined.
  2. Are you refering to a histrogram where there is no space between bars or a bar plot where the width of bars is arbitrary?
  3. Are you looking for the area under the curve which would require fitting the distribution or are you just interested in individual bar heights?

댓글을 달려면 로그인하십시오.

 채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 5월 22일
편집: Scott MacKenzie 2021년 5월 22일
Your question is a bit of a moving target. The question title refers to a bar plot. The question text refers to a histogram with no mention of a bar plot. Then a bar chart is referred to in the comment. And you didn't answer any of the cyclist's questions.
If you are indeed working with a bar chart, and you want the area under the bars, with the bar widths adjusted to fill the available space, then...
% test data
d = randn(1,1000);
tiledlayout('flow');
nexttile;
y = histcounts(d);
bar(y, 'barwidth', 1);
a1 = sum(y)
set(gca, 'xlim', [0 25]);
nexttile;
x = 1:length(y);
area(x, y);
a2 = polyarea(x,y)
set(gca, 'xlim', [0 25]);
Output:
a1 =
1000
a2 =
945
The area is simply the sum(y) because the width of each bar is 1. a1 (1000) is the exact area under the bars in the bar chart.
The second chart is included since it is similar to the plot in your comment. a2 (945) is the area within a polygon defined by the points in the bar chart.

댓글 수: 6

I am sorry for being vague earlier. I am triyng to take input as images, find their histogram graph, and find the area considering the plot as a whole area. I am having a bit trouble with the code you gave. In this code you have assumed random values for d, as,
d = randn(1,1000);
But I cannot abe able to put the actual data of the histogram plot( that is the bar plot) in d. Can you help me with it?
I am plotting the histogram data with this code below:
subplot(2,2,3)
imhist(maskedRgbImage), title('Histogram of Tumor');
xlim([-10 200])
ylim([0 100])
Which gives me this output:
I've never used imhist, but the documentation indicates that it can return the "counts"; i.e., the height of the bars. This is equivalent to y in my code. Try this...
[y, ~] = imhist(maskedRgbImage), title('Histogram of Tumor');
xlim([-10 200])
ylim([0 100])
h = bar(y, 'barwidth', 1);
a1 = sum(y)
Tawsif Mostafiz, this can't be your goal. If this is your goal, its very inefficient.
All this is doing is counting the number of pixels in the image. I'll demonstrate:
Load image and look at number of pixels
I = imread('pout.tif'); % 2D grayscale image
size(I)
ans = 1×2
291 240
number_of_pixels = prod(size(I,[1,2]))
number_of_pixels = 69840
Or even easier,
number_of_pixels = numel(I)
number_of_pixels = 69840
Compute image bin counts and add the counts
c = imhist(I);
sum(c)
ans = 69840
Notice that the sum of the hist counts equal the number of pixels in the image which should make sense since the sum of a histogram that represents counts equals the population size.
Thank you for your insight. I want to see the difference in histogram analysis of different images by observing a single value. If this is inefficient, then can would it be better to fit a Gaussian curve over it and find out mean, variance and standard deviation and compare them for different images? If it is, then how is it possible?
> I want to see the difference in histogram analysis of different images by observing a single value.
What single value? If you're refering to the mean color value then you could compute the mean and std for the raw data or you could fit the distribution depending on how you want to interpret the results.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

William Rose
William Rose 2021년 5월 22일
If you make a histogram using Matlab's histogram command, for example as follows,
data=randn(1,1000);
h=histogram(data);
then you can determine the area as follows:
histArea=dot(h.Values,(h.BinEdges(2:end)-h.BinEdges(1:end-1)));
The above method multiplies each height by its corresponding width. It gives the right answer even if the bin widths are unequal. In the example I gave, the widths will be equal, but you could specify unequal bin widths in the histogram() function, and the area calculation above will still give the right area.

카테고리

제품

릴리스

R2018a

질문:

2021년 5월 21일

댓글:

2021년 5월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by