简体   繁体   English

使用 JPG 蒙版裁剪 TIFF

[英]Crop TIFF using JPG mask

I'm currently working on cloud removals from satellite data (I'm pretty new).我目前正在研究从卫星数据中移除云(我很新)。

This is the image I'm working on (TIFF)这是我正在处理的图像 (TIFF)

在此处输入图像描述

And this is the mask, where black pixels represent clouds (JPG)这是蒙版,黑色像素代表云 (JPG) 在此处输入图像描述

I'm trying to remove the clouds from the TIFF, using the mask to identify the position of the cloud, and the cloudless image itself, like this (the area is the same, but the period is different):我正在尝试从 TIFF 中去除云层,使用蒙版来识别云层的 position,以及无云图像本身,就像这样(面积相同,但周期不同):

在此处输入图像描述

I'm kindly ask how can I achieve that.请问我怎样才能做到这一点。 A Python solution, with libraries like Rasterio or skimage is particularly appreciated.特别感谢 Python 解决方案,其中包含Rasterioskimage等库。

Thanks in advance.提前致谢。

You can read the images with rasterio , PIL , OpenCV or tifffile , so I use OpenCV您可以使用rasterioPILOpenCVtifffile读取图像,所以我使用OpenCV

import cv2
import numpy as np

# Load the 3 images
cloudy = cv2.imread('cloudy.png')
mask   = cv2.imread('mask.jpg')
clear  = cv2.imread('clear.png')

Then just use Numpy where() to choose whether you want the clear or cloudy image at each location according to the mask:然后只需使用 Numpy where()根据掩码在每个位置选择是否要清晰或多云的图像:

res = np.where(mask<128, clear, cloudy)

在此处输入图像描述


Note that if your mask was a single channel PNG rather than JPEG, or if it was read as greyscale like this:请注意,如果您的蒙版是单通道 PNG 而不是 JPEG,或者如果它像这样读取为灰度:

mask   = cv2.imread('mask.jpg', cv2.IMREAD_GRAYSCALE)

you would have to make it broadcastable to the 3 channels of the other two arrays by adding a new axis like this:您必须通过添加如下新轴使其可广播到其他两个 arrays 的 3 个频道:

res = np.where(mask[...,np.newaxis]<128, clear, cloudy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM