数字图像处理 Java 直方图不起作用
Digital Image Processing Java Histogram not working
我正在尝试获取一些不同的直方图来比较原始图像和卷积后的输出图像。它显示图像和源直方图,但是当我调用 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;
}
}
您的 ArrayIndexOutOfBoundsException
可能是由于您的 Sobel 滤波器可以输出负值。您的直方图很可能没有预料到这一点。
我找不到有关您正在使用的库(iptoolkit?)的任何信息,但我会阅读直方图文档以了解它是否提供了有关如何处理负值的任何线索。
不得不缩小图像,使用这种方法来做到这一点。感谢威士忌蜘蛛的帮助!
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;
}
我正在尝试获取一些不同的直方图来比较原始图像和卷积后的输出图像。它显示图像和源直方图,但是当我调用 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;
}
}
您的 ArrayIndexOutOfBoundsException
可能是由于您的 Sobel 滤波器可以输出负值。您的直方图很可能没有预料到这一点。
我找不到有关您正在使用的库(iptoolkit?)的任何信息,但我会阅读直方图文档以了解它是否提供了有关如何处理负值的任何线索。
不得不缩小图像,使用这种方法来做到这一点。感谢威士忌蜘蛛的帮助!
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;
}