#include <iostream>
#include <boost/assert.hpp>
-#include <QFuture>
-#include <QFutureWatcher>
-#include <QtConcurrentRun>
#ifdef OPENCL
#include <opencv/ocl/ocl.hpp>
#include "debugout.h"
OpenCvLuxPlugin::OpenCvLuxPlugin(AbstractRoutingEngine* re, map<string, string> config)
- :QObject(),AbstractSource(re, config), m_capture(0),lastLux(0)
+ :AbstractSource(re, config), lastLux(0)
{
re->setSupported(supported(), this);
- threaded = false;
- kinect = false;
- useOpenCl = false;
- timer = new QTimer(this);
- fps=30;
+ shared = new Shared;
+ shared->parent = this;
+
+ shared->m_capture = NULL;
+
+ shared->threaded = false;
+ shared->kinect = false;
+ shared->useOpenCl = false;
+
+ shared->fps=30;
device="0";
- pixelLowerBound = 0;
- pixelUpperBound = 255;
+ shared->pixelLowerBound = 0;
+ shared->pixelUpperBound = 255;
- connect(timer,SIGNAL(timeout()),this,SLOT(grabImage()));
if(config.find("threaded") != config.end())
{
- threaded = config["threaded"] == "true";
+ shared->threaded = config["threaded"] == "true";
}
if(config.find("kinect") != config.end())
{
- kinect = config["kinect"] == "true";
+ shared->kinect = config["kinect"] == "true";
}
if(config.find("fps") != config.end())
{
- fps = boost::lexical_cast<int>(config["fps"]);
+ shared->fps = boost::lexical_cast<int>(config["fps"]);
}
if(config.find("device") != config.end())
if(config.find("pixelLowerBound") != config.end())
{
- pixelLowerBound = boost::lexical_cast<int>(config["pixelLowerBound"]);
+ shared->pixelLowerBound = boost::lexical_cast<int>(config["pixelLowerBound"]);
- if(pixelLowerBound < 0)
+ if(shared->pixelLowerBound < 0)
{
- pixelLowerBound = 0;
+ shared->pixelLowerBound = 0;
}
}
if(config.find("pixelUpperBound") != config.end())
{
- pixelUpperBound = boost::lexical_cast<int>(config["pixelUpperBound"]);
+ shared->pixelUpperBound = boost::lexical_cast<int>(config["pixelUpperBound"]);
- if(pixelUpperBound > 255)
- pixelUpperBound = 255;
+ if(shared->pixelUpperBound > 255)
+ shared->pixelUpperBound = 255;
}
if(config.find("opencl") != config.end())
{
- useOpenCl = config["opencl"] == "true";
+ shared->useOpenCl = config["opencl"] == "true";
}
void OpenCvLuxPlugin::getPropertyAsync(AsyncPropertyReply *reply)
{
- if(!timer->isActive())
+ if(!shared->m_capture || !shared->m_capture->isOpened())
{
/// we want to turn on the camera for one shot to get an image and determine the intensity
init();
- timer->singleShot(1,this,SLOT(grabImage()));
+ g_timeout_add(1,grabImage,shared);
}
if(reply->property == VehicleProperty::ExteriorBrightness)
{
- replyQueue.append(reply);
+ replyQueue.push_back(reply);
}
else ///We don't support what you are asking for. Reply false
void OpenCvLuxPlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
{
- if(!timer->isActive())
+ if(!shared->mRequests.size())
{
init();
- timer->start(1000 / fps);
+ g_timeout_add(1000 / shared->fps, grabImage, shared);
}
- mRequests.push_back(property);
+ shared->mRequests.push_back(property);
}
void OpenCvLuxPlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
{
- mRequests.remove(property);
-
- if(!mRequests.size())
- {
- timer->stop();
- }
+ shared->mRequests.remove(property);
}
PropertyList OpenCvLuxPlugin::supported()
return Get;
}
-void OpenCvLuxPlugin::grabImage()
+static int grabImage(void *data)
{
+ OpenCvLuxPlugin::Shared* shared = static_cast<OpenCvLuxPlugin::Shared*>(data);
- if(kinect)
+/* if(kinect)
{
m_capture->grab();
m_capture->retrieve( m_image, CV_CAP_OPENNI_GRAY_IMAGE );
}
- else
+ else*/
{
- (*m_capture) >> m_image;
- }
+ cv::Mat m_image;
- if(threaded)
- {
- QFutureWatcher<uint> *watcher = new QFutureWatcher<uint>();
- connect(watcher,SIGNAL(finished()),this,SLOT(imageGrabCompleted()));
+ *(shared->m_capture) >> m_image;
+
+ uint lux = evalImage(m_image,shared);
- QFuture<uint> future = QtConcurrent::run(this, &OpenCvLuxPlugin::evalImage, m_image);
- watcher->setFuture(future);
+ shared->parent->updateProperty(lux);
}
- else
- {
- uint lux = evalImage(m_image);
- updateProperty(lux);
+ if(shared->mRequests.size())
+ {
+ return true;
}
+ delete shared->m_capture;
+ shared->m_capture = NULL;
+ return false;
+
}
-uint OpenCvLuxPlugin::evalImage(cv::Mat qImg)
+static uint evalImage(cv::Mat qImg, OpenCvLuxPlugin::Shared *shared)
{
#ifdef OPENCL
cv::Scalar avgPixelIntensity;
double val = avgPixelIntensity.val[0];
- double qualifiedPixel = (val - pixelLowerBound);
+ double qualifiedPixel = (val - shared->pixelLowerBound);
if(qualifiedPixel < 0) qualifiedPixel = 0;
- uint lux = qualifiedPixel * (130000.0 / pixelUpperBound);
+ uint lux = qualifiedPixel * (130000.0 / shared->pixelUpperBound);
DebugOut(7)<<"average pixel value: "<<qualifiedPixel<<" lux: "<<lux<<endl;
return lux;
}
-void OpenCvLuxPlugin::imageGrabCompleted()
-{
- QFutureWatcher<uint> * watcher = dynamic_cast<QFutureWatcher<uint>* >(sender());
-
- uint lux = watcher->result();
-
- updateProperty(lux);
-}
void OpenCvLuxPlugin::init()
{
- if(m_capture) delete m_capture;
+ if(shared->m_capture) delete shared->m_capture;
- if(kinect)
+ if(shared->kinect)
{
- m_capture = new cv::VideoCapture(CV_CAP_OPENNI);
+ shared->m_capture = new cv::VideoCapture(CV_CAP_OPENNI);
}
else if(device == "")
- m_capture = new cv::VideoCapture(0);
- else m_capture = new cv::VideoCapture(device.toInt());
+ shared->m_capture = new cv::VideoCapture(0);
+ else shared->m_capture = new cv::VideoCapture(atoi(device.c_str()));
- if(!m_capture->isOpened())
+ if(!shared->m_capture->isOpened())
{
- qDebug()<<"we failed to open camera device ("<<device<<") or no camera found";
+ DebugOut()<<"we failed to open camera device ("<<device<<") or no camera found"<<endl;
return;
}
- DebugOut()<<"camera frame width: "<<m_capture->get(CV_CAP_PROP_FRAME_WIDTH)<<endl;
- DebugOut()<<"camera frame height: "<<m_capture->get(CV_CAP_PROP_FRAME_HEIGHT)<<endl;
- DebugOut()<<"camera frame fps: "<<m_capture->get(CV_CAP_PROP_FPS)<<endl;
+
+
+ DebugOut()<<"camera frame width: "<<shared->m_capture->get(CV_CAP_PROP_FRAME_WIDTH)<<endl;
+ DebugOut()<<"camera frame height: "<<shared->m_capture->get(CV_CAP_PROP_FRAME_HEIGHT)<<endl;
+ DebugOut()<<"camera frame fps: "<<shared->m_capture->get(CV_CAP_PROP_FPS)<<endl;
}
void OpenCvLuxPlugin::updateProperty(uint lux)
{
VehicleProperty::ExteriorBrightnessType l(lux);
- Q_FOREACH(AsyncPropertyReply* reply, replyQueue)
+ for(auto itr = replyQueue.begin(); itr != replyQueue.end(); itr++)
{
+ AsyncPropertyReply* reply = *itr;
reply->value = &l;
reply->success = true;
reply->completed(reply);
replyQueue.clear();
- if(!timer->isActive())
- {
- delete m_capture;
- m_capture = NULL;
- }
-
- if(lux != lastLux && mRequests.size())
+ if(lux != lastLux && shared->mRequests.size())
{
lastLux = lux;
routingEngine->updateProperty(VehicleProperty::ExteriorBrightness,&l, uuid());