Last active
July 1, 2019 10:17
-
-
Save loristissino/32f2aff410ff10473645a610048dd598 to your computer and use it in GitHub Desktop.
Basic image processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.awt.Color; | |
import java.awt.Graphics2D; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
public class Main { | |
static BufferedImage imgIn; | |
static BufferedImage imgOut; | |
/** | |
* Loads an image to the global variable imgIn. | |
* @param path The path of the file to open | |
* @return true if successful, false otherwise | |
*/ | |
public static boolean loadImage(String path) { | |
try { | |
imgIn = ImageIO.read(new File(path)); | |
return true; | |
} catch (IOException e) { | |
return false; | |
} | |
} | |
/** | |
* Takes a BufferedImage and returns a matrix of boolean, where the | |
* value is true when the pixel is black and false otherwise. | |
* @param img The image to process | |
* @return the matrix of boolean values | |
*/ | |
public static boolean [][] imageToBooleanMatrix(BufferedImage img){ | |
boolean m[][] = new boolean[img.getHeight()][img.getWidth()]; | |
for (int r=0; r<img.getHeight(); r++){ | |
for (int c=0; c<img.getWidth(); c++){ | |
m[r][c] = img.getRGB(c,r) == Color.BLACK.getRGB(); | |
} | |
} | |
return m; | |
} | |
/** | |
* Takes a matrix of boolean values and returns a BufferedImage. | |
* @param pixels The matrix of boolean values | |
* @return the BufferedImage object | |
*/ | |
public static BufferedImage booleanMatrixToImage(boolean[][] pixels){ | |
BufferedImage img = new BufferedImage(pixels[0].length, pixels.length, BufferedImage.TYPE_INT_RGB); | |
Graphics2D g2d = img.createGraphics(); | |
for (int r=0; r<pixels.length; r++){ | |
for (int c=0; c<pixels[0].length; c++){ | |
g2d.setColor(pixels[r][c] ? Color.BLACK : Color.WHITE); | |
g2d.fillRect(c,r,1,1); | |
} | |
} | |
return img; | |
} | |
/** | |
* Saves the current output BufferedImage (global variable imgOut). | |
* @param path The path of the file to be written | |
* @return true if successful, false otherwise | |
*/ | |
public static boolean saveImage(String path){ | |
File file = new File(path); | |
try { | |
ImageIO.write(imgOut, "png", file); | |
return true; | |
} catch (IOException e) { | |
return false; | |
} | |
} | |
public static boolean [][] invert(boolean [][] m){ | |
boolean [][] v = new boolean[m.length][m[0].length]; | |
for (int r=0; r<m.length; r++){ | |
for (int c=0; c<m[0].length; c++){ | |
v[r][c] = !m[r][c]; | |
} | |
} | |
return v; | |
} | |
public static void main(String[] args) { | |
boolean [][] pixelsIn; | |
boolean [][] pixelsOut; | |
if (loadImage("aaargh.png")) { | |
System.out.println("Input file loaded..."); | |
pixelsIn = imageToBooleanMatrix(imgIn); | |
pixelsOut = invert(pixelsIn); | |
imgOut = booleanMatrixToImage(pixelsOut); | |
if (saveImage("out.png")) { | |
System.out.println("Output file written."); | |
} | |
else { | |
System.out.println("Couldn't write file."); | |
} | |
} | |
else { | |
System.out.println("Couldn't read file."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basic image processing of a black & white image.
We could avoid the use of a matrix, but this is the base for an exercise that involves matrix operations.