From 92e758009ddfd9cb116ea0ec04cce2865473140b Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sat, 17 May 2014 02:07:25 +0100 Subject: [PATCH 1/2] Implement the function cv::Mat PlaYUVerFrame::getCvMat() --- source/app/InputStream.cpp | 9 --------- source/lib/PlaYUVerFrame.cpp | 36 ++++++++++++++++++++++++++++++++---- source/lib/PlaYUVerFrame.h | 3 +++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/source/app/InputStream.cpp b/source/app/InputStream.cpp index b00287d..b502506 100755 --- a/source/app/InputStream.cpp +++ b/source/app/InputStream.cpp @@ -387,15 +387,6 @@ PlaYUVerFrame* InputStream::getCurrFrame() return m_pcCurrFrame; } -#ifdef USE_OPENCV -cv::Mat InputStream::getFrameCvMat() -{ - cv::Mat cvMat( m_uiHeight, m_uiWidth, CV_8UC3 ); - - return cvMat; -} -#endif - Void InputStream::seekInput( UInt64 new_frame_num ) { if( !m_pFile ) diff --git a/source/lib/PlaYUVerFrame.cpp b/source/lib/PlaYUVerFrame.cpp index b039950..917e7c2 100755 --- a/source/lib/PlaYUVerFrame.cpp +++ b/source/lib/PlaYUVerFrame.cpp @@ -37,8 +37,8 @@ PlaYUVerFrame::PlaYUVerFrame( UInt width, UInt height, Int pel_format ) PlaYUVerFrame::PlaYUVerFrame( PlaYUVerFrame *other ) { - init( other->getWidth(), other->getHeight(), other->getPelFormat() ); - copyFrom(other); + init( other->getWidth(), other->getHeight(), other->getPelFormat() ); + copyFrom( other ); } PlaYUVerFrame::~PlaYUVerFrame() @@ -494,8 +494,36 @@ Pixel PlaYUVerFrame::getPixelValue( const QPoint &pos, Int color_space ) #ifdef USE_OPENCV cv::Mat PlaYUVerFrame::getCvMat() { - return cv::Mat(); + Int cvType = CV_8UC3; + switch( m_iPixelFormat ) + { + case YUV420p: + case YUV444p: + case YUV422p: + case YUYV422: + cvType = CV_8UC3; + break; + case GRAY: + cvType = CV_8UC1; + break; + case RGB8: + cvType = CV_8UC3; + break; + default: + break; + } + cv::Mat opencvFrame( m_uiHeight, m_uiWidth, cvType ); + if( m_iPixelFormat != GRAY ) + { + FrametoRGB8(); + memcpy( opencvFrame.data, m_pcRGBPelInterlaced, getBytesPerFrame() * sizeof(Pel) ); + } + else + { + memcpy( opencvFrame.data, &(m_pppcInputPel[LUMA][0][0]), getBytesPerFrame() * sizeof(Pel) ); + } + return opencvFrame; } #endif } - // NAMESPACE +// NAMESPACE diff --git a/source/lib/PlaYUVerFrame.h b/source/lib/PlaYUVerFrame.h index 0c3866f..1bc2fe5 100755 --- a/source/lib/PlaYUVerFrame.h +++ b/source/lib/PlaYUVerFrame.h @@ -31,6 +31,9 @@ #include #include #include +#ifdef USE_OPENCV +#include +#endif #ifdef USE_PIXFC #include "pixfc-sse.h" #endif From cb3a120a0bdd10431b9e0c8af8a8d9da26a5fa2e Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Sat, 17 May 2014 14:41:20 +0100 Subject: [PATCH 2/2] Implement Void PlaYUVerFrame::copyFrom( cv::Mat* opencvFrame ) --- source/lib/PlaYUVerFrame.cpp | 47 +++++++++++++++++++++++++++++++++++- source/lib/PlaYUVerFrame.h | 1 + 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/source/lib/PlaYUVerFrame.cpp b/source/lib/PlaYUVerFrame.cpp index 917e7c2..32f5c72 100755 --- a/source/lib/PlaYUVerFrame.cpp +++ b/source/lib/PlaYUVerFrame.cpp @@ -520,10 +520,55 @@ cv::Mat PlaYUVerFrame::getCvMat() } else { - memcpy( opencvFrame.data, &(m_pppcInputPel[LUMA][0][0]), getBytesPerFrame() * sizeof(Pel) ); + memcpy( opencvFrame.data, &( m_pppcInputPel[LUMA][0][0] ), getBytesPerFrame() * sizeof(Pel) ); } return opencvFrame; } + +Void PlaYUVerFrame::copyFrom( cv::Mat* opencvFrame ) +{ + if( m_iPixelFormat == NO_FMT ) + { + switch( opencvFrame->channels() ) + { + case 1: + m_iPixelFormat = GRAY; + break; + case 3: + m_iPixelFormat = RGB8; + break; + default: + return; + } + } + + if( !isValid() ) + { + init( opencvFrame->cols, opencvFrame->rows, m_iPixelFormat ); + } + + m_bHasRGBPel = false; + + if( m_iPixelFormat != GRAY ) + { + Pel* pInputPelY = m_pppcInputPel[LUMA][0]; + Pel* pInputPelU = m_pppcInputPel[CHROMA_U][0]; + Pel* pInputPelV = m_pppcInputPel[CHROMA_V][0]; + Pel* pcRGBPelInterlaced = m_pcRGBPelInterlaced; + memcpy( pcRGBPelInterlaced, opencvFrame->data, getBytesPerFrame() * sizeof(Pel) ); + for( UInt i = 0; i < m_uiHeight * m_uiWidth; i++ ) + { + *pInputPelY++ = *pcRGBPelInterlaced++; + *pInputPelU++ = *pcRGBPelInterlaced++; + *pInputPelV++ = *pcRGBPelInterlaced++; + } + m_bHasRGBPel = true; + } + else + { + memcpy( m_pppcInputPel[LUMA][0], opencvFrame->data, getBytesPerFrame() * sizeof(Pel) ); + } +} #endif } // NAMESPACE diff --git a/source/lib/PlaYUVerFrame.h b/source/lib/PlaYUVerFrame.h index 1bc2fe5..d47cf6d 100755 --- a/source/lib/PlaYUVerFrame.h +++ b/source/lib/PlaYUVerFrame.h @@ -145,6 +145,7 @@ public: #ifdef USE_OPENCV cv::Mat getCvMat(); + Void copyFrom( cv::Mat* input_frame ); #endif UInt getWidth() const