trying to build a super-simple threaded opencv vidgrabber haar finder

I’m trying to build a super simple barebones opencv vidgrabber haar finder that is threaded, using ofxThread. I’ve tried to craft one using my working non-threaded haar finder and the ofxThread example, but am running into multiple problems. It builds but dies, best I can tell complaining about ofTexture::loadData().

I found another thread where someone built a multi-threaded haar finder but it is not simple and is not making much sense to me.

I did read about the need to not use drawing functions in the thread. I had hoped that calling my thread’s draw() from the main draw() would get around this, but maybe not? If not, how do I thread the finder while also getting its results drawn to the screen?

Here’s the code:

// *** testApp.cpp *** //  
#include "testApp.h"  
void testApp::setup(){ TO.start(); }  
void testApp::update(){ }  
void testApp::draw(){ TO.draw(); }  
// *** testApp.h *** //  
#pragma once  
#include "ofMain.h"  
#include "threadedObject.h"  
class testApp : public ofBaseApp{  
		void setup();  
		void update();  
		void draw();  
        threadedObject TO;  
// *** threadedObject.h *** //  
#include "ofMain.h"  
#include "ofxCvHaarFinder.h"  
#include "ofxOpenCv.h"  
class threadedObject : public ofThread{  
    ofxCvHaarFinder finder;  
    ofVideoGrabber  vidGrabber;  
    ofxCvGrayscaleImage grayImage;  
    ofxCvColorImage colorImage;  
    void start(){  
        startThread(true, false);   // blocking, verbose  
    void stop(){  
    void threadedFunction(){  
        while( isThreadRunning() != 0 ){  
            if( lock() ){  
                bool newFrame = false;  
                newFrame = vidGrabber.isFrameNew();  
                if(newFrame) {  
                    grayImage = colorImage;  
                    finder.findHaarObjects(grayImage, 80, 80);  
    void draw(){  
        if( lock() ){  
            colorImage.draw(0, 0);  
            for(int i = 0; i < finder.blobs.size(); i++) {  
                ofRectangle cur = finder.blobs[i].boundingRect;  
                ofRect(cur.x, cur.y, cur.width, cur.height);  

all the images have a texture that is updated on update. you need to deactivate it with setUseTexture(false) and then use a texture to actually draw the data in draw

what Arturo is saying and be aware that there was (at least in my version) a protected ofxCvGrayscaleImage in ofxCvHaarFinder that uses a texture

The way i got around it was to extend ofxCvHaarFinder as opposed to modding it

AFAIK, locking and the threaded function will give you no performance improvement because what lock does is to prevent any other thread to run.
BTW, OpenGL calls can nly be done from the main thread, hence draw() and anything related to textures.
on ofxThread only threadedfunction runs on a different thread, any other function will run on the main thread.
Check this thread as it might give you a good approach about threads and share images between them.