Kenyakorn Ketsombut
Kenyakorn Ketsombut

Reputation: 2122

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++ ?

Upvotes: 7

Views: 3325

Answers (2)

Pylyp
Pylyp

Reputation: 404

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

Upvotes: 2

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

Since your palette is fixed, you'll want to use a dithering approach such as Floyd Steinberg dithering along with an appropriate color similarity measure, for example in Lab color space.

Upvotes: 1

Related Questions