Ask Your Question

Give random color to each intensity

asked 2019-11-07 02:37:29 -0500

Nbb gravatar image

I have a 2D 100x100 image with intensity in the range [0,36]. How can I assign a random color to each intensity?

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2019-11-07 03:36:57 -0500

kbarni gravatar image

Create a LUT (look-up table) which is a 256 by 1 color image. This will store the colors for each intensity. If your image contains only 36 levels, you can set only the first 36 values.

Mat lookUpTable(1, 256, CV_8U3);
Vec3b p = (Vec3bv);
for( int i = 0; i < 36; ++i)
    p[i] = Vec3b(rand()%256,rand()%256,rand()%256);

This will give totally random colors to the different levels. It might be wiser to use some nice, but still random colors. Define the LUT un the HSV space, and set saturation and value to 255:

for( int i = 0; i < 36; ++i)
    p[i] = Vec3b(rand()%180,255,255);

You might also want to try a systematic LUT instead of a random one. In this case just change the line to p[i] = Vec3b(i*5,255,255);. In this case the hue of the succeeding levels will differ by 10°, a subtle, but noticeable difference.

edit flag offensive delete link more

answered 2019-11-07 03:37:42 -0500

LBerger gravatar image

May be something like that

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-11-07 02:37:29 -0500

Seen: 27 times

Last updated: Nov 07