# Convert cv::Mat to TImage

**URL:** <https://root-forum.cern.ch/t/convert-cv-mat-to-timage/39815>\
**Category:** Newbie\
**Created:** [June 7, 2020, 3:48pm UTC](https://root-forum.cern.ch/t/convert-cv-mat-to-timage/39815 "2020-06-07T15:48:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KHIEM2812](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/khiem2812/32/22300_2.png) [@KHIEM2812](https://root-forum.cern.ch/u/KHIEM2812)\
**Post date:** [June 7, 2020, 3:48pm UTC](https://root-forum.cern.ch/t/convert-cv-mat-to-timage/39815/1 "2020-06-07T15:48:39Z")

</div>

Hi everyone,

I want to know how to convert an image in `cv::Mat` type to `TASImage`. I have read the root document but still very fuzzy. The main issue is that being not sure of converting rgb pixel to the `Double_t `array. I cannot find any equation or interface (`TImagePalette`, `TColor`) to help to do the conversion easily. Look like have to lookup the rgb value in the Palette table (`fColorBlue`, `fColorGreen`,`fColorRed`,`fPoints`) to map to the `Double_t` value but it seems over-complicated.

Another option is that convert cvMat to png and save in a `char*` buffer, then set that buffer to TASImage. So i try some simple macro as below just to understand the `SetImageBuffer` method.

```cpp
TASImage img1("cvImg.jpg");
TASImage tImg;
char *buf;
int sz;
img1.GetImageBuffer(&buf, &sz);
tImg.SetImageBuffer(&buf);// show error here: libpng error: invalid screen gamma in png_set_gamma
tImg.Draw(); // error: Error in <TASImage::Draw>: no image set

```

The macro throws some error about screen gamma. Do you have any suggestions?

\_ROOT Version: 6.20/05  
\_Platform: Ubuntu16  
\_Compiler: cling

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 8, 2020, 6:49am UTC](https://root-forum.cern.ch/t/convert-cv-mat-to-timage/39815/2 "2020-06-08T06:49:44Z")

</div>

I guess the [image tutorials](https://root.cern/doc/master/group__asimage.html) are a good starting point. In particular [img2pad.C](https://root.cern/doc/master/img2pad_8C.html).

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [March 4, 2021, 3:40pm UTC](https://root-forum.cern.ch/t/convert-cv-mat-to-timage/39815/3 "2021-03-04T15:40:08Z")

</div>

Hi,

After same problem appears on different platform, we revisit it and find a solution:

> <https://github.com/root-project/root/pull/7357>

It will be fixed in master and future 6.24 release.  
Sorry for such long delay

Regards,  
Sergey
