首页 > 代码库 > 图像处理之加减乘除

图像处理之加减乘除

昨天和同学一起去蹭Oracle数据库的课,老师很Nice,学到了两点。第一点B树索引,第二点位图索引。

两种很有用的数据结构,这几天好好研究研究。出乎意料的是:下课老师说我们可以去上机,真好嘿嘿,第二节课英语听力,就没去了。

 

今天上机(数字图像处理)用java写的图像 加减乘除操作,效果和Matlab的一比很差,不过还是和大家分享下吧!

 

这是原图:

/*
 * 图像处理
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;



public class Image extends JFrame {

	
	public void ShowImage(String img1, String img2, String img3, String img4, String img5)
	{
		JPanel panel=new JPanel(new BorderLayout());  
        JLabel label1=new JLabel(new ImageIcon(img1));  
        JLabel label2=new JLabel(new ImageIcon(img2)); 
        JLabel label3=new JLabel(new ImageIcon(img3)); 
        JLabel label4=new JLabel(new ImageIcon(img4)); 
        JLabel label5=new JLabel(new ImageIcon(img5)); 
        
       
        panel.add(label1,BorderLayout.WEST); 
        panel.add(label2, BorderLayout.EAST);
        panel.add(label3, BorderLayout.SOUTH); 
        panel.add(label4, BorderLayout.NORTH);
        panel.add(label5, BorderLayout.CENTER);
        
        
        this.getContentPane().setLayout(new BorderLayout());  
        this.getContentPane().add(panel,BorderLayout.CENTER);  
        
        this.setSize(1000, 1000);  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        this.setTitle("显示图像");  
        this.setVisible(true);  
        
        
	}
	
	public static void main(String[] args) throws IOException 
	{
		   
		BufferedImage bi1 = ImageIO.read(new File("bg.jpg"));
		
		BufferedImage bi2 = ImageIO.read(new File("bin.jpg"));
		
		int h2 = bi2.getHeight();
		int w2 = bi1.getWidth();
		
		int h=bi1.getHeight();//获取图像的高
		int w=bi1.getWidth();//获取图像的宽
		
		int rgb=bi1.getRGB(0, 0);//获取指定坐标的ARGB的像素值
		int rgb2 = bi2.getRGB(0, 0);
		
	
		int[][] gray=new int[w][h];
		for (int x = 0; x < w; x++) {
			for (int y = 0; y < h; y++) {
				gray[x][y]=getGray(bi1.getRGB(x, y));
			}
		}
		
		/*
		 * 两幅图像求和相加
		 */
		int[][] add = new int[w][h];
		int[][] sub = new int[w][h];
		int[][] mul = new int[w][h];
		int[][] div = new int[w][h];
		
		
		for(int x = 0; x < w; ++ x)
		{
			for(int y = 0; y < h; ++ y)
			{
				add[x][y] = (getGray(bi1.getRGB(x, y)) + getGray(bi2.getRGB(x, y)))%256;
				sub[x][y] = (getGray(bi1.getRGB(x, y)) - getGray(bi2.getRGB(x, y)))%256;
				mul[x][y] = (getGray(bi1.getRGB(x, y)) * getGray(bi2.getRGB(x, y)))%256;
				//div[x][y] = (getGray(bi1.getRGB(x, y)) / getGray(bi2.getRGB(x, y)))%256;
				
			}
			
		}
		
		BufferedImage nbi=new BufferedImage(w,h,BufferedImage.TYPE_BYTE_BINARY);
		BufferedImage nbi1=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
		BufferedImage nbi2=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
		BufferedImage nbi3=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
		BufferedImage nbi4=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
		
		
		/*
		 * 二值化
		 */
		int SW=160;		
		for (int x = 0; x < w; x++) {
			for (int y = 0; y < h; y++) {
				if(getAverageColor(gray, x, y, w, h)>SW)
				{
					int max=new Color(255,255,255).getRGB();
					nbi.setRGB(x, y, max);
				}else
				{
					int min=new Color(0,0,0).getRGB();
					nbi.setRGB(x, y, min);
				}
			}
		}
		
		
		for(int x = 0; x < w; ++ x)
		{
			for(int y = 0; y < h; ++ y)
			{
				
				nbi1.setRGB(x, y, add[x][y]);
				nbi2.setRGB(x, y, sub[x][y]);
				nbi3.setRGB(x, y, mul[x][y]);
				nbi4.setRGB(x, y, div[x][y]);
				
				
			}
		}
		ImageIO.write(nbi, "jpg", new File("./bin.jpg"));
		ImageIO.write(nbi1, "jpg", new File("./add.jpg"));
		ImageIO.write(nbi2, "jpg", new File("./sub.jpg"));
		ImageIO.write(nbi3, "jpg", new File("./mul.jpg"));
		ImageIO.write(nbi4, "jpg", new File("./div.jpg"));
		
		Image img = new Image();
        img.ShowImage("add.jpg", "sub.jpg", "mul.jpg", "div.jpg", "bin.jpg");
        
		
	
	}
	
	/*
	 * 每个像素多有Red Green Blue 三原色
	 */
	public static int getGray(int rgb)
	{
		String str=Integer.toHexString(rgb);
		int r=Integer.parseInt(str.substring(2,4),16);
		int g=Integer.parseInt(str.substring(4,6),16);
		int b=Integer.parseInt(str.substring(6,8),16);
		//or 直接new个color对象
		 
		Color c=new Color(rgb);
		r=c.getRed();
	    g=c.getGreen();
		b=c.getBlue();
		int top=(r+g+b)/3;
		return (int)(top);
	}
	
	
	      
	 
	/**
	 * 自己加周围8个灰度值再除以9,算出其相对灰度值
	 */
	public static int  getAverageColor(int[][] gray, int x, int y, int w, int h)
    {
		/*
		 * 相加九次取均值
		 */
        int rs = gray[x][y]
                      	+ (x == 0 ? 255 : gray[x - 1][y])
			            + (x == 0 || y == 0 ? 255 : gray[x - 1][y - 1])
			            + (x == 0 || y == h - 1 ? 255 : gray[x - 1][y + 1])
			            + (y == 0 ? 255 : gray[x][y - 1])
			            + (y == h - 1 ? 255 : gray[x][y + 1])
			            + (x == w - 1 ? 255 : gray[x + 1][ y])
			            + (x == w - 1 || y == 0 ? 255 : gray[x + 1][y - 1])
			            + (x == w - 1 || y == h - 1 ? 255 : gray[x + 1][y + 1]);
        return rs / 9;
    }

	
	
}



 

 

 

 

用Java做图像处理似乎还不错,呵呵。 加油封尘浪!