Ask Your Question

How to use InRange at Different Color Space and Range.

asked 2013-05-22 10:21:54 -0600

wuling gravatar image

updated 2020-11-15 02:08:27 -0600

Hi all, how to use InRange at different color space and Range. for example:in RGB space, 0<=R,G,B<=255, so,it's very easy to use:

inRange(const MatND& src, const Scalar& lowerb, const Scalar& upperb, MatND& dst)

But if i want the range out of the lowerb & upperb, for example: I want R color Range: 0<=R<=20, 100<=R<=255. like Aforge Color Filtering as picture. Also, in the other color space how to use InRange where the parameters out of the range. Thanks:)

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-05-22 10:39:47 -0600

berak gravatar image

updated 2013-05-22 10:43:16 -0600

inRange() can't do that, but not all hope's lost:

split your image into channels:

Mat orig; // bgr, probably
Mat chan[3];
split(orig,chan);  // chan[0]==blue, chan[1]==green, ..

apply your own operator on the channels to binarize them:

Mat bin_red = (chan[2]<20)|(chan[2]>100);  // note: single '|'
Mat bin_grn = (chan[1]<72)|(chan[1]>155);
Mat bin_blu = (chan[0]<64)|(chan[1]>155);

Mat bin = bin_red & bin_grn & bin_blu;  // where all conditions were met
edit flag offensive delete link more


It's very good idea,Thanks.

wuling gravatar imagewuling ( 2013-05-22 10:50:30 -0600 )edit

Question Tools


Asked: 2013-05-22 10:21:54 -0600

Seen: 11,238 times

Last updated: May 22 '13