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"
2012-01-30 03:16:30 +00:00
# include <QtCore/QFile>
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 ;
}
}
2012-01-30 03:16:30 +00:00
void Camera : : pause ( )
{
stopTimer ( ) ;
}
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 & &
2012-01-04 17:30:36 +00:00
Settings : : getCamera_imageWidth ( ) & &
Settings : : getCamera_imageHeight ( ) & &
Settings : : getCamera_imageWidth ( ) ! = img - > width & &
Settings : : getCamera_imageHeight ( ) ! = img - > height )
2011-11-23 16:44:14 +00:00
{
// declare a destination IplImage object with correct size, depth and channels
2012-01-03 22:58:46 +00:00
cv : : Mat headerImg = img ;
2012-01-04 17:30:36 +00:00
cv : : Mat imgMat ( Settings : : getCamera_imageHeight ( ) ,
Settings : : getCamera_imageWidth ( ) ,
2012-01-03 22:58:46 +00:00
headerImg . type ( ) ) ;
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
{
2012-01-30 03:16:30 +00:00
QString videoFile = Settings : : getCamera_videoFilePath ( ) ;
if ( QFile : : exists ( videoFile ) )
{
capture_ = cvCaptureFromAVI ( videoFile . toStdString ( ) . c_str ( ) ) ;
if ( ! capture_ )
{
printf ( " WARNING: Cannot open file \" %s \" . If you want to disable loading automatically this video file, clear the Camera/videoFilePath parameter. By default, webcam will be used instead of the file. \n " , videoFile . toStdString ( ) . c_str ( ) ) ;
}
}
if ( ! capture_ )
2011-11-23 16:44:14 +00:00
{
2012-01-30 03:16:30 +00:00
capture_ = cvCaptureFromCAM ( Settings : : getCamera_deviceId ( ) ) ;
if ( capture_ & & Settings : : getCamera_imageWidth ( ) & & Settings : : getCamera_imageHeight ( ) )
{
cvSetCaptureProperty ( capture_ , CV_CAP_PROP_FRAME_WIDTH , double ( Settings : : getCamera_imageWidth ( ) ) ) ;
cvSetCaptureProperty ( capture_ , CV_CAP_PROP_FRAME_HEIGHT , double ( Settings : : getCamera_imageHeight ( ) ) ) ;
}
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 ( )
{
2012-01-04 17:30:36 +00:00
if ( Settings : : getCamera_imageRate ( ) )
2011-11-24 00:00:37 +00:00
{
2012-01-04 17:30:36 +00:00
cameraTimer_ . setInterval ( 1000 / Settings : : getCamera_imageRate ( ) ) ;
2011-11-24 00:00:37 +00:00
}
else
{
cameraTimer_ . setInterval ( 0 ) ;
}
2011-10-25 15:48:19 +00:00
}