Fix opencv conversion problems in PlaYUVerFrame
This commit is contained in:
1 parent
e271fe5022
commit
76a216bd3a
7 files changed
+69
-116
No files matched your search
@@ -76,79 +76,13 @@ std::vector<std::string> LibOpenCVHandler::supportedSaveFormatsName()
|
||||
|
||||
PlaYUVerFrame* LibOpenCVHandler::loadFrame( std::string filename )
|
||||
{
|
||||
cv::Mat pcCvFrame = cv::imread( filename );
|
||||
Int cvChannels = pcCvFrame.channels();
|
||||
std::string pelFormatName;
|
||||
Int iPelFormat = -1;
|
||||
switch( cvChannels )
|
||||
{
|
||||
case 1:
|
||||
pelFormatName = "GRAY";
|
||||
break;
|
||||
case 3:
|
||||
pelFormatName = "BRG24";
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for( UInt i = 0; i < PlaYUVerFrame::supportedPixelFormatListNames().size(); i++ )
|
||||
{
|
||||
if( PlaYUVerFrame::supportedPixelFormatListNames()[i] == pelFormatName )
|
||||
{
|
||||
iPelFormat = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( iPelFormat >= 0 )
|
||||
{
|
||||
PlaYUVerFrame* pcFrame = new PlaYUVerFrame( pcCvFrame.cols, pcCvFrame.rows, iPelFormat );
|
||||
pcFrame->frameFromBuffer( pcCvFrame.ptr(), pcFrame->getBytesPerFrame() );
|
||||
return pcFrame;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Bool LibOpenCVHandler::saveFrame( PlaYUVerFrame* pcFrame, std::string filename )
|
||||
{
|
||||
Int cvType = CV_8UC3;
|
||||
switch( pcFrame->getNumberChannels() )
|
||||
{
|
||||
case 3:
|
||||
cvType = CV_8UC3;
|
||||
break;
|
||||
case 1:
|
||||
cvType = CV_8UC1;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
cv::Mat pcCvFrame( pcFrame->getHeight(), pcFrame->getWidth(), cvType );
|
||||
UChar* pCvPel = pcCvFrame.data;
|
||||
if( pcFrame->getColorSpace() != PlaYUVerFrame::COLOR_GRAY )
|
||||
{
|
||||
UChar* pARGB = pcFrame->getRGBBuffer();
|
||||
for( UInt i = 0; i < pcFrame->getHeight() * pcFrame->getWidth(); i++ )
|
||||
{
|
||||
*pCvPel++ = *pARGB++;
|
||||
*pCvPel++ = *pARGB++;
|
||||
*pCvPel++ = *pARGB++;
|
||||
pARGB++;
|
||||
}
|
||||
}
|
||||
if( pcFrame->getColorSpace() == PlaYUVerFrame::COLOR_GRAY )
|
||||
{
|
||||
|
||||
Pel* pPel = pcFrame->getPelBufferYUV()[LUMA][0];
|
||||
for( UInt i = 0; i < pcFrame->getHeight() * pcFrame->getWidth(); i++ )
|
||||
{
|
||||
*pCvPel++ = *pPel++;
|
||||
}
|
||||
}
|
||||
return cv::imwrite( filename, pcCvFrame );
|
||||
cv::Mat* pcCvFrame = pcFrame->getCvMat();
|
||||
return cv::imwrite( filename, *pcCvFrame );
|
||||
}
|
||||
|
||||
} // NAMESPACE
|
||||
@@ -55,8 +55,6 @@ public:
|
||||
static PlaYUVerFrame* loadFrame( std::string filename );
|
||||
static Bool saveFrame( PlaYUVerFrame* pcFrame, std::string filename );
|
||||
|
||||
private:
|
||||
std::string m_cFilename;
|
||||
};
|
||||
|
||||
} // NAMESPACE
|
||||
|
||||
+44
-31
@@ -521,47 +521,59 @@ Void PlaYUVerFrame::FrametoRGB8Pixfc()
|
||||
#endif
|
||||
}
|
||||
|
||||
Void PlaYUVerFrame::getCvMat( Void** ppCvFrame )
|
||||
cv::Mat* PlaYUVerFrame::getCvMat( Bool convertToGray )
|
||||
{
|
||||
#ifdef USE_OPENCV
|
||||
Int cvType = CV_8UC3;
|
||||
switch( m_pcPelFormat->numberChannels )
|
||||
if( convertToGray && !( m_pcPelFormat->colorSpace == PlaYUVerFrame::COLOR_YUV || m_pcPelFormat->colorSpace == PlaYUVerFrame::COLOR_GRAY ) )
|
||||
{
|
||||
case 3:
|
||||
cvType = CV_8UC4;
|
||||
break;
|
||||
case 1:
|
||||
cvType = CV_8UC1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
return NULL;
|
||||
}
|
||||
Int cvType = CV_MAKETYPE( CV_8U, convertToGray ? 1 : m_pcPelFormat->numberChannels );
|
||||
cv::Mat *pcCvFrame = new cv::Mat( m_uiHeight, m_uiWidth, cvType );
|
||||
if( m_pcPelFormat->colorSpace != PlaYUVerFrame::COLOR_GRAY )
|
||||
UChar* pCvPel = pcCvFrame->data;
|
||||
if( !convertToGray && ( m_pcPelFormat->colorSpace == PlaYUVerFrame::COLOR_YUV || m_pcPelFormat->colorSpace == PlaYUVerFrame::COLOR_RGB ) )
|
||||
{
|
||||
fillRGBBuffer();
|
||||
memcpy( pcCvFrame->data, m_pcARGB32, m_uiWidth * m_uiHeight * 4 * sizeof(UChar) );
|
||||
UChar* pARGB = m_pcARGB32;
|
||||
for( UInt i = 0; i < m_uiWidth * m_uiHeight; i++ )
|
||||
{
|
||||
*pCvPel++ = *pARGB++;
|
||||
*pCvPel++ = *pARGB++;
|
||||
*pCvPel++ = *pARGB++;
|
||||
pARGB++;
|
||||
}
|
||||
}
|
||||
else if( convertToGray || m_pcPelFormat->colorSpace == PlaYUVerFrame::COLOR_GRAY )
|
||||
{
|
||||
Pel* pPel = m_pppcInputPel[LUMA][0];
|
||||
for( UInt i = 0; i < m_uiWidth * m_uiHeight; i++ )
|
||||
{
|
||||
*pCvPel++ = *pPel++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy( pcCvFrame->data, &( m_pppcInputPel[LUMA][0][0] ), m_uiWidth * m_uiHeight * sizeof(Pel) );
|
||||
if( pcCvFrame )
|
||||
delete pcCvFrame;
|
||||
pcCvFrame = NULL;
|
||||
}
|
||||
*ppCvFrame = pcCvFrame;
|
||||
return pcCvFrame;
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
Void PlaYUVerFrame::fromCvMat( Void* voidFrame )
|
||||
Void PlaYUVerFrame::fromCvMat( cv::Mat* pcCvFrame )
|
||||
{
|
||||
#ifdef USE_OPENCV
|
||||
cv::Mat* opencvFrame = ( cv::Mat* )voidFrame;
|
||||
if( m_iPixelFormat == NO_FMT )
|
||||
{
|
||||
switch( opencvFrame->channels() )
|
||||
switch( pcCvFrame->channels() )
|
||||
{
|
||||
case 1:
|
||||
m_iPixelFormat = GRAY;
|
||||
break;
|
||||
case 4:
|
||||
case 3:
|
||||
m_iPixelFormat = RGB24;
|
||||
break;
|
||||
default:
|
||||
@@ -571,31 +583,32 @@ Void PlaYUVerFrame::fromCvMat( Void* voidFrame )
|
||||
|
||||
if( !isValid() )
|
||||
{
|
||||
init( opencvFrame->cols, opencvFrame->rows, m_iPixelFormat, 8 );
|
||||
init( pcCvFrame->cols, pcCvFrame->rows, m_iPixelFormat, 8 );
|
||||
}
|
||||
|
||||
m_bHasRGBPel = false;
|
||||
m_bHasHistogram = false;
|
||||
|
||||
UChar* pCvPel = pcCvFrame->data;
|
||||
if( m_iPixelFormat != GRAY )
|
||||
{
|
||||
Pel* pInputPelY = m_pppcInputPel[LUMA][0];
|
||||
Pel* pInputPelU = m_pppcInputPel[CHROMA_U][0];
|
||||
Pel* pInputPelV = m_pppcInputPel[CHROMA_V][0];
|
||||
UChar* pcRGBPelInterlaced = m_pcARGB32;
|
||||
memcpy( pcRGBPelInterlaced, opencvFrame->data, m_uiWidth * m_uiHeight * 4 * sizeof(Pel) );
|
||||
UInt* buff = ( UInt* )pcRGBPelInterlaced;
|
||||
Pel* pInputPelR = m_pppcInputPel[COLOR_R][0];
|
||||
Pel* pInputPelG = m_pppcInputPel[COLOR_G][0];
|
||||
Pel* pInputPelB = m_pppcInputPel[COLOR_B][0];
|
||||
for( UInt i = 0; i < m_uiHeight * m_uiWidth; i++ )
|
||||
{
|
||||
*pInputPelY++ = pelRed( *buff );
|
||||
*pInputPelU++ = pelGreen( *buff );
|
||||
*pInputPelV++ = pelBlue( *buff++ );
|
||||
*pInputPelR++ = pelRed( *pCvPel++ );
|
||||
*pInputPelG++ = pelGreen( *pCvPel++ );
|
||||
*pInputPelB++ = pelBlue( *pCvPel++ );
|
||||
}
|
||||
m_bHasRGBPel = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy( m_pppcInputPel[LUMA][0], opencvFrame->data, m_uiWidth * m_uiHeight * sizeof(Pel) );
|
||||
Pel* pPel = m_pppcInputPel[LUMA][0];
|
||||
for( UInt i = 0; i < m_uiWidth * m_uiHeight; i++ )
|
||||
{
|
||||
*pPel++ = *pCvPel++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
+6
-2
@@ -34,6 +34,10 @@
|
||||
#include "PlaYUVerFrameStats.h"
|
||||
|
||||
class PixFcSSE;
|
||||
namespace cv
|
||||
{
|
||||
class Mat;
|
||||
}
|
||||
|
||||
namespace plaYUVer
|
||||
{
|
||||
@@ -286,8 +290,8 @@ public:
|
||||
/**
|
||||
* Interface with OpenCV lib
|
||||
*/
|
||||
Void getCvMat( Void** );
|
||||
Void fromCvMat( Void* );
|
||||
cv::Mat* getCvMat( Bool convertToGray = false );
|
||||
Void fromCvMat( cv::Mat* );
|
||||
|
||||
/**
|
||||
* @defgroup PlaYUVerLib_QualityMetrics Quality Metrics Interface
|
||||
|
||||
@@ -46,10 +46,10 @@ std::vector<std::string> PlaYUVerStream::supportedReadFormatsExt()
|
||||
std::vector<std::string> libAvFmt = LibAvContextHandle::supportedReadFormatsExt();
|
||||
formatsExt.insert( formatsExt.end(), libAvFmt.begin(), libAvFmt.end() );
|
||||
#endif
|
||||
#ifdef USE_OPENCV
|
||||
std::vector<std::string> opencvFmt = LibOpenCVHandler::supportedReadFormatsExt();
|
||||
formatsExt.insert( formatsExt.end(), opencvFmt.begin(), opencvFmt.end() );
|
||||
#endif
|
||||
//#ifdef USE_OPENCV
|
||||
// std::vector<std::string> opencvFmt = LibOpenCVHandler::supportedReadFormatsExt();
|
||||
// formatsExt.insert( formatsExt.end(), opencvFmt.begin(), opencvFmt.end() );
|
||||
//#endif
|
||||
return formatsExt;
|
||||
}
|
||||
|
||||
@@ -61,10 +61,10 @@ std::vector<std::string> PlaYUVerStream::supportedReadFormatsName()
|
||||
std::vector<std::string> libAvFmt = LibAvContextHandle::supportedReadFormatsName();
|
||||
formatsName.insert( formatsName.end(), libAvFmt.begin(), libAvFmt.end() );
|
||||
#endif
|
||||
#ifdef USE_OPENCV
|
||||
std::vector<std::string> opencvFmt = LibOpenCVHandler::supportedReadFormatsName();
|
||||
formatsName.insert( formatsName.end(), opencvFmt.begin(), opencvFmt.end() );
|
||||
#endif
|
||||
//#ifdef USE_OPENCV
|
||||
// std::vector<std::string> opencvFmt = LibOpenCVHandler::supportedReadFormatsName();
|
||||
// formatsName.insert( formatsName.end(), opencvFmt.begin(), opencvFmt.end() );
|
||||
//#endif
|
||||
return formatsName;
|
||||
}
|
||||
|
||||
@@ -143,6 +143,7 @@ PlaYUVerStream::PlaYUVerStream()
|
||||
m_uiTotalFrameNum = 0;
|
||||
m_iCurrFrameNum = -1;
|
||||
m_iPixelFormat = -1;
|
||||
m_uiBitsPerPixel = 8;
|
||||
m_dFrameRate = 30;
|
||||
m_iFileFormat = -1;
|
||||
m_pStreamBuffer = NULL;
|
||||
|
||||
@@ -67,16 +67,14 @@ PlaYUVerFrame* DisparityStereoVar::process( std::vector<PlaYUVerFrame*> apcFrame
|
||||
PlaYUVerFrame* InputLeft = apcFrameList[0];
|
||||
PlaYUVerFrame* InputRight = apcFrameList[1];
|
||||
|
||||
cv::Mat* leftImage;
|
||||
InputLeft->getCvMat( ( Void** )&leftImage );
|
||||
cv::Mat* rightImage;
|
||||
InputRight->getCvMat( ( Void** )&rightImage );
|
||||
cv::Mat* leftImage = InputLeft->getCvMat();
|
||||
cv::Mat* rightImage = InputRight->getCvMat();
|
||||
cv::Mat disparityImage, disparityImage8;
|
||||
|
||||
m_cStereoVar( *leftImage, *rightImage, disparityImage );
|
||||
disparityImage.convertTo( disparityImage8, CV_8U );
|
||||
|
||||
m_pcDisparityFrame->fromCvMat( ( Void* )&disparityImage8 );
|
||||
m_pcDisparityFrame->fromCvMat( &disparityImage8 );
|
||||
return m_pcDisparityFrame;
|
||||
}
|
||||
|
||||
|
||||
@@ -196,6 +196,11 @@ Int PlaYUVerTools::Open( Int argc, Char *argv[] )
|
||||
m_pcCurrModuleIf->create( m_apcInputStreams[0]->getCurrFrame() );
|
||||
moduleCreated = true;
|
||||
}
|
||||
if( !moduleCreated )
|
||||
{
|
||||
m_cCmdLineHandler.log( CommandLineHandle::ERROR, "Module is not supported with the selected inputs! " );
|
||||
return 2;
|
||||
}
|
||||
|
||||
if( m_pcCurrModuleIf->m_iModuleType == FRAME_PROCESSING_MODULE )
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user