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

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)..

class captureImage
public static void main()
CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
Player player = Manager.createRealizedPlayer(deviceInfo.getLocator());
// Wait a few seconds for camera to initialise (otherwise img==null)
System.out.println("Capturing Image...");
// Grab a frame from the capture device
FrameGrabbingControl frameGrabber = (FrameGrabbingControl)player.getControl("");
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"));



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

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);

System.out.println("Load error.");
}//end if
catch(InterruptedException e)
}//end catch

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

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

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
System.out.println("Pixel grab not successful");
catch(InterruptedException e)
}//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: