Home > front end >  How do i find the min\max value Numpy array
How do i find the min\max value Numpy array

Time:01-07

I am trying to get the non-white min and max pixel location for this image along the axis = 0. However, np.where isn't working along axis = zero. I have ried other np function and they do not work. I have read the documentation and still no resolve. Does anyone have a function for this? enter image description here

enter image description here

CodePudding user response:

If I understand the question correctly, the goal is to find indices of rows and columns bounding the area of the image that contains all non-white pixels. This can be done, for example, as follows.

Load a sample image:

import numpy as np
import matplotlib.pyplot as plt

img = plt.imread("sample.jpg").copy()
plt.imshow(img)

sample image

Find the bounding box of non-white pixels and display the result:

# indices of non-white pixels 
rows, cols = np.nonzero(np.any(img != 255, axis=-1))
# indices of rows and columns of the bounding box
rbox = rows.min(), rows.max()
cbox = cols.min(), cols.max()

# show the selection
img[rbox[0]:rbox[1] 1, cbox[0]:cbox[1] 1, 1] = 0
plt.imshow(img)

image with the bounding box

  •  Tags:  
  • Related