Sunday, September 7, 2008

Image Processing using Java Media FrameWork (JMF))

Image processing using java is very simple. With the use of Java MediaFrameWork, image processing is just a matter of calling functions to get the job done .. !!

Here i will discuss in detail how image processing can be done in a real world scenario...step wise..

Requirements:
1. JDK 1.4 Download
2. Java Media Framework(JMF) Download
3. A PC :P

Step1: Capture the image.

Image to be processed can be obtained from the real world using camera or a stored image can be used. But usually image processing is a Real World Application. So we need to get the images 'LIVE'. .. now that is simple...

this is the code to capture an image using cam(inbuilt or usb)..

captureImage.java
-----------------


class captureImage
{
public static void main()
{
CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
Player player = Manager.createRealizedPlayer(deviceInfo.getLocator());
player.start();
// Wait a few seconds for camera to initialise (otherwise img==null)
System.out.println("Capturing Image...");
Thread.sleep(2500);
// Grab a frame from the capture device
FrameGrabbingControl frameGrabber = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
Buffer buf = frameGrabber.grabFrame();
// Convert frame to an buffered image so it can be processed and saved
Image img = (new BufferToImage((VideoFormat)buf.getFormat()).createImage(buf));
BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.drawImage(img, null, null);

// Save image to disk as JPG
ImageIO.write(buffImg, "jpg", new File("C:\\cam.jpg"));


player.close();
player.deallocate();

}
}



Now you have the image saved on to your hard disk at the location "c:\" with name cam.jpg.

------------------------------------------------------------------------------

Step 2: Processing the image:

By processing, i mean. Obtain the values of each pixels. Once that is done, then you can play with the pixel values and obtain fabulous results...


imageProcessor.java
-------------------

class imageProcessor extends Frame
{
Image rawImg;
int imgCols;//Number of horizontal pixels
int imgRows;//Number of rows of pixels
Image modImg;//Reference to modified image

//Inset values for the Frame
int inTop;
int inLeft;
static String theProcessingClass = "iMAGE pROCESSOR";

Static String theImgFile = "cam.jpg";

MediaTracker tracker;
int[][][] threeDPix;
int[][][] threeDPixMod;
int[] oneDPix;

ImgIntfc02 imageProcessingObject;


public static void main()
{
rawImg = Toolkit.getDefaultToolkit().getImage(theImgFile);
tracker = new MediaTracker(this);
tracker.addImage(rawImg,1);

try
{
if(!tracker.waitForID(1,10000))
{
System.out.println("Load error.");
System.exit(1);
}//end if
}
catch(InterruptedException e)
{
e.printStackTrace();
System.exit(1);
}//end catch

if((tracker.statusAll(false) & MediaTracker.ERRORED & MediaTracker.ABORTED) != 0)
{
System.out.println("Load errored or aborted");
System.exit(1);
}//end if

imgCols = rawImg.getWidth(this);
imgRows = rawImg.getHeight(this);
oneDPix = new int[imgCols * imgRows];

try
{
PixelGrabber pgObj = new PixelGrabber(rawImg,0,0,imgCols,imgRows,oneDPix,0,imgCols);
if(pgObj.grabPixels() && ((pgObj.getStatus() & ImageObserver.ALLBITS)!= 0))
{
threeDPix = convertToThreeDim(oneDPix,imgCols,imgRows);

}
//end if statement on grabPixels
else
System.out.println("Pixel grab not successful");
}
catch(InterruptedException e)
{
e.printStackTrace();
}//end catch

return 1;
} //end of run.


int[][][] convertToThreeDim(int[] oneDPix,int imgCols,int imgRows)
{
//Create the new 3D array to be populated
// with color data.
int[][][] data = new int[imgRows][imgCols][4];

for(int row = 0;row < arow =" new" col =" 0;" element =" row" col =" 0;col">> 24) & 0xFF;
//Red data
data[row][col][1] = (aRow[col] >> 16) & 0xFF;
//Green data
data[row][col][2] = (aRow[col] >> 8) & 0xFF;
//Blue data
data[row][col][3] = (aRow[col]) & 0xFF;
}//end for loop on col

}//end for loop on row
return data;
}//end convertToThreeDim

int[] convertToOneDim(int[][][] data,int imgCols,int imgRows)
{
int[] oneDPix = new int[imgCols * imgRows * 4];

for(int row = 0,cnt = 0;row < col =" 0;col">


The threeDPix array contain the pixel values. Explore the array and have fun.. Do post back your success stories..

No comments: