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>
2012-05-16 01:42:10 +00:00
# include <opencv2/imgproc/imgproc.hpp>
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 ) :
2012-05-16 01:42:10 +00:00
QObject ( parent )
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 ( ) ;
2012-05-16 01:42:10 +00:00
capture_ . release ( ) ;
2011-10-25 15:48:19 +00:00
}
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
{
2012-05-16 01:42:10 +00:00
if ( capture_ . isOpened ( ) )
2011-10-25 15:48:19 +00:00
{
2012-05-16 01:42:10 +00:00
cv : : Mat img ;
capture_ . read ( img ) ; // capture a frame
if ( img . empty ( ) )
2011-10-25 15:48:19 +00:00
{
2012-05-16 01:42:10 +00:00
printf ( " Camera: Could not grab a frame, the end of the feed may be reached... \n " ) ;
2011-10-25 15:48:19 +00:00
}
else
{
2012-05-16 01:42:10 +00:00
//resize
2012-08-28 13:44:57 +00:00
if ( Settings : : getCamera_2imageWidth ( ) & &
Settings : : getCamera_3imageHeight ( ) & &
Settings : : getCamera_2imageWidth ( ) ! = img . cols & &
Settings : : getCamera_3imageHeight ( ) ! = img . rows )
2012-05-16 01:42:10 +00:00
{
cv : : Mat resampled ;
2012-08-28 13:44:57 +00:00
cv : : resize ( img , resampled , cv : : Size ( Settings : : getCamera_2imageWidth ( ) , Settings : : getCamera_3imageHeight ( ) ) ) ;
2012-05-16 01:42:10 +00:00
emit imageReceived ( resampled ) ;
}
else
{
emit imageReceived ( img . clone ( ) ) ; // clone required
}
2011-11-23 16:44:14 +00:00
}
2011-10-25 15:48:19 +00:00
}
2011-11-23 16:44:14 +00:00
}
bool Camera : : start ( )
{
2012-05-16 01:42:10 +00:00
if ( ! capture_ . isOpened ( ) )
2011-10-25 15:48:19 +00:00
{
2012-08-28 13:44:57 +00:00
QString videoFile = Settings : : getCamera_5videoFilePath ( ) ;
2012-01-30 03:20:04 +00:00
if ( ! videoFile . isEmpty ( ) )
2012-01-30 03:16:30 +00:00
{
2012-05-16 01:42:10 +00:00
capture_ . open ( videoFile . toStdString ( ) . c_str ( ) ) ;
if ( ! capture_ . isOpened ( ) )
2012-01-30 03:16:30 +00:00
{
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 ( ) ) ;
}
}
2012-05-16 01:42:10 +00:00
if ( ! capture_ . isOpened ( ) )
2011-11-23 16:44:14 +00:00
{
2012-05-16 01:42:10 +00:00
//set camera device
2012-08-28 13:44:57 +00:00
capture_ . open ( Settings : : getCamera_1deviceId ( ) ) ;
if ( Settings : : getCamera_2imageWidth ( ) & & Settings : : getCamera_3imageHeight ( ) )
2012-01-30 03:16:30 +00:00
{
2012-08-28 13:44:57 +00:00
capture_ . set ( CV_CAP_PROP_FRAME_WIDTH , double ( Settings : : getCamera_2imageWidth ( ) ) ) ;
capture_ . set ( CV_CAP_PROP_FRAME_HEIGHT , double ( Settings : : getCamera_3imageHeight ( ) ) ) ;
2012-01-30 03:16:30 +00:00
}
2011-11-23 16:44:14 +00:00
}
2011-10-25 15:48:19 +00:00
}
2012-05-16 01:42:10 +00:00
if ( ! capture_ . isOpened ( ) )
2011-11-24 00:00:37 +00:00
{
2012-05-16 01:42:10 +00:00
printf ( " Failed to open a capture object! \n " ) ;
2011-11-24 00:00:37 +00:00
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-08-28 13:44:57 +00:00
if ( Settings : : getCamera_4imageRate ( ) )
2011-11-24 00:00:37 +00:00
{
2012-08-28 13:44:57 +00:00
cameraTimer_ . setInterval ( 1000 / Settings : : getCamera_4imageRate ( ) ) ;
2011-11-24 00:00:37 +00:00
}
else
{
cameraTimer_ . setInterval ( 0 ) ;
}
2011-10-25 15:48:19 +00:00
}