User avatar
ksharindam
Posts: 76
Joined: Sat Jan 09, 2016 4:16 pm

Use opencv mat data to create dispmanx resource

Sun Sep 09, 2018 7:22 am

I am a newbie in dispmanx.
I want to load an image with opencv using

Code: Select all

cv::Mat img = cv::imread("test.jpg");
now i want to use write/use img.data to create dispmanx resource.
how i can utilize img.data in this case.

Code: Select all

void *image;
vc_dispmanx_resource_write_data(resource,
                                type,
                                pitch,
                                image,
                                &dst_rect );
please explain clearly.
My Qt projects at ... https://github.com/ksharindam

User avatar
ksharindam
Posts: 76
Joined: Sat Jan 09, 2016 4:16 pm

Re: Use opencv mat data to create dispmanx resource

Tue Sep 11, 2018 3:46 pm

Finally found it by myself.
Each rows of cv mat data is aligned to 4. But the pitch of image buffer for dispmanx is needed to be multiple of 16. Thats why we can not directly use mat.data for the resource buffer.
We have to allocate a data of pitch*height. and copy each row of cv::mat to that allocated memory.

Code: Select all

uchar* cvimg2VCbuffer(int width, int height, int bytesPerLine, uchar* data)
{
    int pitch = ALIGN_UP(width, 16)*3;
    void* buffer = calloc(1, pitch*height);
    uint8_t *line = (uint8_t*)buffer;
    for (int i = 0;i < height; ++i) {
        memcpy(line, data, bytesPerLine);
        line += pitch;
        data += bytesPerLine;
    }
    return (uchar*)buffer;
}
My Qt projects at ... https://github.com/ksharindam

Return to “Graphics programming”