数字图像处理Java直方图不工作
我试图得到一些不同的直方图,以显示比较原始图像和卷积后的输出图像。它显示图像和源直方图,但当我调用dst直方图时,它会出错并且不显示。如果有人能帮忙,我们将不胜感激 错误代码-数字图像处理Java直方图不工作,java,imaging,Java,Imaging,我试图得到一些不同的直方图,以显示比较原始图像和卷积后的输出图像。它显示图像和源直方图,但当我调用dst直方图时,它会出错并且不显示。如果有人能帮忙,我们将不胜感激 错误代码- Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 853 at iptoolkit.Histogram.<init>(Histogram.java:15) at assignment2.main(assig
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
线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:853
直方图(Histogram.java:15)
位于assignment2.main(assignment2.java:82)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
进程已完成,退出代码为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;
}
}
publicstaticvoidmain(字符串[]args)引发异常
{
主窗口mw=新的主窗口();
mw.println(“测试…”);
亲密src=new-亲密(“C:\\Users\\scott\u 000\\Documents\\Digital Imaging\\Digital Imaging\\Images\\Baboon.bmp”);//源图像的目标
int nRows=src.getRows();//计算图像和列的行
int nCols=src.getCols();
通知dst=新通知(nRows,nCols);//设置目标映像(输出映像)
亲密dst1=新亲密(nRows,nCols);
src.displayImage(400300);//显示源图像(输入)
int[][]掩码=新int[][{{-1,-2,-1},{0,0,0},{1,2,1};//掩码输入(sobel掩码按从上到下的顺序排列
int[][]平均掩码=新int[3][3];
均值掩码[0][0]=1;
均值掩码[0][1]=1;
均值掩码[0][2]=1;
平均掩码[1][0]=1;//1
平均掩码[1][1]=1;//1
平均掩码[1][2]=1;//1
平均掩码[2][0]=1;
平均掩码[2][1]=1;
平均掩码[2][2]=1;
int[]mask5x5=新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
mask5x5[2][3]=1;//1
mask5x5[2][4]=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;
卷积(src,mask5x5,dst);//调用卷积方法,带有输入图像(src)、模板(mask)和输出图像(dst)
dst.setScaling(true);//将图像缩放到255
dst.displayImage();//显示输出图像
卷积(src,meansmask,dst1);//调用卷积方法,带有输入图像(src)、模板(mask)和输出图像(dst)
dst1.setScaling(true);//将图像缩放到255
dst1.displayImage();//显示输出图像
直方图h=新直方图(src);
暗示形象;
histImage=h.makeImage();//设置和显示直方图
histImage.displayImage();
直方图y=新直方图(dst1);
暗示历史意象1;
histImage1=y.makeImage();//设置和显示直方图
histImage1.displayImage();
}
静态内联卷积(内联输入,int[]模板,内联输出)//要设置的参数
{
int nRows=in.getRows();//找出有多少行和列
int nCols=in.getCols();
int nMaskRows=template.length;//设置行和列的长度
int nMaskCols=模板[0]。长度;
int rBoarder=nMaskRows/2;//计算图像的边框
int cBoarder=nMaskCols/2;
int sum;//用于计算
对于(int r=0;r<(nRows-nMaskRows+1);r++)//从第一行(左上)开始,向右工作,行数-掩码行(无论掩码是什么)
{
对于(int c=0;c<(nCols-nMaskCols+1);c++)//同上
{
sum=0;//将sum声明为0
对于(int-mr=0;mr
您的ArrayIndexOutOfBoundsException
可能是因为您的Sobel筛选器可以输出负值。很可能您的直方图没有预料到这一点
我找不到有关您正在使用的库的任何信息(iptoolkit?)但我会阅读直方图文档,看看它是否提供了有关如何处理负值的任何线索。您的
ArrayIndexOutOfBoundsException
可能是因为您的Sobel筛选器可以输出负值。很可能您的直方图没有预料到这一点
我找不到有关您正在使用的库(iptoolkit?)的任何信息,但我会阅读直方图文档,看看它是否提供了有关如何处理负值的任何线索。必须缩小图像的比例,使用此方法。感谢Whiskyspider的帮助
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];
}
}
}
}
静态亲密度(亲密度,int-newMin,int-newMax){
int oldMin,oldMax;
双尺度因子;
int nRows=in.getRows();
int nCols=in.getCo
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;
}