Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyGrayProgram {
- public static void main(String[] args) throws Exception{
- int[][] original = GrayImage.read("mushroom.jpeg");
- //int[][] manipulated = upDown(original);
- //int[][] manipulated = leftRight(original);
- //int[][] manipulated = invert(original);
- //int[][] manipulated = toBlackWhite(original);
- int[][] manipulated = contour(original);
- GrayImage.write("contourGray.jpeg", manipulated);
- GrayImageWindow iw = new GrayImageWindow(original, manipulated);
- }//main
- public static int[][] upDown(int[][] samples) {
- int[][] newSamples = new int[samples.length][samples[0].length];
- for (int row = 0; row < samples.length; row = row + 1)
- for (int col = 0; col < samples[row].length; col = col + 1)
- newSamples[row][col] = samples[samples.length - row - 1 ][col];
- return newSamples;
- }//upDown
- //Before:Grå bild
- //Afer:Bilden flippas runt y-axeln
- public static int[][] leftRight(int[][] samples) {
- int[][] flippedSample = new int[samples.length][samples[0].length];
- for (int row = 0; row < samples.length; row++)
- for (int col = 0; col < samples[row].length; col++) {
- flippedSample[row][col] = samples[row][samples[0].length - col -1];
- }
- return flippedSample;
- }
- //Before:Grå bild
- //After: Bilden tar 255 och subtraherar nuvarande gråskala och resultat ger en inverterad bild
- public static int[][] invert(int[][] samples) {
- int[][] invertedSample = new int[samples.length][samples[0].length];
- for (int row = 0; row < samples.length; row++)
- for (int col = 0; col < samples[row].length; col++)
- invertedSample[row][col] = 255- samples[row][col];
- return invertedSample;
- }
- //Before:En grå bild
- //After:Sätter alla gråa värden störer 128 till vit och alla värden under 128 till svart
- public static int[][] toBlackWhite(int[][] samples){
- int[][] blackWhiteSample = new int[samples.length][samples[0].length];
- for (int row = 0; row < samples.length; row++)
- for (int col = 0; col < samples[row].length; col++){
- if(samples[row][col] >= 128) {
- blackWhiteSample[row][col] = 255;
- }else{
- blackWhiteSample[row][col] = 0;
- }
- }
- return blackWhiteSample;
- }
- //Before:en grå bild
- //After:Letar efter grannar och gör alla "kanter" svarta och resten vitt
- //en pixel ska vara svart om och endast om orginal pixeln är svart och ligger på randen eller har en vit granne.
- public static int[][]contour(int[][] samples) {
- int[][] contourSample = new int[samples.length][samples[0].length];
- for (int row = 0; row < samples.length; row++) {
- for (int col = 0; col < samples[row].length; col++) {
- contourSample[row][col] = 255;
- if ((samples[row][col] < 128)) {
- if (col == 0 || col == samples[0].length - 1 || row == 0 || row == samples.length - 1)
- contourSample[row][col] = 0;
- //}
- if(!(col == 0 || col == samples[0].length - 1 || row == 0 || row == samples.length - 1))
- for (int r = -1; r < 2; r++) {
- for (int c = -1; c < 2; c++) {
- if (samples[row - r][col - c] >= 128) {
- contourSample[row][col] = 0;
- }
- }
- }
- }
- }
- }
- return contourSample;
- } //contour
- }//MyGrayProgram
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement