I have a matrix that I want to extract an ROI from. I am using roipoly to clip the region. The syntax below gives the coordinates of the polygon in the intrinsic coordinate system, I believe. How can I get the matrix indices for each vertex of the polygon?
tst = rand(100);
[xrefout,yrefout,BW,xi2,yi2] = roipoly(tst);
I select some square.
xi2 and yi2 are real valued. To get the pixel value, does one just round xi2 and yi2 to the nearest integer?

 採用された回答

Matt J
Matt J 2021 年 9 月 10 日

0 投票

You must interchange the coordinates (i,j)=(yi2,xi2)

5 件のコメント

Paul Safier
Paul Safier 2021 年 9 月 10 日
xi2 and yi2 are not integers and thus not i,j.
Matt J
Matt J 2021 年 9 月 10 日
Well, you can round them as you've already suggested. The main difference between the two coordinate systems though is the ordering.
Paul Safier
Paul Safier 2021 年 9 月 10 日
What doesn't make sense about just rounding the values, is that according to the example shown here:
a value of 3.5 is possible (bottom right). This would round to 4 and thus be out of the domain that only extended from 1 to 3.
Matt J
Matt J 2021 年 9 月 10 日
編集済み: Matt J 2021 年 9 月 10 日
You can threshold the result if you need to, e.g.,
min(max(round(z),1),3)
The rounding is just an approximation step which we are considering here only because you seem to need a result that can be used to index into a matrix. There is no single sacred way to do it. You can choose whatever integer approximation works best for you.
Paul Safier
Paul Safier 2021 年 9 月 10 日
Yep, I see. That'll do. Thanks.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 9 月 10 日

0 投票

Try using the mask roipoly() returns:
tst = imread('moon.tif'); % Get some matrix.
imshow(tst); % Display it.
% Ask user to use roipoly.
message = sprintf('Left click vertices.\nRight click last point.\nThen double click inside when you are all done.');
uiwait(helpdlg(message));
mask = roipoly(tst);
% Extract values inside the polygon they drew.
valuesInsideMask = tst(mask)

4 件のコメント

Paul Safier
Paul Safier 2021 年 9 月 10 日
Thanks @Image Analyst but I'm after the matrix indices of the roi's vertices. I'm unclear about simply rounding the intrinsic coordinates as it seems possible to obtain an index value out of range; see my comment to Matt J's answer above.
Image Analyst
Image Analyst 2021 年 9 月 11 日
Why do you need only the vertices? What will you do then, once you have them? Nothing further?
Paul Safier
Paul Safier 2021 年 9 月 11 日
The clipped matrix is used as well, sure, but the vertex info is needed elsewhere and printed. It's the vertex info (in indicial format) that I was questioning.
Image Analyst
Image Analyst 2021 年 9 月 11 日
There is also a drawpolygon() function that you might like.

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

製品

リリース

R2020b

質問済み:

2021 年 9 月 10 日

コメント済み:

2021 年 9 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by