I have a binary raster image as shown below:
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]
By running the above code I am able to get locations of "pixel_1" & "pixel_4". Can someone help me out in getting pixel locations of "pixel_2" & "pixel_3".
Find x and then 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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.