1

I have an image on which there is a full-red (b, g, r = 0, 0, 255) shape that I wish to isolate.

I am currently doing the following to change to black all the non-red pixels.

im = image.copy()

b1, g1, r1 = 0, 0, 255
b2, g2, r2 = 0, 0, 0

# Separate the channels from image
b, g, r = im[:, :, 0], im[:, :, 1], im[:, :, 2]

mask = (b != b1) & (g != g1) & (r != r1)
im[:, :, :3][mask] = [b2, g2, r2]

But even though the only red part in image is the red shape added programatically, some pixels are not becoming red and stays black.

So the question is how this could be possible ? Shouldn't them match the mask ?

Here is the image of what I get (I need a black and red image instead)

Image result

1 Answer 1

0

You've been bitten by De Morgan's laws. You need to use OR there, not AND.

Here's a short demo derived from your code.

import numpy as np

img = np.random.choice([0, 255], size=(3, 4, 3), replace=True)
print(img, '\n')

b1, g1, r1 = 0, 0, 255
b2, g2, r2 = 0, 0, 0

# Separate the channels from image
b, g, r = img[:, :, 0], img[:, :, 1], img[:, :, 2]

mask = (b != b1) & (g != g1) & (r != r1)
im = img.copy()
im[:, :, :3][mask] = [b2, g2, r2]
print(im, '\n')

mask = (b != b1) | (g != g1) | (r != r1)
im = img.copy()
im[:, :, :3][mask] = [b2, g2, r2]
print(im, '\n')

typical output

[[[  0   0 255]
  [  0 255   0]
  [255 255 255]
  [255 255   0]]

 [[  0   0   0]
  [255 255 255]
  [255 255 255]
  [  0   0 255]]

 [[255   0   0]
  [255 255 255]
  [255 255   0]
  [  0   0   0]]] 

[[[  0   0 255]
  [  0 255   0]
  [255 255 255]
  [  0   0   0]]

 [[  0   0   0]
  [255 255 255]
  [255 255 255]
  [  0   0 255]]

 [[255   0   0]
  [255 255 255]
  [  0   0   0]
  [  0   0   0]]] 

[[[  0   0 255]
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]]

 [[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  [  0   0 255]]

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

8 Comments

In fact I tried to do that in order to follow the De Morgan's law but then I got an entirely black image :/ Does that mean that my 'full-red' is not that full ?
But thanks anyway, I'll have to find another way to isolate my red shape contour
@JoachimHuet I just had a quick look at the image in your question, and it's definitely not pure red. Some of the pixels have FF in the red channel, but some have FE, and there are values of 1 or 2 in the green channel.
Yeah that's why.. I guess the android RED is not pure red, or maybe it changes during the format conversion.. What would you suggest to get only this reddish contour ?
@JoachimHuet I think you'll have to do a subtraction, using signed arithmetic, and grab pixels that are within a small threshold of the desired RGB value. A simple format conversion may alter colours, due to gamma correction, but that should perform a fixed mapping from one RGB value to another. However, if there's any scaling happening that's quite likely to modify colours.
|

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.