图像相乘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;
public class ch6_4 {
public static void main(String[] args) throws Exception {
long t1=System.currentTimeMillis();
BufferedImage b1=ImageIO.read(new File("2.jpg"));
BufferedImage b2=ImageIO.read(new File("3.jpg"));
BufferedImage nbi=imgFuse(b1,b2,0.6);
ImageIO.write(nbi, "jpg", new File("1b.jpg"));
long t2=System.currentTimeMillis();
System.out.println("程序运行"+(t2-t1)+"毫秒");
}
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());
}
}
return nbi;
}
private static int imgFuse(int pix1, int pix2, double k) {
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;
public class ch6_5 {
public static void main(String[] args) throws Exception {
long t1=System.currentTimeMillis();
BufferedImage b1=ImageIO.read(new File("2.jpg"));
BufferedImage b2=ImageIO.read(new File("3.jpg"));
BufferedImage nbi=imgMul(b1,b2);
ImageIO.write(nbi, "jpg", new File("1b.jpg"));
long t2=System.currentTimeMillis();
System.out.println("程序运行"+(t2-t1)+"毫秒");
}
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());
}
}
return nbi;
}
private static int imgMul(int pix1, int pix2) {
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;
public class ch6_1 {
public static void main(String[] args) throws Exception {
long t1=System.currentTimeMillis();
BufferedImage b1=ImageIO.read(new File("2.jpg"));
BufferedImage b2=ImageIO.read(new File("3.jpg"));
BufferedImage nbi=imgAdd(b1,b2);
ImageIO.write(nbi, "jpg", new File("1b.jpg"));
long t2=System.currentTimeMillis();
System.out.println("程序运行"+(t2-t1)+"毫秒");
}
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());
}
}
return nbi;
}
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;
public class ch6_3 {
public static void main(String[] args) throws Exception {
long t1=System.currentTimeMillis();
BufferedImage b1=ImageIO.read(new File("2.jpg"));
BufferedImage b2=ImageIO.read(new File("3.jpg"));
BufferedImage nbi=imgMul(b1,b2);
ImageIO.write(nbi, "jpg", new File("1b.jpg"));
long t2=System.currentTimeMillis();
System.out.println("程序运行"+(t2-t1)+"毫秒");
}
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());
}
}
return nbi;
}
private static int imgMul(int pix1, int pix2) {
int y=(int)Math.sqrt(pix1*pix2);
return y;
}
}