Finding Area of Polygon defined by roipoly

24 ビュー (過去 30 日間)
abdulla alyammahi
abdulla alyammahi 2020 年 3 月 25 日
編集済み: Image Analyst 2020 年 12 月 26 日
Hello,
I would like to know how I can use the function polyarea to find the area of a region defined by the function roipoly.
I am new to image processing with matlab and basically what I am trying to do is read an image, select a border for something within the image using roipoly, finding the area within the border in pixels and finally converting the area in pixels to area in m^2. Please advise.
Thank you,

回答 (2 件)

Matt J
Matt J 2020 年 3 月 25 日
編集済み: Matt J 2020 年 3 月 25 日
roipoly returns a logical mask BW of the region
BW = roipoly;
so, its area in whole pixels would simply be
area = sum(BW(:));
If you have the coordinates of the vertices, because you called roipoly using this syntax
[BW,xi2,yi2] = roipoly(___);
then fractional pixels can be included in the area calculation by doing
area = polyarea(xi2,yi2)
  2 件のコメント
abdulla alyammahi
abdulla alyammahi 2020 年 3 月 30 日
Thank you very much for your response, I tried it and it worked perfectly and solved my problem.
Matt J
Matt J 2020 年 3 月 31 日
You're welcome, but please click "Accept" to indicate that it worked.

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


Image Analyst
Image Analyst 2020 年 3 月 31 日
編集済み: Image Analyst 2020 年 12 月 26 日
To get the answer in real world units like m^2 you have to first get the area in pixels, using one of these 3 ways:
[mask, x, y] = roipoly(); % Get binary image and (x,y) coordinates of polygon
% Method 1 to get area in pixels:
areaInPixels = polyarea(x, y); % Could have fractional pixel amounts.
% Method 2
areaInPixels = bwarea(mask); % Could have fractional pixel amounts.
% Method 3
areaInPixels = nnz(mask); % Always an integer count of pixels.
Then multiply by the spatial calibration factor to get the area in square meters or millimeters:
areaInM2 = areaInPixels * metersPerPixel ^ 2;
If you don't know how to arrive at meters per pixel, you basically have to draw a distance on your image and tell it how many real world units it is. I have a demo attached where it does exactly that. Please run it.

カテゴリ

Help Center および File ExchangeImage Filtering and Enhancement についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by