leif icon indicating copy to clipboard operation
leif copied to clipboard

Using OpenCV Mat as texture

Open lsdrfrx opened this issue 1 year ago • 4 comments

Hi Coco! It's me again

Give me a hint: is there any option to use OpenCV Mat as LfTexture id? I need to show frames from VideoCapture, and, I guess, I shall do it with lf_image, but I can't find if it is possible

Thanks for help!

lsdrfrx avatar Jun 05 '24 13:06 lsdrfrx

if you have the opengl texture id, you can just do lf_image((LfTexture){.id = your_opencv_opengl_tex_id, .width = your_width, .height = your_height});

On Wed 5. Jun 2024 at 15:52, Christian Guetnga @.***> wrote:

Hi Coco! It's me again

Give me a hint: is there any option to use OpenCV Mat as LfTexture id? I need to show frames from VideoCapture, and, I guess, I shall do it with lf_image, but I can't find if it is possible

Thanks for help!

— Reply to this email directly, view it on GitHub https://github.com/cococry/leif/issues/11, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXNJRYSKFF5PMTTJXEF7LI3ZF4JYLAVCNFSM6AAAAABI2YX2PWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGMZTKOJYGMYDMMY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

cococry avatar Jun 05 '24 13:06 cococry

Thanks. When I figure this out, I'll post the solution here, in case it's useful to someone

lsdrfrx avatar Jun 05 '24 14:06 lsdrfrx

you can use lf_load_texture_from_memory and use mat.ptr(); as data. then render the image with lf_image.

cococry avatar Jun 07 '24 09:06 cococry

I'm doing this:

 cv::Mat im = cv::imread("../data/inference/man.jpg");
 if (im.empty()) {
   std::cout << "Unable to open image" << std::endl;
   exit(-1);
 }

 // Then, inside rendering function
 LfTexture tex = lf_load_texture_from_memory(im.ptr(), sizeof(im), true, LF_TEX_FILTER_NEAREST);

 auto div_props = lf_get_theme().div_props;

 div_props.color = COLOR_BACKGROUND;
 div_props.text_color = COLOR_FOREGROUND;

 lf_push_style_props(div_props);

 lf_div_begin(((vec2s){SIDEBAR_WIDTH, APPBAR_HEIGHT}), ((vec2s){(float)(state.win->get_width() - SIDEBAR_WIDTH), container_height}), false);
 {
   lf_image((LfTexture){.id = tex.id, .width = tex.width, .height = tex.height});
 }
 lf_div_end();

 lf_pop_style_props();

But image is blank. When debugging in GDB, I output tex and see the following:

(gdb) print tex
$1 = {id = 1433094080, width = 21845, height = 208}
(gdb)

Width is crazy, height does not match to image height. What am I doing wrong?

lsdrfrx avatar Jun 07 '24 11:06 lsdrfrx