Home > Mobile >  Creating an image mask using polygon points coordinates
Creating an image mask using polygon points coordinates

Time:12-12

I have a grayscale image with size (1920,1080) that I''m trying to create a mask for. I used an external software to manually get the points of interest (polygon). There are now 27 coordinates points representing a polygon in the middle of the image.

I created a mask using the following:

import numpy as np
import matplotlib.pyplot as plt
from skimage.draw import polygon2mask

#image= grayscale with shape (1920,1080)
coordinates = ([1080.15, 400.122], [1011.45, 400.90], .......) #27 points
polygon = np.array(coordinates)
mask = polygon2mask(image.shape, polygon)
result = ma.masked_array(image,  np.invert(mask))
plt.imshow(result)

the problem I'm facing is the output in a wrong place; it should be somehow centred because I took the coordinates from the center, but it was actually in the edge of the image (bottom):

output

Also, the size seem to be a bit smaller that expected. I'm not sure what is causing this problem, I must have done something wrong in my code.. Kindly help me identifying the problem.

CodePudding user response:

You inverted x and y coordinates. polygon2mask coordinates are in y,x order.

Add

coordinates = [[y,x] for [x,y] in coordinates]

after defining coordinates, and you'll have probably what you expected.

  • Related