0

I have a binary raster image as shown below:

enter image description here

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".

RRSC
  • 257
  • 2
  • 15

1 Answers1

1

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]
Ken T
  • 2,255
  • 1
  • 23
  • 30
  • Your solution worked for me with some changes in your proposed solution - "p3x & p2x were represented originally in your proposed solution as 1-d array. So I just extracted my desired values". – RRSC Aug 25 '20 at 17:27
  • @RRSCNGP I have revised the answer. See if I understand u correctly – Ken T Aug 25 '20 at 18:03
  • ....Yes your revised answer is correct. Thanks for the help. – RRSC Aug 25 '20 at 18:09