0% found this document useful (0 votes)
23 views4 pages

IPexpt 8 Krishna

Uploaded by

fuddifamir222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

IPexpt 8 Krishna

Uploaded by

fuddifamir222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Code:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class MorphologicalOperations {

// Dilation operation
public static BufferedImage dilate(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage outputImage = new BufferedImage(width, height, image.getType());

int[][] structuringElement = {
{1, 1, 1},
{1, 1, 1},
{1, 1, 1}
};

for (int y = 1; y < height - 1; y++) {


for (int x = 1; x < width - 1; x++) {
int max = 0;
for (int ky = -1; ky <= 1; ky++) {
for (int kx = -1; kx <= 1; kx++) {
int pixelValue = new Color(image.getRGB(x + kx, y + ky)).getRed();
max = Math.max(max, pixelValue * structuringElement[ky + 1][kx + 1]);
}
}
outputImage.setRGB(x, y, new Color(max, max, max).getRGB());
}
}
return outputImage;
}

// Erosion operation
public static BufferedImage erode(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage outputImage = new BufferedImage(width, height, image.getType());

int[][] structuringElement = {
{1, 1, 1},
{1, 1, 1},
{1, 1, 1}
};

for (int y = 1; y < height - 1; y++) {


for (int x = 1; x < width - 1; x++) {
int min = 255;
for (int ky = -1; ky <= 1; ky++) {
for (int kx = -1; kx <= 1; kx++) {
int pixelValue = new Color(image.getRGB(x + kx, y + ky)).getRed();
min = Math.min(min, pixelValue * structuringElement[ky + 1][kx + 1]);
}
}
outputImage.setRGB(x, y, new Color(min, min, min).getRGB());
}
}
return outputImage;
}

// Opening (Erosion followed by Dilation)


public static BufferedImage open(BufferedImage image) {
BufferedImage erodedImage = erode(image);
return dilate(erodedImage);
}

// Closing (Dilation followed by Erosion)


public static BufferedImage close(BufferedImage image) {
BufferedImage dilatedImage = dilate(image);
return erode(dilatedImage);
}

public static void main(String[] args) {


try {
// Read input image
File inputFile = new File("D:/Sem7/IP/Pracs/exp8image.jpg");
BufferedImage inputImage = ImageIO.read(inputFile);

// Apply operations
BufferedImage dilatedImage = dilate(inputImage);
BufferedImage erodedImage = erode(inputImage);
BufferedImage openedImage = open(inputImage);
BufferedImage closedImage = close(inputImage);

// Save results to new files


ImageIO.write(dilatedImage, "png", new File("DilatedImage.png"));
ImageIO.write(erodedImage, "png", new File("ErodedImage.png"));
ImageIO.write(openedImage, "png", new File("OpenedImage.png"));
ImageIO.write(closedImage, "png", new File("ClosedImage.png"));

System.out.println("Morphological operations completed and saved.");

} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Input Image(Image.png):

i) Dilated Image ii) Eroded Image

iii) Opened Image iv) Closed Image

Conclusion: Dilation, Erosion, Opening and Closing operations were performed on


an image using Java program.

You might also like