Get plane data

I’m tring to get raw data(YUV) from my camera and copy to cv::Mat(OpenCV).
I refer a sample of jetson multi media (10_camera_recording).
I’d like to get planes data from dma buffer. But my code shown as below does not worked.
Outpurt image is mosaiced.
How should I do ?

part of my code(in ConsumerThread::threadExecute())

int k=0;    // in future, for-loop inpar.num_planes
NvBufferMemMap(dmabuf_fd, k, NvBufferMem_Read_Write, &ptr_top);   //ptr_top is void*
NvBufferMemSyncForCpu(dmabuf_fd, k, &ptr_top);
uint32_t width = par.width[k];
uint32_t height = par.height[k];
uint32_t pitch = par.pitch[k];
uint32_t offset = par.offset[k];
printf("--buffer informaition-[%d]--\n", k);
printf("width :%4d\n", width);
printf("height:%4d\n", height);
printf("pitch :%4d\n", pitch);
printf("offset:%4d\n", offset);
printf("format:%4d\n", par.pixel_format);
cv::Mat src = cv::Mat::zeros(height, width, CV_8UC1);
ptr_cur = (uint8_t *)ptr_top;   //ptr_cur is uint8_t *
for(int y =0;y < height; y++){
    for(int x = 0; x < width; x++){
    src.data[y*src.step+x + 0] = (uint8_t)(*ptr_cur);
        ptr_cur++;
    }
    ptr_cur = (uint8_t *)ptr_top + pitch * y;
}
if(k==0){
cv::imwrite("Y.bmp", src);
}
else if(k==1){
    cv::imwrite("UV.bmp",src);
}
else{
    ;
}		
NvBufferMemSyncForDevice (dmabuf_fd, k, &ptr_top);
NvBufferMemUnMap(dmabuf_fd, k, &ptr_top); 

Environment
HW : Jetson Xavier NX Devkit
SW : JetPack 4.4

Thank you.

Hi,
Please check if you allocate the buffers in pitch linear:

        nativeBuffers[i] = DmaBuffer::create(STREAM_SIZE, NvBufferColorFormat_NV12,
                    DO_CPU_PROCESS ? NvBufferLayout_Pitch : NvBufferLayout_BlockLinear);

You should get correct Y plane if it is in pitch linear.

Thank ou for your reply,
I had DO_CPU_PROCESS set to false at runtime.
What is the impact?
I modifies as below and I get Y plane image.

 nativeBuffers[i] = DmaBuffer::create(STREAM_SIZE, NvBufferColorFormat_NV12,NvBufferLayout_Pitch);

I would like to get YUV image and conver to RGB or BGR image.
My camera’s pixel format is 4 (BT.601, YVU ER, confirmed by getting par.pixel_format).

Hi,
You may refer to this patch:
NVBuffer (FD) to opencv Mat - #6 by DaneLLL