java图像处理sobel边缘检测
java image processing sobel edge detection
我在 java 程序中遇到问题,我在其中使用 sobel 运算符进行边缘检测,但是当我尝试使用该功能时,控制台显示:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 262144
at sun.awt.image.ByteInterleavedRaster.getPixels(ByteInterleavedRaster.java:1015)
at Obrazek.jButtonSobelActionPerformed(Obrazek.java:566)
代码是:
FileInputStream inFile = null;
try {
long beginTime = (new java.util.Date()).getTime();
int i, j;
double Gx[][], Gy[][], G[][];
inFile = new FileInputStream("D://lenacsmall.bmp");
BufferedImage bi = ImageIO.read(inFile);
int width = bi.getWidth();
int height = bi.getHeight();
int[] pixels = new int[width * height];
int[][] output = new int[width][height];
int[] raster = bi.getRaster().getPixels(0,0,width,height,pixels);
int counter = 0;
for(i = 0 ; i < width ; i++ )
{
for(j = 0 ; j < height ; j++ )
{
output[i][j] = pixels[counter];
counter = counter + 1;
}
}
Gx = new double[width][height];
Gy = new double[width][height];
G = new double[width][height];
for (i=0; i<width; i++) {
for (j=0; j<height; j++) {
if (i==0 || i==width-1 || j==0 || j==height-1)
Gx[i][j] = Gy[i][j] = G[i][j] = 0;
else{
Gx[i][j] = output[i+1][j-1] + 2*output[i+1][j] + output[i+1][j+1] -
output[i-1][j-1] - 2*output[i-1][j] - output[i-1][j+1];
Gy[i][j] = output[i-1][j+1] + 2*output[i][j+1] + output[i+1][j+1] -
output[i-1][j-1] - 2*output[i][j-1] - output[i+1][j-1];
G[i][j] = Math.abs(Gx[i][j]) + Math.abs(Gy[i][j]);
}
}
}
counter = 0;
for(int ii = 0 ; ii < width ; ii++ )
{
for(int jj = 0 ; jj < height ; jj++ )
{
pixels[counter] = (int) G[ii][jj];
counter = counter + 1;
}
}
BufferedImage outImg = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
outImg.getRaster().setPixels(0,0,width,height,pixels);
FileOutputStream outFile = null;
try {
outFile = new FileOutputStream("D://copyTop.bmp");
} catch (FileNotFoundException ex) {
}
try {
ImageIO.write(outImg,"BMP",outFile);
} catch (IOException ex) {
}
JFrame TheFrame = new JFrame("obrazek " + width + " " + height);
JLabel TheLabel = new JLabel(new ImageIcon(outImg));
TheFrame.getContentPane().add(TheLabel);
TheFrame.setSize(600, 600);
TheFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
TheFrame.setVisible(true);
} catch (FileNotFoundException ex) {
Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
inFile.close();
} catch (IOException ex) {
Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
}
}
我真的需要一些帮助。希望有人能解答一下post,谢谢
问候 :)
检查时在 for 循环中
if (i==0 || i==width-1 || j==0 || j==height-1)
您可能应该检查 i >= width-2 而不是 i==width-1。
比如width为10就属于if i == 9的语句。
你想捕捉 if i == 8,因为你稍后检查 [i+1],这在你的代码中将超出你的图像数组的范围,因为你可以拥有的最大值是 9(width-1)。
显然这同样适用于 j。
试试这个,使用 sobel 方法进行边缘检测,我昨天刚做过。
`
static BufferedImage inputImg,outputImg;
static int[][] pixelMatrix=new int[3][3];
public static void main(String[] args) {
try {
inputImg=ImageIO.read(new File("your input image"));
outputImg=new BufferedImage(inputImg.getWidth(),inputImg.getHeight(),TYPE_INT_RGB);
for(int i=1;i<inputImg.getWidth()-1;i++){
for(int j=1;j<inputImg.getHeight()-1;j++){
pixelMatrix[0][0]=new Color(inputImg.getRGB(i-1,j-1)).getRed();
pixelMatrix[0][1]=new Color(inputImg.getRGB(i-1,j)).getRed();
pixelMatrix[0][2]=new Color(inputImg.getRGB(i-1,j+1)).getRed();
pixelMatrix[1][0]=new Color(inputImg.getRGB(i,j-1)).getRed();
pixelMatrix[1][2]=new Color(inputImg.getRGB(i,j+1)).getRed();
pixelMatrix[2][0]=new Color(inputImg.getRGB(i+1,j-1)).getRed();
pixelMatrix[2][1]=new Color(inputImg.getRGB(i+1,j)).getRed();
pixelMatrix[2][2]=new Color(inputImg.getRGB(i+1,j+1)).getRed();
int edge=(int) convolution(pixelMatrix);
outputImg.setRGB(i,j,(edge<<16 | edge<<8 | edge));
}
}
File outputfile = new File("your output image");
ImageIO.write(outputImg,"jpg", outputfile);
} catch (IOException ex) {System.err.println("Image width:height="+inputImg.getWidth()+":"+inputImg.getHeight());}
}
public static double convolution(int[][] pixelMatrix){
int gy=(pixelMatrix[0][0]*-1)+(pixelMatrix[0][1]*-2)+(pixelMatrix[0][2]*-1)+(pixelMatrix[2][0])+(pixelMatrix[2][1]*2)+(pixelMatrix[2][2]*1);
int gx=(pixelMatrix[0][0])+(pixelMatrix[0][2]*-1)+(pixelMatrix[1][0]*2)+(pixelMatrix[1][2]*-2)+(pixelMatrix[2][0])+(pixelMatrix[2][2]*-1);
return Math.sqrt(Math.pow(gy,2)+Math.pow(gx,2));
}
`
我在 java 程序中遇到问题,我在其中使用 sobel 运算符进行边缘检测,但是当我尝试使用该功能时,控制台显示:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 262144 at sun.awt.image.ByteInterleavedRaster.getPixels(ByteInterleavedRaster.java:1015) at Obrazek.jButtonSobelActionPerformed(Obrazek.java:566)
代码是:
FileInputStream inFile = null;
try {
long beginTime = (new java.util.Date()).getTime();
int i, j;
double Gx[][], Gy[][], G[][];
inFile = new FileInputStream("D://lenacsmall.bmp");
BufferedImage bi = ImageIO.read(inFile);
int width = bi.getWidth();
int height = bi.getHeight();
int[] pixels = new int[width * height];
int[][] output = new int[width][height];
int[] raster = bi.getRaster().getPixels(0,0,width,height,pixels);
int counter = 0;
for(i = 0 ; i < width ; i++ )
{
for(j = 0 ; j < height ; j++ )
{
output[i][j] = pixels[counter];
counter = counter + 1;
}
}
Gx = new double[width][height];
Gy = new double[width][height];
G = new double[width][height];
for (i=0; i<width; i++) {
for (j=0; j<height; j++) {
if (i==0 || i==width-1 || j==0 || j==height-1)
Gx[i][j] = Gy[i][j] = G[i][j] = 0;
else{
Gx[i][j] = output[i+1][j-1] + 2*output[i+1][j] + output[i+1][j+1] -
output[i-1][j-1] - 2*output[i-1][j] - output[i-1][j+1];
Gy[i][j] = output[i-1][j+1] + 2*output[i][j+1] + output[i+1][j+1] -
output[i-1][j-1] - 2*output[i][j-1] - output[i+1][j-1];
G[i][j] = Math.abs(Gx[i][j]) + Math.abs(Gy[i][j]);
}
}
}
counter = 0;
for(int ii = 0 ; ii < width ; ii++ )
{
for(int jj = 0 ; jj < height ; jj++ )
{
pixels[counter] = (int) G[ii][jj];
counter = counter + 1;
}
}
BufferedImage outImg = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
outImg.getRaster().setPixels(0,0,width,height,pixels);
FileOutputStream outFile = null;
try {
outFile = new FileOutputStream("D://copyTop.bmp");
} catch (FileNotFoundException ex) {
}
try {
ImageIO.write(outImg,"BMP",outFile);
} catch (IOException ex) {
}
JFrame TheFrame = new JFrame("obrazek " + width + " " + height);
JLabel TheLabel = new JLabel(new ImageIcon(outImg));
TheFrame.getContentPane().add(TheLabel);
TheFrame.setSize(600, 600);
TheFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
TheFrame.setVisible(true);
} catch (FileNotFoundException ex) {
Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
inFile.close();
} catch (IOException ex) {
Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
}
}
我真的需要一些帮助。希望有人能解答一下post,谢谢
问候 :)
检查时在 for 循环中
if (i==0 || i==width-1 || j==0 || j==height-1)
您可能应该检查 i >= width-2 而不是 i==width-1。
比如width为10就属于if i == 9的语句。 你想捕捉 if i == 8,因为你稍后检查 [i+1],这在你的代码中将超出你的图像数组的范围,因为你可以拥有的最大值是 9(width-1)。
显然这同样适用于 j。
试试这个,使用 sobel 方法进行边缘检测,我昨天刚做过。
`
static BufferedImage inputImg,outputImg;
static int[][] pixelMatrix=new int[3][3];
public static void main(String[] args) {
try {
inputImg=ImageIO.read(new File("your input image"));
outputImg=new BufferedImage(inputImg.getWidth(),inputImg.getHeight(),TYPE_INT_RGB);
for(int i=1;i<inputImg.getWidth()-1;i++){
for(int j=1;j<inputImg.getHeight()-1;j++){
pixelMatrix[0][0]=new Color(inputImg.getRGB(i-1,j-1)).getRed();
pixelMatrix[0][1]=new Color(inputImg.getRGB(i-1,j)).getRed();
pixelMatrix[0][2]=new Color(inputImg.getRGB(i-1,j+1)).getRed();
pixelMatrix[1][0]=new Color(inputImg.getRGB(i,j-1)).getRed();
pixelMatrix[1][2]=new Color(inputImg.getRGB(i,j+1)).getRed();
pixelMatrix[2][0]=new Color(inputImg.getRGB(i+1,j-1)).getRed();
pixelMatrix[2][1]=new Color(inputImg.getRGB(i+1,j)).getRed();
pixelMatrix[2][2]=new Color(inputImg.getRGB(i+1,j+1)).getRed();
int edge=(int) convolution(pixelMatrix);
outputImg.setRGB(i,j,(edge<<16 | edge<<8 | edge));
}
}
File outputfile = new File("your output image");
ImageIO.write(outputImg,"jpg", outputfile);
} catch (IOException ex) {System.err.println("Image width:height="+inputImg.getWidth()+":"+inputImg.getHeight());}
}
public static double convolution(int[][] pixelMatrix){
int gy=(pixelMatrix[0][0]*-1)+(pixelMatrix[0][1]*-2)+(pixelMatrix[0][2]*-1)+(pixelMatrix[2][0])+(pixelMatrix[2][1]*2)+(pixelMatrix[2][2]*1);
int gx=(pixelMatrix[0][0])+(pixelMatrix[0][2]*-1)+(pixelMatrix[1][0]*2)+(pixelMatrix[1][2]*-2)+(pixelMatrix[2][0])+(pixelMatrix[2][2]*-1);
return Math.sqrt(Math.pow(gy,2)+Math.pow(gx,2));
}
`