The code works ? But
Hello everybody,
My project that I coded is to read an image that contains a histogram like this one
http://nomatterwhat.jeeran.com/hhist.jpg
For an original image that contains a text (lines)
http://nomatterwhat.jeeran.com/image.jpg
, then extract lines images from the original image using the histogram
Here the steps I followed to do that:
1.I first read the histogram image in an image of type Image
// Read histogram image in image of type Image
Image Histimage = Toolkit.getDefaultToolkit().createImage(bi[0].getSource());
2.then I got the histogram array from the image
// then get the array in histogram [] []
// First get the Width and Hieght of origenal histogram
int ImgW = Histimage.getWidth(this);
int ImgH = Histimage.getHeight(this);
histogram =newint[ImgW*ImgH];
// Store pixels in histogram array
PixelGrabber pg =new
PixelGrabber(Histimage, 0, 0, ImgW, ImgH,histogram, 0, ImgW);
try{
pg.grabPixels();
}
catch(InterruptedException e){
System.err.println("Grabbing Interrupted");
e.printStackTrace();
}
3.Then I convert the histogram array to two dimensional array.
// Now Convert the one-Dim histogram array
// into two-Dim array to process first column
int[][] twoDim =newint[ImgW][ImgH];
for (int saNum = 0; saNum < ImgW; ++saNum){
for (int elNum = 0; elNum < ImgH; ++elNum){
twoDim[saNum][elNum] = histogram[ImgH * saNum + elNum];
}
}
4.Then, Compute the values where image has to split by computing the centers of the zeros sequences which represent the blank areas between lines.
// Initiliaze an array list to store points where image should be splitted
List listA =new ArrayList();
listA.add(new Integer (0));
// Copmute the values where image has to split and store them
// in Arraylist.
// read first column of the histogram array
// and checks for blank areas by mark first zero then next one
// the area btwn these two marks should be blank, choose to split
// the image in the avergae value of these marks.
int lo= -3;// no zeros scanned yet
int hi;// the second index
int c= 0;// number of zero sequence scanned
for (hi= 0; hi < ImgH; hi++)
if ( twoDim[hi][100] == -1){// we're scanning zeros
if (lo < 0)// this is the first zero we've scanned
lo= hi;
}
elseif (lo>= 0){// we're not scanning zeros anymore
c = process(listA, c, lo, hi);//process(centers, c, lo, hi);
lo= -3;// register the fact
}
// after the loop (all zeros at the end of the array?):
if (lo >= 0)
c = process(listA, c, lo, hi);//process(centers, c, lo, hi);
5.finally, save the subimages (lines) using the values stored in two dimensional array and the original image
//get the Width and Hieght of origenal histogram
int OImgW = bi[2].getWidth(this);
int OImgH = bi[2].getHeight(this);
// Save subimages
for (int i = 0; i < listA.size(); i++){
Integer intWrapper = (Integer)listA.get(i);
int intPrimitive = intWrapper.intValue() ;
saveImageToFile(bi[2].getSubimage(0,intPrimitive, OImgW, (intPrimitive-1)),"Line.jpg");
}
The code works but ...
1. Saving subimages step - which is the last step - gives me the following error :
java.awt.image.RasterFormatException: (y + height) is outside raster
at sun.awt.image.IntegerInterleavedRaster.createWritableChild(IntegerInt
erleavedRaster.java:453)
at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1060)
at ImageOps.get_horz_Hist(ImageOps.java:166)
at SearchEngine.actionPerformed(SearchEngine.java:646)
at java.awt.MenuItem.processActionEvent(MenuItem.java:589)
at java.awt.MenuItem.processEvent(MenuItem.java:548)
at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:285)
at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:273)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:458)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
2. Further, I dont know how to let the file name change in every iterartion.
saveImageToFile(bi[2].getSubimage(0,intPrimitive, OImgW, (intPrimitive-1)),"Line.jpg");
Thanks in advance for any help .

