0

I am trying to read the raw YUV 4:2:2 (720p) data from the Read buffer, I mapped the physical address of the Read buffer to the virtual address.

mem is a pointer which is pointing to Read buffer base address. I tried with below below-mentioned code but it is returning the empty image, please can anybody help me to read the Raw YUV 4:2:2 format image data from the memory.

I have tried below-mentioned code, but i is returning empty image

cv::Mat img(1,1280 , numCols,CV_8U1, mem);
//mem -> mem is a pointer to Read buffer which have raw YUV 4:2:2 image data .
//len-> Frame resolution (1280 * 720 * 3) (720p)
if (img.empty())
{
cout << "Image is not loaded" << endl;
return 0;
} 
cv::imshow("img",img);  
cv::waitKey();
ZF007
  • 3,708
  • 8
  • 29
  • 48
Gagandeep
  • 11
  • 6
  • It's unclear to me whether you have some image data in memory somewhere that you want to load into an OpenCV Mat, or you have an OpenCV Mat and you are trying to copy the data out of it to another buffer. – Mark Setchell Mar 29 '19 at 13:23
  • I have some image YUV 4:2:2 data in memory, I want to load this image data into an opencv Mat with format BGR. – Gagandeep Mar 29 '19 at 17:00

1 Answers1

0

The Raw YUV 4:2:2 image has a size = 2 * height * width. And You need to create:

cv::Mat img(2 * height, width, CV_8UC1, mem, sizeOfRowInBytes);

The Y channel will be in first height*sizeOfRowInBytes bytes. And after will be UV channels:

  1. half of row with U and half with V;

  2. half of row with U and half with V;

...

Height. half of row with U and half with V.

Nuzhny
  • 1,869
  • 1
  • 7
  • 13
  • I am getting this error -> error: 'CV_8U1' was not declared in this scope – Gagandeep Mar 29 '19 at 11:32
  • did you mean my CV_8UC1 ? – Gagandeep Mar 29 '19 at 11:33
  • Oh, yes. I'm fixed it – Nuzhny Mar 29 '19 at 11:54
  • please can you tell me how to calculate the sizeOfRowInBytes parameter, my width is 1280 & height is 720 – Gagandeep Apr 01 '19 at 10:21
  • It depends from the source. For example in ffmpeg this value containts in AVFrame.linesize. By default sizeOfRowInBytes = imageWidth. But often it equal the nearest power of 2. – Nuzhny Apr 01 '19 at 10:33
  • So in my case what size would you like to recommend me? 1280? – Gagandeep Apr 01 '19 at 11:06
  • Yes, it is the standard way – Nuzhny Apr 01 '19 at 11:26
  • case1 ->https://drive.google.com/open?id=1DwC1tVfISvGSya8zGleKP95jyQ4IyJkX case 2-> https://drive.google.com/open?id=1sSo4znOrYE5gy_FDTEh47LKH0XX4Tqmx case 3-> https://drive.google.com/open?id=1OSd5z3DEpEiD_a4q-0-82CjAqBMGPWNX, Again I shared new links to my output image please refer these links. & I tried your for loop solution it also not giving desired result. – Gagandeep Apr 02 '19 at 05:05
  • Below mentioned code is working fine for me, Mat img(Size(1280,720),CV_8UC2,mem,1280*4); cvtColor(img,img,116);// Convert YUV2BGR I need you feedback, why it is working with 1280*4 ? – Gagandeep Apr 03 '19 at 09:03
  • Mmm... Without raw image I don't can tell anything. What is image? Where is image from? – Nuzhny Apr 03 '19 at 09:45
  • I am using SDI camera for video capturing & this camera writing the frame to Read buffer with YUV4.2.2 format, I am reading the raw image from the Read buffer using above-mentioned code. – Gagandeep Apr 03 '19 at 11:53
  • can you help me to read the BGR data from the Read buffer instead of YUV. Now the size of my Read buffer is 640x480 – Gagandeep Apr 05 '19 at 09:01
  • Yes, but I need an original raw buffer with frame. Save it as binary file – Nuzhny Apr 05 '19 at 09:28
  • Size of the original frame buffer is 640x480, & I am storing the raw image data to this buffer. but we have to read data from this Read buffer not from the file – Gagandeep Apr 05 '19 at 09:43
  • I need this buffer for the help you. Do you can save it to the file and send me? – Nuzhny Apr 05 '19 at 10:03
  • can you tell me how to save this RAW data in the file ? – Gagandeep Apr 05 '19 at 11:59
  • fopen - fwrite - fclose – Nuzhny Apr 05 '19 at 14:06