ImageOps.java

/* * 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 */


import javax.imageio.ImageIO ;
import java.awt.image.BufferedImage ;
import java.io.File ;
import java.awt.image.*;
import java.io.IOException ;
import java.awt.Image ;
import javax.swing.*;
import java.awt.Color ;


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


Image Binarization Class

Binarization.java




import java.awt.* ;
import java.awt.image.* ;
import java.awt.Color ;


public class Binarization
{
BufferedImage img, img1 ;
ImageOps ip ;
int width ;
int height ;

public Binarization( String name )
{
ip = new ImageOps( name );
img1 = ip.returnImage();
width = img1.getWidth( null );
height = img1.getHeight( null );
img = new BufferedImage( width , height , BufferedImage.TYPE_INT_RGB );
for( int i = 0 ; i < width ; i++ )
{
for( int j = 0 ; j < height ; j++ )
{
Color color = ip.getColor( i , j );

int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();

int z = r | g | b ;

if( z > 70 )
{
Color color1 = new Color( 255, 255, 255 );
img.setRGB( i, j , color1.getRGB() );
}
else if( z < 70 )
{
Color color2 = new Color( 0 , 0 , 0 );
img.setRGB( i, j , color2.getRGB() );
}
}
}
}


public BufferedImage getImage()
{
return img ;
}
}




Class for Handling Projections

YProjections.java




/*
* This class Calculats YPorjections of an image
*
*/


import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.image.*;


public class YProjections
{

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;


import java.awt.* ;
import java.awt.image.*;
import javax.swing.* ;


public class DrawProjections extends JFrame
{

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

}
/*
*
* This method paints the Canvas
*
*/

public void paint( Graphics g )
{


setBackground ( Color.black ) ;
g.setColor( Color.red ) ;


/*
* If there is no iamge to be displayed
*/

if( img == null )
{

g.drawString( "No image", 10, 30 );

}

/*
* If there is image to be displayed
* then paint it
*/
else
{

g.drawImage(img, 0, 0, this );

}
}
/*
* This method returns the Dimension
*/

public Dimension getPreferredSize()
{

return new Dimension( img.getWidth( null) , img.getHeight ( null ));

}

public Dimension getMinimumSize()
{

return getPreferredSize();

}

}

The main handling class

MainDemo.java




/*
* Main Demo of Optical Music Recognition System
* @author Beenish Zaidi
* @date 9th February 2005
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;

public class MainDemo extends JFrame
implements ActionListener {

ImageOps ip1 ;
Binarization bin ;
BufferedImage image ;
LoadedImage12 lim ;
DrawProjections dp ;
YProjections yp ;
final JDesktopPane theDesktop ;
JInternalFrame frame ;

JMenu file ;
JMenu ipop ;

JMenuItem open ;
JMenuItem save ;
JMenuItem exit ;
JMenuItem yproj ;
JMenuItem binarize;

String name;
int width, height ;

BufferedImage b2 ;

JMenuBar bar ;

public MainDemo(){

super ( "Optical Music Recognition System" );

bar = new JMenuBar();
setJMenuBar ( bar ) ;

// Create a file Menu

file = new JMenu ( "File" );
file.setMnemonic ( 'F' );

// Items to be added in File menu

open = new JMenuItem ( "Open") ;
open.addActionListener ( this ) ;

save = new JMenuItem ( "Save") ;
save.addActionListener ( this ) ;


exit = new JMenuItem ( "Exit") ;
exit.addActionListener ( this ) ;

// Add the item to the file menu

file.add ( open ) ;
file.add ( save ) ;
file.add ( exit ) ;

ipop = new JMenu ( "Image Processing Operations" );

yproj = new JMenuItem ( "Y-Projections" );
yproj.addActionListener ( this );

binarize = new JMenuItem ( "Binarize");
ipop.add ( yproj ) ;
ipop.add ( binarize ) ;
bar.add ( file ) ;
bar.add ( ipop ) ;

theDesktop = new JDesktopPane();
getContentPane().add ( theDesktop );

setSize( 600 , 500 ) ;
show() ;

}

public void actionPerformed ( ActionEvent e ){

if ( e.getSource() == open ) {

// Open The file.

name = "1stave.png" ;

frame = new JInternalFrame ( "Loaded Image" ,true, true, true, true );

Container c = frame.getContentPane();

ip1 = new ImageOps( name ) ;

image = ip1.returnImage() ;

width = image.getWidth( null );
height = image.getHeight( null );

lim = new LoadedImage12 ( image ) ;

JPanel panel = new JPanel() ;
panel.add ( lim ) ;

c.add ( panel , BorderLayout.CENTER ) ;

frame.setSize ( image.getWidth(null) , image.getHeight (null) ) ;

theDesktop.add ( frame );


}

else if ( e.getSource() == save ) {

ip1.save( "amna.png" );


}

else if ( e.getSource() == exit ) {

System.exit( 0 ) ;

}

else if( e.getSource() == yproj ) {

bin = new Binarization( name );
b2 = bin.getImage();

yp = new YProjections ( b2 );

int projections[] = new int[ height ];

projections = yp.returnProjections();
dp = new DrawProjections ( projections ,width ,
height );
frame = new JInternalFrame ( "Loaded Image" ,true, true, true, true );

Container c = frame.getContentPane();

JPanel panel = new JPanel() ;
panel.add ( dp ) ;

c.add ( panel , BorderLayout.CENTER ) ;

frame.setSize ( image.getWidth(null) , image.getHeight (null) ) ;

theDesktop.add ( frame );

}
}

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


Hosted by www.Geocities.ws

1