Friday, March 6, 2009

JAVA PROGRAMME FOR THRESHOLDING OF AN IMAGE

An image made up of different RGB component is
threshold to just two values black and white.




import java.applet.*;
import java.awt.*;
import java.awt.image.*;

public class threshhold extends Applet
{
Dimension d;
Image img;
int iw, ih;
int pixels[];
int w, h;
int rgb,r,g,b;

public void init()
{
d = getSize();
w = d.width;
h = d.height;

try{
img = getImage(getDocumentBase(),"real.jpg");
MediaTracker t = new MediaTracker(this);
t.addImage(img, 0);
t.waitForID(0);
iw = img.getWidth(null);
ih = img.getHeight(null);
pixels = new int[iw * ih];
PixelGrabber pg = new PixelGrabber(img, 0, 0, iw, ih,pixels, 0, iw);
pg.grabPixels();
}catch(InterruptedException e){};


int a[]= new int[iw*ih];
for(int i=0;i<(iw*ih);i++)
{
rgb = pixels[i];
if(rgb<(-7469172))
{
a[i]=0;
}
else
{
a[i]=255<<24;
}
}
img = createImage(new MemoryImageSource(iw,ih,a,0,iw));
}

public void paint(Graphics g)
{
g.drawImage(img, 0, 0, null);
}
}

No comments:

Post a Comment