Faster execution of poly2mask

Hello everyone, I was wondering if anyone would know how to get this done differently than I do.
I have this image and defined the polygon by its vertices (shown in red), what I want is to be able to extract the value of the color from them by putting it in a vector like the one I show in the figure
When I implemented it I had used poly2mask but in the real case (the one I show here is a small example, my image is 8000X8000 pixels and the number of clipping rectangles is over 400, so poly2mask is very slow and I was looking for something faster.
Thank you very much.

2 Comments

darova
darova on 14 Jun 2020
Edited: darova on 15 Jun 2020
Since you know the coordinates why don't just cut that region?
reg1 = I1(x1:x2,y1:y2);
EDITED: edited the code
darova - please fix. You know I1(x1:x2,y1:x2); is not correct.

Sign in to comment.

Answers (1)

Try this:
m = randi(255, 11, 9) % Set up sample data.
% Define region
row1 = 1;
row2 = 4;
column1 = 7;
column2 = 8;
% Extract sub-image.
subImage = m(row1 : row2, column1 : column2);
% Reshape into row vector.
vec = reshape(subImage, 1, [])

1 Comment

Ok thank you so much, just one more question, If the rectangle shown in red in the image was rotated a certain angle, the poly2mask function would become essential or you can also modify the code you just passed me?

Sign in to comment.

Categories

Find more on Display Image in Help Center and File Exchange

Products

Release

R2020a

Edited:

on 15 Jun 2020

Community Treasure Hunt

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

Start Hunting!