基于關于影像處理中的內核的維基百科頁面,我正在使用卷積矩陣進行圖片過濾的一些基本實驗。
為了計算 RGB 轉換,我通過 a 讀取位圖,BufferedImage然后使用getRgb(). 在測驗最簡單的身份過濾器時,我注意到對于特定圖片,我得到了一些灰色而不是原來的黑色,而對于其他一些圖片,黑色還可以。
經過更多測驗,我發現沒有任何轉換,一個簡單BufferedImage -> int[] -> BufferedImage的結果是灰色的結果。
我錯過了什么?ImageMagickidentify顯示兩者都是沒有 alpha 通道的 8 位 256 色圖片。
betty1.png PNG 339x600 339x600 0 0 8-bit Gray 256c 24526B 0.000u 0:00.000
betty2.jpg JPEG 603x797 603x797 0 0 8-bit Gray 256c 126773B 0.000u 0:00.001
有了這張圖片,結果與預期的一樣。
![BufferedImage -> int[] -> BufferedImage 結果為灰黑色](https://i.stack.imgur.com/GP1Xw.png)
有了這個,結果出乎意料地變灰了。
![BufferedImage -> int[] -> BufferedImage 結果為灰黑色](https://i.stack.imgur.com/4fzsh.png)
這是一個簡單的 sscce 測驗類來顯示問題:
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
/* simple test class for convolution matrix */
public class CopyPic {
public static void main(String args[]) throws FileNotFoundException, IOException {
if (args.length < 1) {
System.err.println("Usage: CopyPic <picture_file>");
System.exit(1);
}
String imgPath = args[0];
String inputName = imgPath.substring(0, imgPath.lastIndexOf("."));
File ifile = new File(imgPath);
InputStream fis_in = new FileInputStream(ifile);
BufferedImage bi_in = ImageIO.read(fis_in);
fis_in.close();
int width = bi_in.getWidth();
int height = bi_in.getHeight();
System.out.println(String.format("%s = %d x %d", imgPath, width, height));
int[] rgb_in = new int[width * height];
bi_in.getRGB(0, 0, width, height, rgb_in, 0, width);
BufferedImage bi_out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// for (int y = 0; y < height; y ) {
// for (int x = 0; x < width; x ) {
// bi_out.setRGB(x, y, rgb_out[y * width x]);
// }
// }
bi_out.setRGB(0, 0, width, height, rgb_in, 0, width);
display(bi_in, bi_out);
String outputName = inputName "-copy.png";
File ofile = new File(outputName);
OutputStream fos_out = new FileOutputStream(ofile);
ImageIO.write(bi_out, "PNG", fos_out);
fos_out.flush();
fos_out.close();
System.out.println("Wrote " outputName);
}
// use that to have internal viewer
private static JFrame frame;
private static JLabel label1, label2;
private static void display(BufferedImage img1, BufferedImage img2) {
if (frame == null) {
frame = new JFrame();
frame.setTitle(String.format("%dx%d Original / Copy", img1.getWidth(), img1.getHeight()));
frame.setSize(img1.getWidth() img2.getWidth(), img1.getHeight());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
label1 = new JLabel();
label1.setIcon(new ImageIcon(img1));
frame.getContentPane().add(label1, BorderLayout.WEST);
label2 = new JLabel();
label2.setIcon(new ImageIcon(img2));
frame.getContentPane().add(label2, BorderLayout.EAST);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
} else {
label1.setIcon(new ImageIcon(img1));
label2.setIcon(new ImageIcon(img2));
}
}
}
uj5u.com熱心網友回復:
當ImageIO.read函式創建 aBufferedImage時,它使用它認為最適合的型別。這種型別可能不是您所期望的。特別是,對于 JPG 影像,型別可能不是TYPE_INT_ARGB.
這是您的第二張影像的情況,并且在您列印該影像的型別時變得明顯:
System.out.println(bi_in.getType());
對于該影像,它10會在我的機器上列印,它代表TYPE_BYTE_GRAY。
因此,要解決您的問題,您應該使用:
BufferedImage bi_out = new BufferedImage(width, height, bi_in.getType());
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/514734.html
標籤:爪哇图片缓冲图像
