Thresholding in color image
I want to threshold an color image with particular range. If the input image pixel is with in that range it must be present in the output image. i.e. my range is : R: 122-200 ; G: 80-100 ; B:40-68
if a particular pixel of image I(x,y) is with in the range eg. I(100,130) { R:130; G:94; B: 53} then the values must be copied to output image O(x,y);
Inrange function gives binary image as output; i need RGB image as output.
i tried the following.
input image -> HSV;
HSV range selection;
HSV->RGB
input matrix: img;
output matrix : A
for (int i=0;i<img.rows;i++)
{
for(int j=0;j<img.cols;j++)
{
Vec3f intensity = img.at<Vec3b>(i,j);
float H=intensity.val[0];
float S=intensity.val[1];
float V=intensity.val[2];
if( ((H>170 && H<=179) && (S>155 && S<255) && (V>150 && V<230))
{
A.data[A.step[0]*i + A.step[1]* j + 0] = H;
A.data[A.step[0]*i + A.step[1]* j + 1] =S;
A.data[A.step[0]*i + A.step[1]* j + 2] = V;
}
}
}
the output is
it works but slows down. is there any alternative?
http://docs.opencv.org/modules/core/d...
If you like pointer you can try too
@berak I got this output from using inrange() . How can i get the output of image containing the rgb image from this? Inrange produces binary image as output
@LBerger. Thanks for your code. but when i tried i got the following output.
"How can i get the output of image containing the rgb image from this?" - use it as a mask with binary_and() or similar.
I have read your code too fast : your data image was not CV_32F but was byte
this code suppose that your data are CV_8UC3 for img and A
@LBerger, you only increase your pointer if it found a value inside the range ? that's why it's only drawing all red pixels on the left side, but not black ones. you will need something like:
@berak is correct. if the value of pixel is with in the range assign that value. else increment the pointer to skip that pixel.
@berak You're right. That's what happen to me when I don't test my code! thanks