Updated Camera class with OpenCV C++ interface (cv::VideoCapture).

Set default camera image ratio to 640/480.

git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@106 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
matlabbe 2012-05-16 01:42:10 +00:00
parent 1d77ae4a49
commit f5837e726f
3 changed files with 36 additions and 49 deletions

View File

@ -4,13 +4,12 @@
#include "Camera.h"
#include <stdio.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include "Settings.h"
#include <QtCore/QFile>
Camera::Camera(QObject * parent) :
QObject(parent),
capture_(0)
QObject(parent)
{
qRegisterMetaType<cv::Mat>("cv::Mat");
connect(&cameraTimer_, SIGNAL(timeout()), this, SLOT(takeImage()));
@ -24,11 +23,7 @@ Camera::~Camera()
void Camera::stop()
{
stopTimer();
if(capture_)
{
cvReleaseCapture(&capture_);
capture_ = 0;
}
capture_.release();
}
void Camera::pause()
@ -38,69 +33,61 @@ void Camera::pause()
void Camera::takeImage()
{
if(capture_)
if(capture_.isOpened())
{
IplImage * img = 0;
if(cvGrabFrame(capture_)) // capture a frame
cv::Mat img;
capture_.read(img);// capture a frame
if(img.empty())
{
img = cvRetrieveFrame(capture_); // retrieve the captured frame
printf("Camera: Could not grab a frame, the end of the feed may be reached...\n");
}
else
{
printf("CameraVideo: Could not grab a frame, the end of the feed may be reached...\n");
}
//resize
if(img &&
Settings::getCamera_imageWidth() &&
if( Settings::getCamera_imageWidth() &&
Settings::getCamera_imageHeight() &&
Settings::getCamera_imageWidth() != img->width &&
Settings::getCamera_imageHeight() != img->height)
Settings::getCamera_imageWidth() != img.cols &&
Settings::getCamera_imageHeight() != img.rows)
{
// declare a destination IplImage object with correct size, depth and channels
cv::Mat headerImg = img;
cv::Mat imgMat(Settings::getCamera_imageHeight(),
Settings::getCamera_imageWidth(),
headerImg.type());
//use cvResize to resize source to a destination image (linear interpolation)
IplImage resampledImg = imgMat;
cvResize(img, &resampledImg);
emit imageReceived(imgMat);
cv::Mat resampled;
cv::resize(img, resampled, cv::Size(Settings::getCamera_imageWidth(), Settings::getCamera_imageHeight()));
emit imageReceived(resampled);
}
else
{
emit imageReceived(cv::Mat(img, true));
emit imageReceived(img.clone()); // clone required
}
}
}
}
bool Camera::start()
{
if(!capture_)
if(!capture_.isOpened())
{
QString videoFile = Settings::getCamera_videoFilePath();
if(!videoFile.isEmpty())
{
capture_ = cvCaptureFromAVI(videoFile.toStdString().c_str());
if(!capture_)
capture_.open(videoFile.toStdString().c_str());
if(!capture_.isOpened())
{
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_)
if(!capture_.isOpened())
{
capture_ = cvCaptureFromCAM(Settings::getCamera_deviceId());
if(capture_ && Settings::getCamera_imageWidth() && Settings::getCamera_imageHeight())
//set camera device
capture_.open(Settings::getCamera_deviceId());
if(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()));
capture_.set(CV_CAP_PROP_FRAME_WIDTH, double(Settings::getCamera_imageWidth()));
capture_.set(CV_CAP_PROP_FRAME_HEIGHT, double(Settings::getCamera_imageHeight()));
}
}
}
if(!capture_)
if(!capture_.isOpened())
{
printf("Failed to create a capture object!\n");
printf("Failed to open a capture object!\n");
return false;
}

View File

@ -35,7 +35,7 @@ protected:
void stopTimer();
private:
CvCapture * capture_;
cv::VideoCapture capture_;
QTimer cameraTimer_;
};

View File

@ -53,8 +53,8 @@ typedef unsigned int uint;
class Settings
{
PARAMETER(Camera, deviceId, int, 0);
PARAMETER(Camera, imageWidth, int, 0);
PARAMETER(Camera, imageHeight, int, 0);
PARAMETER(Camera, imageWidth, int, 640);
PARAMETER(Camera, imageHeight, int, 480);
PARAMETER(Camera, imageRate, int, 2); // Hz
PARAMETER(Camera, videoFilePath, QString, "");