JH Labs Quantize Использование для уменьшения глубины цвета изображения
-
15-11-2019 - |
Вопрос
Я пытаюсь использовать QuantIzeFilter в
http://www.jhlabs.com/ip/filters/index.html
Чтобы уменьшить глубину цвета экрана выстрел.
Вот мой очень простой код:
Robot robo = new Robot();
BufferedImage notQuantized = robo.createScreenCapture( new Rectangle ( 0, 0, 300, 300 ) );
BufferedImage Quantized = new BufferedImage( 300, 300, BufferedImage.TYPE_INT_BGR);
File nonquantized = new File ("C:\\nonquantized.png");
File quantized = new File("C:\\quantized.png");
nonquantized.createNewFile();
quantized.createNewFile();
QuantizeFilter bla = new QuantizeFilter();
int [] outPixels = new int[300*300*3];
int [] inPixels = new int[300*300*3];
notQuantized.getRaster().getPixels( 0, 0, 300, 300, inPixels );
bla.quantize( inPixels, outPixels, 300, 300,2, true, true );
Quantized.getRaster().setPixels( 0, 0, 300, 300, outPixels );
ImageIO.write( Quantized, "png", quantized );
ImageIO.write( notQuantized, "png", nonquantized );
.
Однако то, что я остался с:
<Сильный> Оригинальный IMG:
<Сильные> квантованные IMG:
Дальнейший анализ проблемы показывает, что массив вклада заполнен неправильно; Он заполнен три раза с верхней третью исходного изображения.
Любые указатели, как я могу это исправить?
Дополнительно, любые ссылки хорошие + быстрый алгоритм квантования в Java? Что я ищу, это алгоритм, который приведет к изображению Type_int_bgr и создает новый образ Type_int_bgr, но с меньшим количеством фактических различий в пикселях, поэтому его можно легко сделать.
Например, если у нас есть два пикселя в исходном изображении, со значениями, такими как 255, 255, 234, а другая со значением, таковым, как 255, 255, 236, они оба должны быть преобразованы в 255,255,240. Ура
Решение
The following example will convert your image correctly:
QuantizeFilter q = new QuantizeFilter();
int [] inPixels = new int[image.getWidth()*image.getHeight()*3];
int [] outPixels = new int[image.getWidth()*image.getHeight()*3];
image.getRaster().getPixels( 0, 0, image.getWidth(), image.getHeight(), inPixels );
q.quantize(inPixels, outPixels, image.getWidth(), image.getHeight(), 64, false, false);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0,0,width,height,outPixels);
Also there is no reason to create the files implicitly, as ImageIO.write creates them automatically.
Другие советы
I have had the same problem and its not the code you posted that's at fault it's the QuantizeFilter
class which doesn't go through all the pixels. You need to find this code part
if (!dither) {
for (int i = 0; i < count; i++)
outPixels[i] = table[quantizer.getIndexForColor(inPixels[i])];
and multiply count by 3.
Please note that this is only a a fix if the last 2 parameters are set to false.