- I got an image like this where I'D like to apply some special type of filter on to:
- I got a RGB color (
49, 76, 14
) for the dark green area (the biggest one).
So I'd like to get some kind of threshold property for my filtering purposes: I'd like to filter each of the image pixels to check if it's color is near to the given RGB value (49, 76, 14
). The threshold symbolizes the offset-rate.
Threshold small :
--> `(49, 76, 14) close to (48, 75, 13)`
--> `(49, 76, 14) "not" close to (50, 75, 13)`
Threshold big :
--> `(49, 76, 14) close to (48, 75, 13)`
--> `(49, 76, 14) "also" close to (50, 75, 13)`
Filtering the image with the smallest threshold just the given rgb color will get filtered:
A bigger threshold will also filter colors near the given rgb color:
How can I achieve coding a function that is able to check if one color is close to another color (+threshold property) (+using rgb values)?
Note:
This was the code I was thinking above first but testing it - it doesn't really lead to success (by filtering the given value the small left-top area should get filtered at first because it's color does nearly look like the given one. But this code is first matching the gray one that is obviously not matching to the given color!)
var offset = (threshold/100.0) * 255.0
var color = [r,g,b]
var r = redvalue, g = greenvalue, b = bluevalue
if !(
(r >= color[0]-offset) &&
(r <= color[0]+offset) &&
(g >= color[1]-offset) &&
(g <= color[1]+offset) &&
(b >= color[2]-offset) &&
(b <= color[2]+offset)
) { // apply filter }
Any help would be very appreciated. Some source code written in Swift or JavaScript or even some PseudoCode would be also fine.
Edit: One spontaneous idea was to convert the rgb-colors int lab-colors to afterward compute the euclidean distance from the two colors. But I'm not sure if this solution will work and also not if it is the most efficient one :)