Colouring Pixels

Hi,

I’m working on an application using OF and openCV. When contours are detected coloured pixels should appear on a black background. It’s working except I’m getting little white pixels instead of a more fluid colourful result. Any advice on how I can change the code to make it coloured and more fluid? I’ve attached a file to show what the current output it.

  
void testApp::setup(){  
   // ofEnableAlphaBlending();  
      
    hue=0;  
    sat=0;  
    val=0;   
  
    w=480;  
    h=320;  
  
    counter=0;  
    drawEnabled=true;  
    hCycle=0;   
    hFlip = true;  
    lockedOnPixels=new unsigned char[w*h];  
    lockedOnTexture.allocate(w, h, GL_LUMINANCE);  
    lockedOutput.allocate(w,h);  
   	scribblePixels = new unsigned char[w * h];  
      
	for (int i = 0; i < (w * h); i++) {  
		scribblePixels[i] = 0;  
	}  
	scribbleOutput.allocate(w, h, GL_LUMINANCE);  
  
    blearnbackground = true;  
      
    movie.setVerbose(true);  
   movie.initGrabber(480,320);  
    movie.setUseTexture(false);  
      
    colorImg.allocate(480,320);  
    grayimg.allocate(480,320);  
    grayBg.allocate(480,320);  
    grayDiff.allocate(480,320);  
}  
  

  
void testApp::update(){  
    ofBackground(200, 200, 200);  
    grayimg. flagImageChanged();  
    grayDiff.flagImageChanged();  
    grayBg.flagImageChanged();  
      
     
    //cycle pixels through different colours  
    if (hCycle < 255) hCycle += 0.01;  
	else hCycle = 0;  
    hue=hCycle;  
	color.setHue(hCycle);  
	color.setHsb(hCycle, 255, 255);  
     
    lockedOnTexture.loadData(lockedOnPixels, w, h, GL_LUMINANCE);  
    lockedOutput.setFromPixels(lockedOnPixels, w, h);  
      
    movie.grabFrame();  
      
    if (movie.isFrameNew()){  
        colorImg.setFromPixels(movie.getPixelsRef());  
        grayimg = colorImg; // convert our color image to a grayscale image  
        if (blearnbackground == true) {  
            grayBg = grayimg; // update the background image  
            blearnbackground = false;  
        }  
        grayDiff.absDiff(grayBg, grayimg);  
        grayDiff.threshold(30);  
       contours.findContours(grayDiff, 5, (480*320)/2, 2, false, true);  
    }  
}  
  

  
void testApp::updateFilm(unsigned char *mskPixels){  
      
    unsigned char * huePixels = colorImg.getPixels();  
    unsigned char * satPixels = grayimg.getPixels();  
    unsigned char * briPixels = grayBg.getPixels();  
	for( int i=0; i<w; i++ ){  
        		for( int j=0; j<h; j++ ){  
             int p = j * w + i;  
       			if ( mskPixels[p] > 0){  
  
	hue = huePixels[p + (3 * colorImg.width)];  
	sat = satPixels[p + (3 * grayimg.width)];  
	val = briPixels[p + (3 * grayBg.width)];  
			}  
		}  
	}  
  
}  

  
void testApp::draw(){  
    ofSetBackgroundColor(0,0,0);  
    ofSetHexColor(0xffffff);  
  scribbleOutput.draw(500, 500,480,320);  
    colorImg.draw(0, 0, 480, 320);  
  
    //find blobs draw coloured pixels  
    for(int i = 0; i <contours.nBlobs; i++) {  
    
      int lineWidth = 2;  
         
       for (int i = -lineWidth; i <= lineWidth; i++) {  
           for (int j = -lineWidth; j <= lineWidth; j++) {  
              scribblePixels[((int)(contours.blobs.at(0).centroid.y + i) * w) + (int)((w - lineWidth - 1) - contours.blobs.at(0).centroid.x + j)] = 255;  
          
            }  
             
              
        }  
   }  
  
  //colour the pixels and add to texture  
    updateFilm(scribblePixels);  
  scribbleOutput.loadData(scribblePixels, w, h, GL_LUMINANCE);  
   }  
  

![](http://forum.openframeworks.cc/uploads/default/2969/pixel result .jpg)

Just realized the problem was the ofsetHexColor () function. How can I change this to cycle through a colour array?

Thanks!

do you want:

  1. entire image to change color from frame-to-frame (everything is red, then slowly fades, to green, then to blue, etc) or

  2. do you want each of the pixels to cycle through different colors erratically?

if you want 1, you can just say something like:

  
  
float hue = ofMap(sin(ofGetElapsedTimef()), -1, 1, 0, 255);  
ofColor color = ofColor::fromHsb(hue, 255, 255);  
ofSetColor(color);  
  

if you want the second thing, you should make your image a color image instead of a GL_LUMINANCE. but it’s going to look really crazy because the color for a single pixel is going to change every frame! you would need to start tracking all your blobs, giving them IDs and unique colors.

if you use ofxCv instead there’s an example like that here https://github.com/kylemcdonald/ofxCv/tree/master/example-contours-tracking

good luck!

Thanks! yeah I would want something more like 2, less unified. thanks for the code.

Sorry by changing the image to imgColor instead of GL_Luminance, do you mean changing

  
scribbleOutput.loadData  

? cause when I try to change it to imgColor it errors.