How do I tell matlab to identify vertices within ROI?

4 views (last 30 days)
I want matlab to identify all vertices of the hexagons that will be within an ROI. That ROI can be of any shape (it can be an irregular shape).
Here is my code:
l=12;
k=14;
C=rand(l,k);
xhex=[0 1 2 2 1 0];
yhex=[2 3 2 1 0 1];
for i=1:k
j=i-1;
for q=1:l
m=q-1;
patch((xhex+mod(q,2))+2*j,yhex+2*m,C(q,i))
hold on
end
end
axis equal
h = drawfreehand;
I have attached a picture of a sample ROI.
I will appreciate any advise on this matter. Many thanks.
  1 Comment
Image Analyst
Image Analyst on 16 Jul 2022
This would be so easy with a digital image. Just use h.createMask and bwmorph and find(). But using an analytical image created with patch or fill or whatever, it's trickier (at least for me).

Sign in to comment.

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 7 Jan 2024
Edited: VINAYAK LUHA on 8 Jan 2024
Hi Mariam,
I understand that you're looking to identify the vertices of the hexagons that fall within the boundaries of a freehand-drawn ROI.
Follow the below steps to accomplish the above goal-
  • Find vertices of the hexagons which are inside the ROI using the "inpolygon" function
  • Next, eliminate the duplicate detections and show the vertices within the ROI.
Refer to the snippet below to see the MATLAB code to implement the above steps.
l = 12;
k = 14;
C = rand(l, k);
xhex = [0 1 2 2 1 0];
yhex = [2 3 2 1 0 1];
vertices = [];
for i = 1:k
j = i - 1;
for q = 1:l
m = q - 1;
x = (xhex + mod(q, 2)) + 2 * j;
y = yhex + 2 * m;
patch(x, y, C(q, i));
hold on
vertices = [vertices; [x', y']];
end
end
axis equal
% Let the user draw a freehand ROI
h = drawfreehand;
% Get the position of the ROI
roiPos = h.Position;
% Find which vertices are inside the ROI
inROI = inpolygon(vertices(:,1), vertices(:,2), roiPos(:,1), roiPos(:,2));
% Get the vertices that are inside the ROI
verticesInROI = vertices(inROI, :);
verticesInROI = unique(verticesInROI, 'rows', 'stable');
% Display the vertices within the ROI
disp('Vertices within the ROI:');
disp(verticesInROI);
For better understanding of the functions employed here, you might want to explore the provided documentation links.
Hope this answers your query.
Best regards,
Vinayak Luha
  2 Comments
DGM
DGM on 8 Jan 2024
It's step #1 in an itemized workflow, and it's shown plainly in the example code, yet there's demonstrably no point in creating a mask. It's not actually used for anything, because it's an artifact of a suggestion to use a different workflow based on raster images. I guess the AI picks up on red herrings like that and inserts decorations to satisfy the misdirection.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!