Reduce the colors of an image

793 views
Skip to first unread message

Andrey Canny

unread,
Sep 9, 2011, 2:45:02 PM9/9/11
to javacv
Hello,
I segmented image using opencv and I would like to reduce to 16 or 8
colors in an intelligent way.
Is there some function fit for this job? I made the conversion from
RGB to HSV because I would like to use the information in hue: I think
to divide the range of the color in 8
equal parts, within each subinterval choose the color possessed by the
greatest number of pixels and assign it to all the pixels of the
subinterval. For saturation and value do not know how to behave. Do
you have any idea, maybe even a more efficient algorithm?

Samuel Audet

unread,
Sep 10, 2011, 9:05:13 PM9/10/11
to jav...@googlegroups.com

Andrey Canny

unread,
Sep 11, 2011, 5:04:06 AM9/11/11
to javacv
Thanks first. I had already seen that link. The problem is that I do
not understand how to use the kmeans with a color image. From code I
have:

public static native int cvKMeans2(CvArr samples, int cluster_count,
CvArr labels,
@ByVal CvTermCriteria termcrit, int attempts/*=1*/, CvRNG
rng/*=null*/,
int flags/*=0*/, CvArr _centers/*=null*/, double[]
compactness/*=null*/);

where to put my image to be processed? If I want 16 to 16 clusters
cluster_count imposed, but the values ​​of other parameters that I
give? Should I put every pixel of the image in a separate line, that I
have to go to CvArr samples? How I can use the indexes of cluster
centroids to classify each pixel? In short I am very confused. Can you
help me with code samples or other. Thanks a lot in advance
> First hit on Google:http://stackoverflow.com/questions/5906693/reduce-the-number-of-color...

Samuel Audet

unread,
Sep 15, 2011, 4:43:02 AM9/15/11
to jav...@googlegroups.com
Hello,

I'm not sure myself. I have not done something like that before. But I
am sure that someone else did something like that somewhere on the net...

Samuel

Jarek

unread,
Sep 15, 2011, 11:28:37 PM9/15/11
to jav...@googlegroups.com, Andrey Canny
Andrey:

This is a bit sideways answer, but since you are already in Java, you
can use ImageJ plugins to reduce color. The k-means info is here:
http://ij-plugins.sourceforge.net/plugins/segmentation/k-means.html
You want to use image produced by option: "Show clusters as centroid
value" (this is equivalent to color reduction). Note that the k-means
can be called trough API without ImageJ running. Note that you can
covert to a image stack in order color space, say L*a*b*, use the
k-means plugin, then convert result back to RGB
(http://ij-plugins.sourceforge.net/plugins/color/index.html).

The 3D Color Inspector plugin can do color reduction using also a Median
Cut and Wu Quant algorithms, and has very nice visualization of the
clusters.:
http://rsbweb.nih.gov/ij/plugins/color-inspector.html

Jarek

Andrey Canny

unread,
Sep 18, 2011, 10:05:44 AM9/18/11
to javacv
Thanks, but I work in Android. How do I?
Reply all
Reply to author
Forward
0 new messages