图像代数运算

图像相乘k*p1+(1-k)*p2

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 图像融合
 * k*b1+(1-k)*b2
 * */
public class ch6_4 {

	public static void main(String[] args) throws Exception {
		long t1=System.currentTimeMillis();		//获取程序运行前的时间,单位毫秒
		BufferedImage b1=ImageIO.read(new File("2.jpg"));		//读取图像1
		BufferedImage b2=ImageIO.read(new File("3.jpg"));		//读取图像2
		BufferedImage nbi=imgFuse(b1,b2,0.6);					//处理图像
		ImageIO.write(nbi, "jpg", new File("1b.jpg"));		//输出图像
		long t2=System.currentTimeMillis();				//获取程序运行后的时间
		System.out.println("程序运行"+(t2-t1)+"毫秒");	//程序结束后进行提示

	}

	/**
	 * 图像相加
	 * @param BufferedImage b1 	原始图像1
	 * @param BufferedImage b2 	原始图像2
	 * @param double k 			融合系数
	 * @return BufferedImage	变换后图像
	 * */
	public static BufferedImage imgFuse(BufferedImage b1, BufferedImage b2, double k) {
		int w=Math.min(b1.getWidth(), b2.getWidth());		//新图像的宽度为两幅图像高度的最小值
		int h=Math.min(b1.getHeight(), b2.getHeight());		//新图像的高度为两幅图像高度的最小值
		
		BufferedImage nbi=new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);	
		//循环遍历每一个像素点
		for(int y=0;y<h;y++) {
			for(int x=0;x<w;x++) {
				Color c1=new Color(b1.getRGB(x, y));
				Color c2=new Color(b2.getRGB(x, y));
				int r=imgFuse(c1.getRed(),c2.getRed(),k);
				int g=imgFuse(c1.getGreen(),c2.getGreen(),k);
				int b=imgFuse(c1.getBlue(),c2.getBlue(),k);
				Color nc=new Color(r, g, b);
				nbi.setRGB(x, y, nc.getRGB());	//设置输出图像坐标为(x,y)的像素值
			}
		}		
		return nbi;
	}

	private static int imgFuse(int pix1, int pix2, double k) {
        //提示:输出值必须在0-255之间。
		int y=(int)(k*pix1+(1-k)*pix2);
		if(k>255) y=255;
		return y;

	}

}

图像相乘sqrt(p1* p1+p2* p2)

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 图像相乘
 * sqrt(b1*b1+b2*b2)
 * */
public class ch6_5 {

	public static void main(String[] args) throws Exception {
		long t1=System.currentTimeMillis();		//获取程序运行前的时间,单位毫秒
		BufferedImage b1=ImageIO.read(new File("2.jpg"));		//读取图像1
		BufferedImage b2=ImageIO.read(new File("3.jpg"));		//读取图像2
		BufferedImage nbi=imgMul(b1,b2);						//处理图像
		ImageIO.write(nbi, "jpg", new File("1b.jpg"));		//输出图像
		long t2=System.currentTimeMillis();				//获取程序运行后的时间
		System.out.println("程序运行"+(t2-t1)+"毫秒");	//程序结束后进行提示

	}

	/**
	 * 图像相乘
	 * @param BufferedImage b1 	原始图像1
	 * @param BufferedImage b2 	原始图像2
	 * @return BufferedImage	变换后图像
	 * */
	public static BufferedImage imgMul(BufferedImage b1, BufferedImage b2) {
		int w=Math.min(b1.getWidth(), b2.getWidth());		//新图像的宽度为两幅图像高度的最小值
		int h=Math.min(b1.getHeight(), b2.getHeight());		//新图像的高度为两幅图像高度的最小值
		
		BufferedImage nbi=new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);	
		//循环遍历每一个像素点
		for(int y=0;y<h;y++) {
			for(int x=0;x<w;x++) {
				Color c1=new Color(b1.getRGB(x, y));
				Color c2=new Color(b2.getRGB(x, y));
				int r=imgMul(c1.getRed(),c2.getRed());
				int g=imgMul(c1.getGreen(),c2.getGreen());
				int b=imgMul(c1.getBlue(),c2.getBlue());
				Color nc=new Color(r, g, b);
				nbi.setRGB(x, y, nc.getRGB());			//设置输出图像坐标为(x,y)的像素值
			}
		}		
		return nbi;
	}

	private static int imgMul(int pix1, int pix2) {
      //提示:输出值在0-255之间
		int y=(int)Math.sqrt(pix1*pix1+pix2*pix2);
		if(y>255) y=255;
		return y;
	}

}

图像相加(b1+b2)/2

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 图像相加
 * (b1+b2)/2
 * */
public class ch6_1 {

	public static void main(String[] args) throws Exception {
		long t1=System.currentTimeMillis();		//获取程序运行前的时间,单位毫秒
		BufferedImage b1=ImageIO.read(new File("2.jpg"));		//读取图像1
		BufferedImage b2=ImageIO.read(new File("3.jpg"));		//读取图像2
		BufferedImage nbi=imgAdd(b1,b2);						//处理图像
		ImageIO.write(nbi, "jpg", new File("1b.jpg"));		//输出图像
		long t2=System.currentTimeMillis();				//获取程序运行后的时间
		System.out.println("程序运行"+(t2-t1)+"毫秒");	//程序结束后进行提示

	}

	/**
	 * 图像相加
	 * @param BufferedImage b1 	原始图像1
	 * @param BufferedImage b2 	原始图像2
	 * @return BufferedImage	变换后图像
	 * */
	public static BufferedImage imgAdd(BufferedImage b1, BufferedImage b2) {
		int w=Math.min(b1.getWidth(), b2.getWidth());		//新图像的宽度为两幅图像高度的最小值
		int h=Math.min(b1.getHeight(), b2.getHeight());		//新图像的高度为两幅图像高度的最小值
		
		BufferedImage nbi=new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);	
		//循环遍历每一个像素点
		for(int y=0;y<h;y++) {
			for(int x=0;x<w;x++) {
				Color c1=new Color(b1.getRGB(x, y));
				Color c2=new Color(b2.getRGB(x, y));
				int r=imgAdd(c1.getRed(),c2.getRed());
				int g=imgAdd(c1.getGreen(),c2.getGreen());
				int b=imgAdd(c1.getBlue(),c2.getBlue());
				Color nc=new Color(r, g, b);
				nbi.setRGB(x, y, nc.getRGB());			//设置输出图像坐标为(x,y)的像素值
			}
		}		
		return nbi;
	}

	/**
	 * 图像相加
	 * @param int pix1 	原始图像1像素值
	 * @param int pix2 	原始图像2像素值
	 * @return int		变换后像素值
	 * */
	private static int imgAdd(int pix1, int pix2) {
		int y=(pix1+pix2)/2;
		return y;
	}

}

图像相乘sqrt(b1*b2)

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 图像相乘
 * sqrt(b1*b2)
 * */
public class ch6_3 {

	public static void main(String[] args) throws Exception {
		long t1=System.currentTimeMillis();		//获取程序运行前的时间,单位毫秒
		BufferedImage b1=ImageIO.read(new File("2.jpg"));		//读取图像1
		BufferedImage b2=ImageIO.read(new File("3.jpg"));		//读取图像2
		BufferedImage nbi=imgMul(b1,b2);						//处理图像
		ImageIO.write(nbi, "jpg", new File("1b.jpg"));		//输出图像
		long t2=System.currentTimeMillis();				//获取程序运行后的时间
		System.out.println("程序运行"+(t2-t1)+"毫秒");	//程序结束后进行提示

	}

	/**
	 * 图像相乘
	 * @param BufferedImage b1 	原始图像1
	 * @param BufferedImage b2 	原始图像2
	 * @return BufferedImage	变换后图像
	 * */
	public static BufferedImage imgMul(BufferedImage b1, BufferedImage b2) {
		int w=Math.min(b1.getWidth(), b2.getWidth());		//新图像的宽度为两幅图像高度的最小值
		int h=Math.min(b1.getHeight(), b2.getHeight());		//新图像的高度为两幅图像高度的最小值
		
		BufferedImage nbi=new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);	
		//循环遍历每一个像素点
		for(int y=0;y<h;y++) {
			for(int x=0;x<w;x++) {
				Color c1=new Color(b1.getRGB(x, y));
				Color c2=new Color(b2.getRGB(x, y));
				int r=imgMul(c1.getRed(),c2.getRed());
				int g=imgMul(c1.getGreen(),c2.getGreen());
				int b=imgMul(c1.getBlue(),c2.getBlue());
				Color nc=new Color(r, g, b);
				nbi.setRGB(x, y, nc.getRGB());			//设置输出图像坐标为(x,y)的像素值
			}
		}		
		return nbi;
	}

	/**
	 * 图像相乘
	 * @param int pix1 	原始图像1像素值
	 * @param int pix2 	原始图像2像素值
	 * @return int		变换后像素值
	 * */
	private static int imgMul(int pix1, int pix2) {
		int y=(int)Math.sqrt(pix1*pix2);
		return y;
	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值