0

I have a jpg picture of a face, I need to access the picture pixel by pixel (know what value is at each pixel), and use some sort of DFS to change background color.

image = Image.open("pic.jpg")
image = np.array(image)

First of all, why is the shape of the array (473, 354, 3)? It doesn't make sense to me.

When I do

plt.imshow(image.reshape(473, -1))
plt.show()

I get a picture that looks like the following, which consists of only red, blue and yellow colors (and a mixture of the three?)

This means that the values in the array are not what I can reliably use to make my edge detection decisions.

Why and what should I do?

enter image description here

I want the pixel values to reflect the true color of the original image, not like above.

The background in the actual picture is kinda white, and I want them and all other pixel values to stay that way, so I can implement my algorithm.

1 Answer 1

1

The 3 is because each color (blue, green red) gets its own entry in the array.

For edge detection, you would might do best to collapse the image down to B&W. OpenCV has cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) that will do the trick.

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

4 Comments

So (473, 354, 3) means: the height is 473, the width is 354, and each value in the array is a 3 - tuple?
@Jobs exactly. Also the colors that are shown are not the colors in the real picture but it's a colormap that shows specifici colors for high values. So if you input the picture with dimensions (h,w,(RGB)) probably only the red value is taken into account. It will show red where high values (of red) are and blue where low values (of red) are. You probably should convert to black&white and don't take the colors literally (or switch to a black and white colormap).
Thank you so much! That makes sense. So now I've successfully accomplished the task - changing the background of my picture to white. The size of the picture dropped from 60 kb to 20 kb. Do you guys know why? Is this normal / expected? @syntonym and charles.
What exactly did you do? If you converted to black and white you now have not a 3-tuple in each array cell but a single value - so the size consumption should be a third which seems to be the case for you. Seems like everything is normal.

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.