Skip to content

Instantly share code, notes, and snippets.

@saurabhwahile
Created October 1, 2015 06:19
Show Gist options
  • Save saurabhwahile/0a8ffb6fc695fcb02253 to your computer and use it in GitHub Desktop.
Save saurabhwahile/0a8ffb6fc695fcb02253 to your computer and use it in GitHub Desktop.
Image Morphological Operations
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import java.util.*;
import java.awt.Color;
class Operator
{
public int structuringElement[][];
public int origin[];
public void setStructuringElement(int structuringElement[][], int origin[])
{
this.structuringElement = structuringElement;
this.origin = origin;
}
public int[][] imageMatrix;
int height;
int width;
BufferedImage image;
public void readImage(String path)
{
try
{
image = ImageIO.read(new File(path));
this.height = image.getHeight();
this.width = image.getWidth();
imageMatrix = new int[width][height];
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
int pixel = image.getRGB(i,j);
if(pixel == Color.BLACK.getRGB())
{
imageMatrix[i][j] = 0;
}
else
{
imageMatrix[i][j] = 1;
}
}
}
}
catch(IOException e)
{
System.out.println("Error Loading Image");
}
}
int output[][];
public void dilate()
{
output = new int[width][height];
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
if(imageMatrix[i][j] == 1)
{
for(int k=0; k<3; k++)
{
for(int l=0; l<3; l++)
{
if(i+k>=width||j+l>=height)
{
continue;
}
if(structuringElement[k][l] == 1)
{
output[i+k][j+l] = 1;
}
}
}
}
}
}
imageMatrix = output;
}
public void erode()
{
output = new int[width][height];
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
boolean containerFlag = true;
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
if (i + k >= width || j + l >= height)
{
continue;
}
if (structuringElement[k][l] == 1
&& imageMatrix[i + k][j + l] != 1)
{
containerFlag = false;
}
}
}
if (containerFlag)
{
try
{
output[i + origin[0]][j + origin[1]] = 1;
} catch (ArrayIndexOutOfBoundsException e)
{
}
}
}
}
imageMatrix = output;
}
public void writeImage(String path)
{
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
if(output[i][j]==0)
{
image.setRGB(i, j, Color.BLACK.getRGB());
}
else
{
image.setRGB(i, j, Color.WHITE.getRGB());
}
}
}
try
{
ImageIO.write(image, "bmp", new File(path));
}
catch(IOException e)
{
System.out.println("Error In Output");
}
}
}
public class MorphologicalOperations
{
public static void main(String[] args)
{
Operator operator = new Operator();
int structuringElement[][] =
{
{0, 1, 0},
{1, 1, 1},
{0, 1, 0}
};
int origin[] = {1, 1};
operator.setStructuringElement(structuringElement, origin);
operator.readImage("C:\\Users\\Saurabh\\Desktop\\square.bmp");
operator.dilate();
operator.writeImage("C:\\Users\\Saurabh\\Desktop\\square_dilate_o.bmp");
operator.readImage("C:\\Users\\Saurabh\\Desktop\\square.bmp");
operator.erode();
operator.writeImage("C:\\Users\\Saurabh\\Desktop\\square_erode_o.bmp");
operator.readImage("C:\\Users\\Saurabh\\Desktop\\square.bmp");
operator.erode();
operator.dilate();
operator.writeImage("C:\\Users\\Saurabh\\Desktop\\square_opening_o.bmp");
operator.readImage("C:\\Users\\Saurabh\\Desktop\\square.bmp");
operator.dilate();
operator.erode();
operator.writeImage("C:\\Users\\Saurabh\\Desktop\\square_closing_o.bmp");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment