예제 #1
0
  public void actionPerformed(ActionEvent evt) {
    Graphics g = getGraphics();
    if (evt.getSource() == openItem) {
      JFileChooser chooser = new JFileChooser();
      common.chooseFile(chooser, "./images", 0); // 设置默认目录,过滤文件
      int r = chooser.showOpenDialog(null);

      if (r == JFileChooser.APPROVE_OPTION) {
        String name = chooser.getSelectedFile().getAbsolutePath();

        // 装载图像
        iImage = common.openImage(name, new MediaTracker(this));

        // 取载入图像的宽和高
        iw = iImage.getWidth(null);
        ih = iImage.getHeight(null);
        bImage = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bImage.createGraphics();
        g2.drawImage(iImage, 0, 0, null);
        loadflag = true;
        repaint();
      }
    } else if (evt.getSource() == rotateItem) // 内置旋转
    {
      setTitle("第4章 图像几何变换 内置旋转 作者 孙燮华");
      common.draw(g, iImage, bImage, common.getParam("旋转角(度):", "30"), 0, 0);
    } else if (evt.getSource() == scaleItem) // 内置缩放
    {
      setTitle("第4章 图像几何变换 内置缩放 作者 孙燮华");
      // 参数选择面板
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "1.5", "1.5");
      setPanel(pp, "内置缩放");
      float x = pp.getPadx();
      float y = pp.getPady();
      common.draw(g, iImage, bImage, x, y, 1);
    } else if (evt.getSource() == shearItem) // 内置错切
    {
      setTitle("第4章 图像几何变换 内置错切 作者 孙燮华");
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "0.5", "0.5");
      setPanel(pp, "内置错切");
      float x = pp.getPadx();
      float y = pp.getPady();
      common.draw(g, iImage, bImage, x, y, 2);
    } else if (evt.getSource() == transItem) // 内置平移
    {
      setTitle("第4章 图像几何变换 内置平移 作者 孙燮华");
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "100", "50");
      setPanel(pp, "内置平移");
      float x = pp.getPadx();
      float y = pp.getPady();
      common.draw(g, iImage, bImage, x, y, 3);
    } else if (evt.getSource() == rotItem) // 旋转算法
    {
      setTitle("第4章 图像几何变换 旋转算法 作者 孙燮华");
      pix = common.grabber(iImage, iw, ih);

      // 旋转,输出图像宽高
      int owh = (int) (Math.sqrt(iw * iw + ih * ih + 0.5));
      opix = geom.imRotate(pix, common.getParam("旋转角(度):", "30"), iw, ih, owh);

      // 将数组中的象素产生一个图像
      MemoryImageSource memoryImage =
          new MemoryImageSource(owh, owh, ColorModel.getRGBdefault(), opix, 0, owh);
      oImage = createImage(memoryImage);
      common.draw(g, iImage, oImage, iw, ih, owh, 4);
    } else if (evt.getSource() == mirItem) // 镜象算法(type:5)
    {
      setTitle("第4章 图像几何变换 镜象算法 作者 孙燮华");
      Parameters pp = new Parameters("选择镜象类型", "水平", "垂直");
      setPanel(pp, "镜象算法");

      pix = common.grabber(iImage, iw, ih);
      opix = geom.imMirror(pix, iw, ih, pp.getRadioState());
      ImageProducer ip = new MemoryImageSource(iw, ih, opix, 0, iw);
      oImage = createImage(ip);
      common.draw(g, iImage, oImage, iw, ih, 0, 5);
    } else if (evt.getSource() == shrItem) // 错切算法(type:6)
    {
      setTitle("第4章 图像几何变换 错切算法 作者 孙燮华");
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "0.5", "0.5");
      setPanel(pp, "错切算法");

      pix = common.grabber(iImage, iw, ih);

      float shx = pp.getPadx();
      float shy = pp.getPady();

      // 计算包围盒的宽和高
      int ow = (int) (iw + (ih - 1) * shx);
      int oh = (int) ((iw - 1) * shy + ih);

      if (shx > 0 && shy > 0) {
        opix = geom.imShear(pix, shx, shy, iw, ih, ow, oh);
        ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
        oImage = createImage(ip);
        common.draw(g, iImage, oImage, iw, ih, 0, 6);
      } else JOptionPane.showMessageDialog(null, "参数必须为正数!");
    } else if (evt.getSource() == trnItem) {
      setTitle("第4章 图像几何变换 平移算法 作者 孙燮华");
      Parameters pp = new Parameters("参数", "x方向:", "y方向:", "100", "50");
      setPanel(pp, "平移算法");
      pix = common.grabber(iImage, iw, ih);
      int tx = (int) pp.getPadx();
      int ty = (int) pp.getPady();

      if (tx > 0 && ty > 0) {
        int ow = iw + tx;
        int oh = ih + ty;
        opix = geom.imTrans(pix, tx, ty, iw, ih, ow, oh);
        ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
        oImage = createImage(ip);
        common.draw(g, iImage, oImage, iw, ih, 0, 7);
      } else JOptionPane.showMessageDialog(null, "参数必须为正数!");
    } else if (evt.getSource() == nearItem) {
      setTitle("第4章 图像几何变换 最邻近插值算法 作者 孙燮华");
      pix = common.grabber(iImage, iw, ih);

      float p =
          (Float.valueOf(JOptionPane.showInputDialog(null, "输入缩放参数(0.1-3.0)", "1.50")))
              .floatValue();
      int ow = (int) (p * iw); // 计算目标图宽高
      int oh = (int) (p * ih);
      opix = geom.nearNeighbor(pix, iw, ih, ow, oh, p);
      ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
      oImage = createImage(ip);
      common.draw(g, oImage, "最邻近插值", p);
    } else if (evt.getSource() == linrItem) {
      setTitle("第4章 图像几何变换 双线性插值算法 作者 孙燮华");
      pix = common.grabber(iImage, iw, ih);

      float p =
          (Float.valueOf(JOptionPane.showInputDialog(null, "输入缩放参数(0.1-3.0)", "1.50")))
              .floatValue();
      int ow = (int) (p * iw); // 计算目标图宽高
      int oh = (int) (p * ih);
      opix = geom.bilinear(pix, iw, ih, ow, oh, p);
      ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
      oImage = createImage(ip);
      common.draw(g, oImage, "双线性插值", p);
    } else if (evt.getSource() == cubicItem) {
      setTitle("第4章 图像几何变换 三次卷积插值算法 作者 孙燮华");
      pix = common.grabber(iImage, iw, ih);

      float p =
          (Float.valueOf(JOptionPane.showInputDialog(null, "输入缩放参数(1.1-3.0)", "1.50")))
              .floatValue();
      if (p < 1) {
        JOptionPane.showMessageDialog(null, "参数p必须大于1!");
        return;
      }
      int ow = (int) (p * iw); // 计算目标图宽高
      int oh = (int) (p * ih);
      opix = geom.scale(pix, iw, ih, ow, oh, p, p);
      ImageProducer ip = new MemoryImageSource(ow, oh, opix, 0, ow);
      oImage = createImage(ip);
      common.draw(g, oImage, "三次卷积插值", p);
    } else if (evt.getSource() == okButton) dialog.dispose();
    else if (evt.getSource() == exitItem) System.exit(0);
  }
예제 #2
0
파일: Gray.java 프로젝트: zqyhellen/graph
    public void actionPerformed(ActionEvent evt)
    {
    	Graphics graph = getGraphics();
    	MediaTracker tracker = new MediaTracker(this);
    	      	  
        if (evt.getSource() == openItem) 
        {
            JFileChooser chooser = new JFileChooser();
            common.chooseFile(chooser, "./images", 0);
            int r = chooser.showOpenDialog(null);                    
            
            if(r == JFileChooser.APPROVE_OPTION) 
            {  
                String name = chooser.getSelectedFile().getAbsolutePath();
                
                if(runflag)
                { 
                    loadflag  = false;
                    runflag   = false;
                }   
			    if(!loadflag)
			    {
				    iImage = common.openImage(name, tracker);    
				    iw = iImage.getWidth(null);
				    ih = iImage.getHeight(null);				    
				    repaint();
			    }
			    else if(loadflag && (!runflag))
			    {			        
				    iImage2 = common.openImage(name, tracker);    
				    common.draw(graph, iImage, "pic 1", iImage2, "pic 2");
				    repaint();			    	
			    }				               
            }
            loadflag = true;
        }
        else if (evt.getSource() == grayItem)
        {
        	setTitle("图像预处理");
        	if(loadflag)        	
        	{ 
        	    pixels = common.grabber(iImage, iw, ih);
				
				pixels = elements.toGray(pixels, iw, ih);
				
				showPix(graph, pixels, "gray pic");
				repaint();				
			}
        }
        else if (evt.getSource() == contrastItem)
        {
            setTitle("图像预处理");
            if(loadflag)
            {	
            	pixels = common.grabber(iImage, iw, ih);
            	int con = common.getParam("type in contrast level(-100~100)","0");
                double contrast = (double)con/100.0;
                pixels = doContrast(pixels, iw, ih, contrast);
                showPix(graph, pixels, "contrast pic");
                repaint();
            }
        }
        else if (evt.getSource() == exitItem) 
            System.exit(0);       
    }