Ask Your Question

LUT for 16bit image

asked 2019-01-10 02:17:45 -0500

snehal gravatar image

updated 2019-01-10 05:09:24 -0500


I want to do lookuptable for 16 bit image. I have image of 16 bit(CV_16UC1), then i naormalize image with RGB give value and store in Mat variable called lut(type - CV_16UC3). After that, i split Mat lut and store in lut_channels.

int planes = 3;

vector<mat> lut_channels(planes);

vector<mat> merge_elem;

int glevl = 65535;

int dim(glevl + 1);

Mat lut(1, &dim, CV_16UC3);

for (double j = 0; j <= glevl; ++j)


double color = j / glevl;<Vec3s>(j)[0] = glevl*color*r;<Vec3s>(j)[1] = glevl*color*g;<Vec3s>(j)[2] = glevl*color*b;


split(lut, lut_channels);

for (int k = 0; k < planes; ++k)


    Mat Plane_images = Mat(Mat::zeros(image.size(), image.type()));

    LUT(image, lut_channels[k], Plane_images);    //crash here



It is crashes in LUT function.

Can someone help me to solve this problem.

Thanks in advance.

edit retag flag offensive close merge delete


sorry, but that's not possible with CV_16U input, please have another look at the docs ,

input is restricted to CV_8U/S

berak gravatar imageberak ( 2019-01-10 03:43:24 -0500 )edit

can you take another look at your example code ? it does not compile, there are unknown variables, typos, etc.

it gets much better here, if we can reproduce it !

berak gravatar imageberak ( 2019-01-10 03:48:29 -0500 )edit

yes i update it.. i hope it will compile. You can take any values of r, g, b for calculation.

snehal gravatar imagesnehal ( 2019-01-10 04:57:25 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-01-10 05:11:54 -0500

kbarni gravatar image

You can do this operation "manually" (not using OpenCV funcions). You need a table corresponding an RGB color for each of the 65535 gray levels from the 16 bit image:

Scalar lut_table[65536];
// each value of the table is an RGB tuple

// initialize the lut_table variable with the LUT values you want...

//Applying the LUT table to the image
Mat dst(src.rows,src.cols,CV_8UC3)
for(int y=0;y<src.rows;y++)
    for(int x=0;x<src.cols;x++)<Scalar>(y,x)=lut_table[<uint16_t>(y,x)];

The code should work, but I didn't test it, so it might contain errors.

You have to write your own generator for the LUT table (line 3 of the code). For example the following code generates a 8 bit grayscale LUT:

for(int i=0;i<65536;i++)
edit flag offensive delete link more


Getting black image when i did this.

    Vec3w lut_arr[65536];

Mat color(image.size(), CV_16UC3);

for (int j = 0; j < image.rows; j++)//rows
    for (int i = 0; i < image.cols; i++)//cols
    {<Vec3w>(j, i) = lut_arr[<ushort>(j, i)];
snehal gravatar imagesnehal ( 2019-01-15 06:45:36 -0500 )edit

You probably need to debug it to see what's the problem.

Check if LUT has correct values (e.g. lut_arr[32770] should be (128,128,128))

Then check the range of the image. If you have values between (0...255), then everything will be mapped to black (0,0,0); or if it's a 10 bit (which can be the case for raw data for CMOS sensors) image (0..1024) represented on 16 bits, then the values will be mapped to (0..4).

You can also try other LUTs: e.g. map every value to a random RGB pair to get different colors for every gray value.

kbarni gravatar imagekbarni ( 2019-01-15 07:12:52 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-01-10 02:17:45 -0500

Seen: 28 times

Last updated: Jan 10