Advertisement
Guest User

layout anomaly detection

a guest
May 26th, 2020
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1. import java.awt.Graphics2D;
  2. import java.awt.Image;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.PrintStream;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import javax.imageio.ImageIO;
  12.  
  13. public class LayoutAnomalyDetection {
  14.  
  15. public static double noise (BufferedImage image, double[][][] mask) {
  16. double score = 0;
  17. int w = image.getWidth ();
  18. int h = image.getHeight ();
  19. int size = w * h;
  20. double red = 0;
  21. double green = 0;
  22. double blue = 0;
  23. for (int y = 0; y < h; y++) {
  24. for (int x = 0; x < w; x++) {
  25. int rgb = image.getRGB (x, y);
  26. red += ((rgb >> 16) & 0xff);
  27. green += ((rgb >> 8) & 0xff);
  28. blue += (rgb & 0xff);
  29. }
  30. }
  31. red /= size;
  32. green /= size;
  33. blue /= size;
  34. size *= 765;
  35. for (int y = 0; y < h; y++) {
  36. for (int x = 0; x < w; x++) {
  37. int rgb = image.getRGB (x, y);
  38. score += Math.abs (((rgb >> 16) & 0xff) - red) * mask[y][x][0];
  39. score += Math.abs (((rgb >> 8) & 0xff) - green) * mask[y][x][1];
  40. score += Math.abs ((rgb & 0xff) - blue) * mask[y][x][2];
  41. }
  42. }
  43. return score / size;
  44. }
  45.  
  46.  
  47. public static double[] average (BufferedImage image) {
  48. int w = image.getWidth ();
  49. int h = image.getHeight ();
  50. int size = w * h;
  51. double red = 0;
  52. double green = 0;
  53. double blue = 0;
  54. for (int y = 0; y < h; y++) {
  55. for (int x = 0; x < w; x++) {
  56. int rgb = image.getRGB (x, y);
  57. red += ((rgb >> 16) & 0xff);
  58. green += ((rgb >> 8) & 0xff);
  59. blue += (rgb & 0xff);
  60. }
  61. }
  62. red /= size;
  63. green /= size;
  64. blue /= size;
  65. return new double[] {red, green, blue};
  66. }
  67.  
  68.  
  69. public static double diffScore (double[][][] mask1, double[][][] mask2) {
  70. double score = 0;
  71. int w = mask1[0].length;
  72. int h = mask1.length;
  73. for (int y = 0; y < h; y++) {
  74. for (int x = 0; x < w; x++) {
  75. for (int c = 0; c < 3; c++) {
  76. score += Math.abs (mask1[y][x][c] - mask2[y][x][c]);
  77. }
  78. }
  79. }
  80. return score / (3 * w * h);
  81. }
  82.  
  83.  
  84. public static double[][][] mask (BufferedImage image) {
  85. int w = image.getWidth ();
  86. int h = image.getHeight ();
  87. double[][][] mask = new double[h][w][3];
  88. int size = w * h;
  89. double red = 0;
  90. double green = 0;
  91. double blue = 0;
  92. for (int y = 0; y < h; y++) {
  93. for (int x = 0; x < w; x++) {
  94. int rgb = image.getRGB (x, y);
  95. red += ((rgb >> 16) & 0xff);
  96. green += ((rgb >> 8) & 0xff);
  97. blue += (rgb & 0xff);
  98. }
  99. }
  100. red /= size;
  101. green /= size;
  102. blue /= size;
  103. double factor = 0;
  104. double[] pixel;
  105. for (int y = 0; y < h; y++) {
  106. for (int x = 0; x < w; x++) {
  107. pixel = mask[y][x];
  108. int rgb = image.getRGB (x, y);
  109. mask[y][x][0] = Math.abs (((rgb >> 16) & 0xff) - red);
  110. mask[y][x][1] = Math.abs (((rgb >> 8) & 0xff) - green);
  111. mask[y][x][2] = Math.abs ((rgb & 0xff) - blue);
  112. for (int c = 0; c < 3; c++) {
  113. factor = Math.max (factor, pixel[c]);
  114. }
  115. }
  116. }
  117. factor = 1.0 / factor;
  118. for (int y = 0; y < h; y++) {
  119. for (int x = 0; x < w; x++) {
  120. for (int c = 0; c < 3; c++) {
  121. mask[y][x][c] *= factor;
  122. }
  123. }
  124. }
  125. return mask;
  126. }
  127.  
  128.  
  129. public static BufferedImage loadImage (File file, int size) throws Exception {
  130. Image tile = ImageIO.read (file).getScaledInstance (size, size, Image.SCALE_FAST);
  131. BufferedImage img = new BufferedImage (size, size, BufferedImage.TYPE_INT_RGB);
  132. Graphics2D g = img.createGraphics ();
  133. g.drawImage (tile, 0, 0, null);
  134. return img;
  135. }
  136.  
  137.  
  138. public static BufferedImage blend (File[] imageFiles, int size) {
  139. try {
  140. int[][][] buffer = new int[size][size][3];
  141. int pos = 0;
  142. for (File file : imageFiles) {
  143. BufferedImage img = loadImage (file, size);
  144. for (int y = 0; y < size; y++) {
  145. for (int x = 0; x < size; x++) {
  146. int rgb = img.getRGB (x, y);
  147. int[] pixel = buffer[y][x];
  148. pixel[0] += (rgb >> 16) & 0xff;
  149. pixel[1] += (rgb >> 8) & 0xff;
  150. pixel[2] += rgb & 0xff;
  151. }
  152. }
  153. pos++;
  154. }
  155. BufferedImage img = new BufferedImage (size, size, BufferedImage.TYPE_INT_RGB);
  156. for (int y = 0; y < size; y++) {
  157. for (int x = 0; x < size; x++) {
  158. int[] pixel = buffer[y][x];
  159. int red = pixel[0] / pos;
  160. int green = pixel[1] / pos;
  161. int blue = pixel[2] / pos;
  162. img.setRGB (x, y, (red << 16) + (green << 8) + blue);
  163. }
  164. }
  165. return img;
  166. }
  167. catch (Exception e) {
  168. e.printStackTrace ();
  169. return null;
  170. }
  171. }
  172.  
  173.  
  174. public static BufferedImage blend (File imagePath, int size) {
  175. List<File> files = new ArrayList<File> ();
  176. for (File f : imagePath.listFiles ()) {
  177. String name = f.getName ().toLowerCase ();
  178. if (name.endsWith (".png") || name.endsWith (".jpg") || name.endsWith (".jpeg")) {
  179. files.add (f);
  180. }
  181. }
  182. return blend (files.toArray (new File[files.size ()]), size);
  183. }
  184.  
  185.  
  186. public static double applyColorSchemePenalty (double score, double red, double green, double blue,
  187. double redGreenRatio, double redBlueRatio, double greenBlueRatio, double brightness) {
  188. double rg = red / (green * redGreenRatio);
  189. double rb = red / (blue * redBlueRatio);
  190. double gb = green / (blue * greenBlueRatio);
  191. double lumos =(red + green + blue) / (brightness * 3);
  192. if (rg < 1) rg = 1 / rg;
  193. if (rb < 1) rb = 1 / rb;
  194. if (gb < 1) gb = 1 / gb;
  195. if (lumos < 1) lumos = 1 / lumos;
  196. double mult = 1;
  197. if (rg > 1.2) mult -= (rg - 1) * 0.2;
  198. if (rb > 1.2) mult -= (rb - 1) * 0.2;
  199. if (gb > 1.2) mult -= (gb - 1) * 0.2;
  200. if (lumos > 1.5) mult -= (lumos - 1) * 0.1;
  201. mult = Math.max (0, mult);
  202. return score * mult;
  203. }
  204.  
  205.  
  206. public static void main (String[] args) {
  207. try {
  208. File path = new File (args[0]);
  209. int size = 400;
  210. File maskFile = new File (args[1]);
  211. BufferedImage maskImg = maskFile.isDirectory()
  212. ? blend (maskFile, size)
  213. : loadImage (maskFile, size);
  214. double[][][] mask = mask (maskImg);
  215. double[] maskColor = average (maskImg);
  216. double redGreenRatio = maskColor[0] / maskColor[1];
  217. double redBlueRatio = maskColor[0] / maskColor[2];
  218. double greenBlueRatio = maskColor[1] / maskColor[2];
  219. double brightness = (maskColor[0] + maskColor[1] + maskColor[2]) / 3;
  220. System.out.println (redGreenRatio + " " + redBlueRatio + " "+greenBlueRatio + " " + brightness);
  221. PrintStream out = new PrintStream (args[2]);
  222. out.println ("file,score,noise,red,green,blue");
  223. int pos = 0;
  224. for (File f : path.listFiles ()) {
  225. String name = f.getName ().toLowerCase ();
  226. if (name.endsWith (".png") || name.endsWith (".jpg") || name.endsWith (".jpeg")) {
  227. pos++;
  228. System.out.println (pos + ":\t " + name);
  229. BufferedImage image = loadImage (f, size);
  230. double[] color = average (image);
  231. double noise = noise (image, mask);
  232. out.println (f + "," + (applyColorSchemePenalty (1 - diffScore (mask, mask (image)),
  233. color[0], color[1], color[2],
  234. redGreenRatio, redBlueRatio, greenBlueRatio, brightness) + noise * 2.0)
  235. + "," + noise + "," + color[0] + "," + color[1] + "," + color[2]);
  236. }
  237. }
  238. out.close ();
  239. }
  240. catch (Exception e) {
  241. e.printStackTrace ();
  242. }
  243. System.exit (0);
  244. }
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement