如何获取二值图像中 4 个角白色像素的像素位置

How to get pixel locations of 4 corner white pixels in a binary image

我有一个二进制光栅图像,如下所示:

import rasterio
import numpy as np

path = r'D:\LE07_L1TP_147048_20070221_20170105_01_T1\LE07_L1TP_147048_20070221_20170105_01_T1_B1.tif'

with rasterio.open(path) as dataset:
    image = dataset.read(1)
    mask = np.where(image > 1, 255, 0)

white_pixels = np.array(np.where(mask == 255))
pixel_1 = white_pixels[:,0]
pixel_4 = white_pixels[:,-1]

通过 运行 上面的代码,我可以得到“pixel_1”和“pixel_4”的位置。有人可以帮助我获取“pixel_2”和“pixel_3”的像素位置。

找到 x,然后找到 y。

white_pixels_x = np.nonzero(np.sum(mask, axis=0))

p3x = white_pixel_x[0][0]
p2x = white_pixel_x[0][-1]

p3y = np.nonzero(mask[:, p3x])[0][0]
p2y = np.nonzero(mask[:, p2x])[0][0]