What's wrong with this polyshape?

The polyshape created has nothing to do with the original polygon: what am I doing wrong?

6 件のコメント

Geoff Hayes
Geoff Hayes 2020 年 4 月 23 日
Joana - please attach or copy and paste your code to this question rather than posting screen shots of the code.
Joana Brito
Joana Brito 2020 年 4 月 24 日
plot([106.2620, 107.2080, 106.5060, 104.6750, 105.7130], [666.0000, 670.0000, 644.0000, 628.0000, 656.0000]);
BW=poly2mask([106.2620, 107.2080, 106.5060, 104.6750, 105.7130], [666.0000, 670.0000, 644.0000, 628.0000, 656.0000], 800, 800);
imshow(BW))
Muhammad Usman
Muhammad Usman 2020 年 4 月 24 日
Generally when you draw the polygons in MATLAB by using points, then in order to close the shape of any polygon the first point is last point too...
Here is an example:
plot([0,1,1,0,0],[0,0,1,1,0])
axis([-0.5 1.5 -0.5 1.5])
Ameer Hamza
Ameer Hamza 2020 年 4 月 24 日
Joana, the region you specified is only 3 pixels wide. It cannot be correctly made unless you increase its width.
Joana Brito
Joana Brito 2020 年 4 月 24 日
Muhammad, the poly2mask function should close the polygon automatically
Joana Brito
Joana Brito 2020 年 4 月 24 日
Ameer, how do I do that? How do I scale the x and y axis in order for the pixels to match the shape?

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

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 24 日

0 投票

Try this code. As I mentioned in my comment to your question that the polygon is just 3 pixels wide. In this code, I scaled the coordinate to make it 50 pixels wide. Now it should be visible.
x = [106.2620, 107.2080, 106.5060, 104.6750, 105.7130];
y = [666.0000, 670.0000, 644.0000, 628.0000, 656.0000];
x = (x - min(x))./(max(x)-min(x))*50+100;
BW=poly2mask(x, y, 800, 800);
imshow(BW)

4 件のコメント

Joana Brito
Joana Brito 2020 年 4 月 24 日
and why didn't you scale the y axis?
because I will use this binary image to take out information such as Centroid/MinorAxisLength/... and then apply it to the original polygon. Do I scale it back?
Ameer Hamza
Ameer Hamza 2020 年 4 月 24 日
I scaled the x-values, just to demonstrate the issue with making a mask from the polygon coordinate. I guess you are planning to use regionprops to find the required information. If your goal is to find the centroid of the polygon, then why not use centroid(): https://www.mathworks.com/help/matlab/ref/polyshape.centroid.html. Also, regionprops consider the region as an ellipse and returns the major and minor axis. However, your shape is a polygon. How are you defining the axes of the polygon?
Joana Brito
Joana Brito 2020 年 4 月 24 日
Thank you for you sugestions! But the goal is to find an ellipse which encloses the polygon and for that finding the major and minor axes with regionprops
I did it like you said and now my shape is rotated, for some reason
Ameer Hamza
Ameer Hamza 2020 年 4 月 24 日
The polygon is not rotated, it is just drawn upside down because the imshow() function makes a y-axis, which increases in the downward direction, whereas plot() make y-axis increasing in the upward direction. Apart from appearance, the vertex coordinates are the same.
If you want to do this with regionprops, then you will need to apply some type scaling before making the poly2mask and then apply reverse scaling at the end. I recommend avoiding the image processing toolbox function because it is really a geometry problem, and there are other available tools to solve this problem efficiently. For example, check this FEX submission: https://www.mathworks.com/matlabcentral/fileexchange/3215-fit_ellipse. It fit an ellipse to a set of data points and directly outputs the major and minor axes.

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

カテゴリ

ヘルプ センター および File ExchangeElementary Polygons についてさらに検索

質問済み:

2020 年 4 月 23 日

コメント済み:

2020 年 4 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by