Ask Your Question

Revision history [back]

Creating a mask from WKT multipolygons using openCV and tiff libraries?

I'm trying to create a binary mask from a WKT multipolygon which is a csv file with python openCV, shapely and tiff libraries in Jupiter Notebook. There's also another csv file that contains Xmax and Ymin of the image. However, I keep getting a black image. here's the code:

from collections import defaultdict
import csv, sys, cv2
from shapely.geometry import MultiPolygon, Polygon
import shapely.wkt
import shapely.affinity
import numpy as np
import tifffile as tiff
import matplotlib.pyplot as plt

maxInt = sys.maxsize
decrement = True


while decrement:
    # decrease the maxInt value by factor 10 
    # as long as the OverflowError occurs.

    decrement = False
    try:
        csv.field_size_limit(maxInt)
    except OverflowError:
        maxInt = int(maxInt/10)
        decrement = True


x_max = y_min = None
for _im_id, _x, _y in csv.reader(open('GridSize.csv')):
    if _im_id == IM_ID:
        x_max, y_min = float(_x), float(_y)
        break

sample_polygons = None
for _im_id, _poly_type, _poly in csv.reader(open('WKT_polygons.csv')):
    if _im_id == IM_ID and _poly_type == POLY_TYPE:
        sample_polygons = shapely.wkt.loads(_poly)
        break


im_rgb = tiff.imread('image.tif')
im_size = im_rgb.shape[:2]

def polygons_mask(polygons):
    img_mask = np.zeros(im_size, np.uint8)
    if not polygons:
        return img_mask
    int_coords = lambda x: np.array(x).round().astype(np.int32)
    exteriors = [int_coords(poly.exterior.coords) for poly in polygons]
    interiors = [int_coords(pi.coords) for poly in polygons
                 for pi in poly.interiors]
    cv2.fillPoly(img_mask, exteriors, 1)
    cv2.fillPoly(img_mask, interiors, 0)
    return img_mask

_mask = polygons_mask(sample_polygons)
tiff.imshow(_mask)[0]

Any help is appreciated? image description

click to hide/show revision 2
retagged

updated 2017-06-30 01:51:32 -0600

berak gravatar image

Creating a mask from WKT multipolygons using openCV and tiff libraries?

I'm trying to create a binary mask from a WKT multipolygon which is a csv file with python openCV, shapely and tiff libraries in Jupiter Notebook. There's also another csv file that contains Xmax and Ymin of the image. However, I keep getting a black image. here's the code:

from collections import defaultdict
import csv, sys, cv2
from shapely.geometry import MultiPolygon, Polygon
import shapely.wkt
import shapely.affinity
import numpy as np
import tifffile as tiff
import matplotlib.pyplot as plt

maxInt = sys.maxsize
decrement = True


while decrement:
    # decrease the maxInt value by factor 10 
    # as long as the OverflowError occurs.

    decrement = False
    try:
        csv.field_size_limit(maxInt)
    except OverflowError:
        maxInt = int(maxInt/10)
        decrement = True


x_max = y_min = None
for _im_id, _x, _y in csv.reader(open('GridSize.csv')):
    if _im_id == IM_ID:
        x_max, y_min = float(_x), float(_y)
        break

sample_polygons = None
for _im_id, _poly_type, _poly in csv.reader(open('WKT_polygons.csv')):
    if _im_id == IM_ID and _poly_type == POLY_TYPE:
        sample_polygons = shapely.wkt.loads(_poly)
        break


im_rgb = tiff.imread('image.tif')
im_size = im_rgb.shape[:2]

def polygons_mask(polygons):
    img_mask = np.zeros(im_size, np.uint8)
    if not polygons:
        return img_mask
    int_coords = lambda x: np.array(x).round().astype(np.int32)
    exteriors = [int_coords(poly.exterior.coords) for poly in polygons]
    interiors = [int_coords(pi.coords) for poly in polygons
                 for pi in poly.interiors]
    cv2.fillPoly(img_mask, exteriors, 1)
    cv2.fillPoly(img_mask, interiors, 0)
    return img_mask

_mask = polygons_mask(sample_polygons)
tiff.imshow(_mask)[0]

Any help is appreciated? image description