作者H45 (!H45)
站內java
標題Re: [問題] 還是.bmp檔案的問題 ~"~
時間Mon Oct 22 18:35:34 2007
※ 引述《H45 (!H45)》之銘言:
: 本程式輸出的像素值有 (255, 255, 255), (255, 255, 0), (255, 0, 255),
: (0, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 0, 0)
: 以上共有八種可能,所以這並不是二值影像。
try {
BufferedImage image = ImageIO.read(new File("lena256.bmp"));
final int width = image.getWidth();
final int height = image.getHeight();
for (int y=0; y<height; y++) {
for (int x=0; x<width; x++) {
int rgb = image.getRGB(x, y) & 0x00ffffff;
int red = (rgb & 0x00ff0000) >> 16;
int green = (rgb & 0x0000ff00) >> 8;
int blue = rgb & 0x000000ff;
if (red > 127 && green > 127 && blue > 127) {
rgb = 0x00ffffff;
} else {
rgb = 0x00000000;
}
image.setRGB(x, y, rgb);
}
}
ImageIO.write(image, "png", new File("binary.png"));
System.out.println("OK.");
} catch (IOException e) {
e.printStackTrace();
}
這個程式碼經測試與原篇的 Matlab 圖完全相同。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.116.247.13
推 shiengchyi:h45大 你的lena.bmp 是存成哪一種? 我改存成24位元的就 10/22 19:28
→ shiengchyi:正確了....Orz 10/22 19:29