How to subtract one color from an image?

5 views (last 30 days)
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
Ankurdeep Kaur
Ankurdeep Kaur on 27 Jan 2017
Ok, suggest me a method to do the same.
Ankurdeep Kaur
Ankurdeep Kaur on 27 Jan 2017
@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

Benjamin Kraus
Benjamin Kraus on 27 Jan 2017
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
Image Analyst
Image Analyst on 11 Feb 2017
Like Adam, Guillaume, and I said, you don't want to subtract different leaf images to compare them anyway.
Ankurdeep Kaur
Ankurdeep Kaur on 13 Feb 2017
@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)

Image Analyst
Image Analyst on 28 Jan 2017
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.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!