Problem with tracking other colors? and at the same time?

**Hey guys!

I have made a program that tracks the red color, but somehow I can’t get it to track e.g. the blue color or the green color?

  
void testApp::setup(){  
 color1.hue=0;  
    color1.sat=0;  
  
    // tolerances  
    satHigh = 255;  
    satLow = 100;  
    hueHigh = 12;  
    hueLow = 0;  

  
  
void testApp::draw(){  
  
    glDisable(GL_BLEND);  
  
	ofSetColor(255,255,255,255);  
  
	video.draw(0,0);  
  
	glEnable(GL_BLEND);  
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);  
  
	ofSetColor(255,0,0,45);  
  
	grayimg.draw(0,0);  
}  

How does the hue and sat values work in openframeworks?

cheers bassd**

Hi - could you post the code where you are actually doing the tracking of the red color?

All you posted was your setup and draw functions.

Theo

Ok here you go:

  
  
void testApp::setup(){  
  
    camWidth=420;  
    camHeight=340;  
  
    color1.pos = ofxVec2f(0,0);  
  
    video.initGrabber(camWidth, camHeight);  
    grayimg.allocate(camWidth,camHeight);  
  
    img.allocate(camWidth,camHeight);  
    imgHSV.allocate(camWidth,camHeight);  
    manipImg.allocate(camWidth, camHeight);  
    maskImg.allocate(camWidth,camHeight);  
  
    sat.allocate(camWidth, camHeight);  
    hue.allocate(camWidth, camHeight);  
    bri.allocate(camWidth, camHeight);  
  
    imgColor1.allocate(camWidth, camHeight);  
    imgColor2.allocate(camWidth, camHeight);  
    imgColor3.allocate(camWidth, camHeight);  
  
    trackedPixelsColor1 = new unsigned char [camWidth * camHeight];  
  
    outputTexColor1.allocate(camWidth, camHeight, GL_LUMINANCE);  
  
    color1.hue=0;  
    color1.sat=0;  
  
    // tolerances  
    satHigh = 255;  
    satLow = 100;  
    hueHigh = 12;  
    hueLow = 0;  
}  

  
  
void testApp::update(){  
  
    video.grabFrame();  
  
	img.setFromPixels(video.getPixels(), camWidth,camHeight);  
  
	imgHSV = img;  
	imgHSV.convertRgbToHsv();  
  
	imgHSV.convertToGrayscalePlanarImages(hue, sat, bri);  
  
	unsigned char* huePixels = hue.getPixels();  
	unsigned char* satPixels = sat.getPixels();  
	unsigned char* briPixels = bri.getPixels();  
  
	int nPixels = camWidth * camHeight;  
  
	//color1 = red  
    for (int i = 0; i < nPixels; i++){                                           //let's go through every pixel in hue pixels  
        if ((huePixels[i] >= color1.hue + hueLow && huePixels[i] <= color1.hue + hueHigh) &&    //if the hue is of a certain range  
            (satPixels[i] >= color1.sat + satLow && satPixels[i] <= color1.sat + satHigh)){    //if the saturation is of a certain range  
            trackedPixelsColor1[i] = 255;                                      //mark this corresponding pixel white  
        } else {  
            trackedPixelsColor1[i] = 0;                                        //if it doesn't fit then color the corresponding pixel black  
        }  
    }  
  
    outputTexColor1.loadData(trackedPixelsColor1,camWidth, camHeight,GL_LUMINANCE);  
  
	grayimg.setFromPixels(trackedPixelsColor1,camWidth, camHeight);  
  
    grayimg.erode_3x3();  
	grayimg.dilate_3x3();  
}  
  

I have changed the values of tolerance to this:

  
 // tolerances  
    satHigh = 255;  
    satLow = 100;  
    hueHigh = 255;  
    hueLow = 100;  

and the values of “ofsetColor” to this:

  
ofSetColor(0,0,255,45);  

**Now it tracks the blue color, but it also tracks the red color(draws it like blue pixels) :frowning: How can I only track pure blue?

cheers bassd**

Ok I have changed my tolerance values to this:

  
  
    satHigh = 255;  
    satLow = 50;  
    hueHigh = 110;  
    hueLow = 100;  

**It only tracks the blue color now, but is there any chance that I can track more colors at the same time ? e.g. red and blue?

cheers bassd**

you need to have different ranges, eg hueBlueLow, hueBlueHigh, hueRedLow, hueRedHigh, etc. if your hue is between hueBlueLow and hueBlueHigh then it’s blue, else if it is between hueRedLow and hueRedHigh then it’s red, etc.

[quote author=“damian”]you need to have different ranges, eg hueBlueLow, hueBlueHigh, hueRedLow, hueRedHigh, etc. if your hue is between hueBlueLow and hueBlueHigh then it’s blue, else if it is between hueRedLow and hueRedHigh then it’s red, etc.[/quote]Ok but my problem now is how to track two colors at the same time :frowning:

Worst case scenario you write one general function to track a colour with the parameters you give it and you run it twice on the same image, once for the first colour, again for the second colour.

/A

Ok thx a lot :slight_smile: