openCV masking: Problems going from ofFbo to pixels to IPLIMage

Hello collective wisdom, I need your help.

I’m working on masking parts of the camera image before doing an OpenCV analysis.

Since the image I’m analysing is very distorted and I want to get rid of all the image outside the observed zone, I’m drawing the mask using polygons inside an FBO (and not a normal ROI).

Now I’m hitting a wall when trying to transfer that nice FBO-drawing into to the greyscale image that will ultimately mask the camera input using OpenCVs copy operator with a third IPL-Image.

This thread shows very nicely how the masking is done, except for the FBO part. The example shows loading a mask from a file on disk:

The sequence of image containers I have ATM is as follows:

  • FBO in GL_RGBA mode - I’m drawing the b/w mask into the rgba fbo/texture [*my first crunch point*]
  • ofPixels in OF_IMAGE_RGBA - next I’m copying the fbo-content into the 4 channel pixels-array with readToPixels()
  • ofxCvColorimage - next I’m copying the pixels array into the texture of the ofxCvColorimage [*this is the second crunch point*]
  • ofxCvGrayscaleImage - finally I try to copy the color image to the grayscale image to convert from 4 channels to one [*the third crunchpoint*]
    ofSetColor(255, 255, 255, 255);  
    ofRect(0, 0, 640, 480);  
    ofSetColor(0, 0, 0);    
    ofRect(0, 0, 640, 20);   // the actual masked zone           
// crunchpoint #1  
// I tried using an FBO set to GL_LUMINANCE copied to a pixels array   
// of type OF_IMAGE_MONO that would copy directly to a ofxCvGrayscaleImage  
// that's not working  
maskFBO.readToPixels(maskPixels); // copy to pixels  
ofTexture temp = maskColorImage.getTextureReference();  // get texture reference from destination image  
temp.loadData(maskPixels);                              // load pixels into destination texture  
// crunchpoint #2:   
// for some reason maskColorImage.setFromPixels(maskPixels) doesn't work properly: the output is scrambled  
// with this workaround I can verify that the texture is correct by calling maskColorImage.draw()  
maskGrayImage = maskColorImage;  
// crunchpoint #3:  
// the copy operation from ofxCvColorImage to ofxGrayscaleImage doesn't work  
// maskGrayImage.draw() shows a black image  

the rendered images look like this (see attached)

the mask as it should be via textureReference and loadData()
the mask i get when using maskColorImage.setFromPixels(maskPixels)
the resulting ofxCvGreyscaleImage

I’m working in of_osx_0071_release on Mac OS X 10.6.8 with Xcode 4.2.

I’ be grateful for any hint on how to solve this