I am working on a project that transposes sheet music. So far I can load in
the image and scan the image to determine whether black (the music notation,
staff, clef
and notes etc.) and white (the background) is present. I do this using PixelGrabber.

I now need to scan the image, one pixel at a time, to determine where the
music starts (black) and whether or not the pixels build up into recognised
music notation.
This data is then stored in an array, to reproduce onto a new image canvas.

To start with i want to determine whether a staff is present, (five black
lines and four blocks of white background) and I will go from there. The
pixels which make up
the lines may be off centre or off line as the image is scanned using OCR,
so this has to be taken into account.

Has anybody got any ideas cos i'm at a loss.

Here is the code for reading black in an image

// the threshold is set high as the black in the scanned image is not true
black

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

public class ReadBlack {

public static void main(String args[]) { // main method
new ReadBlackFrame();
}
}

class ReadBlackFrame extends Frame {

/* variable declarations */
Image img;
MediaTracker mt;
boolean error = false;
int iw, ih;
int pixels [];
final int blackThreshold = 0xffbbbbbb;

ReadBlackFrame() {
super();

/* Get the image */
img = Toolkit.getDefaultToolkit().
getImage("Images/staff.gif");
MediaTracker mt = new MediaTracker(this);
mt.addImage(img,0);
try{mt.waitForID(0);}
catch(InterruptedException ie){}

/* Add the window listener */
addWindowListener(new WindowAdapter() {
public void windowClosing
(WindowEvent evt) {
dispose();
System.exit(0);
}
}
);

/* Size the frame */
setSize(50,110);

/* Name the frame */
setTitle("Read Black");

/* Center the frame */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width -
frameDim.width) / 2,
(screenDim.height -
frameDim.height) / 2);

/* Show the frame */
setVisible(true);

/*Grab pixels */
try {
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) { };

/* check for black */
int sz = iw * ih;

for (int i=0; i<sz; i++) {
int p = pixels;
int a = 0xff & (p >> 24);
int r = 0xff & (p >> 16);
int g = 0xff & (p >> 8);
int b = 0xff & (p);

if (p < blackThreshold) {
System.out.println("black is present");
} // end of if loop
} // end of for loop

System.out.println ( "size :" + sz ) ;
}

/* Draw the image - if error in loading image
- display image not found */
public void paint(Graphics g) {
if(error) { // checks for error
Dimension d = getSize();
g.setColor(Color.red); // draws error message
g.fillRect(0 ,0 ,d.width, d.height);
g.setColor(Color.black);
g.drawString("Image not found",
15, d.height/2);
}
else if (img != null) {
// draws image checked against MediaTracker
g.drawImage(img, 5, 30, this);
}
}

public void update(Graphics g) {
paint(g);
}

/* method for checking image has loaded using ImageObserver
*/
public boolean imageUpdate(Image img, int flags,
int x, int y, int w, int h) {
if ((flags & ALLBITS) != 0) {
repaint();
}
else if ((flags & (ABORT|ERROR)) !=0) {
error = true; //file not found
repaint();
}
return (flags & (ALLBITS|ABORT|ERROR)) ==0;
}
}