import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ProcessImage { public static int TOLERANCE = 50; BufferedImage image0; int w, h; public void setImage(String path) { try { image0 = ImageIO.read(new File(path)); } catch (IOException e) { } w = image0.getWidth(null); h = image0.getHeight(null); } //SetImage private boolean isInsideTolerance(BufferedImage im, int x, int y, Color background){ //razdalja barv po standardu CIE76, evklidska razdalja Color pixel=new Color(im.getRGB(x, y)); double diff=(pixel.getRed()-background.getRed()) * (pixel.getRed()-background.getRed()); diff+=(pixel.getGreen()-background.getGreen())*(pixel.getGreen()-background.getGreen()); diff+=(pixel.getBlue()-background.getBlue())*(pixel.getBlue()-background.getBlue()); diff = Math.sqrt(diff); if (diff > TOLERANCE) return false; return true; } private boolean isInsideTolerance(BufferedImage im, int x, int y, Color background, int oneColor){ //0 red, 1 green, 2 blue Color pixel=new Color(im.getRGB(x, y)); double diff; if(oneColor == 0){ if(Math.abs(pixel.getRed() - 255) < TOLERANCE && pixel.getGreen() < 3 * TOLERANCE && pixel.getBlue() < 3 * TOLERANCE) return true; } if(oneColor == 1){ if(Math.abs(pixel.getGreen() - 255) < TOLERANCE && pixel.getRed() < 3 * TOLERANCE && pixel.getBlue() < 3 * TOLERANCE) return true; } if(oneColor == 2){ if(Math.abs(pixel.getBlue() - 255) < TOLERANCE && pixel.getGreen() < 3 * TOLERANCE && pixel.getRed() < 3 * TOLERANCE) return true; } //System.out.println("DIFF: " + diff); return false; } public String findSide(Color c){ Result rez = findSide(image0, c); return ("" + (rez.l + rez.r)); } public String findSide(Color c, int oneColor){ Result rez = findSide(image0, c, oneColor); System.out.println(rez); return ("" + (rez.l + rez.r));//TO POPRAVIMO } public Result findBites(Color c, int oneColor){ return(findBites(image0, c, oneColor)); } private Result findBites(BufferedImage im, Color c, int oneColor){ int lCounter = 0, rCounter = 0, mCounter = 0; for(int i = 0; i < im.getHeight(); i++){ for(int j = 0; j < im.getWidth(); j++){ if(isInsideTolerance(im, j, i, c, oneColor)){ if(j < im.getWidth() / 3){ lCounter++; } else if((j > (im.getWidth() / 3)) && (j < (2 * im.getWidth() / 3))){ mCounter++; } else{ rCounter++; } } } } System.out.println("FIND: "+rCounter + " "+mCounter+" "+lCounter); return(new Result(rCounter, lCounter, mCounter)); } private Result findSide(BufferedImage im, Color c){ int lCounter = 0, rCounter = 0; for(int i = 0; i < im.getHeight(); i++){ for(int j = 0; j < im.getWidth(); j++){ if(isInsideTolerance(im, j, i, c)){ if(j < im.getWidth() / 2) lCounter++; else rCounter++; } } } System.out.println("FIND: "+lCounter + " "+rCounter+" "+c + " " + im.getHeight()); return(new Result(lCounter, rCounter, lCounter - rCounter)); } private Result findSide(BufferedImage im, Color c, int oneColor){ int lCounter = 0, rCounter = 0; for(int i = 0; i < im.getHeight(); i++){ for(int j = 0; j < im.getWidth(); j++){ if(isInsideTolerance(im, j, i, c, oneColor)){ if(j < im.getWidth() / 2) lCounter++; else rCounter++; } } } System.out.println("FIND: "+lCounter + " "+rCounter+" "+c + " " + im.getHeight()); return(new Result(lCounter, rCounter, lCounter - rCounter)); } /** * @param args */ public static void main(String[] args) { if(args.length == 1){ ProcessImage pp = new ProcessImage(); pp.setImage(args[0]); System.out.println("Stran: " + pp.findSide(Color.RED)); } else{ System.out.println("java ProcessImage path"); } } } class Result{ int l; int r; int diff; Result(int ll, int rr, int difff){ l = ll; r = rr; diff = difff; } public String toString(){ return("" + l + " " + r + " " + diff); } }