How to subtract one color from an image?

If I have two images, both having red and black colors with RGB values [1 0 0] and [0 0 0]. The areas covered by red and black colors in both images are different. I just want to show that the two images are similar as they are having the same RGB values. I hope this can be done if somehow I am able to subtract the black color from the images and show that both the leftover color's rgb value is same.(i.e. red, [1 0 0])

7 Comments

I don't understand your question.
Black is the absence of colour. It's nothingness. You can't subtract nothing from an image, it does not make sense. As you've noted black in RGB is [0 0 0], subtract 0 from anything and you still have the anything.
Your statement that the two images are similar as they have the same RGB values is bizarre as well. In the previous sentence, you said one has red (RGB [1 0 0]) the other is black (RGB [0 0 0]) with presumably some colour (white?) somewhere else. RGB [1 0 0] is not the same at all as RGB [0 0 0].
Can you clarify what it is you want, preferably with some example images.
ok..let me ask the question in a different manner. I have two images (uploaded, please ignore the noise) I want to prove that the images are similar as the pattern of the appearance of the colors (red, green, blue,cyan and magenta is in order).
You should probably edit your original question to make more sense. The images you posted have numerous colours, none of which are black.
What i meant earlier was that the red color in both the images was extracted which led to an image with red and black color, as uploaded and wanted to compare the red color of one image with the other's on the basis of the RGB value [1 0 0]
Well, you can just subtract one from another if your images are red and black only, but it isn't going to give you a very good or useful measure of how similar they are. For a start you will get a mix or positive and negative numbers and it will be very sensitive to any pixel differences.
Ok, suggest me a method to do the same.
@Guillaume- I hope you have understood the question now. Will you suggest me something here, or shall I post a new question?

Sign in to comment.

 Accepted Answer

Let's say the two images are A and B, both are MxNx3 matrices:
red = shiftdim([1 0 0],-1);
black = shiftdim([0 0 0],-1);
isAred = all(A == red, 3);
isAblack = all(A == black, 3);
isBred = all(B == red, 3);
isBblack = all(A == black, 3);
bothAandBred = isAred & isBred;
bothAandBblack = isAblack & isBblack;
bothSameColor = bothAandBred | bothAandBblack;
similarity = sum(bothSameColor(:))/numel(bothSameColor);
If you are guaranteed that the colors are all either red or black, the code above has a lot of redundancies. In that case, once you have isAred and isBred you can think of them as 2D black and white images, where white has replaced red. 1 indicates red/white, 0 indicates black. That makes the comparison (perhaps using subtraction) a lot easier.
There are also some image comparison tools in the Image Processing Toolbox you may want to consider. For example imabsdiff.
Note: The code above will only work in R2016b because of the new implicit dimension expansion that was introduced in R2016b (search for "Implicit Expansion" in the R2016b Release Note. To achieve the same behavior in R2016a or earlier you would have to use something like bsxfun.
isAred = all(bsxfun(@eq, A, shiftdim([1 0 0],-1)), 3);

3 Comments

Thank you for the reply. But when I used this code, an error occurred saying :
Undefined function 'bsxfun' for input arguments of type 'matlab.graphics.primitive.Image'.
Error in cc211m (line 112)
isAred = all(bsxfun(@eq, A, shiftdim([1 0 0],-1)), 3);
Can you help me with this?
Like Adam, Guillaume, and I said, you don't want to subtract different leaf images to compare them anyway.
@Benjamin Kraus- Thank you so much for the help. I reached a step closer to my work by the help of your code. Can you please mention the algorithm you used for this similarity?

Sign in to comment.

More Answers (1)

This question is very deceptive and confusing. You don't even have these images originally. What you have, and showed in your other post, is a continuous RGB image of a leaf. Then, somehow, in a way you have not shared with us, you classified regions in the leaf and assigned these different class colors. Presumably the different images came from different leaves. It makes little sense to subtract the pseudocolored images. I mean what if one leaf was shifted slightly in the field of view? The whole subtraction would be drastically different! Don't do that. You are on a wild goose chase, going in the wrong direction. Don't waste your time.
What you need to do to compare the images is measure a bunch of things, like area, color, texture, feret diameters, etc. that describe the leaves. Then compare the feature vectors.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!