Skip to content

Instantly share code, notes, and snippets.

@Dascr32
Last active August 29, 2015 14:18
Show Gist options
  • Save Dascr32/aa44f0821e2a6ca7b8a8 to your computer and use it in GitHub Desktop.
Save Dascr32/aa44f0821e2a6ca7b8a8 to your computer and use it in GitHub Desktop.
Process a bit map image (greyscale, color) into 1's or 0's for object detecting algorithms.
public class ImageProcessing {
private int imageHeight;
private int imageWidth;
private int imageMatrix[][];
private Color color;
final int COLOUR_DEPTH_BITS = 1; //1 bit (black or white)
public void loadImage(String imagePath) {
BufferedImage image = null;
// Load image from file
try {
image = ImageIO.read(new File(imagePath));
} catch (Exception e) {
e.printStackTrace();
}
imageHeight = image.getHeight();
imageWidth = image.getWidth();
imageMatrix = new int[imageHeight][imageWidth];
for (int h = 0; h < imageHeight; h++) {
for (int w = 0; w < imageWidth; w++) {
color = new Color(image.getRGB(w, h));
int luminance = 0.2126 * color.getRed(); + 0.7152 * color.getGreen(); + 0.0722 * color.getBlue();
imageMatrix[h][w] = convertColor(luminance);
}
}
}
public void printImageMatrix() {
for (int i = 0; i < imageMatrix.length; i++) {
System.out.println("");
for (int j = 0; j < imageMatrix[0].length; j++) {
System.out.print(imageMatrix[i][j] + " ");
}
}
}
private int convertColor(int luminance) {
if (luminance < 128) {
return 0;
} else {
return 1;
}
}
}
@richin13
Copy link

richin13 commented Apr 7, 2015

Buenísimo (Y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment