0% found this document useful (0 votes)
59 views6 pages

Sub:-Dsip Juhi. A. Gianani Div - C2, Roll No 31 Image Negative

The document contains code to perform three image processing techniques: image negative, gray level slicing, and thresholding. Image negative code inverts the pixel colors of an input image. Gray level slicing code filters pixels within a specified gray level range. Thresholding code converts the image to black and white based on a threshold gray level value. Each code section loads an image, applies the technique, and saves the output image.

Uploaded by

mayur chablani
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)
59 views6 pages

Sub:-Dsip Juhi. A. Gianani Div - C2, Roll No 31 Image Negative

The document contains code to perform three image processing techniques: image negative, gray level slicing, and thresholding. Image negative code inverts the pixel colors of an input image. Gray level slicing code filters pixels within a specified gray level range. Thresholding code converts the image to black and white based on a threshold gray level value. Each code section loads an image, applies the technique, and saves the output image.

Uploaded by

mayur chablani
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/ 6

SUB :- DSIP

JUHI. A. GIANANI
DIV – C2 , ROLL NO 31

Image Negative

Code:-
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

class Negative
{
public static void main(String args[])
{
File f = new File("/home/lab405pc19/Desktop/rose.jpeg");

BufferedImage img = null;


try
{
img = ImageIO.read(f);
}

catch(IOException e)
{
e.printStackTrace();
}

int width = img.getWidth();


int height = img.getHeight();

System.out.println("H : "+ height + " W: "+ width);

for (int i = 0; i < height; i++)


{
for (int j = 0; j < width; j++)
{
int pixel = img.getRGB(j, i);
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;

int nred = 255 - red;


int ngreen = 255 - green;
int nblue = 255 - blue;

pixel = (alpha<<24) | (nred<<16) | (ngreen<<8) | nblue;


img.setRGB(j, i, pixel);
}
}

try
{
f = new File("/home/lab405pc19/Desktop/new.jpeg");
ImageIO.write(img, "jpg", f);
}
catch(IOException e)
{
System.out.println(e);
}

}
}

Output:-

Gray Level Slicing

Code:-
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

class Scaling
{
public static void main(String args[])throws IOException
{
BufferedImage img = null;
File f = null;
int height,width;
int x,y;
int r,g,b,p,q;
int min=40,max=160;

//Reading the image


try
{
f = new File("rose.jpeg");
img = ImageIO.read(f);

}
catch(IOException e)
{
System.out.println(e);
}

//Get image width and height


height = img.getHeight();
width = img.getWidth();

//Negation
for(y=0; y<height; y++)
{
for(x=0; x<width; x++)
{
p = img.getRGB(x,y);

r = (p>>16) & 0xff;


g = (p>>8) & 0xff;
b = p & 0xff;

q = (r+g+b)/3;

if(q>=min && q<=max)


img.setRGB(x,y,16777215);
else
img.setRGB(x,y,0);
}
}

//Writing the image


try
{
f = new File("scaling.jpg");
ImageIO.write(img, "jpg", f);
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Output:-

Thresholding

Code:-
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

class Thresholding
{
public static void main(String args[])throws IOException
{
BufferedImage img = null;
File f = null;
int height,width;
int x,y;
int r,g,b,p,q;
int threshold = 90;

//Reading the image


try
{
f = new File("rose.jpeg");
img = ImageIO.read(f);

}
catch(IOException e)
{
System.out.println(e);
}

//Get image width and height


height = img.getHeight();
width = img.getWidth();

//Negation
for(y=0; y<height; y++)
{
for(x=0; x<width; x++)
{
p = img.getRGB(x,y);

r = (p>>16) & 0xff;


g = (p>>8) & 0xff;
b = p & 0xff;

q = (r+g+b)/3;

if(q >= threshold)


img.setRGB(x,y,16777215);
else
img.setRGB(x,y,0);
}
}

//Writing the image


try
{
f = new File("threshold.jpg");
ImageIO.write(img, "jpg", f);
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Output:-

You might also like