pythonnumpycoordinatespolygonmask

Python - convert binary mask to polygon


Given a simple binary mask (e.g. the boundary of a rectangle).

binary mask

How can I use a polygon to get the x-y coordinates?

This is what I have tried so far:

coords = np.transpose(np.nonzero(mask))

However, this approach generates a filled object and not the desired boundary.

plt.plot(coords[:, 1], coords[:,0])

unwanted output

Basically, I want a list of x-y coordinates of the white pixels to use this list to re-draw the rectangle (not filled).


Solution

  • You can use np.column_stack() + np.where(). The idea is to determine the white pixels in the binary image then order then in corresponding (x, y) order

    coords = np.column_stack(np.where(image > 0))
    

    Another way is to find the coordinates of the bounding rectangle using OpenCV's cv2.boundingRect(). This will give you the width, height, and top-left (x,y) coordinates. Here's an example finding the coordinates then drawing the polygon onto a blank mask

    import cv2
    import numpy as np
    
    image = cv2.imread('1.png', 0)
    x,y,w,h = cv2.boundingRect(image)
    mask = np.ones(image.shape, dtype=np.uint8) * 255
    mask = cv2.merge([mask,mask,mask])
    cv2.rectangle(mask, (x, y), (x + w, y + h), (36,255,12), 2)
    
    cv2.imshow('mask', mask)
    cv2.waitKey()