Search code examples
opencvtgastb-image

Any solution for TGA format loading in OpenCV4


There's no support TGA format for OpenCV currently.

And I know there's a single header file library named stb_image that allow you to read/write TGA image. But the use case with OpenCV on the Internet are so few. (more often to see people use it with OpenGL)

The second method I found. There's a short code included (the answer) in this topic: Loading a tga/bmp file in C++/OpenGL

Someone use this code to read TGA file into cv::Mat just like the code below.

Tga tgaImg = Tga("/tmp/test.tga");
Mat img(tgaImg.GetHeight(), tgaImg.GetWidth(), CV_8UC4);
memcpy(img.data, tgaImg.GetPixels().data(), tgaImg.GetHeight() * tgaImg.GetWidth() * 4);

But this is only for reading part. I wonder if stb_image can do the same thing like the code above. I mean the image data structure might be different. (not look into them yet)

I would like to ask people who also experience this before. Since DDS/TGA image format are also popular using in game texture, there must be people have already found the way. I mean read/write TGA format in OpenCV code.

Thanks.


Solution

  • For saving opencv image in tga use stbi_write_tga. This function takes pointer to image data as argument, which is img.data in case of cv::Mat type.