nan outside polygon ones inside

10 views (last 30 days)
zawaiter
zawaiter on 9 Feb 2012
Answered: Hari on 11 Jun 2025
i have the vertices and facec of a figure i want to set all area outside this boundaries to nan and save the image matrix.plz some help i am still having a problem with this issue ,i have wrote two codes one depending on what kevin explained and a second one depending on image analyst explanation,here what they recommend. http://www.mathworks.cn/matlabcentral/answers/27570-how-to-set-nan-to-areas-outsid-a-polygon-on-the-image. in the first code i noticed some ones in one side outside the head area and some zeros in the other side inside the head area, as if the mask is shifted to one side. i guess the solution may be in the part you mentioned about getting the distance and make a thresholding but i don’t know how to do it. the second code recommended by image analyst, fit the head boundaries very well although there is a lot of zeros inside the head area. i believe i might made a little mistake here or there but i am not able to locate it and fix it.i wish you allow me to send the codes and data to you and take look at them , i will be so grateful . thanks again

Answers (1)

Hari
Hari on 11 Jun 2025
Hi,
I understand that you have the vertices and faces of a figure (likely a head), and you'd like to create an image mask such that all pixels outside the polygonal boundary are set to NaN in the image matrix. You've tried two approaches based on suggestions from the MATLAB community but are facing alignment issues and incorrect masking.
I assume that the figure lies in 2D (or can be projected to 2D), and you are trying to mask a grayscale image based on the polygon defined by the vertices.
In order to mask all areas outside the polygon and set them to NaN, you can follow the below steps:
Step 1: Create a binary mask from the polygon
Use the "poly2mask" function to generate a mask from the x and y coordinates of the polygon:
% Assuming 'vertices' is an Nx2 matrix [x, y] and 'image' is the original image
mask = poly2mask(vertices(:,1), vertices(:,2), size(image,1), size(image,2));
This will create a logical mask where true represents the inside of the polygon.
Step 2: Apply the mask to the image
Convert areas outside the polygon to NaN:
maskedImage = double(image); % convert to double to support NaN
maskedImage(~mask) = NaN;
Note: Using double is important because NaN cannot be assigned to integer-type arrays.
Step 3: Save the masked image
You can save the resulting image matrix using:
save('maskedImage.mat', 'maskedImage');
Optional: Visual check
To verify if the mask aligns correctly, overlay it:
imshow(maskedImage, []);
hold on;
plot(vertices(:,1), vertices(:,2), 'r-', 'LineWidth', 1.5);
This lets you confirm there is no shift between the polygon and the actual image content.
Refer to the documentation of:
Hope this helps!

Community Treasure Hunt

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

Start Hunting!