How to subtract one color from an image?
5 views (last 30 days)
Show older comments
Ankurdeep Kaur
on 26 Jan 2017
Commented: Ankurdeep Kaur
on 13 Feb 2017
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
Accepted Answer
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
on 11 Feb 2017
Like Adam, Guillaume, and I said, you don't want to subtract different leaf images to compare them anyway.
More Answers (1)
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.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!