2011-10-25 15:48:19 +00:00
|
|
|
/*
|
2011-12-02 18:34:08 +00:00
|
|
|
* Copyright (C) 2011, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
2011-10-25 15:48:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Camera.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <opencv2/imgproc/imgproc_c.h>
|
2011-11-23 16:44:14 +00:00
|
|
|
#include "Settings.h"
|
2011-10-25 15:48:19 +00:00
|
|
|
|
2011-11-23 16:44:14 +00:00
|
|
|
Camera::Camera(QObject * parent) :
|
2011-10-25 15:48:19 +00:00
|
|
|
QObject(parent),
|
2011-11-23 16:44:14 +00:00
|
|
|
capture_(0)
|
2011-10-25 15:48:19 +00:00
|
|
|
{
|
2011-11-23 18:19:55 +00:00
|
|
|
qRegisterMetaType<cv::Mat>("cv::Mat");
|
2011-11-23 16:44:14 +00:00
|
|
|
connect(&cameraTimer_, SIGNAL(timeout()), this, SLOT(takeImage()));
|
2011-10-25 15:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Camera::~Camera()
|
|
|
|
|
{
|
2011-11-23 16:44:14 +00:00
|
|
|
this->stop();
|
2011-10-25 15:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-23 16:44:14 +00:00
|
|
|
void Camera::stop()
|
2011-10-25 15:48:19 +00:00
|
|
|
{
|
2011-11-24 00:00:37 +00:00
|
|
|
stopTimer();
|
2011-10-25 15:48:19 +00:00
|
|
|
if(capture_)
|
|
|
|
|
{
|
|
|
|
|
cvReleaseCapture(&capture_);
|
|
|
|
|
capture_ = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-23 16:44:14 +00:00
|
|
|
void Camera::takeImage()
|
2011-10-25 15:48:19 +00:00
|
|
|
{
|
|
|
|
|
if(capture_)
|
|
|
|
|
{
|
2011-11-23 16:44:14 +00:00
|
|
|
IplImage * img = 0;
|
2011-10-25 15:48:19 +00:00
|
|
|
if(cvGrabFrame(capture_)) // capture a frame
|
|
|
|
|
{
|
|
|
|
|
img = cvRetrieveFrame(capture_); // retrieve the captured frame
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("CameraVideo: Could not grab a frame, the end of the feed may be reached...\n");
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-23 16:44:14 +00:00
|
|
|
//resize
|
|
|
|
|
if(img &&
|
|
|
|
|
Settings::getCamera_imageWidth().toInt() &&
|
|
|
|
|
Settings::getCamera_imageHeight().toInt() &&
|
2011-11-23 18:08:33 +00:00
|
|
|
Settings::getCamera_imageWidth().toInt() != img->width &&
|
|
|
|
|
Settings::getCamera_imageHeight().toInt() != img->height)
|
2011-11-23 16:44:14 +00:00
|
|
|
{
|
|
|
|
|
// declare a destination IplImage object with correct size, depth and channels
|
|
|
|
|
cv::Mat imgMat(cvSize(Settings::getCamera_imageWidth().toInt(), Settings::getCamera_imageHeight().toInt()),
|
|
|
|
|
img->depth,
|
|
|
|
|
img->nChannels );
|
2011-10-25 15:48:19 +00:00
|
|
|
|
2011-11-23 16:44:14 +00:00
|
|
|
//use cvResize to resize source to a destination image (linear interpolation)
|
|
|
|
|
IplImage resampledImg = imgMat;
|
|
|
|
|
cvResize(img, &resampledImg);
|
|
|
|
|
emit imageReceived(imgMat);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
emit imageReceived(cv::Mat(img, true));
|
|
|
|
|
}
|
2011-10-25 15:48:19 +00:00
|
|
|
}
|
2011-11-23 16:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Camera::start()
|
|
|
|
|
{
|
|
|
|
|
if(!capture_)
|
2011-10-25 15:48:19 +00:00
|
|
|
{
|
2011-11-24 00:00:37 +00:00
|
|
|
capture_ = cvCaptureFromCAM(Settings::getCamera_deviceId().toInt());
|
|
|
|
|
if(capture_)
|
2011-11-23 16:44:14 +00:00
|
|
|
{
|
2011-11-24 00:00:37 +00:00
|
|
|
cvSetCaptureProperty(capture_, CV_CAP_PROP_FRAME_WIDTH, double(Settings::getCamera_imageWidth().toInt()));
|
|
|
|
|
cvSetCaptureProperty(capture_, CV_CAP_PROP_FRAME_HEIGHT, double(Settings::getCamera_imageHeight().toInt()));
|
2011-11-23 16:44:14 +00:00
|
|
|
}
|
2011-10-25 15:48:19 +00:00
|
|
|
}
|
2011-11-24 00:00:37 +00:00
|
|
|
if(!capture_)
|
|
|
|
|
{
|
|
|
|
|
printf("Failed to create a capture object!\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-10-25 15:48:19 +00:00
|
|
|
|
2011-11-24 00:00:37 +00:00
|
|
|
startTimer();
|
2011-11-23 16:44:14 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-24 00:00:37 +00:00
|
|
|
void Camera::startTimer()
|
|
|
|
|
{
|
|
|
|
|
updateImageRate();
|
|
|
|
|
cameraTimer_.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Camera::stopTimer()
|
|
|
|
|
{
|
|
|
|
|
cameraTimer_.stop();
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-23 16:44:14 +00:00
|
|
|
void Camera::updateImageRate()
|
|
|
|
|
{
|
2011-11-24 00:00:37 +00:00
|
|
|
if(Settings::getCamera_imageRate().toInt())
|
|
|
|
|
{
|
|
|
|
|
cameraTimer_.setInterval(1000/Settings::getCamera_imageRate().toInt());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cameraTimer_.setInterval(0);
|
|
|
|
|
}
|
2011-10-25 15:48:19 +00:00
|
|
|
}
|
|
|
|
|
|