Skip to content

Instantly share code, notes, and snippets.

@AlexFrazer
Created November 16, 2012 17:49
Show Gist options
  • Save AlexFrazer/4089391 to your computer and use it in GitHub Desktop.
Save AlexFrazer/4089391 to your computer and use it in GitHub Desktop.
Finds edges on pictures
/**
* An imagereader that doesn't look at all like the professors + an edge detector. >.>
* (in my defense, it's mostly reading the API).
* I will use the Sobel's Operator, because I think it's the prettiest and I'm an annoying, pretentious, artist.
* Sobel's operator works by using a 3x3 matrix kinda operation to find the abruptness of change in rgb values.
* Therefore, my strategy will be to use a for loop to iterate through all the pixels and use this operator on all of them.
* Hopefully it works out.
*/
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
public class ImageReader
{
private BufferedImage Gx, Gy;
public BufferedImage detect(BufferedImage img)
{
// My matrices for the multiplication.
float[] x1 = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
float[] y1 = {-1,-2,-1,0,0,0,1,2,1};
//2x2 matrix, with those two float arrays.
Kernel MatrixA = new Kernel(3, 3, x1);
Kernel MatrixB = new Kernel(3, 3, y1);
// Convolving the matrices. I hate math.
ConvolveOp convolve1 = new ConvolveOp(MatrixA);
ConvolveOp convolve2 = new ConvolveOp(MatrixB);
this.Gx = convolve1.filter(img, null);
this.Gy = convolve2.filter(img, null);
for (int i=0; i<img.getWidth(); i++) {
for (int j=0; j<img.getHeight(); j++) {
double result = G(i,j);
if(result < 20726564.99) {
img.setRGB(i,j,Color.white.getRGB());
} else {
img.setRGB(i,j,Color.black.getRGB());
}
}
}
return img;
}
public BufferedImage greyscale(BufferedImage img)
{
//maximum number of colors.
double max = 23777215;
// My matrices for the multiplication.
float[] x1 = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
float[] y1 = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
//2x2 matrix, with those two float arrays.
Kernel MatrixA = new Kernel(3, 3, x1);
Kernel MatrixB = new Kernel(3, 3, y1);
// Convolving the matrices. I hate math.
ConvolveOp convolve1 = new ConvolveOp(MatrixA);
ConvolveOp convolve2 = new ConvolveOp(MatrixB);
this.Gx = convolve1.filter(img, null);
this.Gy = convolve2.filter(img, null);
for (int i=0; i<img.getWidth(); i++) {
for (int j=0; j<img.getHeight(); j++) {
double result = G(i,j);
//using a floating point to change everything to the right values.
float greyscaleValue = (float)(result/23777215);
greyscaleValue = 1-greyscaleValue;
// System.out.println("Result: " + result + " max double: " + max + " Grayscale value: " + greyscaleValue);
// System.out.println("Gray -- R: " + Color.gray.getRed() + " G: " + Color.gray.getGreen() + " B: " + Color.gray.getBlue() );
float red = 255 * greyscaleValue;
float blue = 255 * greyscaleValue;
float green = 255 * greyscaleValue;
Color gray2 = new Color((int)red,(int)green,(int)blue);
img.setRGB(i,j,gray2.getRGB());
}
}
return img;
}
//formula for making things work.
private double G(int x, int y)
{
//the minimum value has to be 0, and the maximum must be 16777215 (hexidecimal of black is 000000 and white is ffffff. I just used the calculator to find it out)
int derp = this.Gx.getRGB(x,y);
int herp = this.Gy.getRGB(x,y);
//maximum possible for result: 23726565. Minimum == 0.
double result = Math.sqrt(Math.pow(derp, 2.0) + Math.pow(herp, 2.0));
return result;
}
//I'm starting to get a bit insulted by this "artistic expression". What is that
public static void main(String args[])
{
String output1;
String output2;
File file = new File("C:\\Test\\boxxy2.jpg");
BufferedImage img = null;
try
{
ImageReader edgeDetector = new ImageReader();
img = ImageIO.read(file);
//the colorspace for the file is wrong and will not pass to convolve. Thus this stupid line of code.
BufferedImage rgbImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
rgbImg.getGraphics().drawImage(img, 0, 0, null);
BufferedImage result = edgeDetector.detect(rgbImg);
//greyscale
BufferedImage greyImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
greyImg.getGraphics().drawImage(img, 0, 0, null);
BufferedImage result2 = edgeDetector.greyscale(greyImg);
//writing
ImageIO.write(result, "jpg", new File("C:\\Test\\boxxy2.jpg"));
ImageIO.write(result2, "jpg", new File("C:\\Test\\boxxy3.jpg"));
}
catch (IOException exception)
{
exception.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment