问题描述
我正在开发一个图像处理项目,在该项目中,我必须填充破裂绘画的数字化图像.我必须将彩色图像转换为灰度图像,对灰度图像的2D数组执行一些计算,然后将其写回灰度图像.的代码是:
I am developing a project on image processing where I have to fill the digitized images of cracked paintings. I have to convert a color image to grayscale, performing some calculations on the 2D Array of the gray image and writing it back as gray image. The code for this is:
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<height;i++)
for(int j=0;j<width;j++)
arrOriginal[i][j]=imgOriginal.getRGB(j,i)& 0xFF;
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
im.setRGB(j,i,arrOriginal[i][j]);
ImageIO.write(im,"PNG",new File("Image1.PNG"));
但是输出的图像要暗得多,我没有找回原始图像(我还没有做任何更改).
But the output image is very much darker, I am not getting the original image back (I have not done any changes yet).
我认为setRGB()语句中应该有一些更改,但是我不知道是什么.
I think there should be some changes in setRGB() statement but I don't know what.
要写回图像,我也尝试过:
To write image back, I have also tried:
`
BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = im.getRaster();
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
raster.setSample(j,i,0,arrOriginal[i][j]);
`
但是它也不会给我原始图像.
But it also don't give me original image back.
任何人都可以为我提供此问题的解决方案吗?预先感谢.
Can anyone provide me the solution of this problem?Thanks in advance.
推荐答案
我对基于Java的图像处理一无所知,但是总体上我对图像处理了解很多,所以我看看是否可以给你任何想法.如果我错了,请不要射击我-我只是在提出一个主意.
I don't know anything about Java-based image processing, but I do know quite a lot about image processing in general, so I will see if I can give you any ideas. Please don't shoot me if I am wrong - I am just suggesting an idea.
在灰度图像中,红色,绿色和蓝色值都相同,即Red = Green = Blue.因此,当您调用getRGB并使用0xff进行与"运算时,您可能只得到蓝色分量,但这没关系,因为红色和绿色是相同的-因为它是灰度的.
In a greyscale image, the red, green and blue values are all the same, i.e. Red=Green=Blue. So, when you call getRGB and do the AND with 0xff, you are probably getting the blue component only, but that is ok as the red and green are the same - because it's greyscale.
我怀疑问题在于,当您将其写回以创建新的输出图像时,您仅设置了蓝色分量,而不是红色和绿色,它们应该仍然相同.尝试写回
I suspect the problem is that when you write it back to create your new output image, you are only setting the blue component and not the red and green - which should still be the same. Try writing back
original pixel + (original pixel << 8 ) + (original pixel <<16)
这样,您不仅可以设置蓝色组件,还可以设置红色和绿色组件.
so that you set not only the Blue, but also the Red and Green components.
这篇关于用Java在灰度图像中写入像素数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!