Remove implementation of CvCapture interface for Intel PerC camera. Add IVideoCapture interface and implementation of this one for Intel PerC camera

This commit is contained in:
vbystricky 2014-02-17 17:50:15 +04:00
parent 73dfc4cb8c
commit ff8b8ef24c
5 changed files with 845 additions and 714 deletions

View File

@ -530,6 +530,18 @@ enum { CAP_INTELPERC_DEPTH_MAP = 0, // Each pixel is a 16-bit integ
CAP_INTELPERC_IMAGE = 3
};
class IVideoCapture
{
public:
virtual ~IVideoCapture() {}
virtual double getProperty(int) { return 0; }
virtual bool setProperty(int, double) { return 0; }
virtual bool grabFrame() { return true; }
virtual bool retrieveFrame(int, cv::OutputArray) { return 0; }
virtual int getCaptureDomain() { return CAP_ANY; } // Return the type of the capture object: CAP_VFW, etc...
};
class CV_EXPORTS_W VideoCapture
{
public:
@ -554,9 +566,11 @@ public:
protected:
Ptr<CvCapture> cap;
Ptr<IVideoCapture> icap;
private:
static Ptr<IVideoCapture> createCameraCapture(int index);
};
class CV_EXPORTS_W VideoWriter
{
public:

View File

@ -40,6 +40,7 @@
//M*/
#include "precomp.hpp"
#include "cap_intelperc.hpp"
#if defined _M_X64 && defined _MSC_VER && !defined CV_ICC
#pragma optimize("",off)
@ -345,14 +346,6 @@ CV_IMPL CvCapture * cvCreateCameraCapture (int index)
return capture;
break; // CV_CAP_GIGANETIX
#endif
#ifdef HAVE_INTELPERC
case CV_CAP_INTELPERC:
capture = cvCreateCameraCapture_IntelPerC(index);
if (capture)
return capture;
break; // CV_CAP_INTEL_PERC
#endif
}
}
@ -497,6 +490,7 @@ VideoCapture::VideoCapture(int device)
VideoCapture::~VideoCapture()
{
icap.release();
cap.release();
}
@ -510,24 +504,36 @@ bool VideoCapture::open(const String& filename)
bool VideoCapture::open(int device)
{
if (isOpened()) release();
icap = createCameraCapture(device);
if (!icap.empty())
return true;
cap.reset(cvCreateCameraCapture(device));
return isOpened();
}
bool VideoCapture::isOpened() const { return !cap.empty(); }
bool VideoCapture::isOpened() const
{
return (!cap.empty() || !icap.empty());
}
void VideoCapture::release()
{
icap.release();
cap.release();
}
bool VideoCapture::grab()
{
if (!icap.empty())
return icap->grabFrame();
return cvGrabFrame(cap) != 0;
}
bool VideoCapture::retrieve(OutputArray image, int channel)
{
if (!icap.empty())
return icap->retrieveFrame(channel, image);
IplImage* _img = cvRetrieveFrame(cap, channel);
if( !_img )
{
@ -567,14 +573,62 @@ VideoCapture& VideoCapture::operator >> (UMat& image)
bool VideoCapture::set(int propId, double value)
{
if (!icap.empty())
return icap->setProperty(propId, value);
return cvSetCaptureProperty(cap, propId, value) != 0;
}
double VideoCapture::get(int propId)
{
if (!icap.empty())
return icap->getProperty(propId);
return cvGetCaptureProperty(cap, propId);
}
Ptr<IVideoCapture> VideoCapture::createCameraCapture(int index)
{
int domains[] =
{
#ifdef HAVE_INTELPERC
CV_CAP_INTELPERC,
#endif
-1, -1
};
// interpret preferred interface (0 = autodetect)
int pref = (index / 100) * 100;
if (pref)
{
domains[0]=pref;
index %= 100;
domains[1]=-1;
}
// try every possibly installed camera API
for (int i = 0; domains[i] >= 0; i++)
{
#if defined(HAVE_INTELPERC) || \
(0)
Ptr<IVideoCapture> capture;
switch (domains[i])
{
#ifdef HAVE_INTELPERC
case CV_CAP_INTELPERC:
capture = Ptr<IVideoCapture>(new cv::VideoCapture_IntelPerC());
if (capture)
return capture;
break; // CV_CAP_INTEL_PERC
#endif
}
#endif
}
// failed open a camera
return Ptr<IVideoCapture>();
}
VideoWriter::VideoWriter()
{}

View File

@ -1,43 +1,27 @@
#include "precomp.hpp"
#ifdef HAVE_INTELPERC
#include "pxcsession.h"
#include "pxcsmartptr.h"
#include "pxccapture.h"
#include "cap_intelperc.hpp"
class CvIntelPerCStreamBase
namespace cv
{
protected:
struct FrameInternal
{
IplImage* retrieveFrame()
{
if (m_mat.empty())
return NULL;
m_iplHeader = IplImage(m_mat);
return &m_iplHeader;
}
cv::Mat m_mat;
private:
IplImage m_iplHeader;
};
public:
CvIntelPerCStreamBase()
///////////////// IntelPerCStreamBase //////////////////
IntelPerCStreamBase::IntelPerCStreamBase()
: m_profileIdx(-1)
, m_frameIdx(0)
, m_timeStampStartNS(0)
{
}
virtual ~CvIntelPerCStreamBase()
IntelPerCStreamBase::~IntelPerCStreamBase()
{
}
bool isValid()
bool IntelPerCStreamBase::isValid()
{
return (m_device.IsValid() && m_stream.IsValid());
}
bool grabFrame()
bool IntelPerCStreamBase::grabFrame()
{
if (!m_stream.IsValid())
return false;
@ -46,24 +30,23 @@ public:
if (!setProperty(CV_CAP_PROP_INTELPERC_PROFILE_IDX, 0))
return false;
}
PXCSmartPtr<PXCImage> pxcImage; PXCSmartSP sp;
if (PXC_STATUS_NO_ERROR > m_stream->ReadStreamAsync(&pxcImage, &sp))
PXCSmartSP sp;
m_pxcImage.ReleaseRef();
if (PXC_STATUS_NO_ERROR > m_stream->ReadStreamAsync(&m_pxcImage, &sp))
return false;
if (PXC_STATUS_NO_ERROR > sp->Synchronize())
return false;
if (0 == m_timeStampStartNS)
m_timeStampStartNS = pxcImage->QueryTimeStamp();
m_timeStamp = (double)((pxcImage->QueryTimeStamp() - m_timeStampStartNS) / 10000);
m_timeStampStartNS = m_pxcImage->QueryTimeStamp();
m_timeStamp = (double)((m_pxcImage->QueryTimeStamp() - m_timeStampStartNS) / 10000);
m_frameIdx++;
return prepareIplImage(pxcImage);
return true;
}
int getProfileIDX() const
int IntelPerCStreamBase::getProfileIDX() const
{
return m_profileIdx;
}
public:
virtual bool initStream(PXCSession *session) = 0;
virtual double getProperty(int propIdx)
double IntelPerCStreamBase::getProperty(int propIdx)
{
double ret = 0.0;
switch (propIdx)
@ -95,7 +78,7 @@ public:
};
return ret;
}
virtual bool setProperty(int propIdx, double propVal)
bool IntelPerCStreamBase::setProperty(int propIdx, double propVal)
{
bool isSet = false;
switch (propIdx)
@ -103,7 +86,12 @@ public:
case CV_CAP_PROP_INTELPERC_PROFILE_IDX:
{
int propValInt = (int)propVal;
if ((0 <= propValInt) && (propValInt < m_profiles.size()))
if (0 > propValInt)
{
m_profileIdx = propValInt;
isSet = true;
}
else if (propValInt < m_profiles.size())
{
if (m_profileIdx != propValInt)
{
@ -120,9 +108,7 @@ public:
};
return isSet;
}
protected:
PXCSmartPtr<PXCCapture::Device> m_device;
bool initDevice(PXCSession *session)
bool IntelPerCStreamBase::initDevice(PXCSession *session)
{
if (NULL == session)
return false;
@ -160,8 +146,7 @@ protected:
return false;
}
PXCSmartPtr<PXCCapture::VideoStream> m_stream;
void initStreamImpl(PXCImage::ImageType type)
void IntelPerCStreamBase::initStreamImpl(PXCImage::ImageType type)
{
if (!m_device.IsValid())
return;
@ -185,18 +170,11 @@ protected:
m_stream.ReleaseRef();
}
}
protected:
std::vector<PXCCapture::VideoStream::ProfileInfo> m_profiles;
int m_profileIdx;
int m_frameIdx;
pxcU64 m_timeStampStartNS;
double m_timeStamp;
virtual bool validProfile(const PXCCapture::VideoStream::ProfileInfo& /*pinfo*/)
bool IntelPerCStreamBase::validProfile(const PXCCapture::VideoStream::ProfileInfo& /*pinfo*/)
{
return true;
}
void enumProfiles()
void IntelPerCStreamBase::enumProfiles()
{
m_profiles.clear();
if (!m_stream.IsValid())
@ -212,21 +190,17 @@ protected:
m_profiles.push_back(pinfo);
}
}
virtual bool prepareIplImage(PXCImage *pxcImage) = 0;
};
class CvIntelPerCStreamImage
: public CvIntelPerCStreamBase
{
public:
CvIntelPerCStreamImage()
///////////////// IntelPerCStreamImage //////////////////
IntelPerCStreamImage::IntelPerCStreamImage()
{
}
virtual ~CvIntelPerCStreamImage()
IntelPerCStreamImage::~IntelPerCStreamImage()
{
}
virtual bool initStream(PXCSession *session)
bool IntelPerCStreamImage::initStream(PXCSession *session)
{
if (!initDevice(session))
return false;
@ -236,7 +210,7 @@ public:
enumProfiles();
return true;
}
virtual double getProperty(int propIdx)
double IntelPerCStreamImage::getProperty(int propIdx)
{
switch (propIdx)
{
@ -332,9 +306,9 @@ public:
break;
//Add image stream specific properties
}
return CvIntelPerCStreamBase::getProperty(propIdx);
return IntelPerCStreamBase::getProperty(propIdx);
}
virtual bool setProperty(int propIdx, double propVal)
bool IntelPerCStreamImage::setProperty(int propIdx, double propVal)
{
switch (propIdx)
{
@ -403,48 +377,38 @@ public:
break;
//Add image stream specific properties
}
return CvIntelPerCStreamBase::setProperty(propIdx, propVal);
return IntelPerCStreamBase::setProperty(propIdx, propVal);
}
public:
IplImage* retrieveFrame()
bool IntelPerCStreamImage::retrieveAsOutputArray(cv::OutputArray image)
{
return m_frame.retrieveFrame();
}
protected:
FrameInternal m_frame;
bool prepareIplImage(PXCImage *pxcImage)
{
if (NULL == pxcImage)
if (!m_pxcImage.IsValid())
return false;
PXCImage::ImageInfo info;
pxcImage->QueryInfo(&info);
m_pxcImage->QueryInfo(&info);
PXCImage::ImageData data;
pxcImage->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::COLOR_FORMAT_RGB24, &data);
m_pxcImage->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::COLOR_FORMAT_RGB24, &data);
if (PXCImage::SURFACE_TYPE_SYSTEM_MEMORY != data.type)
return false;
cv::Mat temp(info.height, info.width, CV_8UC3, data.planes[0], data.pitches[0]);
temp.copyTo(m_frame.m_mat);
temp.copyTo(image);
pxcImage->ReleaseAccess(&data);
m_pxcImage->ReleaseAccess(&data);
return true;
}
};
class CvIntelPerCStreamDepth
: public CvIntelPerCStreamBase
{
public:
CvIntelPerCStreamDepth()
///////////////// IntelPerCStreamDepth //////////////////
IntelPerCStreamDepth::IntelPerCStreamDepth()
{
}
virtual ~CvIntelPerCStreamDepth()
IntelPerCStreamDepth::~IntelPerCStreamDepth()
{
}
virtual bool initStream(PXCSession *session)
bool IntelPerCStreamDepth::initStream(PXCSession *session)
{
if (!initDevice(session))
return false;
@ -454,7 +418,7 @@ public:
enumProfiles();
return true;
}
virtual double getProperty(int propIdx)
double IntelPerCStreamDepth::getProperty(int propIdx)
{
switch (propIdx)
{
@ -510,9 +474,9 @@ public:
break;
//Add depth stream sepcific properties
}
return CvIntelPerCStreamBase::getProperty(propIdx);
return IntelPerCStreamBase::getProperty(propIdx);
}
virtual bool setProperty(int propIdx, double propVal)
bool IntelPerCStreamDepth::setProperty(int propIdx, double propVal)
{
switch (propIdx)
{
@ -539,70 +503,47 @@ public:
break;
//Add depth stream sepcific properties
}
return CvIntelPerCStreamBase::setProperty(propIdx, propVal);
return IntelPerCStreamBase::setProperty(propIdx, propVal);
}
public:
IplImage* retrieveDepthFrame()
bool IntelPerCStreamDepth::retrieveDepthAsOutputArray(cv::OutputArray image)
{
return m_frameDepth.retrieveFrame();
return retriveFrame(CV_16SC1, 0, image);
}
IplImage* retrieveIRFrame()
bool IntelPerCStreamDepth::retrieveIRAsOutputArray(cv::OutputArray image)
{
return m_frameIR.retrieveFrame();
return retriveFrame(CV_16SC1, 1, image);
}
IplImage* retrieveUVFrame()
bool IntelPerCStreamDepth::retrieveUVAsOutputArray(cv::OutputArray image)
{
return m_frameUV.retrieveFrame();
return retriveFrame(CV_32FC2, 2, image);
}
protected:
virtual bool validProfile(const PXCCapture::VideoStream::ProfileInfo& pinfo)
bool IntelPerCStreamDepth::validProfile(const PXCCapture::VideoStream::ProfileInfo& pinfo)
{
return (PXCImage::COLOR_FORMAT_DEPTH == pinfo.imageInfo.format);
}
protected:
FrameInternal m_frameDepth;
FrameInternal m_frameIR;
FrameInternal m_frameUV;
bool prepareIplImage(PXCImage *pxcImage)
bool IntelPerCStreamDepth::retriveFrame(int type, int planeIdx, cv::OutputArray frame)
{
if (NULL == pxcImage)
if (!m_pxcImage.IsValid())
return false;
PXCImage::ImageInfo info;
pxcImage->QueryInfo(&info);
m_pxcImage->QueryInfo(&info);
PXCImage::ImageData data;
pxcImage->AcquireAccess(PXCImage::ACCESS_READ, &data);
m_pxcImage->AcquireAccess(PXCImage::ACCESS_READ, &data);
if (PXCImage::SURFACE_TYPE_SYSTEM_MEMORY != data.type)
return false;
if (PXCImage::COLOR_FORMAT_DEPTH != data.format)
return false;
cv::Mat temp(info.height, info.width, type, data.planes[planeIdx], data.pitches[planeIdx]);
temp.copyTo(frame);
{
cv::Mat temp(info.height, info.width, CV_16SC1, data.planes[0], data.pitches[0]);
temp.copyTo(m_frameDepth.m_mat);
}
{
cv::Mat temp(info.height, info.width, CV_16SC1, data.planes[1], data.pitches[1]);
temp.copyTo(m_frameIR.m_mat);
}
{
cv::Mat temp(info.height, info.width, CV_32FC2, data.planes[2], data.pitches[2]);
temp.copyTo(m_frameUV.m_mat);
}
pxcImage->ReleaseAccess(&data);
m_pxcImage->ReleaseAccess(&data);
return true;
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CvCapture_IntelPerC : public CvCapture
{
public:
CvCapture_IntelPerC(int /*index*/)
///////////////// VideoCapture_IntelPerC //////////////////
VideoCapture_IntelPerC::VideoCapture_IntelPerC()
: m_contextOpened(false)
{
pxcStatus sts = PXCSession_Create(&m_session);
@ -611,9 +552,9 @@ public:
m_contextOpened = m_imageStream.initStream(m_session);
m_contextOpened &= m_depthStream.initStream(m_session);
}
virtual ~CvCapture_IntelPerC(){}
VideoCapture_IntelPerC::~VideoCapture_IntelPerC(){}
virtual double getProperty(int propIdx)
double VideoCapture_IntelPerC::getProperty(int propIdx)
{
double propValue = 0;
int purePropIdx = propIdx & ~CV_CAP_INTELPERC_GENERATORS_MASK;
@ -631,7 +572,7 @@ public:
}
return propValue;
}
virtual bool setProperty(int propIdx, double propVal)
bool VideoCapture_IntelPerC::setProperty(int propIdx, double propVal)
{
bool isSet = false;
int purePropIdx = propIdx & ~CV_CAP_INTELPERC_GENERATORS_MASK;
@ -650,7 +591,7 @@ public:
return isSet;
}
bool grabFrame()
bool VideoCapture_IntelPerC::grabFrame()
{
if (!isOpened())
return false;
@ -663,52 +604,31 @@ public:
return isGrabbed;
}
virtual IplImage* retrieveFrame(int outputType)
bool VideoCapture_IntelPerC::retrieveFrame(int outputType, cv::OutputArray frame)
{
IplImage* image = 0;
switch (outputType)
{
case CV_CAP_INTELPERC_DEPTH_MAP:
image = m_depthStream.retrieveDepthFrame();
break;
return m_depthStream.retrieveDepthAsOutputArray(frame);
case CV_CAP_INTELPERC_UVDEPTH_MAP:
image = m_depthStream.retrieveUVFrame();
break;
return m_depthStream.retrieveUVAsOutputArray(frame);
case CV_CAP_INTELPERC_IR_MAP:
image = m_depthStream.retrieveIRFrame();
break;
return m_depthStream.retrieveIRAsOutputArray(frame);
case CV_CAP_INTELPERC_IMAGE:
image = m_imageStream.retrieveFrame();
break;
return m_imageStream.retrieveAsOutputArray(frame);
}
CV_Assert(NULL != image);
return image;
return false;
}
int VideoCapture_IntelPerC::getCaptureDomain()
{
return CV_CAP_INTELPERC;
}
bool isOpened() const
bool VideoCapture_IntelPerC::isOpened() const
{
return m_contextOpened;
}
protected:
bool m_contextOpened;
PXCSmartPtr<PXCSession> m_session;
CvIntelPerCStreamImage m_imageStream;
CvIntelPerCStreamDepth m_depthStream;
};
CvCapture* cvCreateCameraCapture_IntelPerC(int index)
{
CvCapture_IntelPerC* capture = new CvCapture_IntelPerC(index);
if( capture->isOpened() )
return capture;
delete capture;
return 0;
}
#endif //HAVE_INTELPERC

View File

@ -0,0 +1,145 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef _CAP_INTELPERC_HPP_
#define _CAP_INTELPERC_HPP_
#include "precomp.hpp"
#ifdef HAVE_INTELPERC
#include "pxcsession.h"
#include "pxcsmartptr.h"
#include "pxccapture.h"
namespace cv
{
class IntelPerCStreamBase
{
public:
IntelPerCStreamBase();
virtual ~IntelPerCStreamBase();
bool isValid();
bool grabFrame();
int getProfileIDX() const;
public:
virtual bool initStream(PXCSession *session) = 0;
virtual double getProperty(int propIdx);
virtual bool setProperty(int propIdx, double propVal);
protected:
PXCSmartPtr<PXCCapture::Device> m_device;
bool initDevice(PXCSession *session);
PXCSmartPtr<PXCCapture::VideoStream> m_stream;
void initStreamImpl(PXCImage::ImageType type);
protected:
std::vector<PXCCapture::VideoStream::ProfileInfo> m_profiles;
int m_profileIdx;
int m_frameIdx;
pxcU64 m_timeStampStartNS;
double m_timeStamp;
PXCSmartPtr<PXCImage> m_pxcImage;
virtual bool validProfile(const PXCCapture::VideoStream::ProfileInfo& /*pinfo*/);
void enumProfiles();
};
class IntelPerCStreamImage
: public IntelPerCStreamBase
{
public:
IntelPerCStreamImage();
virtual ~IntelPerCStreamImage();
virtual bool initStream(PXCSession *session);
virtual double getProperty(int propIdx);
virtual bool setProperty(int propIdx, double propVal);
public:
bool retrieveAsOutputArray(OutputArray image);
};
class IntelPerCStreamDepth
: public IntelPerCStreamBase
{
public:
IntelPerCStreamDepth();
virtual ~IntelPerCStreamDepth();
virtual bool initStream(PXCSession *session);
virtual double getProperty(int propIdx);
virtual bool setProperty(int propIdx, double propVal);
public:
bool retrieveDepthAsOutputArray(OutputArray image);
bool retrieveIRAsOutputArray(OutputArray image);
bool retrieveUVAsOutputArray(OutputArray image);
protected:
virtual bool validProfile(const PXCCapture::VideoStream::ProfileInfo& pinfo);
protected:
bool retriveFrame(int type, int planeIdx, OutputArray frame);
};
class VideoCapture_IntelPerC : public IVideoCapture
{
public:
VideoCapture_IntelPerC();
virtual ~VideoCapture_IntelPerC();
virtual double getProperty(int propIdx);
virtual bool setProperty(int propIdx, double propVal);
virtual bool grabFrame();
virtual bool retrieveFrame(int outputType, OutputArray frame);
virtual int getCaptureDomain();
bool isOpened() const;
protected:
bool m_contextOpened;
PXCSmartPtr<PXCSession> m_session;
IntelPerCStreamImage m_imageStream;
IntelPerCStreamDepth m_depthStream;
};
}
#endif //HAVE_INTELPERC
#endif //_CAP_INTELPERC_HPP_

View File

@ -136,8 +136,6 @@ CvCapture* cvCreateFileCapture_OpenNI( const char* filename );
CvCapture* cvCreateCameraCapture_Android( int index );
CvCapture* cvCreateCameraCapture_XIMEA( int index );
CvCapture* cvCreateCameraCapture_AVFoundation(int index);
CvCapture* cvCreateCameraCapture_IntelPerC(int index);
CVAPI(int) cvHaveImageReader(const char* filename);
CVAPI(int) cvHaveImageWriter(const char* filename);