本文共 1142 字,大约阅读时间需要 3 分钟。
我正在开发一个有关图像处理的项目,在该项目中,我必须填充破裂绘画的数字化图像。我必须将彩色图像转换为灰度图像,对灰度图像的2D数组执行一些计算,然后将其写回灰度图像。此代码是:
BufferedImage colorImage=ImageIO.read(new File(strImagePath));
BufferedImage image = new BufferedImage(colorImage.getWidth(),colorImage.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(colorImage, 0, 0, null);
g.dispose();
ImageIO.write(image,"PNG",new File("Image.PNG"));
BufferedImage imgOriginal=ImageIO.read(new File("Image.PNG"));
int width=image.getWidth();
int height=image.getHeight();
BufferedImage im=new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
int arrOriginal[][]=new int[height][width];
for(int i=0;i
for(int j=0;j
arrOriginal[i][j]=imgOriginal.getRGB(j,i)& 0xFF;
for(int i=0;i
for(int j=0;j
im.setRGB(j,i,arrOriginal[i][j]);
ImageIO.write(im,"PNG",new File("Image1.PNG"));
但是输出的图像要暗得多,我没有找回原始图像(我还没有做任何更改)。
我认为setRGB()语句中应该有一些更改,但我不知道是什么。
为了写回图像,我也尝试过:
`
BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = im.getRaster();
for(int i=0;i
for(int j=0;j
raster.setSample(j,i,0,arrOriginal[i][j]);
`
但这也不会给我原始图像。
谁能提供我这个问题的解决方案?提前致谢。
转载地址:https://blog.csdn.net/weixin_33303537/article/details/114917386 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!