Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce colors to a specified palette

I need to solve the following problem:

INPUT: Image IM, Palette PA

OUTPUT: IM only with the colours of PA

The input image is in RGB but I can convert it to HSV. The colour target palette I specify contains at the moment: black, white, light gray, gray, dark gray, blue, pink, red, purple, green, yellow, brown, orange.

I searched a lot for that, but I can only find reducing an image to the most common colours or reducing it to a fixed palette like 16 colours EGA graphics.

I found the best answer of this: How do I convert any image to a 4-color paletted image using the Python Imaging Library?

It has an input palette and reduces the image to that. Is there an equal way to do in in OpenCV with C++ ?

like image 822
Kenyakorn Ketsombut Avatar asked Oct 22 '22 18:10

Kenyakorn Ketsombut


1 Answers

Suppose that rgb color space is 3D cube

rgb color space

And Palette PA is several points in this cube. So our problem is reduced to finding the nearest Palette PA point for any given rgb point in 3D space.

I think the better solution is k-d tree

enter image description here

At the end of wiki page you can find several links for c++ implementations

like image 177
Pylyp Avatar answered Nov 01 '22 12:11

Pylyp