Guest User

Untitled

a guest
Dec 13th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package com.test.image.main;
  5.  
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.nio.file.Files;
  11. import java.nio.file.Path;
  12. import java.nio.file.Paths;
  13. import java.util.Arrays;
  14. import java.util.Properties;
  15. import java.util.Random;
  16. import java.util.stream.Stream;
  17.  
  18. import javax.imageio.ImageIO;
  19.  
  20. public class ImageMain {
  21.  
  22. private static int medianSize;
  23.  
  24. private static int[] wMedian;
  25.  
  26. private static int numberOfBuckets;
  27.  
  28. private static double[] rgbScale;
  29.  
  30. private static String imgDir;
  31.  
  32. private static double[] wLinear;
  33.  
  34. private static double fractionOfSaltAndPepper;
  35.  
  36. private static int linearSize;
  37.  
  38. private static int gaussianMean;
  39.  
  40. private static int gaussianStd;
  41.  
  42. private static String toString(long nanoSecs) {
  43. int minutes = (int) (nanoSecs / 60000000000.0);
  44. int seconds = (int) (nanoSecs / 1000000000.0) - (minutes * 60);
  45. int millisecs = (int) ( ((nanoSecs / 1000000000.0) - (seconds + minutes * 60)) * 1000);
  46.  
  47.  
  48. if (minutes == 0 && seconds == 0)
  49. return millisecs + "ms";
  50. else if (minutes == 0 && millisecs == 0)
  51. return seconds + "s";
  52. else if (seconds == 0 && millisecs == 0)
  53. return minutes + "min";
  54. else if (minutes == 0)
  55. return seconds + "s " + millisecs + "ms";
  56. else if (seconds == 0)
  57. return minutes + "min " + millisecs + "ms";
  58. else if (millisecs == 0)
  59. return minutes + "min " + seconds + "s";
  60.  
  61. return minutes + "min " + seconds + "s " + millisecs + "ms";
  62. }
  63.  
  64. private static void initializeConfig() {
  65. Properties prop = new Properties();
  66. InputStream input = null;
  67.  
  68. try {
  69.  
  70. input = ClassLoader.getSystemResourceAsStream(("config.properties"));
  71. prop.load(input);
  72. prop.forEach((k, v) -> System.out.println("key: " + k + " value:" + v));
  73.  
  74. medianSize = getIntProperty(prop, "medianSize");
  75. wMedian = getIntArrayProperty(prop, "wMedian");
  76. numberOfBuckets = getIntProperty(prop, "numberOfBuckets");
  77. imgDir = getStringProperty(prop, "directory");
  78. linearSize = getIntProperty(prop, "linearSize");
  79. gaussianMean = getIntProperty(prop, "gaussianMean");
  80. gaussianStd = getIntProperty(prop, "gaussianStd");
  81. fractionOfSaltAndPepper = getDoubleProperty(prop, "fractionOfSaltAndPepper");
  82. rgbScale = getDoubleArrayProperty(prop, "rgbScale");
  83. wLinear = getDoubleArrayProperty(prop, "wLinear");
  84.  
  85. }
  86. catch (IOException ex) {
  87. ex.printStackTrace();
  88. }
  89. finally {
  90. if (input != null) {
  91. try {
  92. input.close();
  93. }
  94. catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. }
  99. }
  100.  
  101. private static double getDoubleProperty(Properties prop, String property) {
  102. return null != prop.getProperty(property) ? Double.parseDouble(prop.getProperty(property)) : 0;
  103. }
  104.  
  105. private static int getIntProperty(Properties prop, String property) {
  106. return null != prop.getProperty(property) ? Integer.parseInt(prop.getProperty(property)) : 0;
  107. }
  108.  
  109. private static String getStringProperty(Properties prop, String property) {
  110. return null != prop.getProperty(property) ? prop.getProperty(property) : "";
  111. }
  112.  
  113. private static int[] getIntArrayProperty(Properties prop, String property) {
  114. String strProperty = prop.getProperty(property);
  115. if (null != strProperty) {
  116.  
  117. return Arrays.stream(strProperty.split(",")).map(String::trim).mapToInt(Integer::parseInt).toArray();
  118. }
  119. return null;
  120. }
  121.  
  122. private static double[] getDoubleArrayProperty(Properties prop, String property) {
  123. String strProperty = prop.getProperty(property);
  124. if (null != strProperty) {
  125.  
  126. return Arrays.stream(strProperty.split(",")).map(String::trim).mapToDouble(Double::parseDouble).toArray();
  127. }
  128. return null;
  129. }
  130.  
  131. private static String createOutputDirectory() {
  132. String path = "output_"; // + new Random(6877989l).nextInt();
  133. try {
  134. Files.createDirectories(Paths.get(path));
  135. return path;
  136. }
  137. catch (IOException e) {
  138. // TODO Auto-generated catch block
  139. e.printStackTrace();
  140. }
  141. return null;
  142. }
  143.  
  144. private static BufferedImage getImageFromArray(int[][] pixels, int width, int height) {
  145. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  146. for (int row = 0; row < height; row++) {
  147. for (int col = 0; col < width; col++) {
  148. image.setRGB(col, row, pixels[row][col]);
  149. }
  150. }
  151. return image;
  152. }
  153.  
  154. private static void toGrayScale(String outputDir, Path file, double[] rgbScale ) throws IOException {
  155.  
  156. BufferedImage image = ImageIO.read(file.toFile());
  157. long startTime = System.nanoTime();
  158. int width = image.getWidth();
  159. int height = image.getHeight();
  160. int[][] result = new int[height][width];
  161.  
  162. for (int row = 0; row < height; row++) {
  163. for (int col = 0; col < width; col++) {
  164. int pixel = image.getRGB(col, row);
  165. //get alpha
  166. int a = (pixel>>24) & 0xff;
  167. //get red
  168. int r = (pixel>>16) & 0xff ;
  169. //System.out.println("result[row][col] RED = "+ r );
  170. r = (int) (r * rgbScale[0]);
  171. //System.out.println("result[row][col] RED = "+ r);
  172. //get green
  173. int g = (pixel>>8) & 0xff;
  174. //System.out.println("result[row][col] GREEN = "+ g );
  175. g = (int) (g * rgbScale[1]);
  176. //System.out.println("result[row][col] GREEN = "+ g );
  177. //get blue
  178. int b = pixel & 0xff;
  179. //System.out.println("result[row][col] Blue = "+ b );
  180. b = (int) (b * rgbScale[2]);
  181. //System.out.println("result[row][col] Blue = "+ b );
  182. int avg = (r+g+b)/3;
  183. result[row][col] = (a<<24) | (avg<<16) | (avg<<8) | avg;
  184. //System.out.println("result[row][col] = "+ result[row][col] );
  185. }
  186. }
  187.  
  188. BufferedImage newImage = getImageFromArray(result,width, height);
  189. Files.createDirectories(Paths.get(outputDir + "/GreyScaleAvg/"));
  190. File f = new File(outputDir + "/GreyScaleAvg/"+ file.getFileName().toString());
  191. ImageIO.write(newImage, "png", f);
  192.  
  193. long endTime = System.nanoTime();
  194. System.out.println("GrayScale - Time taken for "+ file.getFileName().toString() + " is " + toString(endTime - startTime));
  195.  
  196. }
  197.  
  198. private static void toSaltPaperFraction(String outputDirectory, Path file, double fractionOfSaltAndPepper) throws IOException {
  199.  
  200. BufferedImage image = ImageIO.read(file.toFile());
  201. long startTime = System.nanoTime();
  202. int width = image.getWidth();
  203. int height = image.getHeight();
  204. int[][] result = new int[height][width];
  205.  
  206. for (int row = 0; row < height; row++) {
  207. for (int col = 0; col < width; col++) {
  208. if ( new Random().nextDouble() < fractionOfSaltAndPepper) {
  209. if ( new Random().nextDouble() < 0.5) {
  210. result[row][col] = 255;
  211. }else {
  212. result[row][col] = 0;
  213. }
  214.  
  215. }else {
  216. int pixel = image.getRGB(col, row);
  217. //get alpha
  218. int a = (pixel>>24) & 0xff;
  219. //get red
  220. int r = (pixel>>16) & 0xff ;
  221. r = (int) (r * rgbScale[0]);
  222. //get green
  223. int g = (pixel>>8) & 0xff;
  224. g = (int) (g * rgbScale[1]);
  225. //get blue
  226. int b = pixel & 0xff;
  227. b = (int) (b * rgbScale[2]);
  228. int avg = (r+g+b)/3;
  229. result[row][col] = (a<<24) | (avg<<16) | (avg<<8) | avg;
  230. }
  231. }
  232. }
  233.  
  234. BufferedImage newImage = getImageFromArray(result,width, height);
  235. Files.createDirectories(Paths.get(outputDirectory + "/SaltnPepper/"));
  236. File f = new File(outputDirectory + "/SaltnPepper/"+ file.getFileName().toString());
  237. ImageIO.write(newImage, "png", f);
  238.  
  239. long endTime = System.nanoTime();
  240. System.out.println("Salt n pepper - Time taken for "+ file.getFileName().toString() + " is " + toString(endTime - startTime));
  241.  
  242.  
  243. }
  244.  
  245. private static int nextGaussianInt(Random r, int mean, int deviation) {
  246. int g = (int) ((int) mean + r.nextGaussian()*deviation);
  247. System.out.println("g = "+ g);
  248. return g;
  249. }
  250.  
  251. private static void toGaussianNoise(String outputDirectory, Path file, int gaussianMean, int gaussianStd) throws IOException {
  252. // TODO Auto-generated method stub
  253. BufferedImage image = ImageIO.read(file.toFile());
  254. long startTime = System.nanoTime();
  255. int width = image.getWidth();
  256. int height = image.getHeight();
  257. int[][] result = new int[height][width];
  258. Random random = new Random();
  259. for (int row = 0; row < height; row++) {
  260. for (int col = 0; col < width; col++) {
  261. int pixel = image.getRGB(col, row);
  262. //get alpha
  263. int a = (pixel>>24) & 0xff;
  264. //get red
  265. int r = (pixel>>16) & 0xff ;
  266. r = (int) (r * rgbScale[0]);
  267. //get green
  268. int g = (pixel>>8) & 0xff;
  269. g = (int) (g * rgbScale[1]);
  270. //get blue
  271. int b = pixel & 0xff;
  272. b = (int) (b * rgbScale[2]);
  273. int avg = (r+g+b)/3 + nextGaussianInt(random, gaussianMean, gaussianStd) ;
  274. if (avg< 0) {
  275. avg = 0;
  276. }else if (avg > 255) {
  277. avg = 255;
  278. }
  279. result[row][col] = (a<<24) | (avg<<16) | (avg<<8) | avg;
  280.  
  281. }
  282. }
  283.  
  284. BufferedImage newImage = getImageFromArray(result,width, height);
  285. Files.createDirectories(Paths.get(outputDirectory + "/GaussianNoise/"));
  286. File f = new File(outputDirectory + "/GaussianNoise/"+ file.getFileName().toString());
  287. ImageIO.write(newImage, "png", f);
  288.  
  289. long endTime = System.nanoTime();
  290. System.out.println("GaussianNoise - Time taken for "+ file.getFileName().toString() + " is " + toString(endTime - startTime));
  291.  
  292. }
  293.  
  294.  
  295. /**
  296. * @param args
  297. * @throws IOException
  298. */
  299. public static void main(String[] args) throws IOException {
  300.  
  301. initializeConfig();
  302.  
  303. String outputDirectory = createOutputDirectory();
  304.  
  305. if (outputDirectory != null) {
  306. System.out.println(" Directory " + outputDirectory + " created ");
  307. }
  308. else {
  309. System.err.println(" could not create directory ");
  310. }
  311.  
  312. try (Stream<Path> paths = Files.walk(Paths.get(imgDir))) {
  313. paths.filter(Files::isRegularFile).forEach(file -> {
  314. try {
  315. toGrayScale(outputDirectory, file, rgbScale);
  316. toSaltPaperFraction(outputDirectory, file, fractionOfSaltAndPepper);
  317. toGaussianNoise(outputDirectory, file, gaussianMean, gaussianStd);
  318. }
  319. catch (IOException e) {
  320. // TODO Auto-generated catch block
  321. e.printStackTrace();
  322. }
  323. });
  324. }
  325.  
  326. }
  327. }
Add Comment
Please, Sign In to add comment