1、图 像 处 理 作 业一、题目:编写程序对一个 bmp 图像统计直方图,并进行均衡化处理。二、程序完成情况:(一) 、简介本程序采用 java 语言编写,在 Borland JBuilder X 下编译通过。由于本人在软件编程方面并不擅长,且 java 语言学习时间不长,程序内一定会有很多不合适的语句。谢谢老师在百忙之中批改我的作业!(二)程序界面1、主界面2、选择文件3、打开4、统计直方图5、均衡化处理6、处理后的直方图三、源程序Frame1.javapackage cl.lesson.txcl;import java.io.*;import java.awt.*;import java.a
2、wt.Toolkit;import java.awt.image.*;import .*;import .MalformedURLException;import java.awt.event.*;import javax.swing.*;import javax.swing.filechooser.FileFilter;import java.awt.Image;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.awt.image.MemoryImageSource;import java.io.F
3、ileInputStream;import java.io.FileOutputStream;/java 包调用/* Title: * Description: * Copyright: Copyright (c) 2004* Company: * 陈磊* version 1.0*/文件信息public class Frame1 extends JFrame JPanel contentPane;JMenuBar jMenuBar1 = new JMenuBar();JMenu jMenuFile = new JMenu();JMenuItem jMenuFileExit = new JMen
4、uItem();JMenu jMenuHelp = new JMenu();JMenuItem jMenuHelpAbout = new JMenuItem();JToolBar jToolBar = new JToolBar();JButton jButton1 = new JButton();JButton jButton2 = new JButton();JButton jButton3 = new JButton();ImageIcon image1;ImageIcon image2;ImageIcon image3;BorderLayout borderLayout1 = new B
5、orderLayout();JTabbedPane jTabbedPane1 = new JTabbedPane();Toolkit tk;MediaTracker mtrack;Image img;int ImageWidth;int ImageHeight;/分别为图像的宽度与高度值int PixelsSource;/ImageWidth*ImageHeighString theFile;String theDir;Thread runMe;JMenuItem jMenuFileOpen = new JMenuItem();JMenuItem jMenuSave = new JMenuIt
6、em();JMenu jMenuPlay = new JMenu();JMenuItem jMenuItem1 = new JMenuItem();JMenuItem jMenuItem2 = new JMenuItem();/Construct the framepublic Frame1() enableEvents(AWTEvent.WINDOW_EVENT_MASK);try jbInit();catch(Exception e) e.printStackTrace();/Component initializationprivate void jbInit() throws Exce
7、ption image1 = new ImageIcon(cl.lesson.txcl.Frame1.class.getResource(“openFile.png“);image2 = new ImageIcon(cl.lesson.txcl.Frame1.class.getResource(“closeFile.png“);image3 = new ImageIcon(cl.lesson.txcl.Frame1.class.getResource(“help.png“);contentPane = (JPanel) this.getContentPane();contentPane.set
8、Layout(borderLayout1);this.setSize(new Dimension(533, 409);this.setTitle(“图象处理“);jMenuFile.setText(“文件“);jMenuFileExit.setText(“离开“);jMenuFileExit.addActionListener(new Frame1_jMenuFileExit_ActionAdapter(this);jMenuHelp.setToolTipText(“);jMenuHelp.setText(“帮助“);jMenuHelpAbout.setText(“关于“);jMenuHelp
9、About.addActionListener(new Frame1_jMenuHelpAbout_ActionAdapter(this);jButton1.setIcon(image1);jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this);jButton1.setToolTipText(“打开文件“);jButton2.setIcon(image2);jButton2.setToolTipText(“存储文件“);jButton3.setIcon(image3);jButton3.addActionListen
10、er(new Frame1_jButton3_actionAdapter(this);jButton3.setToolTipText(“帮助“);contentPane.setAlignmentY(float) 0.5);contentPane.setDebugGraphicsOptions(0);contentPane.setToolTipText(“);jMenuFileOpen.setToolTipText(“);jMenuFileOpen.setText(“打开“);jMenuFileOpen.addActionListener(new Frame1_jMenuFileOpen_act
11、ionAdapter(this);jMenuSave.setText(“存储“);jMenuSave.addActionListener(new Frame1_jMenuSave_actionAdapter(this);jMenuPlay.setText(“图像处理“);jMenuItem1.setText(“绘制直方图“);jMenuItem1.addActionListener(new Frame1_jMenuItem1_actionAdapter(this);jMenuItem2.setText(“均衡化处理“);jMenuItem2.addActionListener(new Fram
12、e1_jMenuItem2_actionAdapter(this);jToolBar.add(jButton1);jToolBar.add(jButton2);jToolBar.add(jButton3);contentPane.add(jTabbedPane1, BorderLayout.CENTER);jMenuFile.add(jMenuFileOpen);jMenuFile.add(jMenuSave);jMenuFile.add(jMenuFileExit);jMenuHelp.add(jMenuHelpAbout);jMenuBar1.add(jMenuFile);jMenuBar
13、1.add(jMenuPlay);jMenuBar1.add(jMenuHelp);this.setJMenuBar(jMenuBar1);contentPane.add(jToolBar, BorderLayout.NORTH);jMenuPlay.add(jMenuItem1);jMenuPlay.add(jMenuItem2);/File | Exit action performedpublic void jMenuFileExit_actionPerformed(ActionEvent e) System.exit(0);/Help | About action performedp
14、ublic void jMenuHelpAbout_actionPerformed(ActionEvent e) Frame1_AboutBox dlg = new Frame1_AboutBox(this);Dimension dlgSize = dlg.getPreferredSize();Dimension frmSize = getSize();Point loc = getLocation();dlg.setLocation(frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) /
15、2 + loc.y);dlg.setModal(true);dlg.pack();dlg.show();/菜单说明帮助项/Overridden so we can exit when window is closedprotected void processWindowEvent(WindowEvent e) super.processWindowEvent(e);if (e.getID() = WindowEvent.WINDOW_CLOSING) jMenuFileExit_actionPerformed(null);/菜单关闭项void jButton3_actionPerformed
16、(ActionEvent e) Frame1_AboutBox dlg = new Frame1_AboutBox(this);Dimension dlgSize = dlg.getPreferredSize();Dimension frmSize = getSize();Point loc = getLocation();dlg.setLocation(frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);dlg.setModal(true);dlg.pack();
17、dlg.show();/快捷图标帮助public void getImageFromFile(String path)tk=Toolkit.getDefaultToolkit();img = tk.getImage(path);/以下为主要内容,选择打开一个 bmp 图像文件,并对其分析读取。void jButton1_actionPerformed(ActionEvent e) Image image; /构造一个目标图String result=“; /返回结果FileDialog fd = new FileDialog(this,“请选择一个 bmp 图像文件!“);fd.setMode
18、(FileDialog.LOAD);fd.setVisible(true);theFile = fd.getFile();theDir = fd.getDirectory();String thePath = theDir+theFile;FileInputStream fs=new FileInputStream(thePath);int bflen=14;byte bf=new bytebflen;fs.read(bf,0,bflen); /读取 14 字节 BMP 文件头int bilen=40;byte bi=new bytebilen;fs.read(bi,0,bilen); /读取
19、 40 字节 BMP 信息头/ 获取一些重要数据int nwidth=(int)bi7System.out.println(“宽:“+nwidth);int nheight=(int)bi11System.out.println(“高:“+nheight);/位数int nbitcount=(int)bi15System.out.println(“位数:“+nbitcount);/源图大小int nsizeimage=(int)bi23System.out.println(“源图大小:“+nsizeimage);/对 24 位 BMP 进行解析if(nbitcount=24)int npad=
20、(nsizeimage/nheight)-nwidth*3;int ndata=new intnheight*nwidth;byte brgb=new byte(nwidth+npad)*3*nheight;fs.read (brgb,0,(nwidth+npad)*3*nheight);int nindex=0;for(int j=0;jnheight;j+)for(int i=0;inwidth;i+)ndata nwidth*(nheight-j-1)+i=(255nindex+=3;nindex+=npad;Toolkit kit=Toolkit.getDefaultToolkit();image=kit.createImage(new MemoryImageSource(nwidth,nheight,ndata,0,nwidth);result=“从 BMP 得到图像 image“;System.out.println(“从 BMP 得到图像 image“);elseresult=“不是 24 位 BMP,失败!“;System.out.println(“不是 24 位 BMP,失败!“);image=(Image)null;fs.close(); /关闭输入流