package ImageProcess;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import ImageProcess.histshow;
public class ImageProc extends Frame {
Image im,tmp,img;
Graphics g2;
int iw,ih;
int[] pixels;
boolean flag_load=false;
boolean flag_grey=false;
String fileopen = null, filename = null;
//构造方法
public ImageProc(){
this.setTitle("图像分割系统");
Panel pdown;
Button load,grey,run,original,Rpbert_Run,save,quit,hist;
//添加窗口监听事件
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
pdown=new Panel();
pdown.setBackground(Color.lightGray);
load=new Button("打开图像");
hist=new Button("直方图");
grey = new Button("灰度图像");
save=new Button("保存灰度图像");
original= new Button("原图");
run=new Button("全局阈值分割");
Rpbert_Run=new Button("Roberts边缘分割");
quit=new Button("退出");
this.add(pdown,BorderLayout.SOUTH);
pdown.add(load);
pdown.add(grey);
pdown.add(save);
pdown.add(original);
pdown.add(run);
pdown.add(Rpbert_Run);
pdown.add(original);
pdown.add(hist);
pdown.add(quit);
//按钮的监听事件
load.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
jLoad_ActionPerformed(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
hist.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
jHist_ActionPerformed(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
grey.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
jGrey_ActionPerformed(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
original.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
jOriginal_ActionPerformed(e);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
run.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jRun_ActionPerformed(e);
}
});
Rpbert_Run.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jRpbert_Run_ActionPerformed(e);
}
});
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
jSave_ActionPerformed(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
quit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jQuit_ActionPerformed(e);
}
});
}
public void jHist_ActionPerformed(ActionEvent e) throws IOException{
histshow h = new histshow();
h.getData(pixels, iw, ih);
h.setSize(480,350);
h.setVisible(true);
}
public void jLoad_ActionPerformed(ActionEvent e) throws IOException{
FileDialog filedialog_open;
filedialog_open = new FileDialog(this, "打开文件对话框", FileDialog.LOAD);
filedialog_open.setFile("*.jpg");
filedialog_open.setVisible(true);
fileopen = filedialog_open.getDirectory();// 返回文件对话框中显示的文件所属的目录
filename = filedialog_open.getFile();// 返回当前文件对话框中显示的文件名的字符串表
File inputFile = new File(fileopen+filename);
BufferedImage input = ImageIO.read(inputFile);
//BufferedImage input1 = ImageIO.read(inputFile);
//img=input1;
im = input;
iw=im.getWidth(this);
ih=im.getHeight(this);
pixels=new int[iw*ih];
try {
PixelGrabber pg=new PixelGrabber(im, 0, 0, iw, ih, pixels, 0, iw);
pg.grabPixels();
} catch (InterruptedException e3) {
e3.printStackTrace();
}
//ImageProducer ip=new MemoryImageSource(iw, ih, pixels, 0, iw);
tmp=input;
//img=input;
flag_load=true;
repaint();
}
public void jGrey_ActionPerformed(ActionEvent e) throws IOException {
if(flag_load){
File inputFile = new File(fileopen+filename);
BufferedImage input = ImageIO.read(inputFile);//ImageIO类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法。
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)));
//通过移位方法:Gray =(R*76+G*151+B*28)>>8;
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 jOriginal_ActionPerformed(ActionEvent e) throws IOException
{
if(flag_load)
{
File inputFile = new File(fileopen+filename);
String pathFile = null;
pathFile=fileopen+filename;
BufferedImage input = ImageIO.read(inputFile);
iw = input.getWidth(this);
ih = input.getHeight(this);
pixels = new int[iw*ih];
ImageShow h = new ImageShow();
h.getData(pathFile, iw, ih,pixels);
h.setSize(480,350);
h.setVisible(true);
}else{
JOptionPane.showMessageDialog(null, "请先打开一副图片!","提示:",
JOptionPane.WARNING_MESSAGE);
}
}
public void jRun_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();
}
//设定二值化的阈值,默认值为100
int grey=100;
Object tmpGrey="100";
String s=JOptionPane.showInputDialog(null, "输入二值化的阈值(0-255):", tmpGrey);
//还会有异常抛出
if(s!=null){
grey=Integer.parseInt(s);
}
if(grey>255){
grey=255;
}else if(grey<0)
{
grey=0;
}
//对图像进行二值化处理。Alpha值保持不变
ColorModel cm=ColorModel.getRGBdefault();
for(int i=0;i<iw*ih;i++)
{
int red,green,blue;
int alpha=cm.getAlpha(pixels[i]);
if(cm.getRed(pixels[i])>grey)
{
red=255;
}else{red=0;}
if(cm.getGreen(pixels[i])>grey)
{
green=255;
}else{green=0;}
if(cm.getBlue(pixels[i])>grey)
{
blue=255;
}else{blue=0;}
pixels[i]=alpha<<24|red<<16|green<<8|blue;
//通过移位重新构成某一点像素的 RGB 值
/*
* 返回 ((alpha左移24位) 或(red左移16位) 或 (green左移8位) 或 ( blue))返回值是一个数值.
<<表示左移, 左移一位表示原来的值乘2.
| :当两边操作数的位有一边为1时,结果为1,否则为0。如1100|1010=1110
*
*
* 像素是个24位的真彩色,最高8位复制alpha,下来8位赋值red,等等。最低的8位是blue颜色通道
*
* */
}
//将数组中的像素产生一个图像
/*
* ImageProducer
* 可为 Image 生