# Applying LUT on a cv::Mat

**URL:** <https://forum.opencv.org/t/applying-lut-on-a-cv-mat/18573>\
**Category:** C++\
**Created:** [August 23, 2024, 10:30am UTC](https://forum.opencv.org/t/applying-lut-on-a-cv-mat/18573 "2024-08-23T10:30:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ionut\_Cosmin\_Mihai](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ionut_cosmin_mihai/32/10995_2.png) [@Ionut\_Cosmin\_Mihai](https://forum.opencv.org/u/Ionut_Cosmin_Mihai)\
**Post date:** [August 23, 2024, 10:30am UTC](https://forum.opencv.org/t/applying-lut-on-a-cv-mat/18573/1 "2024-08-23T10:30:35Z")

</div>

I’m trying to apply a LUT on a cv::Mat image.  
I read the documentation for cv::LUT here [OpenCV: Operations on arrays](https://docs.opencv.org/4.x/d2/de8/group __core__ array.html#gab55b8d062b7f5587720ede032d34156f) but I don’t understand what “look-up table of 256 elements;” means: Is it that the LUT can have maximum 256 segmentations?

What format should the InputArray have?(values of 0-1, 0-255?)

Is there any example that can you point me to?

Thank you!

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 23, 2024, 1:07pm UTC](https://forum.opencv.org/t/applying-lut-on-a-cv-mat/18573/2 "2024-08-23T13:07:22Z")

</div>

> [@Ionut\_Cosmin\_Mihai](#):
>
> What format should the InputArray have?(values of 0-1, 0-255?)

single channel, uchar [0…255].  
think of each entry as an _index_ into the table.  
one application of it is ‘color mapping’, where you map grayscale[0…255] values to (false) colors, e.g. a heatmap

> [@Ionut\_Cosmin\_Mihai](#):
>
> Is it that the LUT can have maximum 256 segmentations?

yes, exactly.

---

<div class="post-metadata">

**Author:** ![Ionut\_Cosmin\_Mihai](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ionut_cosmin_mihai/32/10995_2.png) [@Ionut\_Cosmin\_Mihai](https://forum.opencv.org/u/Ionut_Cosmin_Mihai)\
**Post date:** [August 24, 2024, 12:32pm UTC](https://forum.opencv.org/t/applying-lut-on-a-cv-mat/18573/3 "2024-08-24T12:32:41Z")

</div>

Thanks, so the cv::LUT function is channel independent. It cannot apply complicated luts that need trilinear interpolation.  
I’m wonder if I can achieve this using [cv::cuda::LookUpTable](https://docs.opencv.org/4.1.1/df/d29/classcv_1_1cuda_1_1LookUpTable.html).

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 24, 2024, 12:43pm UTC](https://forum.opencv.org/t/applying-lut-on-a-cv-mat/18573/4 "2024-08-24T12:43:47Z")

</div>

> [@Ionut\_Cosmin\_Mihai](#):
>
> It cannot apply complicated luts that need trilinear interpolation.

maybe explain, what you need in greater detail ?

---

<div class="post-metadata">

**Author:** ![Ionut\_Cosmin\_Mihai](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ionut_cosmin_mihai/32/10995_2.png) [@Ionut\_Cosmin\_Mihai](https://forum.opencv.org/u/Ionut_Cosmin_Mihai)\
**Post date:** [August 24, 2024, 1:11pm UTC](https://forum.opencv.org/t/applying-lut-on-a-cv-mat/18573/5 "2024-08-24T13:11:05Z")

</div>

I need to apply a lut that I take from a .cube file to a cv::Mat.

From what I tested and from what I understand cv::LUT is cahnnel independent which means that it transforms the value in a channel independent of the other 2 channels.  
For example the value 23 of the R channel will always map to x no matter of the values of G and B.

The steps for applying a more complex LUT are:  
For each pixel:

1. Search the closest points in the LUT(the result will be 8 points that form a parallelepiped)
2. Do trilinear interpolation between the points found which will result in the new pixel value.

I’m searching for ways to implement this with functions that already exist.

I hope that it is more clear now and thank you for looking into this.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [August 24, 2024, 2:07pm UTC](https://forum.opencv.org/t/applying-lut-on-a-cv-mat/18573/6 "2024-08-24T14:07:43Z")

</div>

there has been discussion of “cube” files in this forum before. you might find inspiration in there.

---

<div class="post-metadata">

**Author:** ![Ionut\_Cosmin\_Mihai](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ionut_cosmin_mihai/32/10995_2.png) [@Ionut\_Cosmin\_Mihai](https://forum.opencv.org/u/Ionut_Cosmin_Mihai)\
**Post date:** [August 30, 2024, 8:11am UTC](https://forum.opencv.org/t/applying-lut-on-a-cv-mat/18573/7 "2024-08-30T08:11:39Z")

</div>

I could not find any such post, the best solution that I found was to use an OpenGL shader. I took into consideration using CUDA, but I needed something cross platform.  
It was more complicated than I expected, if anyone knows a better solution, I would be curious to learn it.
