Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. public class MyGrayProgram {
  2. public static void main(String[] args) throws Exception{
  3. int[][] original = GrayImage.read("mushroom.jpeg");
  4. //int[][] manipulated = upDown(original);
  5. //int[][] manipulated = leftRight(original);
  6. //int[][] manipulated = invert(original);
  7. //int[][] manipulated = toBlackWhite(original);
  8. int[][] manipulated = contour(original);
  9. GrayImage.write("contourGray.jpeg", manipulated);
  10. GrayImageWindow iw = new GrayImageWindow(original, manipulated);
  11. }//main
  12.  
  13. public static int[][] upDown(int[][] samples) {
  14. int[][] newSamples = new int[samples.length][samples[0].length];
  15. for (int row = 0; row < samples.length; row = row + 1)
  16. for (int col = 0; col < samples[row].length; col = col + 1)
  17. newSamples[row][col] = samples[samples.length - row - 1 ][col];
  18. return newSamples;
  19. }//upDown
  20. //Before:Grå bild
  21. //Afer:Bilden flippas runt y-axeln
  22. public static int[][] leftRight(int[][] samples) {
  23. int[][] flippedSample = new int[samples.length][samples[0].length];
  24. for (int row = 0; row < samples.length; row++)
  25. for (int col = 0; col < samples[row].length; col++) {
  26. flippedSample[row][col] = samples[row][samples[0].length - col -1];
  27. }
  28. return flippedSample;
  29. }
  30. //Before:Grå bild
  31. //After: Bilden tar 255 och subtraherar nuvarande gråskala och resultat ger en inverterad bild
  32. public static int[][] invert(int[][] samples) {
  33. int[][] invertedSample = new int[samples.length][samples[0].length];
  34. for (int row = 0; row < samples.length; row++)
  35. for (int col = 0; col < samples[row].length; col++)
  36. invertedSample[row][col] = 255- samples[row][col];
  37. return invertedSample;
  38. }
  39.  
  40. //Before:En grå bild
  41. //After:Sätter alla gråa värden störer 128 till vit och alla värden under 128 till svart
  42. public static int[][] toBlackWhite(int[][] samples){
  43. int[][] blackWhiteSample = new int[samples.length][samples[0].length];
  44. for (int row = 0; row < samples.length; row++)
  45. for (int col = 0; col < samples[row].length; col++){
  46. if(samples[row][col] >= 128) {
  47. blackWhiteSample[row][col] = 255;
  48. }else{
  49. blackWhiteSample[row][col] = 0;
  50. }
  51. }
  52. return blackWhiteSample;
  53. }
  54. //Before:en grå bild
  55. //After:Letar efter grannar och gör alla "kanter" svarta och resten vitt
  56. //en pixel ska vara svart om och endast om orginal pixeln är svart och ligger på randen eller har en vit granne.
  57. public static int[][]contour(int[][] samples) {
  58. int[][] contourSample = new int[samples.length][samples[0].length];
  59. for (int row = 0; row < samples.length; row++) {
  60. for (int col = 0; col < samples[row].length; col++) {
  61. contourSample[row][col] = 255;
  62. if ((samples[row][col] < 128)) {
  63. if (col == 0 || col == samples[0].length - 1 || row == 0 || row == samples.length - 1)
  64. contourSample[row][col] = 0;
  65.  
  66. //}
  67. if(!(col == 0 || col == samples[0].length - 1 || row == 0 || row == samples.length - 1))
  68. for (int r = -1; r < 2; r++) {
  69. for (int c = -1; c < 2; c++) {
  70. if (samples[row - r][col - c] >= 128) {
  71. contourSample[row][col] = 0;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return contourSample;
  79. } //contour
  80.  
  81.  
  82.  
  83. }//MyGrayProgram
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement