代码之家  ›  专栏  ›  技术社区  ›  Scotty

数字图像处理Java直方图不工作

  •  1
  • Scotty  · 技术社区  · 7 年前

    我试图得到一些不同的直方图,以显示比较原始图像和卷积后的输出图像。它显示了图像和源直方图,但当我调用dst直方图时,它出错并且不显示。如果有人能帮忙,我们将不胜感激!

    错误代码-

    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
    

        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;
        }
    
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   martinez314    7 年前

    ArrayIndexOutOfBoundsException 可能是因为Sobel过滤器可以输出负值。很可能你的柱状图并没有预料到这一点。

    我找不到有关您正在使用的库的任何信息(iptoolkit?)但是我阅读了直方图文档,看看它是否提供了关于如何处理负值的任何线索。

        2
  •  0
  •   Scotty    7 年前

    static IntImage scale(IntImage in, int newMin, int newMax) {
        int oldMin, oldMax;
        double scaleFactor;
        int nRows = in.getRows();
        int nCols = in.getCols();
        IntImage out = new IntImage(nRows, nCols);
    
        oldMin = oldMax = in.pixels[0][0];
        for (int r = 0; r < nRows; r++)
        {
            for (int c = 0; c < nCols; c++)
            {
                if (in.pixels[r][c] < oldMin)
                {
                    oldMin = in.pixels[r][c];
                } else {
                    if (in.pixels[r][c] > oldMax)
                    {
                        oldMax = in.pixels[r][c];
    
    
                    }
                }
            }
    
        }
    

    //计算比例因子

        scaleFactor = (double) (newMax - newMin) / (double) (oldMax - oldMin);
        for (int r = 0; r < nRows; r++)
        {
            for (int c = 0; c < nCols; c++)
            {
                out.pixels[r][c] = (int) Math.round(newMin +
                        (in.pixels[r][c] - oldMin) * scaleFactor);
    
    
            }
    
    
        } //scale
    
        return out;
    
    }