# LUT for 16bit image

Hi,

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;

lut.at<Vec3s>(j)[0] = glevl*color*r;

lut.at<Vec3s>(j)[1] = glevl*color*g;

lut.at<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

merge_elem.push_back(Plane_images);


}

It is crashes in LUT function.

Can someone help me to solve this problem.

edit retag 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

( 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 !

( 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.

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

Sort by ยป oldest newest most voted

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

//...now 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++)
dst.at<Scalar>(y,x)=lut_table[src.at<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++)
lut_table[i]=Scalar(i/256,i/256,i/256);

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
{
color.at<Vec3w>(j, i) = lut_arr[lut.at<ushort>(j, i)];
}
}

( 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.

( 2019-01-15 07:12:52 -0500 )edit