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

View File

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

View File

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