Converting YUV into BGR or RGB in OpenCV

后端 未结 6 592
时光说笑
时光说笑 2020-11-30 04:35

I have a TV capture card that has a feed coming in as a YUV format. I\'ve seen other posts here similar to this question and attempted to try every possible method stated, b

6条回答
  •  情话喂你
    2020-11-30 04:52

    I use the following C++ code using OpenCV to convert yuv data (YUV_NV21) to rgb image (BGR in OpenCV)

    int main()
    {
      const int width  = 1280;
      const int height = 800;
    
      std::ifstream file_in;
      file_in.open("../image_yuv_nv21_1280_800_01.raw", std::ios::binary);
      std::filebuf *p_filebuf = file_in.rdbuf();
      size_t size = p_filebuf->pubseekoff(0, std::ios::end, std::ios::in);
      p_filebuf->pubseekpos(0, std::ios::in);
    
      char *buf_src = new char[size];
      p_filebuf->sgetn(buf_src, size);
    
      cv::Mat mat_src = cv::Mat(height*1.5, width, CV_8UC1, buf_src);
      cv::Mat mat_dst = cv::Mat(height, width, CV_8UC3);
    
      cv::cvtColor(mat_src, mat_dst, cv::COLOR_YUV2BGR_NV21);
      cv::imwrite("yuv.png", mat_dst);
    
      file_in.close();
      delete []buf_src;
    
      return 0;
    }
    

    and the converted result is like the image yuv.png.

    you can find the testing raw image from here and the whole project from my Github Project

提交回复
热议问题