|
|
|
@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.awt.image.RenderedImage;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
public class java实验图像处理 {
|
|
|
|
|
|
|
|
|
|
static BufferedImage bufferedImage_start,bufferedImage_end ;//创建2个BufferedImage对象
|
|
|
|
|
public static BufferedImage gray(BufferedImage b){ //定义灰度方法 返回值为BufferedImage对象
|
|
|
|
|
int width = b.getWidth();
|
|
|
|
|
int height =b.getHeight();
|
|
|
|
|
bufferedImage_end = new BufferedImage(width,height, BufferedImage.TYPE_3BYTE_BGR ); //构建新的对象模型
|
|
|
|
|
// 遍历图片的RGB值,把得到的灰度值存到bufferedImage_end中,然后返回bufferedImage_end
|
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
|
|
|
|
|
|
Color color = new Color(bufferedImage_start.getRGB(x,y));//构建Color获取图片像素点
|
|
|
|
|
int gray = (int)(color.getRed() * 0.299 + color.getGreen() * 0.587 + color.getBlue() *0.114);
|
|
|
|
|
Color color_end = new Color(gray,gray,gray); //将设置的像素设置到bufferedImage_end
|
|
|
|
|
bufferedImage_end.setRGB(x,y,color_end.getRGB());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return bufferedImage_end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
try {
|
|
|
|
|
bufferedImage_start = ImageIO.read(new File("D:/FFOutput/实例.bmp"));
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
RenderedImage rendImage =gray(bufferedImage_start);
|
|
|
|
|
File file = new File("D:/FFOutput/实例灰度.bmp");
|
|
|
|
|
ImageIO.write(rendImage, "bmp", file);
|
|
|
|
|
}catch(IOException e){
|
|
|
|
|
System.out.println(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|