Fix minor errors when loading images with opencv
This commit is contained in:
1 parent
d1ad36c28a
commit
ee8f0c65d4
3 files changed
+82
-44
No files matched your search
@@ -83,22 +83,22 @@ std::vector<std::string> LibOpenCVHandler::supportedSaveFormatsName()
|
|||||||
return formatsName;
|
return formatsName;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bool LibOpenCVHandler::loadFrame( PlaYUVerFrame* pcFrame, std::string filename )
|
PlaYUVerFrame* LibOpenCVHandler::loadFrame( std::string filename )
|
||||||
{
|
{
|
||||||
cv::Mat pcCvFrame = cv::imread( filename );
|
cv::Mat pcCvFrame = cv::imread( filename );
|
||||||
|
Int cvChannels = pcCvFrame.channels();
|
||||||
std::string pelFormatName;
|
std::string pelFormatName;
|
||||||
Int iPelFormat;
|
Int iPelFormat = -1;
|
||||||
switch( pcCvFrame.channels() )
|
switch( cvChannels )
|
||||||
{
|
{
|
||||||
case 1:
|
case 1:
|
||||||
pelFormatName = "GRAY";
|
pelFormatName = "GRAY";
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 3:
|
||||||
pelFormatName = "RGB8";
|
pelFormatName = "BRG24";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( UInt i = 0; i < PlaYUVerFrame::supportedPixelFormatListNames().size(); i++ )
|
for( UInt i = 0; i < PlaYUVerFrame::supportedPixelFormatListNames().size(); i++ )
|
||||||
@@ -109,12 +109,18 @@ Bool LibOpenCVHandler::loadFrame( PlaYUVerFrame* pcFrame, std::string filename )
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
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 )
|
Bool LibOpenCVHandler::saveFrame( PlaYUVerFrame* pcFrame, std::string filename )
|
||||||
{
|
{
|
||||||
|
|
||||||
Int cvType = CV_8UC3;
|
Int cvType = CV_8UC3;
|
||||||
switch( pcFrame->getNumberChannels() )
|
switch( pcFrame->getNumberChannels() )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
static Bool loadFrame( PlaYUVerFrame* pcFrame, std::string filename );
|
static PlaYUVerFrame* loadFrame( std::string filename );
|
||||||
static Bool saveFrame( PlaYUVerFrame* pcFrame, std::string filename );
|
static Bool saveFrame( PlaYUVerFrame* pcFrame, std::string filename );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
+66
-34
@@ -237,7 +237,7 @@ Bool PlaYUVerStream::open( std::string filename, UInt width, UInt height, Int in
|
|||||||
m_uiStreamHandler = YUV_IO;
|
m_uiStreamHandler = YUV_IO;
|
||||||
}
|
}
|
||||||
//#ifdef USE_OPENCV
|
//#ifdef USE_OPENCV
|
||||||
// else if ( m_cFormatName == "png" )
|
// else if( m_cFormatName == "png" || m_cFormatName == "jpg" )
|
||||||
// {
|
// {
|
||||||
// m_uiStreamHandler = OPENCV_HANDLER;
|
// m_uiStreamHandler = OPENCV_HANDLER;
|
||||||
// }
|
// }
|
||||||
@@ -279,6 +279,19 @@ Bool PlaYUVerStream::open( std::string filename, UInt width, UInt height, Int in
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_OPENCV
|
||||||
|
if( m_uiStreamHandler == OPENCV_HANDLER )
|
||||||
|
{
|
||||||
|
m_pcCurrFrame = LibOpenCVHandler::loadFrame( m_cFilename );
|
||||||
|
if( m_pcCurrFrame )
|
||||||
|
{
|
||||||
|
m_uiWidth = m_pcCurrFrame->getWidth();
|
||||||
|
m_uiHeight = m_pcCurrFrame->getHeight();
|
||||||
|
m_iPixelFormat = m_pcCurrFrame->getPelFormat();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if( m_uiWidth <= 0 || m_uiHeight <= 0 || m_iPixelFormat < 0 )
|
if( m_uiWidth <= 0 || m_uiHeight <= 0 || m_iPixelFormat < 0 )
|
||||||
{
|
{
|
||||||
close();
|
close();
|
||||||
@@ -286,49 +299,59 @@ Bool PlaYUVerStream::open( std::string filename, UInt width, UInt height, Int in
|
|||||||
return m_bInit;
|
return m_bInit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( m_uiStreamHandler == YUV_IO )
|
if( m_uiStreamHandler != OPENCV_HANDLER )
|
||||||
{
|
{
|
||||||
if( !openFile() )
|
if( m_uiStreamHandler == YUV_IO )
|
||||||
{
|
{
|
||||||
close();
|
if( !openFile() )
|
||||||
throw "[PlaYUVerStream] Cannot open file";
|
{
|
||||||
return m_bInit;
|
close();
|
||||||
|
throw "[PlaYUVerStream] Cannot open file";
|
||||||
|
return m_bInit;
|
||||||
|
}
|
||||||
|
m_cFormatName = "YUV";
|
||||||
|
m_cCodedName = "Raw Video";
|
||||||
}
|
}
|
||||||
m_cFormatName = "YUV";
|
|
||||||
m_cCodedName = "Raw Video";
|
|
||||||
}
|
|
||||||
|
|
||||||
getMem1D<PlaYUVerFrame*>( &m_ppcFrameBuffer, m_uiFrameBufferSize );
|
getMem1D<PlaYUVerFrame*>( &m_ppcFrameBuffer, m_uiFrameBufferSize );
|
||||||
for( UInt i = 0; i < m_uiFrameBufferSize; i++ )
|
for( UInt i = 0; i < m_uiFrameBufferSize; i++ )
|
||||||
{
|
|
||||||
m_ppcFrameBuffer[i] = new PlaYUVerFrame( m_uiWidth, m_uiHeight, m_iPixelFormat );
|
|
||||||
if( !m_ppcFrameBuffer[i] )
|
|
||||||
{
|
{
|
||||||
close();
|
m_ppcFrameBuffer[i] = new PlaYUVerFrame( m_uiWidth, m_uiHeight, m_iPixelFormat );
|
||||||
throw "[PlaYUVerStream] Cannot allocated frame buffer";
|
if( !m_ppcFrameBuffer[i] )
|
||||||
return m_bInit;
|
{
|
||||||
|
close();
|
||||||
|
throw "[PlaYUVerStream] Cannot allocated frame buffer";
|
||||||
|
return m_bInit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
m_uiFrameBufferIndex = 0;
|
||||||
m_uiFrameBufferIndex = 0;
|
m_pcCurrFrame = m_ppcFrameBuffer[m_uiFrameBufferIndex];
|
||||||
m_pcCurrFrame = m_ppcFrameBuffer[m_uiFrameBufferIndex];
|
|
||||||
|
|
||||||
UInt64 frame_bytes_input = m_pcCurrFrame->getBytesPerFrame();
|
UInt64 frame_bytes_input = m_pcCurrFrame->getBytesPerFrame();
|
||||||
|
|
||||||
if( m_uiStreamHandler == YUV_IO && m_bIsInput )
|
if( m_uiStreamHandler == YUV_IO && m_bIsInput )
|
||||||
{
|
{
|
||||||
fseek( m_pFile, 0, SEEK_END );
|
fseek( m_pFile, 0, SEEK_END );
|
||||||
UInt64 fileSize = ftell( m_pFile );
|
UInt64 fileSize = ftell( m_pFile );
|
||||||
#if 0
|
#if 0
|
||||||
if( fileSize % frame_bytes_input )
|
if( fileSize % frame_bytes_input )
|
||||||
{
|
{
|
||||||
Int remaining = fileSize % frame_bytes_input;
|
Int remaining = fileSize % frame_bytes_input;
|
||||||
close();
|
close();
|
||||||
throw "[PlaYUVerStream] Incorrect configuration: failed ( fileSize % frame_bytes_input = " << remaining << " )";
|
throw "[PlaYUVerStream] Incorrect configuration: failed ( fileSize % frame_bytes_input = " << remaining << " )";
|
||||||
return m_bInit;
|
return m_bInit;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
m_uiTotalFrameNum = fileSize / frame_bytes_input;
|
m_uiTotalFrameNum = fileSize / frame_bytes_input;
|
||||||
fseek( m_pFile, 0, SEEK_SET );
|
fseek( m_pFile, 0, SEEK_SET );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_pcNextFrame = NULL;
|
||||||
|
m_uiFrameBufferSize = 0;
|
||||||
|
m_uiFrameBufferIndex = 0;
|
||||||
|
m_uiTotalFrameNum = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( m_bIsInput && m_uiTotalFrameNum <= 0 )
|
if( m_bIsInput && m_uiTotalFrameNum <= 0 )
|
||||||
@@ -512,6 +535,11 @@ Void PlaYUVerStream::readFrame()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( m_uiStreamHandler == OPENCV_HANDLER )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef USE_FFMPEG
|
#ifdef USE_FFMPEG
|
||||||
if( m_uiStreamHandler == FFMPEG )
|
if( m_uiStreamHandler == FFMPEG )
|
||||||
{
|
{
|
||||||
@@ -676,6 +704,10 @@ Bool PlaYUVerStream::seekInput( UInt64 new_frame_num )
|
|||||||
setNextFrame();
|
setNextFrame();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if( m_uiStreamHandler == OPENCV_HANDLER )
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
#ifdef USE_FFMPEG
|
#ifdef USE_FFMPEG
|
||||||
if( m_uiStreamHandler == FFMPEG )
|
if( m_uiStreamHandler == FFMPEG )
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user