|
| 1 | +import javax.imageio.ImageIO; |
| 2 | +import java.io.File; |
| 3 | +import java.awt.image.BufferedImage; |
| 4 | +import java.awt.image.PixelGrabber; |
| 5 | +import java.awt.Image; |
| 6 | +import java.awt.image.ImageObserver; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.Comparator; |
| 11 | + |
| 12 | +public class ImageAnalyze { |
| 13 | + private BufferedImage bi; |
| 14 | + private ArrayList<PixelGroup> pixelGroups = new ArrayList<PixelGroup>(); |
| 15 | + |
| 16 | + private int threshold = 50; |
| 17 | + private Painter painter; |
| 18 | + |
| 19 | + private int tilesAcross = 2, tilesDown = 2; |
| 20 | + |
| 21 | + public static void main( String[] args ) { |
| 22 | + String fn = "image.png"; |
| 23 | + int threshold = 50; |
| 24 | + if ( args.length > 0 ){ |
| 25 | + fn = args[0]; |
| 26 | + } |
| 27 | + if ( args.length > 1 ){ |
| 28 | + threshold = Integer.parseInt( args[1] ); |
| 29 | + } |
| 30 | + ImageAnalyze ia = new ImageAnalyze( fn, threshold ); |
| 31 | + ia.analyze(); |
| 32 | + } |
| 33 | + public ImageAnalyze ( String fn, int threshold ){ |
| 34 | + this.threshold = threshold; |
| 35 | + try { |
| 36 | + this.bi = ImageIO.read( new File( fn ) ); |
| 37 | + } |
| 38 | + catch ( Exception e ){ |
| 39 | + e.printStackTrace(); |
| 40 | + System.exit(1); |
| 41 | + } |
| 42 | + } |
| 43 | + public void analyze(){ |
| 44 | + this.handlepixels( this.bi, 0, 0, this.bi.getWidth(), this.bi.getHeight() ); |
| 45 | + } |
| 46 | + public void handlesinglepixel(PixelPrim pixel) { |
| 47 | + |
| 48 | + for ( PixelGroup pg : this.pixelGroups ){ |
| 49 | + if ( pg.isSimilar( pixel ) ){ |
| 50 | + return; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + this.pixelGroups.add( new PixelGroup( pixel, this.threshold ) ); |
| 55 | + } |
| 56 | + |
| 57 | + public void handlepixels(Image img, int x, int y, int w, int h) { |
| 58 | + int[] pixels = new int[w * h]; |
| 59 | + PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w); |
| 60 | + try { |
| 61 | + pg.grabPixels(); |
| 62 | + } catch (InterruptedException e) { |
| 63 | + System.err.println("interrupted waiting for pixels!"); |
| 64 | + return; |
| 65 | + } |
| 66 | + if ((pg.getStatus() & ImageObserver.ABORT) != 0) { |
| 67 | + System.err.println("image fetch aborted or errored"); |
| 68 | + return; |
| 69 | + } |
| 70 | + for (int j = 0; j < h; j++) { |
| 71 | + for (int i = 0; i < w; i++) { |
| 72 | + handlesinglepixel(new PixelPrim( x+i, y+j, pixels[j * w + i] )); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + BufferedImage off_Image = new BufferedImage(w*tilesAcross, h*tilesDown, BufferedImage.TYPE_INT_ARGB); |
| 77 | + |
| 78 | + Collections.sort( this.pixelGroups, new CustomComparator() ); |
| 79 | + |
| 80 | + int sublist_topindex = 5; |
| 81 | + if ( this.pixelGroups.size() < 5 ){ |
| 82 | + sublist_topindex = this.pixelGroups.size(); |
| 83 | + } |
| 84 | + List<PixelGroup> pgBySize = this.pixelGroups.subList( 0, sublist_topindex ); |
| 85 | + |
| 86 | + PixelGroup lowestsD = null; |
| 87 | + for ( PixelGroup pgr : pgBySize ) { |
| 88 | + System.out.println( pgr ); |
| 89 | + if ( lowestsD == null || pgr.standardDeviation() < lowestsD.standardDeviation() ){ |
| 90 | + lowestsD = pgr; |
| 91 | + } |
| 92 | + } |
| 93 | + System.out.println( "======================================\n" + lowestsD + "\n\n=============================" ); |
| 94 | + lowestsD.isBackground = true; |
| 95 | + |
| 96 | + this.painter = new PainterDot( w, h ); |
| 97 | + |
| 98 | + int xOffset=0,yOffset=0; |
| 99 | + for ( int tid = 0; tid < ( tilesAcross * tilesDown ); tid++ ){ |
| 100 | + this.painter.paintTile( off_Image, this.pixelGroups, xOffset * w, yOffset * h ); |
| 101 | + xOffset++; |
| 102 | + if ( xOffset >= tilesAcross ){ |
| 103 | + xOffset = 0; |
| 104 | + yOffset++; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + try { |
| 109 | + // retrieve image |
| 110 | + File outputfile = new File("/tmp/patimageout.png"); |
| 111 | + ImageIO.write(off_Image, "png", outputfile); |
| 112 | + } catch (Exception e) { |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + public class CustomComparator implements Comparator<PixelGroup> { |
| 120 | + @Override |
| 121 | + public int compare(PixelGroup o1, PixelGroup o2) { |
| 122 | + return o2.pixels.size() - o1.pixels.size(); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments