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.