[英]Write pixel data in a grayscale image in java
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).但是输出图像要暗得多,我没有取回原始图像(我还没有做任何更改)。
I think there should be some changes in setRGB() statement but I don't know what.我认为 setRGB() 语句应该有一些变化,但我不知道是什么。
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.
提前致谢。
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.我对基于 Java 的图像处理一无所知,但我确实对图像处理有很多了解,所以我会看看我是否可以给你任何想法。 Please don't shoot me if I am wrong - I am just suggesting an idea.
如果我错了,请不要向我开枪 - 我只是提出一个想法。
In a greyscale image, the red, green and blue values are all the same, ie 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.
因此,当您调用 getRGB 并使用 0xff 进行 AND 运算时,您可能只获得蓝色分量,但这没关系,因为红色和绿色是相同的 - 因为它是灰度的。
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.这样您不仅可以设置蓝色,还可以设置红色和绿色组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.