简体   繁体   中英

How do i find the min\max value Numpy array

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? 在此处输入图像描述

在此处输入图像描述

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)

示例图像

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)

带有边界框的图像

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.

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