2012-10-11 45 views
0

我正在执行中值滤波器的程序。所有我试图完成的是取一个像素值,找到它的相邻像素,并将它们存储在一个数组中,对该数组进行排序,并取中间值(对于大小为9的中间值数组为索引位置4)。并在与考虑中的像素完全相同的坐标处代替该值。在WritableRaster中设置值时坐标不在边界

如果我删除它的内容并只输出i,j,x,y的值,则循环正常工作。当我添加声明,其中我做neighborhoodVal.add(imageRaster.getSample(x, y, 0));我得到一个java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!错误。

从我观察到的,我发现的j值变为1时的i值变为264,其奇怪,因为我循环仅在图像内的所有像素中,外两个环路(i和j)循环通过除了那些在图像边缘的像素之外的所有像素。和内环(x & y)循环遍历[i][j]个像素的所有8个邻居。

我使用的图像有width = 265height=269。 的代码如下:

public class MedianFilter { 


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

     int i, j, x, y; 
     ArrayList<Integer> neighborhoodVal = new ArrayList<Integer>(); 
     ; 
     String imagePath = "images/assignment10/noisyShapes.jpg"; 
     BufferedImage inputImage = convertToGrayScale(ImageIO.read(new File(
       imagePath))); 
     WritableRaster imageRaster = inputImage.getRaster(); 
     BufferedImage filteredImage = new BufferedImage(inputImage.getWidth(), 
       inputImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY); 
     WritableRaster newWRaster = filteredImage.getRaster(); 



     for (i = 1; i < imageRaster.getHeight()-1; i++) { 
      for (j = 1; j < imageRaster.getWidth()-1; j++) { 
       for (x = Math.max(0, i - 1); x <= Math.min(i + 1,imageRaster.getWidth()); x++) { 
        for (y = Math.max(0, j - 1); y <= Math.min(j + 1,imageRaster.getHeight()); y++) { 

         System.out.println(i+","+j+"------->"+x+","+y); 

         neighborhoodVal.add(imageRaster.getSample(x, y, 0)); 
         Collections.sort(neighborhoodVal); 
        } 
       } 
       newWRaster.setSample(i, j, 0, (neighborhoodVal 
           .get((neighborhoodVal.size()/2) + 1))); 
       neighborhoodVal.clear(); 
      } 

     } 

     File f = new File("images/assignment10/medianFilterOutput.jpg"); 
     ImageIO.write(filteredImage, "JPG", f); 

    } 
    public static BufferedImage convertToGrayScale(BufferedImage givenImage) 
      throws IOException { 
     // TODO Auto-generated method stub 
     BufferedImage image = new BufferedImage(givenImage.getWidth(), 
       givenImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY); 
     Graphics grp = image.getGraphics(); 
     grp.drawImage(givenImage, 0, 0, null); 
     return image; 
    } 
} 

,我用这个形象:

enter image description here

如何j变成1?我在哪里错了?

+0

在内部循环中,您可能会使用“x <= Math.min(i + 1,imageRaster.getWidth())”而不是“... x <=”来搜索图像的边缘。 imageRaster.getWidth() - 1“。 – Rethunk

+0

@Rethunk我试图这样做,问题仍然存在 – md1hunox

+0

糟糕,那根本就不是。不过,我认为我发现了这个问题。 – Rethunk

回答

1

在外部循环中,您将使用“i”与.getHeight(),但在内部循环中,“x”与.getWidth()一起使用。所以它看起来像我和j的界限需要交换。这可能是一个疲倦的错字;我可以在下面的示例代码中自己做。由于外部循环已经忽略了边界像素,所以内部循环可以是“for(x = i-1; i < = i + 1; i ++”and“for(y = j - 1; y < = j + 1; j ++)“并且保证在范围内

如果我可以提出一个建议:在图像处理中,通常是逐行扫描(也就是”y“)最外层循环,然后通过X,并且还要用(X,Y)的像素坐标和(i,j),其中偏移。例如:

int r = 1; //radius, in case you want to use median over a larger kernel 

for(int y = r; y < height - r; y++) 
{ 
    for (int x = r; x < width - r; x++) 
    { 
     for(int j = y - r; j <= y + r; j++) 
     { 
     for(i = x - r; i <= x + r; i++) 
     { 
      //get all neighborhood values 
     } 
     }  

     //find median 
    } 
} 

这就是说,一旦你使用了一个公约,坚持下去意外sw应用getWidth()和getHeight()边界很容易,但是如果你总是以y,x,j,i的顺序嵌套循环,那么你会更有可能在不考虑它的情况下输入正确的边界。