2011-10-25 15:48:19 +00:00
|
|
|
/*
|
|
|
|
|
* Camera.cpp
|
|
|
|
|
*
|
|
|
|
|
* Created on: 2011-10-21
|
|
|
|
|
* Author: matlab
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#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-23 16:44:14 +00:00
|
|
|
cameraTimer_.stop();
|
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-23 16:44:14 +00:00
|
|
|
if(!capture_)
|
|
|
|
|
{
|
|
|
|
|
capture_ = cvCaptureFromCAM(Settings::getCamera_deviceId().toInt());
|
|
|
|
|
if(capture_)
|
|
|
|
|
{
|
|
|
|
|
cvSetCaptureProperty(capture_, CV_CAP_PROP_FRAME_WIDTH, double(Settings::getCamera_imageWidth().toInt()));
|
|
|
|
|
cvSetCaptureProperty(capture_, CV_CAP_PROP_FRAME_HEIGHT, double(Settings::getCamera_imageHeight().toInt()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!capture_)
|
|
|
|
|
{
|
|
|
|
|
printf("Failed to create a capture object!\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-10-25 15:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-23 16:44:14 +00:00
|
|
|
cameraTimer_.start(1000/Settings::getCamera_imageRate().toInt());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Camera::updateImageRate()
|
|
|
|
|
{
|
|
|
|
|
cameraTimer_.setInterval(1000/Settings::getCamera_imageRate().toInt());
|
2011-10-25 15:48:19 +00:00
|
|
|
}
|
|
|
|
|
|