# How to get matrix indices from roipoly

24 visualizzazioni (ultimi 30 giorni)
Paul Safier il 10 Set 2021
Commentato: Image Analyst il 11 Set 2021
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?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Matt J il 10 Set 2021
You must interchange the coordinates (i,j)=(yi2,xi2)
##### 5 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Matt J il 10 Set 2021
Modificato: Matt J il 10 Set 2021
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 il 10 Set 2021
Yep, I see. That'll do. Thanks.

Accedi per commentare.

### Più risposte (1)

Image Analyst il 10 Set 2021
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));
% Extract values inside the polygon they drew.
##### 4 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Paul Safier il 11 Set 2021
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 il 11 Set 2021
There is also a drawpolygon() function that you might like.

Accedi per commentare.

### Categorie

Scopri di più su ROI-Based Processing in Help Center e File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by