easyidp.cvtools.imarray_crop

easyidp.cvtools.imarray_crop(imarray: ndarray, mask: ndarray, nodata: float | int | None = None) tuple[ndarray, ndarray, ndarray]

Crop a given ndarray image by given polygon or mask.

This is a basic numpy processing tool for cropping images with polygon or mask regions.

パラメータ:
  • imarray (np.ndarray) -- The image data in numpy ndarray. Shape can be (height, width) for DSM or (height, width, bands) for DOM.

  • mask (np.ndarray) --

    Either: - Polygon coordinates with shape (N, 2) in (horizontal, vertical) order - Boolean mask with shape (H, W) matching imarray dimensions

    注意

    For polygon, coordinate order is reversed from numpy indexing. horizontal = numpy axis 1, vertical = numpy axis 0.

  • nodata (float | int | None, optional) -- Value to use for pixels outside the polygon/mask, by default None. If None, the original values are preserved (no masking applied).

戻り値:

  • imarray_out (np.ndarray(W, H, 3).dtype=uint8) -- The cropped image array. If nodata is not None, pixels outside the mask are set to nodata value.

  • roi_offset (np.ndarray(2,). dtype=int32) -- The (horizontal, vertical) pixel offset of the crop region's top-left corner.

  • mask_out (np.ndarray(W, H). dtype=bool) -- The (height, width) boolean mask for the cropped region. True values indicate pixels inside the polygon/mask.

サンプル

>>> import numpy as np
>>> import easyidp as idp
>>>
>>> # Create sample image and polygon
>>> imarray = np.random.rand(100, 100)
>>> polygon = np.array([[20, 20], [80, 20], [80, 80], [20, 80], [20, 20]])
>>>
>>> # Crop with nodata applied
>>> cropped, offset, mask = idp.cvtools.imarray_crop(imarray, polygon, nodata=0)
>>>
>>> # Crop preserving original values (nodata=None)
>>> cropped, offset, mask = idp.cvtools.imarray_crop(imarray, polygon, nodata=None)
>>>
>>> # save cropped to rgba png file
>>> cropped_png = np.dstack((cropped, mask.astype(np.uint8)*255))
>>> skimage.io.imsave("save_path.png", cropped_png)