Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-29-2008, 02:00 PM
Member
 
Join Date: Aug 2008
Posts: 9
Mazharul is on a distinguished road
Binarizarion of gray image/ArrayIndexOutOfBound error reported
Hi.. …. all
When creating binary image of a gray image by dithering by Floyd –steinberg error diffusion method which is depicted in the function “processImage(BufferedImage inputImage)” an error is reported like: Exception in thread “main”java.lang.ArrayIndexOutOfBound………[/b]

Program is compiled by Jcreator..............

Or can you suggest me something different ………….method implemented by Java.......

Secondly I want to write in file row-by-row akin to image i.e file cursor will be next line after writing 1st row pixels values of image

Code:
Function: which create binary image according to dithering by Floyd –steinberg error diffusion method public static BufferedImage processImage(BufferedImage inputImage) { // Create a binary image for the results of processing int w = inputImage.getWidth()-1; int h = inputImage.getHeight()-1; BufferedImage outputImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY); // Work on a copy of input image because it is modified by diffusion WritableRaster input = inputImage.copyData(null); WritableRaster output = outputImage.getRaster(); final int threshold = 128; int value, error; for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) { value = input.getSample(x, y, 0); // Threshold value and compute error if (value < threshold) { output.setSample(x, y, 0, 0); error = value; } else { output.setSample(x, y, 0, 1); error = value - 255; } // Spread error amongst neighbouring pixels value = input.getSample(x+1, y, 0); input.setSample(x+1, y, 0, clamp(value + 0.4375f * error)); value = input.getSample(x-1, y+1, 0); input.setSample(x-1, y+1, 0, clamp(value + 0.1875f * error)); value = input.getSample(x, y+1, 0); input.setSample(x, y+1, 0, clamp(value + 0.3125f * error)); value = input.getSample(x+1, y+1, 0); input.setSample(x+1, y+1, 0, clamp(value + 0.0625f * error)); } return outputImage; } // Forces a value to a 0-255 integer range public static int clamp(float value) { return Math.min(Math.max(Math.round(value), 0), 255); } Main Program Code: import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; public class BufferConversion { private JScrollPane getContent(BufferedImage image) { BufferedImage img=processImage(image); int[][] data = extractData(img); String path = "bufferConversion.txt"; writeToFile(data, path); JLabel label = new JLabel(new ImageIcon(image), JLabel.CENTER); return new JScrollPane(label); } public static BufferedImage processImage(BufferedImage inputImage) { // Create a binary image for the results of processing int w = inputImage.getWidth()-1; int h = inputImage.getHeight()-1; BufferedImage outputImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY); // Work on a copy of input image because it is modified by diffusion WritableRaster input = inputImage.copyData(null); WritableRaster output = outputImage.getRaster(); final int threshold = 128; int value, error; for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) { value = input.getSample(x, y, 0); // Threshold value and compute error if (value < threshold) { output.setSample(x, y, 0, 0); error = value; } else { output.setSample(x, y, 0, 1); error = value - 255; } // Spread error amongst neighbouring pixels value = input.getSample(x+1, y, 0); input.setSample(x+1, y, 0, clamp(value + 0.4375f * error)); value = input.getSample(x-1, y+1, 0); input.setSample(x-1, y+1, 0, clamp(value + 0.1875f * error)); value = input.getSample(x, y+1, 0); input.setSample(x, y+1, 0, clamp(value + 0.3125f * error)); value = input.getSample(x+1, y+1, 0); input.setSample(x+1, y+1, 0, clamp(value + 0.0625f * error)); } return outputImage; } // Forces a value to a 0-255 integer range public static int clamp(float value) { return Math.min(Math.max(Math.round(value), 0), 255); } private int[][] extractData(BufferedImage image) { int w = image.getWidth(); int h = image.getHeight(); System.out.printf("w = %d h = %d%n", w, h); WritableRaster raster = image.getRaster(); int[][] data = new int[h][w]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { data[y][x] = raster.getSample(x, y, 0); } } return data; } private void writeToFile(int[][] data, String path) { int h = data.length; int w = data[0].length; int[] array = new int[h*w]; for(int y = 0; y < h; y++) { for(int x = 0; x < w; x++) { int index = y*w + x; array[index] = data[y][x]; } } try { File file = new File(path); BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(file))); for(int i = 0; i < array.length; i++) { String s = String.valueOf(array[i]) + " "; bw.write(s, 0, s.length()); } bw.close(); } catch(IOException e) { System.out.println("write error: " + e.getMessage()); } } public static void main(String[] args) throws IOException { String path = "C:/shobuj.jpg"; BufferedImage image = ImageIO.read(new File(path)); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new BufferConversion().getContent(image)); f.setSize(500,400); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-29-2008, 04:02 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
an error is reported like ...
If you don't post the full text of the message, how is anyone to know what it is? Need FULL TEXT OF MESSAGE! ITS VERY IMPORTANT!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-29-2008, 04:20 PM
Member
 
Join Date: Aug 2008
Posts: 9
Mazharul is on a distinguished road
Sir
error reported:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getSample(Unkn own Source)
at BufferConversion.processImage(BufferConversion.jav a:55)
at BufferConversion.getContent(BufferConversion.java: 10)
at BufferConversion.main(BufferConversion.java:125)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-29-2008, 04:34 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
There are many getSample() calls in your program. Which one is at line 55?
Did You left off the index value from the error message?
What are the values of the first 2 args in the call to getSample() at line 55?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-29-2008, 04:59 PM
Member
 
Join Date: Aug 2008
Posts: 9
Mazharul is on a distinguished road
// Spread error amongst neighbouring pixels

value = input.getSample(x+1, y, 0);
input.setSample(x+1, y, 0, clamp(value + 0.4375f * error));
//Error is here
value = input.getSample(x-1, y+1, 0);
input.setSample(x-1, y+1, 0, clamp(value + 0.1875f * error));
value = input.getSample(x, y+1, 0);
input.setSample(x, y+1, 0, clamp(value + 0.3125f * error));
value = input.getSample(x+1, y+1, 0);
input.setSample(x+1, y+1, 0, clamp(value + 0.0625f * error));

}
sir...
values of the first 2 args in the call to getSample() at line 55 is pixel values which is situated at west-south conrner of (x,y) co-ordinate.

sir why error is not reported due to code fragment which is above of previous segment
WritableRaster input = inputImage.copyData(null);
WritableRaster output = outputImage.getRaster();
final int threshold = 128;
int value, error;

for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
//it is not reporting error
value = input.getSample(x, y, 0);
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-29-2008, 06:59 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Add some println()s to show the values of x and y and also the values of h and w.
What was the index value when it was out of bounds? Was it in the errror message?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
error 530 error authentication required rgale JavaServer Pages (JSP) and JSTL 0 05-12-2008 06:28 PM
Add an image to JFrame Eranga AWT / Swing 2 03-14-2008 10:24 AM
Getting warnings reported by Connection object Java Tip Java Tips 0 01-20-2008 10:57 AM
Loading An Image Help Please! shaungoater Java 2D 2 01-09-2008 10:14 AM
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 07:29 PM


All times are GMT +3. The time now is 12:19 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org