0% found this document useful (0 votes)
15 views3 pages

IPexpt 6 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)
15 views3 pages

IPexpt 6 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/ 3

Code(Gaussian Filter):

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

public class exp6 {

// Apply a stronger Gaussian blur using a 5x5 kernel


public static BufferedImage smoothImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();

BufferedImage outputImage = new BufferedImage(width, height, image.getType());

// Gaussian kernel (5x5) for stronger smoothing


float[][] kernel = {
{1 / 273f, 4 / 273f, 7 / 273f, 4 / 273f, 1 / 273f},
{4 / 273f, 16 / 273f, 26 / 273f, 16 / 273f, 4 / 273f},
{7 / 273f, 26 / 273f, 41 / 273f, 26 / 273f, 7 / 273f},
{4 / 273f, 16 / 273f, 26 / 273f, 16 / 273f, 4 / 273f},
{1 / 273f, 4 / 273f, 7 / 273f, 4 / 273f, 1 / 273f}
};

// Convolve the kernel with the image


for (int y = 2; y < height - 2; y++) {
for (int x = 2; x < width - 2; x++) {
float red = 0, green = 0, blue = 0;

// Apply the kernel to each pixel and its neighbors


for (int ky = -2; ky <= 2; ky++) {
for (int kx = -2; kx <= 2; kx++) {
int pixelColor = image.getRGB(x + kx, y + ky);
Color color = new Color(pixelColor);

red += kernel[ky + 2][kx + 2] * color.getRed();


green += kernel[ky + 2][kx + 2] * color.getGreen();
blue += kernel[ky + 2][kx + 2] * color.getBlue();
}
}

// Clamp the values to be within the valid range [0, 255]


int newRed = Math.min(255, Math.max(0, (int) red));
int newGreen = Math.min(255, Math.max(0, (int) green));
int newBlue = Math.min(255, Math.max(0, (int) blue));

// Set the new pixel color in the output image


Color newColor = new Color(newRed, newGreen, newBlue);
outputImage.setRGB(x, y, newColor.getRGB());
}
}

return outputImage;
}

public static void main(String[] args) {


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

// Smooth the image with a stronger filter


BufferedImage smoothedImage = smoothImage(inputImage);

// Write the smoothed image to a new PNG file


File outputFile = new File("OutputGaussian.png");
ImageIO.write(smoothedImage, "png", outputFile);

System.out.println("Image smoothing complete. Output saved as 'OutputGaussian.png'.");


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

Output Image (OutputGaussian.png)

Conclusion: Image smoothening was done using Java Program.

You might also like