Results 1 to 2 of 2
  1. #1
    Mazharul is offline Member
    Join Date
    Aug 2008
    Posts
    12
    Rep Power
    0

    Default error reported when binarization of gray image

    Reproted error:

    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)


    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






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

  2. #2
    SubOptimal is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default

    Hi,

    you use the wrong codomain for the x coordinate. See the lines highlighted in red.

    Quote Originally Posted by Mazharul View Post
    WritableRaster input = inputImage.copyData(null);
    WritableRaster output = outputImage.getRaster();
    final int threshold = 128;
    int value, error;

    for (int y = 0; y < h; ++y)

    // the loop for x coordinate must start with 1
    // for (int x = 0; x < w; ++x) {

    for (int x = 1; x < w; ++x) {

    ...

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

    // otherwise it fail here, because you want to access the x coordinate -1
    // in case the value of x is 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));
    SubOptimal

Similar Threads

  1. Replies: 5
    Last Post: 08-29-2008, 05:59 PM
  2. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 05:28 PM
  3. Getting warnings reported by Connection object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 09:57 AM
  4. Loading An Image Help Please!
    By shaungoater in forum Java 2D
    Replies: 2
    Last Post: 01-09-2008, 09:14 AM
  5. Replies: 0
    Last Post: 01-08-2008, 06:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •