Problem with ofxCvFloatImage setFromPixels()

Hi,

I’ve been playing around with of for some time but I’m still a beginner and now I’m completely stuck on this problem.
I really hope someone can help me figure this out.

I’ve included simplified code to isolate the problem and a screenshot of running the app below.

Header:

#include "ofMain.h"
#include "ofxOpenCv.h"

class ofApp : public ofBaseApp{
    public:
        /*
            The usual stuff
        */
        ofFloatImage mx;
        ofFloatImage my;

        ofxCvFloatImage cx;
        ofxCvFloatImage cy;

};

Implementation:

void ofApp::setup(){
    mx.loadImage("x.exr");
    my.loadImage("y.exr");

    //cx.setUseTexture(false);
    cx.allocate(mx.width, mx.height);
    cy.allocate(my.width, my.height);

    cx.setFromPixels(mx.getPixels(), mx.width, mx.height);
    cy.setFromPixels(my.getPixels(), my.width, my.height);

}

//--------------------------------------------------------------
void ofApp::draw(){
    ofSetColor(255);
    mx.draw(0,0);
    my.draw(256,0);
    cx.draw(0,256);
    cy.draw(256,256);
    ofSetColor(255,0,0);
    ofDrawBitmapString("X", 20, 20);
    ofDrawBitmapString("Y", 276, 20);
    ofDrawBitmapString("cX", 20, 276);
    ofDrawBitmapString("cY", 276, 276);
}

Result:

I think the image and the code say pretty much everything. I don’t understand why the images look so weird but suspect maybe something with the pixel type?
I’ve googled around and read through the docs and tried more things than I care to admit.

My question is what am I doing wrong?

Help is greatly appreciated

Hi @koda, ofCvFloatImage seems to support only gray images:

void ofxCvFloatImage::init() {
    ipldepth = IPL_DEPTH_32F;
    iplchannels = 1;
    gldepth = GL_FLOAT;
    glchannels = GL_LUMINANCE;
    bFloatPixelsDirty = true;
    cvGrayscaleImage = NULL;
    scaleMin = 0.0f;
    scaleMax = 1.0f;
}

I don’t know what you’re trying to do with ofxOpenCv, but in general, ofxCv is more actively developed and has better APIs.

Hi Micuat, thanks for replying.

After posting I found ofxCv and got everything up and running within minutes. But what I really want is to know what I was doing wrong.
So you’re saying ofCvFloatImage expects 1 channel (gray) and gets 3 channels (rgb)? Which might explain why everything looks scaled up by about a factor of 3x :smile:. I’ll look into that and will update if i solve it.

Problem Solved!
For the record here’s some code that worked:

Header:

    #include "ofMain.h"
    #include "ofxOpenCv.h"
    
    class ofApp : public ofBaseApp{
    
        public:
/*
The usual stuff
*/

            ofFloatImage mx;
            ofFloatImage my;
            ofFloatImage mxg; //added
            ofFloatImage myg; //added
    
            ofxCvFloatImage cx;
            ofxCvFloatImage cy;
    };

Implementation:

void ofApp::setup(){
    mx.loadImage("x.exr");
    my.loadImage("y.exr");

    mxg.allocate(mx.width, mx.height, OF_IMAGE_GRAYSCALE);
    myg.allocate(my.width, my.height, OF_IMAGE_GRAYSCALE);

    for (int x=0; x<mxg.width; x++){
        for (int y=0; y<mxg.height; y++){
            mxg.setColor(x, y, mx.getColor(x, y));
        }
    }
    mxg.update();

    for (int x=0; x<mxg.width; x++){
        for (int y=0; y<mxg.height; y++){
            myg.setColor(x, y, my.getColor(x, y));
        }
    }
    myg.update();

    //cx.setUseTexture(false);
    cx.allocate(mxg.width, mxg.height);
    cy.allocate(myg.width, myg.height);

    //cx.setFromPixels(mx.getPixels(), mx.width, mx.height);
    cx.setFromPixels(mxg.getPixels(), mxg.width, mxg.height);
    cy.setFromPixels(myg.getPixels(), myg.width, myg.height);

}


void ofApp::draw(){
    ofSetColor(255);
    //mx.draw(0,0);
    //my.draw(256,0);
    mxg.draw(0,0);
    myg.draw(256,0);
    cx.draw(0,256);
    cy.draw(256,256);
    ofSetColor(255,0,0);
    ofDrawBitmapString("X", 20, 20);
    ofDrawBitmapString("Y", 276, 20);
    ofDrawBitmapString("cX", 20, 276);
    ofDrawBitmapString("cY", 276, 276);
}
1 Like