JH Labs Quantize Uso per ridurre la profondità del colore dell'immagine
-
15-11-2019 - |
Domanda
Sto cercando di usare il quantizefilter in
http://www.jhlabs.com/ip/filters/index.html
Per ridurre la profondità del colore di uno schermo.
Ecco il mio codice molto semplice:
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 );
.
Tuttavia, a cosa sono rimasto è:
Original IMG:
Un'ulteriore analisi del problema mostra che l'array di inpixel viene riempito in modo errato; È riempito tre volte con il terzo superiore dell'immagine originale.
Qualsiasi puntatori come posso risolverlo?
Inoltre, qualsiasi collegamento buono + algoritmo di quantizzazione rapida in Java? Quello che cerco è un algoritmo che prenderà un'immagine di tipo_int_bgr e produrrà un'immagine di un nuovo tipo_int_bgr ma con meno differenza effettiva in pixel, quindi potrebbe essere facilmente deflata.
Ad esempio, se abbiamo due pixel nell'immagine originale, con valori come 255, 255, 234 e un altro con valore come 255, 255, 236, dovrebbero essere convertiti entrambi a 255.255.240. Saluti
Soluzione
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.
Altri suggerimenti
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.