Search code examples
javaimaging

Digital Image Processing Java Histogram not working


I am trying to get a few different histograms to show up to compare the original image and the output image after convolving it. It shows the image and the source histogram but when i call the dst histogram, it errors and does not display. If anyone could help, it would be much appreciated!

Error code -

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 853
    at iptoolkit.Histogram.<init>(Histogram.java:15)
    at assignment2.main(assignment2.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 0

Code -

    public static void main(String[] args) throws Exception 
    {

        MainWindow mw = new MainWindow();
        mw.println("Testing...");

        IntImage src = new IntImage("C:\\Users\\scott_000\\Documents\\Digital Imaging\\Digital Imaging\\Images\\Baboon.bmp"); //destination for source image
        int nRows = src.getRows(); //calculating the rows of the images and columns
        int nCols = src.getCols();
        IntImage dst = new IntImage(nRows, nCols); //setting destination image(output image)
        IntImage dst1 = new IntImage(nRows, nCols);


        src.displayImage(400, 300); //displaying the source image (input)

        int [][] mask = new int[][] { {-1, -2, -1}, {0, 0, 0}, {1, 2, 1} }; //mask imput (sobel masks in order from top to bottom

        int [][] meanMask = new int[3][3];
        meanMask[0][0] = 1;
        meanMask[0][1] = 1;
        meanMask[0][2] = 1;
        meanMask[1][0] = 1;   // 1 1 1
        meanMask[1][1] = 1;   // 1 1 1
        meanMask[1][2] = 1;   // 1 1 1
        meanMask[2][0] = 1;
        meanMask[2][1] = 1;
        meanMask[2][2] = 1;

        int [][] mask5x5 = new int[5][5];
        mask5x5[0][0] = 1;
        mask5x5[0][1] = 1;
        mask5x5[0][2] = 1;
        mask5x5[0][3] = 1;
        mask5x5[0][4] = 1;
        mask5x5[1][0] = 1;
        mask5x5[1][1] = 1;
        mask5x5[1][2] = 1;
        mask5x5[1][3] = 1;
        mask5x5[1][4] = 1;
        mask5x5[2][0] = 1;
        mask5x5[2][1] = 1;
        mask5x5[2][2] = 1;   // 1 1 1 1 1
        mask5x5[2][3] = 1;   // 1 1 1 1 1
        mask5x5[2][4] = 1;   // 1 1 1 1 1
        mask5x5[3][0] = 1;
        mask5x5[3][1] = 1;
        mask5x5[3][2] = 1;
        mask5x5[3][3] = 1;
        mask5x5[3][4] = 1;
        mask5x5[4][0] = 1;
        mask5x5[4][1] = 1;
        mask5x5[4][2] = 1;
        mask5x5[4][3] = 1;
        mask5x5[4][4] = 1;


        convolve(src, mask5x5, dst); //calling convolve method, with input image(src), template(mask) and output image(dst)
        dst.setScaling(true); //scales the image down to 255
        dst.displayImage(); //display the output image

        convolve(src, meanMask, dst1); //calling convolve method, with input image(src), template(mask) and output image(dst)
        dst1.setScaling(true); //scales the image down to 255
        dst1.displayImage(); //display the output image

        Histogram h = new Histogram(src);
        IntImage histImage;
        histImage = h.makeImage(); //setting and displaying histogram
        histImage.displayImage();

        Histogram y = new Histogram(dst1);
        IntImage histImage1;
        histImage1 = y.makeImage(); //setting and displaying histogram
        histImage1.displayImage();





    }


    static IntImage convolve(IntImage in, int[][] template, IntImage out) //parameters to be set
    {
        int nRows = in.getRows(); //find out how many of rows there are and cols
        int nCols = in.getCols();

        int nMaskRows = template.length; //set length of rows and cols
        int nMaskCols = template[0].length;
        int rBoarder = nMaskRows / 2; //calculation for the border of the image
        int cBoarder = nMaskCols / 2;
        int sum; //used for the calculation

        for (int r = 0; r < (nRows - nMaskRows + 1); r++) //start at the first row(top left) and work to the right, number of rows - mask rows(whatever the mask is)
        {
            for (int c = 0; c < (nCols - nMaskCols + 1); c++) //same as above
            {
                sum = 0; //declaring the sum as 0
                for (int mr = 0; mr < nMaskRows; mr++)
                {
                    for (int mc = 0; mc < nMaskCols; mc++)
                    {
                        sum += in.pixels[r + mr][c + mc] * template[mr][mc]; //change this for calculating the edge preserving smoothing (mean, median etc)
                    }
                }
                out.pixels[r + rBoarder][c + cBoarder] = sum;
            }
        }

        return out;
    }

}

Solution

  • Your ArrayIndexOutOfBoundsException is probably being caused by the fact that your Sobel filter can output negative values. Most likely your Histogram is not expecting that.

    I can't find any information about the library you're using (iptoolkit?) but I'd read the Histogram documentation to see it if gives any clues about how to handle negative values.