2

I have to Encode an Image for Semantic Segmentation. The Input Image is of shape (128, 256, 3) with 128 x 256 RGB Values. I want an Output Shape of (128, 256) where every 1 represent, that the pixel matched the given Color and 0 represents, that another RGB Value was present.

[[20, 20, 20], [30, 30, 30], [40, 40, 40]] with the Filter [20,20,20] should result in [1, 0, 0]

Any Help would be greatly appreciated.

Optimally This Method should be feasible to apply to an array of shape (16, 128, 256, 3) with 16 pictures in it, applying the filter to every picture.

2
  • Correct me if I'm wrong, your example, the matrix [[20, 20, 20], [30, 30, 30], [40, 40, 40]] is 2D, so the filter should be 20 only and the result will be [[1,1,1],[0,0,0],[0,0,0]], is it correct? Commented Jun 17, 2021 at 8:13
  • The Matrix in the Question is just an example. The Natrix in my Code is of shape (16, 128, 256, 3) or (8, 128, 1256, 3) based on my batch_size. Commented Jun 17, 2021 at 8:18

1 Answer 1

1

Using np.where for one image:

filter_pixel = np.array([20, 20, 20])
image = np.array([[[20, 20, 20], [30, 30, 30], [40, 40, 40]],
                  [[20, 10, 20], [20, 20, 20], [40, 40, 40]]])

new_image = np.where(np.all(image == filter_pixel, axis=2), 1,0)
print(new_image)

Output:

[[1 0 0]
 [0 1 0]]

edit, for a number of images:

filter_pixels = np.array([[20, 20, 20], [30, 30, 30]])
filter_pixels = filter_pixels[:, np.newaxis, np.newaxis, :]
images = np.array([[[[20, 20, 20], [30, 30, 30], [40, 40, 40]],
                    [[20, 10, 20], [20, 20, 20], [40, 40, 40]]],
                  [[[20, 20, 20], [30, 30, 30], [40, 40, 40]],
                   [[20, 10, 30], [20, 20, 20], [30, 30, 30]]]])

new_images = np.where(np.all(images == filter_pixels, axis=3), 1, 0)

print(new_images)

Output:

[[[1 0 0]
  [0 1 0]]

 [[0 1 0]
  [0 0 1]]]

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

7 Comments

The Problem with this approach is that it returns 1 if one element matches the filter. e.g. image = np.array([[20, 20, 20], [20, 30, 30], [40, 40, 40]]) would result in [1, 1, 0] which is not what i want
Yea , you are right. Just edited to make it work.
Thanks, I figured it out with your approach. I had to use axis=3 since my array has one dimension more than your test array, but it works just fine and is computationally fast. Thanks a Lot!
I added the complete answer for you question, it now works for a number of images.
The Array I have after succesfully encoding it always consists of one one and the rest zeros. So for example the output could be [1,0,0,0,0,0,0,0,0,0,0,0]. This would be interpreted as class 0 (from 12). Now i want to replace these classes with the respective rgb values again, so if class 0 corrosponds to RBG [80, 80, 80] i want to insert that into the array changing its shape from (16, 128, 256, 12) to (16, 128, 256, 3) again. The output then should match the before existing array which I encoded
|

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.