Thread Subject: Please Help, How to calculate the area percentage of each color in a contour map?

Subject: Please Help, How to calculate the area percentage of each color in a contour map?

From: Hooman

Date: 10 Jun, 2009 16:06:01

Message: 1 of 17

Hello all,
have a rectangular m by n matrix of points and based on a simple function I want to make a contour map with it. but additional to that I also want to know what is the percentage of area of each color in my contour map, (assume I have only four colors).

can anyone help me with this?

Subject: Please Help, How to calculate the area percentage of each color

From: Rune Allnor

Date: 10 Jun, 2009 17:39:08

Message: 2 of 17

On 10 Jun, 18:06, "Hooman " <hoom...@rci.rutgers.edu> wrote:
> Hello all,
> have a rectangular m by n matrix of points and based on a simple function I want to make a contour map with it. but additional to that I also want to know what is the percentage of area of each color in my contour map, (assume I have only four colors).
>
> can anyone help me with this?

First of all, make sure the plot contains closed contour
lines:

C=contourf(X); % Note trailing 'f' in function name

This function uses patches, which are closed polygons,
when rendering the plots; CONTOUR (without the 'F')
may or may not produce closed lines.

Next comes the tricky step: compute the area enclosed
by each closed contour line. The basic idea would be to
segment the closed contours into non-overlapping triangles,
and then compute and sum the areas of each triangle.

Once this is done, the percentage of each color is computed
as the total area enclosed by the contour level minus the
total area enclosed by the contours *one* step higher.

Rune

Subject: Please Help, How to calculate the area percentage of each color in a contour map?

From: Lucas

Date: 10 Jun, 2009 18:45:04

Message: 3 of 17

Polyarea will help speed up the coding (not the code though) - just make sure every area you use is convex

Subject: Please Help, How to calculate the area percentage of each color

From: ImageAnalyst

Date: 10 Jun, 2009 20:35:57

Message: 4 of 17

On Jun 10, 12:06 pm, "Hooman " <hoom...@rci.rutgers.edu> wrote:
> Hello all,
> have a rectangular m by n matrix of points and based on a simple function I want to make a contour map with it. but additional to that I also want to know what is the percentage of area of each color in my contour map, (assume I have only four colors).
>
> can anyone help me with this?

----------------------------------------------------------------------------------------------------
You can take the histogram of your array to get the area fractions
between certain numerical levels. For example, if your array goes
from 0-1000 and you have contours every 100, then you can get a
histogram with 10 bins. The number in each bin divided by the number
of pixels in the image is the area fraction of each numerical value
range ("colors" as you call them). So I could say that
0-100 has 23% (or whatever) of the area
101-200 has 15% (or whatever) of the area
201-300 has 8% (or whatever) of the area
301-400 has 19% (or whatever) of the area
etc.

Subject: Please Help, How to calculate the area percentage of each color in a contour map?

From: Bruno Luong

Date: 10 Jun, 2009 21:07:02

Message: 5 of 17

"Lucas " <l.antispamchars.wilkins@sussex.moreantispam.ac.unitedkingdomdomainname> wrote in message <h0ouvg$gja$1@fred.mathworks.com>...
> Polyarea will help speed up the coding (not the code though) - just make sure every area you use is convex

Polyarea does not require for convexity. It should work for simply connected (read no hole) polygon with arbitrary shape. There is no need for fancy triangulation either. The formula is based on algebric areas of all triangles formed by two vertices and origin. When one sum all that areas, it remains the intrinsic area of the polygon.

Bruno

Subject: Please Help, How to calculate the area percentage of each color

From: Hooman

Date: 10 Jun, 2009 21:57:01

Message: 6 of 17

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c1d7887a-da8f-4521-baa5-9d764061ac5e@o36g2000vbi.googlegroups.com>...
> On Jun 10, 12:06?pm, "Hooman " <hoom...@rci.rutgers.edu> wrote:
> > Hello all,
> > have a rectangular m by n matrix of points and based on a simple function I want to make a contour map with it. but additional to that I also want to know what is the percentage of area of each color in my contour map, (assume I have only four colors).
> >
> > can anyone help me with this?
>
> ----------------------------------------------------------------------------------------------------
> You can take the histogram of your array to get the area fractions
> between certain numerical levels. For example, if your array goes
> from 0-1000 and you have contours every 100, then you can get a
> histogram with 10 bins. The number in each bin divided by the number
> of pixels in the image is the area fraction of each numerical value
> range ("colors" as you call them). So I could say that
> 0-100 has 23% (or whatever) of the area
> 101-200 has 15% (or whatever) of the area
> 201-300 has 8% (or whatever) of the area
> 301-400 has 19% (or whatever) of the area
> etc.

First of all thanks for your reply.
could you be more specific, since I have never used the histogram I am not sure how to use it.
It would be perfect if you tell me what should I do in a more basic way or tell me which part of Matlab help should I read to get this.

Thanks alot

Subject: Please Help, How to calculate the area percentage of each color

From: ImageAnalyst

Date: 11 Jun, 2009 02:39:40

Message: 7 of 17

On Jun 10, 5:57 pm, "Hooman " <hoom...@rci.rutgers.edu> wrote:
> First of all thanks for your reply.
> could you be more specific, since I have never used the histogram I am not sure how to use it.
> It would be perfect if you tell me what should I do in a more basic way or tell me which part of Matlab help should I read to get this.
>
> Thanks alot-
-----------------------------------------------------------------------
Perhaps this demo will get you started:

% Demo macro
% by ImageAnalyst
clc;
close all;
% Create some sample image data with 256 separate discrete values.
[X,Y,Z] = peaks(60);
minv = min(min(Z));
maxv = max(max(Z));
subplot(1, 2, 1);
originalImage = uint8(255 * (Z - minv) / (maxv - minv));
imagesc(originalImage);
title('Original Image');
% Get its histogram. The 256 gray levels will be binned into bins of
width 256/16 = 16 gray levels.
[pixelCounts grayLevels] = imhist(originalImage, 16);
% Plot the histogram (relative areas).
subplot(1, 2, 2);
bar(pixelCounts);
title('Histogram');
% Get the area fractions.
areaPercentages = 100.0 * single(pixelCounts) / sum(pixelCounts);
% Display in the command window.
message = sprintf('gl, grayLevels(gl), areaFractions(gl)');
disp(message);
for gl = 1 : length(grayLevels)
message = sprintf('%d\t%5.1f\t%6.2f%%',gl, grayLevels(gl),
areaPercentages(gl));
disp(message);
end
set(gcf, 'Position', get(0, 'ScreenSize')); % Maximize figure.
msgbox('Done. Look in the command window.');

Subject: Please Help, How to calculate the area percentage of each color

From: Hooman

Date: 11 Jun, 2009 05:06:01

Message: 8 of 17

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <47524990-144b-48a8-b54f-9270ca25fb05@t21g2000yqi.googlegroups.com>...
> On Jun 10, 5:57 pm, "Hooman " <hoom...@rci.rutgers.edu> wrote:
> > First of all thanks for your reply.
> > could you be more specific, since I have never used the histogram I am not sure how to use it.
> > It would be perfect if you tell me what should I do in a more basic way or tell me which part of Matlab help should I read to get this.
> >
> > Thanks alot-
> -----------------------------------------------------------------------
> Perhaps this demo will get you started:
>
> % Demo macro
> % by ImageAnalyst
> clc;
> close all;
> % Create some sample image data with 256 separate discrete values.
> [X,Y,Z] = peaks(60);
> minv = min(min(Z));
> maxv = max(max(Z));
> subplot(1, 2, 1);
> originalImage = uint8(255 * (Z - minv) / (maxv - minv));
> imagesc(originalImage);
> title('Original Image');
> % Get its histogram. The 256 gray levels will be binned into bins of
> width 256/16 = 16 gray levels.
> [pixelCounts grayLevels] = imhist(originalImage, 16);
> % Plot the histogram (relative areas).
> subplot(1, 2, 2);
> bar(pixelCounts);
> title('Histogram');
> % Get the area fractions.
> areaPercentages = 100.0 * single(pixelCounts) / sum(pixelCounts);
> % Display in the command window.
> message = sprintf('gl, grayLevels(gl), areaFractions(gl)');
> disp(message);
> for gl = 1 : length(grayLevels)
> message = sprintf('%d\t%5.1f\t%6.2f%%',gl, grayLevels(gl),
> areaPercentages(gl));
> disp(message);
> end
> set(gcf, 'Position', get(0, 'ScreenSize')); % Maximize figure.
> msgbox('Done. Look in the command window.');

thanks alot, that was a very good demo,
I have some questions about this,

1 - how would I know which gray(out of 16) is for which color, is there anyway for me to find out which grayscale is which color?

2- if I define 4 bins instead of 16, is it possible that the algorithm put more than one color in 1 bin and no color in another bin or that wont happen?

3- what should I use as my input image matrix here, can I use the same matrix that I use for contour map input or I need something else?

Thanks alot for your help and for your time,
Best wishes,
Hooman

Subject: Please Help, How to calculate the area percentage of each color

From: Rune Allnor

Date: 11 Jun, 2009 08:35:44

Message: 9 of 17

On 10 Jun, 22:35, ImageAnalyst <imageanal...@mailinator.com> wrote:
> On Jun 10, 12:06 pm, "Hooman " <hoom...@rci.rutgers.edu> wrote:
>
> > Hello all,
> > have a rectangular m by n matrix of points and based on a simple function I want to make a contour map with it. but additional to that I also want to know what is the percentage of area of each color in my contour map, (assume I have only four colors).
>
> > can anyone help me with this?
>
> ---------------------------------------------------------------------------­-------------------------
> You can take the histogram of your array to get the area fractions
> between certain numerical levels.  

Yes, but that does not solve the problem. Consider this:

d = zeros(5);
d(3,3) = 1;

Now we have 25 data points of which 24 points (96%) contain
the value 0 and one point (4%) contains the value 1. These
are the numbers the historgram method will find.

However, when one plots the contour map,

contourf(d)

one sees that there are non-zero contributions from a number
of value intervals that the histogram just misses.

So the OP needs to sort out whether he wants info on the
data or on the figure.

Rune

Subject: Please Help, How to calculate the area percentage of each color

From: Rune Allnor

Date: 11 Jun, 2009 09:45:10

Message: 10 of 17

On 10 Jun, 23:07, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> "Lucas " <l.antispamchars.wilk...@sussex.moreantispam.ac.unitedkingdomdomainname> wrote in message <h0ouvg$gj...@fred.mathworks.com>...
> > Polyarea will help speed up the coding (not the code though) - just make sure every area you use is convex
>
> Polyarea does not require for convexity. It should work for simply connected (read no hole) polygon with arbitrary shape. There is no need for fancy triangulation either. The formula is based on algebric areas of all triangles formed by two vertices and origin. When one sum all that areas, it remains the intrinsic area of the polygon.
>
> Bruno

If I understand you correctly, this is a kind of
discretized version of Stoke's theorem? Nifty!

Rune

Subject: Please Help, How to calculate the area percentage of each color

From: Bruno Luong

Date: 11 Jun, 2009 10:04:01

Message: 11 of 17

Rune Allnor <allnor@tele.ntnu.no> wrote in message <420ff0ce-a618-443d-ba1d-d0c2f2155803@z9g2000yqi.googlegroups.com>...

>
> If I understand you correctly, this is a kind of
> discretized version of Stoke's theorem? Nifty!
>

Yes, and you summarize it very well in two words.

Bruno

Subject: Please Help, How to calculate the area percentage of each color

From: ImageAnalyst

Date: 11 Jun, 2009 11:27:13

Message: 12 of 17

Well let me ask you this first. You have a 2D matrix of number of
numbers (N by M like you said) for example
1 3 5
5 1 3
3 5 1
5 3 1
You didn't say a 3D array (N by M by 3) for a true color matrix. So,
given that you have a 2D monochrome image, what are you calling the
color? For monochrome images, the "color" or the intensity value is
called "gray level."
Do you really have a N by M by 3 matrix? Or do you have a colormap to
go along with your indexed monochrome image?

Subject: Please Help, How to calculate the area percentage of each color

From: ImageAnalyst

Date: 11 Jun, 2009 11:36:55

Message: 13 of 17

Not sure I understand you Rune. So your image contains 24 0's and one
1. So you specify that you take a histogram with 4 bins (and you can
specify their positions if you want), and then the histogram function
will return a histogram that looks like
Bin(value) # elements
0 24
1 1
2 0
3 0

Take this and divide by 25 and you'll get the area fractions
Bin (Value) Area Fraction
0 0.96
1 0.04
2 0
3 0

At least that's how I'm thinking about it. I still have questions
about when he talks about color, but even if it's a color image, you
could do color classification first to boil it down to a labeled image
that you could then take the histogram of. Yes, the original post was
worded sort of strange and ambigously but I'm looking at the contour
drawing and area fraction determination as separate issues. (I mean,
who would really want the area of the colored contour line itself that
outlines the region - that's merely the perimeter (in pixels) of the
region?)

Subject: Please Help, How to calculate the area percentage of each color

From: Rune Allnor

Date: 11 Jun, 2009 11:53:20

Message: 14 of 17

On 11 Jun, 13:36, ImageAnalyst <imageanal...@mailinator.com> wrote:
> Not sure I understand you Rune.  So your image contains 24 0's and one
> 1.  So you specify that you take a histogram with 4 bins (and you can
> specify their positions if you want), and then the histogram function
> will return a histogram that looks like
> Bin(value)       # elements
> 0                        24
> 1                          1
> 2                          0
> 3                          0
>
> Take this and divide by 25 and you'll get the area fractions
> Bin (Value)            Area Fraction
> 0                         0.96
> 1                          0.04
> 2                             0
> 3                             0
>
> At least that's how I'm thinking about it.

We can discuss the number of bins in the histogram, but
yes, we agree on the problem statement and on the approach,
PROVIDED the context of the problem allows the MxN array
to be interpreted as a discretized *image*.

>  I still have questions
> about when he talks about color, but even if it's a color image, you
> could do color classification first to boil it down to a labeled image
> that you could then take the histogram of.

'Pixel value' or 'grey level' is OK for this discussion;
no need to introduce color representations or color
spaces, as far as I am concerned.

> Yes, the original post was
> worded sort of strange and ambigously but I'm looking at the contour
> drawing and area fraction determination as separate issues. (I mean,
> who would really want the area of the colored contour line itself that
> outlines the region - that's merely the perimeter (in pixels) of the
> region?)

It depends on how the data and information is to be used.
Suppose you see somebody who shows the contour plot from
my post, with all the colors from the interpolated levels,
but use the histogram from the base data. If you hear
the speaker talk about the contour plot showing 96% zeros
and 4% ones, and does not mention the other colors, you
will instantly loose confidence in the speaker, the
material, or both. Because the plot clearly shows a more
complex relation than the speaker talks about.

Again, the OP needs to clarify what he wants to communicate,
and then select a suitable method.

Rune

Subject: Please Help, How to calculate the area percentage of each color

From: Hooman

Date: 11 Jun, 2009 12:36:01

Message: 15 of 17

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <d0f14eab-b6f2-402f-9aa4-79946d6ad2b6@q37g2000vbi.googlegroups.com>...
> Well let me ask you this first. You have a 2D matrix of number of
> numbers (N by M like you said) for example
> 1 3 5
> 5 1 3
> 3 5 1
> 5 3 1
> You didn't say a 3D array (N by M by 3) for a true color matrix. So,
> given that you have a 2D monochrome image, what are you calling the
> color? For monochrome images, the "color" or the intensity value is
> called "gray level."
> Do you really have a N by M by 3 matrix? Or do you have a colormap to
> go along with your indexed monochrome image?

Well I have a M by N matrix and each cell is a double number between 1 and 5, and I want to have a contour map or interpolation from this matrix with red for numbers between 1 to 2, yellow for numbers between 2-3, green for numbers between 3 and 4 and blue for numbers between 4 and 5.

Then I want to find out what is the area percentage of each color.
The contour part is easy but I don't know how to calculate the area percentage of the each color.

Subject: Calculate area of polygon area with holes

From: Naresh Pai

Date: 30 Jul, 2009 20:33:19

Message: 16 of 17

Hi Bruno,
      I was reading one of your posts on mathworks. I have a similar issue where I would like to calculate the area of a polygon which as holes in it. The polyarea function seems to count this area too. Is there a work around ?

Naresh

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h0p79m$21s$1@fred.mathworks.com>...
> "Lucas " <l.antispamchars.wilkins@sussex.moreantispam.ac.unitedkingdomdomainname> wrote in message <h0ouvg$gja$1@fred.mathworks.com>...
> > Polyarea will help speed up the coding (not the code though) - just make sure every area you use is convex
>
> Polyarea does not require for convexity. It should work for simply connected (read no hole) polygon with arbitrary shape. There is no need for fancy triangulation either. The formula is based on algebric areas of all triangles formed by two vertices and origin. When one sum all that areas, it remains the intrinsic area of the polygon.
>
> Bruno

Subject: Calculate area of polygon area with holes

From: Bruno Luong

Date: 31 Jul, 2009 06:46:05

Message: 17 of 17

"Naresh Pai" <npai@uark.edu> wrote in message <h4t02f$ijh$1@fred.mathworks.com>...
> Hi Bruno,
> I was reading one of your posts on mathworks. I have a similar issue where I would like to calculate the area of a polygon which as holes in it. The polyarea function seems to count this area too. Is there a work around ?
>
> Naresh
>


If you have the array of vertexes *properly* ordered: i.e., going around the area where the polygon is always located on the left side, jumping from one (external) contour to another (internal) contour is allowed (but you must ensure going in and out always through the same pair of vertices), you can also apply the algebric area formula (see bellow). It would remove the hole area properly.

I don't know how CONTOUR function returns the contours, but I once write my own contour function where the result oriented correctly with respect to the sign of the function, which is great to compute areas even with hole (I solve some level-set problem IIRC).

The bottom line is compute the area with hole is not hard, what is hard is order properly the vertices.

Bruno

function a=carea(cx, cy)
% function a=carea(cx, cy); OR
% function a=carea(c);
%
% PURPOSE: returns the algebric area of a contour line
% carea returns a>0 for contour that is counterclockwise oriented
% a<0 otherwise
% INPUT: c (n x 2)-vector of coordinates of the polygonal vertex
% if the number of vertex n is not equal to n, then input
% c of dimension (2 x n) is also alright
% cx, cy: passing x and y-coordinates separately
%

if ~isempty(cx)
    if nargin>=2
      if size(cx,1)~=1
        cx=cx';
      end
      if size(cy,1)~=1
        cy=cy';
      end
      x=[cx cx(1)];
      y=[cy cy(1)];
    else
      if size(cx,2)~=2
          cx=cx';
      end
      x=[cx(:,1); cx(1,1)];
      y=[cx(:,2); cx(1,2)];
    end
    a=sum((x(1:end-1)-x(2:end)).*(y(1:end-1)+y(2:end)))/2;
else
    a=0;
end

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
area Sprinceana 22 Jun, 2009 08:10:21
rssFeed for this Thread

Contact us at files@mathworks.com