Iplimage to ofxcvgrayscaleimage problem

Hello again :slight_smile:

I have some troubles converting an IplImage that is used as a depth image from cvFindStereoCorrespondence to ofxcvgrayscaleimage, which is very convenient in order to use the draw function afterwards :slight_smile:

I convert using the = operator, but I get this error : “images need to match in size, channels, and depth”

In my header file I have declared :

  
  
 ofxCvGrayscaleImage depthimg;  
IplImage* depthImage;  
  

At my .cpp file the code that concerns my problem is:

  
    depthImage = cvCreateImage(cvSize(image1->width,image1->height), IPL_DEPTH_8U, 1);  
  
    image1_wrp = cvCreateImage(cvSize(image1->width,image1->height),image1->depth,1);  
    image2_wrp = cvCreateImage(cvSize(image2->width,image2->height),image2->depth,1);  
  
    // warp the source image using the homography  
    cvWarpPerspective( image1, image1_wrp ,homography_out1);  
  
    cvWarpPerspective( image2  , image2_wrp,homography_out1);  
  
  
    // compute the disparity map  
    cvFindStereoCorrespondence(image1_wrp , image2_wrp, CV_DISPARITY_BIRCHFIELD, depthImage,50, 15, 3, 6, 8, 15);  
  
    //convert  
    depthimg = depthImage;  
  

I know that the = operator is using the ofxcvImage = operator function which is this one:

  
void ofxCvImage::operator = ( const IplImage* mom ) {  
if( mom->width == width && mom->height == height &&  
mom->nChannels == cvImage->nChannels &&  
        mom->depth == cvImage->depth )  
    {  
cvCopy( mom, cvImage );  
        flagImageChanged();  
} else {  
        ofLog(OF_LOG_ERROR, "in =, images need to match in size, channels, and depth");  
}  
}  

How can I set the width, height, depth and channels so that I won’t get this error?
Thank you very much!

Also if I change the last line to :

  
depthimg.setFromPixels((unsigned char*)depthImage->imageData, depthImage->width, depthImage->height);  

I get “in setFromPixels, size mismatch” …so it’s the size. Hmmm…

I am also having issues with openCV telling me 2 images that ARE the same size, aren’t. Keep me posted if you figure it out.

Hey there.

I was also having the same problem, didnt manage to fix it bout found a workarround.
When you allocate a cvGrayscaleImage, it sets everything up for you.
So instead of creating iplImages myself, I just create a cvImage and copy its iplImage like so;

  
  
ofxCvGrayscaleImage cvImageGray1;  
ofxCvGrayscaleImage cvImageGray2;  
ofxCvGrayscaleImage cvImageGrayDest;  
  
cvImageGray1.allocate(width, height);  
cvImageGray2.allocate(width, height);  
cvImageGrayDest.allocate(width, height);  
  
IplImage* iplImageGray1;  
IplImage* iplImageGray2;  
IplImage* iplImageGrayDest;  
  
iplImageGray1 = cvImageGray1.getCvImage();  //Get the pre-setup iplImage from the cvImage  
iplImageGray2 = cvImageGray2.getCvImage();    
iplImageGrayDest = cvImageGray2.getCvImage();    
  
//Then you can use cv functions like so  
cvAdd(iplImageGray1,iplImageGray2,iplImageGrayDest);  
//then flag the dest as changed, and cvImageGrayDest is good to go  
cvImageGrayDest.flagImageChanged();  
  
  

Worked for me, but im sure the proper method is somewhere in the O’REILLY OpenCV (http://amzn.to/qhefsw) book if you want to check it out.

So, what I found out is that this simple piece of code :

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	camWidth 		= 320;	// try to grab at this size.  
	camHeight 		= 240;  
  
// Left camera init  
vidGrabber.setVerbose(true);  
 vidGrabber.listDevices();  
   vidGrabber.setDeviceID(1);  
      vidGrabber.initGrabber(camWidth,camHeight);  
  
//Right camera init  
   vidGrabber2.setVerbose(true);  
   vidGrabber2.setDeviceID(0);  
   vidGrabber2.initGrabber(camWidth,camHeight);  
  
//allocating memory for images  
  cvColor2.allocate(camWidth, camHeight);  
  cvColor1.allocate(camWidth, camHeight);  
  
  cvGray1.allocate(camWidth,camHeight);  
  cvGray2.allocate(camWidth,camHeight);  
  cvGray1_warped.allocate(camWidth,camHeight);  
  cvGray2_warped.allocate(camWidth,camHeight);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
	ofBackground(100,100,100);  
  
	vidGrabber.grabFrame();  
  
    // init boolean from header file for every new frame  
    bNewFrame = vidGrabber.isFrameNew();  
  
if (bNewFrame){  
    // get video from camera and send it to ofxCvColorImage  
    cvColor1.setFromPixels(vidGrabber.getPixels(), camWidth, camHeight);  
    // ofxCvColorImage to ofxCvGrayscaleImage  
    cvGray1 = cvColor1;  
}  
  
    // again, same procedure for the Right camera  
	vidGrabber2.grabFrame();  
  
	bNewFrame2 = vidGrabber2.isFrameNew();  
  
if (bNewFrame2){  
    cvColor2.setFromPixels(vidGrabber2.getPixels(), camWidth, camHeight);  
    cvGray2 = cvColor2;  
}  
    // allocate memory for the IplImage* depth image  
    depthImage = cvCreateImage(cvGetSize(cvGray1.getCvImage()), IPL_DEPTH_8U, 1);  
  
    // compute the desparity map  
    cvFindStereoCorrespondence(cvGray1.getCvImage() , cvGray2.getCvImage(), CV_DISPARITY_BIRCHFIELD, depthImage,50, 15, 3, 6, 8, 15);  
  
  
    //allocate memory for ofxCvGrayscaleImage for converting from IplImage to ofxCvGrayscaleImage  
    depthimg.allocate(depthImage->width,depthImage->height);  
  
    //convert  
    depthimg = depthImage;  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
    ofSetColor(0xffffff);  
  
    vidGrabber.draw(20,20);  
  
    vidGrabber2.draw(20+camWidth,20,camWidth,camHeight);  
  
    cvGray2.draw(20+camWidth, 300, camWidth,camHeight);  
    cvGray1.draw(20,300);  
  
    depthimg.draw(600,400);  
  
  }  
  

…is running on “of_preRelease_v0062_linux64_FAT” and NOT on “of_preRelease_v007_linux64_FAT”.

in the second case I get a SIGSEGV, Segmentation fault.

@TheronBurger : I am going to try your code today and report what happened, thank you very much!!!

First of all, in your code, when does the conversion happen? When you say “cvImageGrayDest.flagImageChanged();” ? Or do I have to do something afterwards?

Anyway, I debbuged the simple code I posted previously, and the problem is when I do the allocation! I get the Segmentation fault and a “OF_LOG_WARNING:in allocate, reallocating a ofxCvImage”

So, unfortunately the conversion is not right now my problem :confused: