/*
* This class performs Image reading and image writing
* by using ImageIO class
*
* @param img It is the Image object
* @version j2sdk1.4.0
* @author Beenish Zaidi
*/
public class ImageOps {
BufferedImage b;
public ImageOps ( int w , int h ) {
b = new BufferedImage ( w , h, BufferedImage.TYPE_INT_RGB ) ;
}
public ImageOps ( String fileName )
{
try
{
b = ImageIO.read ( new File ( fileName ) );
}
catch ( Exception e )
{
System.out.println ( "Exception occured during reading of the file: " );
}
}
public ImageOps ( BufferedImage buff ){
b = buff ;
}
public Color getColor( int i, int j )
{
return new Color( b.getRGB( i, j ) );
}
public void setGray( int i, int j, int c )
{
Color color = new Color( c, c, c );
b.setRGB( i, j , color.getRGB() );
}
public BufferedImage returnImage()
{
return b ;
}
}
BufferedImage image1 ;
int width ;
int height ;
ImageOps ip ;
int yprojections[] ;
int count = 0 ;
public YProjections ( BufferedImage bif , int w, int h) {
System.out.println("Entering Y projections");
ip = new ImageOps ( bif );
image1 = ip.returnImage();
width = w;
height = h;
System.out.println( "width: " + width );
System.out.println( "height: " + height );
yprojections = new int [ height ];
for ( int i = 0 ; i < height ; i++)
{
for ( int j = 0 ; j < width ; j++ )
{
Color color = getColor( i, j );
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
int z = ( r | g| b );
if ( z == 0 )
count++;
}
yprojections [ i ] = count ;
count = 0 ;
}
System.out.println("width"+ width );
}
public int[] returnProjections()
{
return yprojections;
}
public Color getColor( int i, int j )
{
return new Color( image1.getRGB( i, j ) );
}
}
Class Used for Drawing Projections
DrawProjections.java
/*
* This class loads a resulting image.
* This class also provides the methods
* to display the XProjections and YProjections.
*
* @param img Contains the Image to be displayed
* @param yesImage A boolean value that represents whether there is an
* image to be displayed or not
* @param YProjections[] An Interger array that contains the total number
* of black pixels with respect to the rows
* @param XProjections[] An array containing total number of black pixels
* with respect to the columns
* @param x It initializes the x-coordinate
* @param y It intializes the y coordinate
* @param h Height of the image
* @param w Width fo the Image
* @param signal Used to determine whether YProjections or XProjections
is to be plotted
* @param maximumX The maximum Value within the XProjections
*
* @version j2sdk1.4.0
* @author Beenish Zaidi */
//package com.allaya.imageprocessing;
Image img ;
int h ;
int w ;
String s ;
int length = 0 ;
int maximum = 0 ;
int projections[] ;
int x = 10 ;
int y = 10 ;
int number = 0 ;
/*
*
* The constructor that takes an Image
*
*/
public DrawProjections( )
{
s = "Now in Projections";
}
public DrawProjections( int array[], int width, int height )
{
number = 1 ;
length = array.length ;
projections = new int[ length ] ;
w = width ;
h = height ;
set( projections );
setSize( 400, 300 );
show();
}
/*
*
* This method sets an Image on the canvas
*
*/
public void set( int array[] )
{
for( int i = 0 ; i < array.length ; i++ )
{
if( maximum < array[i] )
maximum = array[i];
}
repaint();
}
/*
*
* This method paints the Canvas
*
*/
public void paint( Graphics g )
{
setBackground ( Color.black ) ;
g.setColor( Color.red ) ;
if( number == 0 )
{
g.drawString( s, 50, 50 );
}
else
{
for( int i = 0 ; i < length ; i++,y++ )
{
int p = projections[i];
g.drawLine( x , y, x + p , y );
}
}
}
}
Class for Loading the Image
LoadedImage12.java
/*
* This class loads a resulting image.
* This class also provides the methods
* to display the XProjections and YProjections.
*
* @param img Contains the Image to be displayed
* @param yesImage A boolean value that represents whether there is an
* image to be displayed or not
* @param YProjections[] An Interger array that contains the total number
* of black pixels with respect to the rows
* @param XProjections[] An array containing total number of black pixels
* with respect to the columns
* @param x It initializes the x-coordinate
* @param y It intializes the y coordinate
* @param h Height of the image
* @param w Width fo the Image
* @param signal Used to determine whether YProjections or XProjections
is to be plotted
* @param maximumX The maximum Value within the XProjections
*
* @version j2sdk1.4.0
* @author Beenish Zaidi * @date 9th Feburary 2005
*/
//package com.allaya.imageprocessing;
import java.awt.* ;
import java.awt.image.*;
public class LoadedImage12 extends Canvas
{
Image img ;
int h ;
int w ;
/*
*
* The constructor that takes an Image
*
*/
public LoadedImage12( BufferedImage i )
{
set(i);
}
/*
*
* This method sets an Image on the canvas
*
*/
public void set( BufferedImage bi )
{
Image i = ( Image ) bi ;
MediaTracker mt = new MediaTracker( this );
mt.addImage( i, 0 );
try
{
mt.waitForAll();
}
catch( InterruptedException e )
{
}
img = i ;
repaint();
public static void main ( String args[] ) {
MainDemo md = new MainDemo () ;
md.setDefaultCloseOperation ( EXIT_ON_CLOSE ) ;
}
}
GuideLines to Compile the Code
Guide Lines
1- First Compile the Class ImageOps.java
2- Then Compile the class Binarization.java
3- Thirdly Compile the class YProjection.java
4- Compile the Class DrawProjections.java
5- Compile the Class LoadedImage12.java
6- Lastly Compile the Main class called MainDemo.java