1
我目前正在尝试编写一个程序,其中应用用户输入确定邻域大小和文件类型的平均过滤器。我的程序符合并运行,但是我得到的输出图像与接收的输入图像无关。我不确定它的读数是否会将其丢弃或输出,或者甚至是数学,但是我的图像最终会成为注意到一个大的盐和胡椒的形象。这里是我的代码,因为现在我申请的3×3均值滤波只是为了获得它运行正常Java ImageIO平均过滤器
img = ImageIO.read(new File(fileName));
//get dimensions
maxHeight = img.getHeight();
maxWidth = img.getWidth();
//create 2D Array for new picture
int pictureFile[][] = new int [maxHeight][maxWidth];
for(int i = 0; i < maxHeight; i++){
for(int j = 0; j < maxWidth; j++){
pictureFile[i][j] = img.getRGB(j, i);
}
}
//Apply Mean Filter
for (int v=1; v<=maxHeight-2; v++) {
for (int u=1; u<=maxWidth-2; u++) {
//compute filter result for position (u,v)
int sum = 0;
for (int j=-1; j<=1; j++) {
for (int i=-1; i<=1; i++) {
int p = pictureFile[u+i][v+j];
sum = sum + p;
}
}
int q = (int) (sum/9);
pictureFile[u][v] = q;
}
}
//Turn the 2D array back into an image
BufferedImage theImage = new BufferedImage(
maxHeight,
maxWidth,
BufferedImage.TYPE_BYTE_GRAY);
int value;
for(int y = 0; y<maxHeight; y++){
for(int x = 0; x<maxWidth; x++){
value = pictureFile[y][x] ;
theImage.setRGB(x, y, value);
}
}
File outputfile = new File("saved.png");
ImageIO.write(theImage, "png", outputfile);
1)为了更好地帮助越早,张贴[SSCCE(http://sscce.org/)。 2)对于处理图像的SSCCE,热链接到一个或通过代码生成。 3)连接到几个小的“原始/转换/预期”图像将非常有帮助。 4)你有问题吗?如果是这样,现在是时候提问了。 – 2012-02-02 18:46:51