0

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

4
  • What is w.shape? Commented Jan 5, 2022 at 3:14
  • This is the shape [236, 213, 3] Commented Jan 5, 2022 at 3:26
  • Read what np.where does again. Taking the max of what it returns is giving you the largest non-white index, not the index of the largest non-white value Commented Jan 5, 2022 at 4:45
  • AJ I read the documentation. And clearly this is working in axis = 1. Do you know of another way? Commented Jan 5, 2022 at 15:46

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it worked. your help was greatly appreciated. do u have a cash app?
@TammaraBrown No cash app, I am glad it was helpful.
i have another question stackoverflow.com/questions/70661012/… ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.