Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Well, i just spent 20 minutes writing up an answer that is working with threads - to be told by the forum that as a new user I have to wait 19 more hours to post an answer. My answer was nowhere to be seen, and upon clicking back on my browser it was forever gone.

It's been a while since the days I'd habitually copy everything in the clipboard to avoid this carry on.

The code at least: http://pastebin.com/T606VtCF

:D

Well, i just spent 20 minutes writing up an Forum/browser software deleted a more detailed answer that - but the following code is working with threads :)

I get 120fps in fastLoop and 63fps in the viewLoop - adding processing to fastLoop shows an eventual slowdown, once this reaches 63 they are both 63 and the threads I guess are then redundant.

It's pretty bare - but worked straight away, which was surprising. Maybe someone can point out issues with it?

#include "cameralibrary.h"
#include "face_supportcode.h"  

#include "opencv2/opencv.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>              
#include <thread>  
#include <mutex>          

#include "serial.h"

using namespace cv;
using namespace std;
using namespace CameraLibrary;

#define CAMERA_WIDTH 640
#define CAMERA_HEIGHT 480

mutex mtx;   // might not actually need to be told by the forum that as a new user I have to wait 19 more hours to post an answer. My answer was nowhere to be seen, and upon clicking back on my browser global ...

void fastLoopCode(Mat& frameCV) {

        //mutex mtx;   //try it was forever gone.  

It's been a while since the days I'd habitually copy everything in the clipboard to avoid this carry on.

The code at least: http://pastebin.com/T606VtCF

:D

here CameraManager::X().WaitForInitialization(); Camera *camera = CameraManager::X().GetCamera(); camera->SetVideoType(Core::GrayscaleMode); Frame *frame; camera->Start(); while(1){ frame = camera->GetLatestFrame(); if(frame){ mtx.lock(); frame->Rasterize(CAMERA_WIDTH, CAMERA_HEIGHT, (unsigned int) frameCV.step, 8, frameCV.data); mtx.unlock(); frame->Release(); } } camera->Release(); CameraManager::X().Shutdown(); } void viewLoopCode(Mat& frameCV) { namedWindow("frame", WINDOW_AUTOSIZE); while(1){ imshow("frame", frameCV); waitKey(1); } } int main(int argc, char** argv){ Mat frameCV(Size(CAMERA_WIDTH, CAMERA_HEIGHT), CV_8UC1); thread fastLoop (fastLoopCode, frameCV); thread viewLoop (viewLoopCode, frameCV); fastLoop.join(); viewLoop.join(); }

Forum/browser software deleted a more detailed answer - but the following code is working with threads :)

I get 120fps in fastLoop and 63fps in the viewLoop - adding processing to fastLoop shows an eventual slowdown, once this reaches 63 they are both 63 and the threads I guess are then redundant.

It's pretty bare - but worked straight away, which was surprising. Maybe someone can point out issues with it?

#include "cameralibrary.h"
"cameralibrary.h"     //specific to my camera ... 
#include "face_supportcode.h"  

#include "opencv2/opencv.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>              
#include <thread>  
#include <mutex>          

#include "serial.h"

using namespace cv;
using namespace std;
using namespace CameraLibrary;

#define CAMERA_WIDTH 640
#define CAMERA_HEIGHT 480

mutex mtx;   // might not actually need to be global ...

void fastLoopCode(Mat& frameCV) {

        //mutex mtx;   //try it in here

        CameraManager::X().WaitForInitialization();
        Camera *camera = CameraManager::X().GetCamera();
        camera->SetVideoType(Core::GrayscaleMode);

        Frame *frame;

        camera->Start();

        while(1){

                frame = camera->GetLatestFrame();      

                if(frame){                                     

                        mtx.lock();
                        frame->Rasterize(CAMERA_WIDTH, CAMERA_HEIGHT, (unsigned int) frameCV.step, 8, frameCV.data);
                        mtx.unlock();

                        frame->Release();
                }
        }

        camera->Release();
        CameraManager::X().Shutdown();
}

void viewLoopCode(Mat& frameCV) {

        namedWindow("frame", WINDOW_AUTOSIZE); 

        while(1){

                imshow("frame", frameCV);
                waitKey(1);    
        }
}


int main(int argc, char** argv){

        Mat frameCV(Size(CAMERA_WIDTH, CAMERA_HEIGHT), CV_8UC1);

        thread fastLoop (fastLoopCode, frameCV);
        thread viewLoop (viewLoopCode, frameCV);

        fastLoop.join();
        viewLoop.join();
}

Forum/browser software deleted a more detailed answer - but the following code is working with threads :)

I get 120fps in fastLoop and 63fps in the viewLoop - adding processing to fastLoop shows an eventual slowdown, once this reaches 63 they are both 63 and the threads I guess are then redundant.

It's pretty bare - but worked straight away, which was surprising. Maybe someone can point out issues with it?

#include "cameralibrary.h"     //specific //#include something specific to my your camera ... 
#include "face_supportcode.h"  
...

#include "opencv2/opencv.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>              
#include <thread>  
#include <mutex>          

#include "serial.h"

using namespace cv;
using namespace std;
using namespace CameraLibrary;

#define CAMERA_WIDTH 640
#define CAMERA_HEIGHT 480

mutex mtx;   // might not actually need to be global ...

void fastLoopCode(Mat& frameCV) {

        //mutex mtx;   //try it in here

        CameraManager::X().WaitForInitialization();
        Camera *camera = CameraManager::X().GetCamera();
        camera->SetVideoType(Core::GrayscaleMode);
//initialize your camera etc.

        Frame *frame;

        camera->Start();
*frame;      //direct data from camera...

        while(1){
                 frame = camera->GetLatestFrame();      

                if(frame){                                     

                        mtx.lock();
                        frame->Rasterize(CAMERA_WIDTH, CAMERA_HEIGHT, (unsigned int) frameCV.step, 8, frameCV.data);
//whatever code required to turn the camera data into an openCV Mat
                        mtx.unlock();

                        frame->Release();
                }
        }
         camera->Release();
        CameraManager::X().Shutdown();
}

void viewLoopCode(Mat& frameCV) {

        namedWindow("frame", WINDOW_AUTOSIZE); 

        while(1){

                imshow("frame", frameCV);
                waitKey(1);    
        }
}
 
int main(int argc, char** argv){

        Mat frameCV(Size(CAMERA_WIDTH, CAMERA_HEIGHT), CV_8UC1);

        thread fastLoop (fastLoopCode, frameCV);
        thread viewLoop (viewLoopCode, frameCV);

        fastLoop.join();
        viewLoop.join();
}

Forum/browser software deleted a more detailed answer - but the following code is working with threads :)

I get 120fps in fastLoop and 63fps in the viewLoop - adding processing to fastLoop shows an eventual slowdown, once this reaches 63 they are both 63 and the threads I guess are then redundant.

It's pretty bare - but worked straight away, which was surprising. Maybe someone can point out issues with it?

Quick and hopefully correct explanation: mtx.lock means all memory locations within the lock are only accessible by the code within the lock, for the duration of the lock - this avoids concurrent read writes of the Mat memory locations by each thread. It is my understanding that it grants the fastLoop thread more priority over the viewLoop thread.

//#include something specific to your camera ...

#include "opencv2/opencv.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <thread>  
#include <mutex>          

using namespace cv;
using namespace std;

mutex mtx;   // might not actually need to be global ...

void fastLoopCode(Mat& frameCV) {

        //mutex mtx;   //try it in here

        //initialize your camera etc.

        Frame *frame;      //direct data from camera...

        while(1){
                frame = camera->GetLatestFrame();      

                if(frame){                                     

                        mtx.lock();
                        //whatever code required to turn the camera data into an openCV Mat
                        mtx.unlock();

                        frame->Release();
                }
        }
        camera->Release();
}

void viewLoopCode(Mat& frameCV) {

        namedWindow("frame", WINDOW_AUTOSIZE); 

        while(1){

                imshow("frame", frameCV);
                waitKey(1);    
        }
}

int main(int argc, char** argv){

        Mat frameCV(Size(CAMERA_WIDTH, CAMERA_HEIGHT), CV_8UC1);

        thread fastLoop (fastLoopCode, frameCV);
        thread viewLoop (viewLoopCode, frameCV);

        fastLoop.join();
        viewLoop.join();
}