天天看點

opencv yuv420與Mat互轉

項目用到opencv 融合圖檔的功能,經過一天的調試,達到預期目标,先将如何調用opencv庫實作YUV42與Mat互轉記錄下來。

一.下載下傳opencv編譯的庫下載下傳位址是:http://opencv.org/releases.html ,根據需要選擇相應的版本,我們項目中用到的版本是2.4.13。

二.下載下傳Eigen庫,下載下傳位址是:http://eigen.tuxfamily.org/index.php?title=Main_Page,将Eigen庫解壓到本地,引用opencv庫是要用到

     Eigen庫(例如解壓後的目錄是F:\vs2015-project\CGFusePlay\lib\eigen-eigen-da9b4e14c255,将路徑F:\vs2015-project\CGFusePlay\lib\eigen-eigen-da9b4e14c255\Eigen

添加到編譯工程中),否則會編譯不通過。

三,将YUV420資料與Mat互轉:

mainYUuvImage.create( mainHeight * 3 / 2, mainWidth, CV_8UC1);
	CopyYUVToImage(mainYUuvImage.data, mainY, mainU, mainV, mainWidth, mainHeight);
	cv::Mat mainRgbImage;
	cv::cvtColor(mainYUuvImage, mainRgbImage, CV_YUV2BGR_I420);
					
	cv::Mat subYuvImage;
	subYuvImage.create(subHeight * 3 / 2,subWidth,  CV_8UC1);
	CopyYUVToImage(subYuvImage.data, subY, subU, subV, subWidth, subHeight);
	cv::Mat subRgbImage;
	cv::cvtColor(subYuvImage, subRgbImage, CV_YUV2BGR_I420);

	double alpha = 0.5;
	cv::Mat dstRgbImage;
	
	cv::Mat dstYuvImage;
	cv::cvtColor(dstRgbImage, dstYuvImage, CV_BGR2YUV_I420);

	CopyImageToYUV(mainY, mainU, mainV, dstYuvImage.data,mainWidth, mainHeight);      
static void CopyYUVToImage(uchar * dst ,uint8_t *pY, uint8_t *pU , uint8_t *pV,int width, int height)
{
  uint32_t size = width * height;
  memcpy(dst, pY, size);
  memcpy(dst + size, pU, size /4);
  memcpy(dst + size + size /4, pV, size / 4);
}

static void CopyImageToYUV(uint8_t *pY, uint8_t *pU, uint8_t *pV, uchar * src ,int width, int height)
{
  uint32_t size = width * height;
  memcpy(pY, src, size);
  memcpy(pU, src + size, size / 4);
  memcpy(pV, src + size + size / 4, size / 4);
}
      

  

如需交流,可以加QQ群1038388075,766718184,或者QQ:350197870

 部落客提供ffmpeg 視訊教程 播放位址: http://www.iqiyi.com/u/1426749687

視訊下載下傳位址:http://www.chungen90.com/?news_33/

 Demo下載下傳位址: http://www.chungen90.com/?news_34