From ee8f0c65d480be1e21819eff75db33ee0d3d643a Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Thu, 5 Mar 2015 01:07:28 +0000 Subject: [PATCH] Fix minor errors when loading images with opencv --- lib/LibOpenCVHandler.cpp | 24 ++++++---- lib/LibOpenCVHandler.h | 2 +- lib/PlaYUVerStream.cpp | 100 ++++++++++++++++++++++++++------------- 3 files changed, 82 insertions(+), 44 deletions(-) diff --git a/lib/LibOpenCVHandler.cpp b/lib/LibOpenCVHandler.cpp index 290bcb4..7d2aa6e 100644 --- a/lib/LibOpenCVHandler.cpp +++ b/lib/LibOpenCVHandler.cpp @@ -83,22 +83,22 @@ std::vector LibOpenCVHandler::supportedSaveFormatsName() return formatsName; } -Bool LibOpenCVHandler::loadFrame( PlaYUVerFrame* pcFrame, std::string filename ) +PlaYUVerFrame* LibOpenCVHandler::loadFrame( std::string filename ) { cv::Mat pcCvFrame = cv::imread( filename ); - + Int cvChannels = pcCvFrame.channels(); std::string pelFormatName; - Int iPelFormat; - switch( pcCvFrame.channels() ) + Int iPelFormat = -1; + switch( cvChannels ) { case 1: pelFormatName = "GRAY"; break; - case 4: - pelFormatName = "RGB8"; + case 3: + pelFormatName = "BRG24"; break; default: - return false; + return NULL; } for( UInt i = 0; i < PlaYUVerFrame::supportedPixelFormatListNames().size(); i++ ) @@ -109,12 +109,18 @@ Bool LibOpenCVHandler::loadFrame( PlaYUVerFrame* pcFrame, std::string filename ) 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 ) { - Int cvType = CV_8UC3; switch( pcFrame->getNumberChannels() ) { diff --git a/lib/LibOpenCVHandler.h b/lib/LibOpenCVHandler.h index 7acd81e..d24cd76 100644 --- a/lib/LibOpenCVHandler.h +++ b/lib/LibOpenCVHandler.h @@ -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 ); private: diff --git a/lib/PlaYUVerStream.cpp b/lib/PlaYUVerStream.cpp index e43c006..bf999db 100644 --- a/lib/PlaYUVerStream.cpp +++ b/lib/PlaYUVerStream.cpp @@ -237,7 +237,7 @@ Bool PlaYUVerStream::open( std::string filename, UInt width, UInt height, Int in m_uiStreamHandler = YUV_IO; } //#ifdef USE_OPENCV -// else if ( m_cFormatName == "png" ) +// else if( m_cFormatName == "png" || m_cFormatName == "jpg" ) // { // m_uiStreamHandler = OPENCV_HANDLER; // } @@ -279,6 +279,19 @@ Bool PlaYUVerStream::open( std::string filename, UInt width, UInt height, Int in } #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 ) { close(); @@ -286,49 +299,59 @@ Bool PlaYUVerStream::open( std::string filename, UInt width, UInt height, Int in return m_bInit; } - if( m_uiStreamHandler == YUV_IO ) + if( m_uiStreamHandler != OPENCV_HANDLER ) { - if( !openFile() ) + if( m_uiStreamHandler == YUV_IO ) { - close(); - throw "[PlaYUVerStream] Cannot open file"; - return m_bInit; + if( !openFile() ) + { + close(); + throw "[PlaYUVerStream] Cannot open file"; + return m_bInit; + } + m_cFormatName = "YUV"; + m_cCodedName = "Raw Video"; } - m_cFormatName = "YUV"; - m_cCodedName = "Raw Video"; - } - getMem1D( &m_ppcFrameBuffer, m_uiFrameBufferSize ); - for( UInt i = 0; i < m_uiFrameBufferSize; i++ ) - { - m_ppcFrameBuffer[i] = new PlaYUVerFrame( m_uiWidth, m_uiHeight, m_iPixelFormat ); - if( !m_ppcFrameBuffer[i] ) + getMem1D( &m_ppcFrameBuffer, m_uiFrameBufferSize ); + for( UInt i = 0; i < m_uiFrameBufferSize; i++ ) { - close(); - throw "[PlaYUVerStream] Cannot allocated frame buffer"; - return m_bInit; + m_ppcFrameBuffer[i] = new PlaYUVerFrame( m_uiWidth, m_uiHeight, m_iPixelFormat ); + if( !m_ppcFrameBuffer[i] ) + { + close(); + throw "[PlaYUVerStream] Cannot allocated frame buffer"; + return m_bInit; + } } - } - m_uiFrameBufferIndex = 0; - m_pcCurrFrame = m_ppcFrameBuffer[m_uiFrameBufferIndex]; + m_uiFrameBufferIndex = 0; + 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 ) - { - fseek( m_pFile, 0, SEEK_END ); - UInt64 fileSize = ftell( m_pFile ); + if( m_uiStreamHandler == YUV_IO && m_bIsInput ) + { + fseek( m_pFile, 0, SEEK_END ); + UInt64 fileSize = ftell( m_pFile ); #if 0 - if( fileSize % frame_bytes_input ) - { - Int remaining = fileSize % frame_bytes_input; - close(); - throw "[PlaYUVerStream] Incorrect configuration: failed ( fileSize % frame_bytes_input = " << remaining << " )"; - return m_bInit; - } + if( fileSize % frame_bytes_input ) + { + Int remaining = fileSize % frame_bytes_input; + close(); + throw "[PlaYUVerStream] Incorrect configuration: failed ( fileSize % frame_bytes_input = " << remaining << " )"; + return m_bInit; + } #endif - m_uiTotalFrameNum = fileSize / frame_bytes_input; - fseek( m_pFile, 0, SEEK_SET ); + m_uiTotalFrameNum = fileSize / frame_bytes_input; + 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 ) @@ -512,6 +535,11 @@ Void PlaYUVerStream::readFrame() return; } + if( m_uiStreamHandler == OPENCV_HANDLER ) + { + return; + } + #ifdef USE_FFMPEG if( m_uiStreamHandler == FFMPEG ) { @@ -676,6 +704,10 @@ Bool PlaYUVerStream::seekInput( UInt64 new_frame_num ) setNextFrame(); return true; } + if( m_uiStreamHandler == OPENCV_HANDLER ) + { + return true; + } #ifdef USE_FFMPEG if( m_uiStreamHandler == FFMPEG ) {