首页 > 代码库 > Java图像处理
Java图像处理
要求实现简单的图像处理功能:
(1)打开一副图像;
(2)对打开的图像进行简单处理,至少包括两种操作,如均值滤波、直方图均衡化等;
(3)对处理之后的图像进行保存;
import java.awt.EventQueue;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
/**
* A program for viewing images.
* @version 1.22 2017-3-17
* @author **
*/
public class ImageViewer
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new ImageViewerFrame();
frame.setTitle("Image Processing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
class ImageViewerFrame extends JFrame
{
private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH = 600;
private static final int DEFAULT_HEIGHT = 480;
String name = " ";
Image im, tmp;
int i, iw, ih;
int[] pixels;
boolean flag_load = false;
boolean flag_grey = false;
public ImageViewerFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// use a label to display the images
label = new JLabel();
add(label);
// set up the file chooser
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
// set up the menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
// show file chooser dialog
int result = chooser.showOpenDialog(null);
// if file selected, set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION)
{
name = chooser.getSelectedFile().getPath();
//label.setIcon(new ImageIcon(name));
File inputFile = new File(name);
if(name != " ")
flag_load = true;
BufferedImage input = null;
try {
input = ImageIO.read(inputFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iw = input.getWidth();
ih = input.getHeight();
pixels = new int[iw*ih];
im = input;
tmp = input;
flag_load = true;
repaint();
}
}
});
JMenuItem SaveItem = new JMenuItem("Save");
menu.add(SaveItem);
SaveItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
jSave_ActionPerformed(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
JMenuItem SaveAsItem = new JMenuItem("Save As");
menu.add(SaveAsItem);
SaveAsItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
jSave_ActionPerformed1(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
JMenu menu1 = new JMenu("Edit");
menuBar.add(menu1);
JMenuItem deal1 = new JMenuItem("均值滤波");
menu1.add(deal1);
deal1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jMean_ActionPerformed(e);
}
});
JMenuItem deal2 = new JMenuItem("直方图均衡化");
menu1.add(deal2);
deal2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
jGrey_ActionPerformed(e);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jRun_ActionPerformed(e);
}
});
JMenuItem deal3 = new JMenuItem("黑白");
menu1.add(deal3);
deal3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
jGrey_ActionPerformed(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
public void jMean_ActionPerformed(ActionEvent e) {
if(flag_load){
try{
PixelGrabber pg = new PixelGrabber(im,0,0,iw,ih,pixels,0,iw);
pg.grabPixels();
}catch(InterruptedException e3){
e3.printStackTrace();
}
BufferedImage grayImage = new BufferedImage(iw, ih,
BufferedImage.TYPE_INT_RGB);
ColorModel cm = ColorModel.getRGBdefault();
for(int i=1;i<ih-1;i++){
for(int j=1;j<iw-1;j++){
int red1 = cm.getRed(pixels[(i-1)*iw+j-1]);
int red2 = cm.getRed(pixels[(i-1)*iw+j]);
int red3 = cm.getRed(pixels[(i-1)*iw+j+1]);
int red4 = cm.getRed(pixels[i*iw+j-1]);
int red6 = cm.getRed(pixels[i*iw+j+1]);
int red7 = cm.getRed(pixels[(i+1)*iw+j-1]);
int red8 = cm.getRed(pixels[(i+1)*iw+j]);
int red9 = cm.getRed(pixels[(i+1)*iw+j+1]);
int meanRed = (red1+red2+red3+red4+red6+red7+red8+red9)/8;
int green1 = cm.getGreen(pixels[(i-1)*iw+j-1]);
int green2 = cm.getGreen(pixels[(i-1)*iw+j]);
int green3 = cm.getGreen(pixels[(i-1)*iw+j+1]);
int green4 = cm.getGreen(pixels[i*iw+j-1]);
int green6 = cm.getGreen(pixels[i*iw+j+1]);
int green7 = cm.getGreen(pixels[(i+1)*iw+j-1]);
int green8 = cm.getGreen(pixels[(i+1)*iw+j]);
int green9 = cm.getGreen(pixels[(i+1)*iw+j+1]);
int meanGreen = (green1+green2+green3+green4+green6+green7+green8+green9)/8;
int blue1 = cm.getBlue(pixels[(i-1)*iw+j-1]);
int blue2 = cm.getBlue(pixels[(i-1)*iw+j]);
int blue3 = cm.getBlue(pixels[(i-1)*iw+j+1]);
int blue4 = cm.getBlue(pixels[i*iw+j-1]);
int blue6 = cm.getBlue(pixels[i*iw+j+1]);
int blue7 = cm.getBlue(pixels[(i+1)*iw+j-1]);
int blue8 = cm.getBlue(pixels[(i+1)*iw+j]);
int blue9 = cm.getBlue(pixels[(i+1)*iw+j+1]);
int meanBlue = (blue1+blue2+blue3+blue4+blue6+blue7+blue8+blue9)/8;
int rgb = 255<<24|meanRed<<16|meanGreen<<8|meanBlue;
grayImage.setRGB(j, i, rgb);
}
}
tmp = grayImage;
repaint();
}else{
JOptionPane.showMessageDialog(null, "请打开一张图片!","提示:",
JOptionPane.WARNING_MESSAGE);
}
}
public void jSave_ActionPerformed(ActionEvent e) throws IOException{
if(flag_load)
{
BufferedImage bi = new BufferedImage(tmp.getWidth(null),tmp.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(tmp,0, 0,null);
g.dispose();
File save_path=new File(name+"(1)"+".jpg");
ImageIO.write(bi, "JPG", save_path);
}
else
{
JOptionPane.showMessageDialog(null, "请打开一张图片!","提示:",JOptionPane.WARNING_MESSAGE);
}
}
public void jSave_ActionPerformed1(ActionEvent e) throws IOException{
if(flag_load)
{
int result = chooser.showOpenDialog(null);
BufferedImage bi = new BufferedImage(tmp.getWidth(null),tmp.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(tmp,0, 0,null);
g.dispose();
String name1 = chooser.getSelectedFile().getPath();
if (result == JFileChooser.APPROVE_OPTION)
{
File save_path=new File(name1);
ImageIO.write(bi, "JPG", save_path);
}
}
else
{
JOptionPane.showMessageDialog(null, "请打开一张图片!","提示:",JOptionPane.WARNING_MESSAGE);
}
}
public void jRun_ActionPerformed(ActionEvent e){
if(flag_load&&flag_grey){
try{
PixelGrabber pg = new PixelGrabber(tmp,0,0,iw,ih,pixels,0,iw);
pg.grabPixels();
}catch(InterruptedException e3){
e3.printStackTrace();
}
BufferedImage greyImage = new BufferedImage(iw, ih,
BufferedImage.TYPE_BYTE_GRAY);
//获取图像的直方图
int[] histogram = new int[256];
for(int i=0; i<ih-1; i++){
for(int j=0; j<iw-1; j++){
int grey = pixels[i*iw+j]&0xff;
histogram[grey]++;
}
}
//直方图均衡化
double a = (double)255/(iw*ih);
double[] c = new double [256];
c[0] = (a*histogram[0]);
for(int i=1; i<256; i++){
c[i] = c[i-1]+(int)(a*histogram[i]);
}
for(int i=0; i<ih; i++){
for(int j=0; j<iw; j++){
int grey = pixels[i*iw+j]&0x0000ff;
int hist = (int)(c[grey]);
pixels[i*iw+j] = 255<<24|hist<<16|hist<<8|hist;
greyImage.setRGB(j, i, pixels[i*iw+j]);
}
}
tmp = greyImage;
flag_load = true;
repaint();
}else{
}
}
public void jGrey_ActionPerformed(ActionEvent e) throws IOException{
if(flag_load){
File inputFile = new File(name);
BufferedImage input = ImageIO.read(inputFile);
iw = input.getWidth(this);
ih = input.getHeight(this);
pixels = new int[iw*ih];
BufferedImage grayImage = new BufferedImage(iw, ih,
BufferedImage.TYPE_BYTE_GRAY);
for(int i=0; i<iw; i++){
for(int j=0; j<ih; j++){
int rgb = input.getRGB(i, j);
int grey = (int) (0.3*((rgb&0xff0000 )>>16)+0.59*((rgb&0xff00 )>>8)+0.11*((rgb&0xff)));
rgb = 255<<24|grey<<16|grey<<8|grey;
grayImage.setRGB(i, j, rgb);
}
}
tmp = grayImage;
try{
PixelGrabber pg = new PixelGrabber(tmp,0,0,iw,ih,pixels,0,iw);
pg.grabPixels();
}catch(InterruptedException e3){
e3.printStackTrace();
}
flag_grey = true;
repaint();
} else{
JOptionPane.showMessageDialog(null, "请打开一张图片!","提示:",
JOptionPane.WARNING_MESSAGE);
}
}
public void paint(Graphics g){
if(flag_load){
g.drawImage(tmp,DEFAULT_WIDTH/8,DEFAULT_HEIGHT/6,this);
}else{}
}
}
Java图像处理