Sei sulla pagina 1di 5

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:-

Potrebbero piacerti anche