Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.PrintStream;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.imageio.ImageIO;
- public class LayoutAnomalyDetection {
- public static double noise (BufferedImage image, double[][][] mask) {
- double score = 0;
- int w = image.getWidth ();
- int h = image.getHeight ();
- int size = w * h;
- double red = 0;
- double green = 0;
- double blue = 0;
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- int rgb = image.getRGB (x, y);
- red += ((rgb >> 16) & 0xff);
- green += ((rgb >> 8) & 0xff);
- blue += (rgb & 0xff);
- }
- }
- red /= size;
- green /= size;
- blue /= size;
- size *= 765;
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- int rgb = image.getRGB (x, y);
- score += Math.abs (((rgb >> 16) & 0xff) - red) * mask[y][x][0];
- score += Math.abs (((rgb >> 8) & 0xff) - green) * mask[y][x][1];
- score += Math.abs ((rgb & 0xff) - blue) * mask[y][x][2];
- }
- }
- return score / size;
- }
- public static double[] average (BufferedImage image) {
- int w = image.getWidth ();
- int h = image.getHeight ();
- int size = w * h;
- double red = 0;
- double green = 0;
- double blue = 0;
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- int rgb = image.getRGB (x, y);
- red += ((rgb >> 16) & 0xff);
- green += ((rgb >> 8) & 0xff);
- blue += (rgb & 0xff);
- }
- }
- red /= size;
- green /= size;
- blue /= size;
- return new double[] {red, green, blue};
- }
- public static double diffScore (double[][][] mask1, double[][][] mask2) {
- double score = 0;
- int w = mask1[0].length;
- int h = mask1.length;
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- for (int c = 0; c < 3; c++) {
- score += Math.abs (mask1[y][x][c] - mask2[y][x][c]);
- }
- }
- }
- return score / (3 * w * h);
- }
- public static double[][][] mask (BufferedImage image) {
- int w = image.getWidth ();
- int h = image.getHeight ();
- double[][][] mask = new double[h][w][3];
- int size = w * h;
- double red = 0;
- double green = 0;
- double blue = 0;
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- int rgb = image.getRGB (x, y);
- red += ((rgb >> 16) & 0xff);
- green += ((rgb >> 8) & 0xff);
- blue += (rgb & 0xff);
- }
- }
- red /= size;
- green /= size;
- blue /= size;
- double factor = 0;
- double[] pixel;
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- pixel = mask[y][x];
- int rgb = image.getRGB (x, y);
- mask[y][x][0] = Math.abs (((rgb >> 16) & 0xff) - red);
- mask[y][x][1] = Math.abs (((rgb >> 8) & 0xff) - green);
- mask[y][x][2] = Math.abs ((rgb & 0xff) - blue);
- for (int c = 0; c < 3; c++) {
- factor = Math.max (factor, pixel[c]);
- }
- }
- }
- factor = 1.0 / factor;
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- for (int c = 0; c < 3; c++) {
- mask[y][x][c] *= factor;
- }
- }
- }
- return mask;
- }
- public static BufferedImage loadImage (File file, int size) throws Exception {
- Image tile = ImageIO.read (file).getScaledInstance (size, size, Image.SCALE_FAST);
- BufferedImage img = new BufferedImage (size, size, BufferedImage.TYPE_INT_RGB);
- Graphics2D g = img.createGraphics ();
- g.drawImage (tile, 0, 0, null);
- return img;
- }
- public static BufferedImage blend (File[] imageFiles, int size) {
- try {
- int[][][] buffer = new int[size][size][3];
- int pos = 0;
- for (File file : imageFiles) {
- BufferedImage img = loadImage (file, size);
- for (int y = 0; y < size; y++) {
- for (int x = 0; x < size; x++) {
- int rgb = img.getRGB (x, y);
- int[] pixel = buffer[y][x];
- pixel[0] += (rgb >> 16) & 0xff;
- pixel[1] += (rgb >> 8) & 0xff;
- pixel[2] += rgb & 0xff;
- }
- }
- pos++;
- }
- BufferedImage img = new BufferedImage (size, size, BufferedImage.TYPE_INT_RGB);
- for (int y = 0; y < size; y++) {
- for (int x = 0; x < size; x++) {
- int[] pixel = buffer[y][x];
- int red = pixel[0] / pos;
- int green = pixel[1] / pos;
- int blue = pixel[2] / pos;
- img.setRGB (x, y, (red << 16) + (green << 8) + blue);
- }
- }
- return img;
- }
- catch (Exception e) {
- e.printStackTrace ();
- return null;
- }
- }
- public static BufferedImage blend (File imagePath, int size) {
- List<File> files = new ArrayList<File> ();
- for (File f : imagePath.listFiles ()) {
- String name = f.getName ().toLowerCase ();
- if (name.endsWith (".png") || name.endsWith (".jpg") || name.endsWith (".jpeg")) {
- files.add (f);
- }
- }
- return blend (files.toArray (new File[files.size ()]), size);
- }
- public static double applyColorSchemePenalty (double score, double red, double green, double blue,
- double redGreenRatio, double redBlueRatio, double greenBlueRatio, double brightness) {
- double rg = red / (green * redGreenRatio);
- double rb = red / (blue * redBlueRatio);
- double gb = green / (blue * greenBlueRatio);
- double lumos =(red + green + blue) / (brightness * 3);
- if (rg < 1) rg = 1 / rg;
- if (rb < 1) rb = 1 / rb;
- if (gb < 1) gb = 1 / gb;
- if (lumos < 1) lumos = 1 / lumos;
- double mult = 1;
- if (rg > 1.2) mult -= (rg - 1) * 0.2;
- if (rb > 1.2) mult -= (rb - 1) * 0.2;
- if (gb > 1.2) mult -= (gb - 1) * 0.2;
- if (lumos > 1.5) mult -= (lumos - 1) * 0.1;
- mult = Math.max (0, mult);
- return score * mult;
- }
- public static void main (String[] args) {
- try {
- File path = new File (args[0]);
- int size = 400;
- File maskFile = new File (args[1]);
- BufferedImage maskImg = maskFile.isDirectory()
- ? blend (maskFile, size)
- : loadImage (maskFile, size);
- double[][][] mask = mask (maskImg);
- double[] maskColor = average (maskImg);
- double redGreenRatio = maskColor[0] / maskColor[1];
- double redBlueRatio = maskColor[0] / maskColor[2];
- double greenBlueRatio = maskColor[1] / maskColor[2];
- double brightness = (maskColor[0] + maskColor[1] + maskColor[2]) / 3;
- System.out.println (redGreenRatio + " " + redBlueRatio + " "+greenBlueRatio + " " + brightness);
- PrintStream out = new PrintStream (args[2]);
- out.println ("file,score,noise,red,green,blue");
- int pos = 0;
- for (File f : path.listFiles ()) {
- String name = f.getName ().toLowerCase ();
- if (name.endsWith (".png") || name.endsWith (".jpg") || name.endsWith (".jpeg")) {
- pos++;
- System.out.println (pos + ":\t " + name);
- BufferedImage image = loadImage (f, size);
- double[] color = average (image);
- double noise = noise (image, mask);
- out.println (f + "," + (applyColorSchemePenalty (1 - diffScore (mask, mask (image)),
- color[0], color[1], color[2],
- redGreenRatio, redBlueRatio, greenBlueRatio, brightness) + noise * 2.0)
- + "," + noise + "," + color[0] + "," + color[1] + "," + color[2]);
- }
- }
- out.close ();
- }
- catch (Exception e) {
- e.printStackTrace ();
- }
- System.exit (0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement