Vit
Vit

Reputation: 815

How to read Y-component from H264 4:2:0 video without default YUV2RGB->RGB2GRAY conversion

I need fast work with gray-scale frames hence I need only Y-component from YUV 4:2:0 video I have. As I can see from profiler a lot of time is wasting for useless YUV->RGB conversion.

VideoCapture::cap.set(CV_CAP_PROP_CONVERT_RGB, false) 

gives no effect, as I can see from splitting

Mat ch[3]; split(frame,ch); imshow("it must be U-channel", ch[1]);

frames is still in RGB, not YUV, i.e. VideoCapture ignores CV_CAP_PROP_CONVERT_RGB flag (I believe ffmpeg library which is used by openCV for video-decoding can understand such kind of flags).

So, please, does someone have idea how to get gray-scaled image from video sequence faster? OpenCL methods are ok too since I convert Mat to oclMat to process this frames after reading ( including current useless ocl::cvtColor(A, B, CV_RGB2GRAY) ).

Upvotes: 0

Views: 1208

Answers (1)

Roman Arzumanyan
Roman Arzumanyan

Reputation: 1814

For regular video decoding you don't need OpenCV. It can be done with ffmpeg library. Download & build latest version of ffmpeg & take a look at sample, in which you need video_decode_example function. In it's body, at line 401:

if (got_picture) {
    printf("saving frame %3d\n", frame);
    fflush(stdout);

    /* the picture is allocated by the decoder. no need to free it */
    snprintf(buf, sizeof(buf), outfilename, frame);
    pgm_save(picture->data[0], picture->linesize[0], c->width, c->height, buf);
    frame++;
}

As ffmpeg works with Y Cb Cr color representation, Y plane of decoded frame is stored in picture->data[0]. Copy it to your OpenCL memory object, and that's it.

Upvotes: 3

Related Questions