Hey I’ve been trying to use ofxCv::CLD with an ofImage from ofVideoGrabber. The application compiles but crashes with CLD, but works great with ofxCv::Canny and ofxCv::Sobel. If I load a jpeg image instead of setting one from video, all 3 functions work fine. Thanks in advance for any insights!
Here is the code:
// ofApp.h
#include "ofMain.h"
#include "ofxCv.h"
class ofApp : public ofBaseApp{
public:
// the usual stuff, then:
ofVideoGrabber cam;
ofImage camImage;
ofImage outputImage;
};
// ofApp.cpp
void ofApp::setup(){
int width = 640;
int height = 480;
cam.setup(width, height);
outputImage.allocate(width, height, OF_IMAGE_GRAYSCALE);
}
void ofApp::update(){
cam.update();
if(cam.isFrameNew())
{
camImage.setFromPixels(cam.getPixels());
// doesn't seem to matter if .update() is called
//camImage.update();
// this crashes
//ofxCv::CLD(camImage, outputImage);
// this works fine; no crash
ofxCv::Canny(camImage, outputImage, 100, 150);
// this also works fine; no crash
//camImage.setImageType(OF_IMAGE_GRAYSCALE);
//ofxCv::Sobel(camImage, outputImage);
outputImage.update();
}
}
//--------------------------------------------------------------
void ofApp::draw(){
outputImage.draw(0, 0);
}
Hey @edapx thanks for a quick reply! The error output in the Application Output window of Qt Creator is:
The program has unexpectedly finished.
The process was ended forcefully.
/home/chilina/oF0.10.1/apps/ofxCv/cldVideo1901/bin/cldVideo1901_debug crashed.
I did try allocating camImage with all three ofImageTypes and the “crash” persists. I don’t know how to get more specific error info on the crash; this kind of error/behavior often occurs when (for instance) reading a std::vector beyond its end. I can call camImage.update() in ofApp::update, and then camImage.draw() in ofApp::draw to draw it, so it seems like camImage is allocated and filled with pixels from cam.
I would check if the two images have the same size. Then it maybe worth to put a debugger in ofxCv::CLD(camImage, outputImage); to find the line that causes the crash.
OK I think I have it “kind of” running, and thanks so much for your help @edapx. The images do have the same values for .getWidth() and .getHeight().
After some testing, I have the following (speculative) comments:
ofxCv::CLD needs a grayscale (OF_IMAGE_GRAYSCALE) image as a source.
That grayscale image needs to be a “real image”; calling .allocate() and then setting all of the colors to back or white still crashes. Maybe the OpenCv component of ofxCv needs “something real” to work on.
I need to call ofxCv::CLD once in ofApp::setup. The CLD example in the addon does not do this, and it still works. However my app still crashes unless this call is made, even if the destination image (outputImage in my case) is allocated.
So, here is the code that runs:
// ofApp.h, same as above
// ofApp.cpp
void ofApp::setup(){
int width = 640;
int height = 480;
cam.setup(width, height);
camImage.load("image.jpg");
camImage.resize(width, height);
camImage.setImageType(OF_IMAGE_GRAYSCALE);
ofxCv::CLD(camImage, outputImage);
outputImage.update();
}
void ofApp::update(){
cam.update();
if(cam.isFrameNew())
{
camImage.setFromPixels(cam.getPixels());
camImage.setImageType(OF_IMAGE_GRAYSCALE);
camImage.update(); // not sure if this is necessary, but it can't hurt
ofxCv::CLD(camImage, outputImage);
outputImage.update();
}
}
void ofApp::draw(){
outputImage.draw(0, 0);
}
this is not needed. It would be needed just in case you would like to draw camImage on the screen, as the pixel data inside the image should be reflected in the texture on the GPU.