Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are many ways to do it. The methods suggested by jeff7 are OK, but some drawbacks are:</p> <ul> <li>method 1 have parameters N and M, that you must choose, and you must also convert it to another colorspace.</li> <li>method 2 answered can be very slow, since you should compute a 16.7 Milion bins histogram and sort it by frequency (to obtain the 64 higher frequency values)</li> </ul> <p>I like to use an algorithm based on the <strong>Most Significant Bits</strong> to use in a RGB color and convert it to a 64 color image. If you're using C/OpenCV, you can use something like the function below.</p> <p>If you're working with gray-level images I recommed to use the LUT() function of the OpenCV 2.3, since it is faster. There is a tutorial on how to use LUT to reduce the number of colors. See: <a href="http://opencv.itseez.com/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html">Tutorial: How to scan images, lookup tables...</a> However I find it more complicated if you're working with RGB images. </p> <pre><code>void reduceTo64Colors(IplImage *img, IplImage *img_quant) { int i,j; int height = img-&gt;height; int width = img-&gt;width; int step = img-&gt;widthStep; uchar *data = (uchar *)img-&gt;imageData; int step2 = img_quant-&gt;widthStep; uchar *data2 = (uchar *)img_quant-&gt;imageData; for (i = 0; i &lt; height ; i++) { for (j = 0; j &lt; width; j++) { // operator XXXXXXXX &amp; 11000000 equivalent to XXXXXXXX AND 11000000 (=192) // operator 01000000 &gt;&gt; 2 is a 2-bit shift to the right = 00010000 uchar C1 = (data[i*step+j*3+0] &amp; 192)&gt;&gt;2; uchar C2 = (data[i*step+j*3+1] &amp; 192)&gt;&gt;4; uchar C3 = (data[i*step+j*3+2] &amp; 192)&gt;&gt;6; data2[i*step2+j] = C1 | C2 | C3; // merges the 2 MSB of each channel } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload