java使用Opencv小例子,从合并的两个图片中减去一张图片

将两个图片合并很容易(下面第二个程序是利用Java合并两张图片),但有时候需要从两个合并的图片中分离出一张图片,查了一下OpenCV提供了图片相减的功能,其实不止图片相减,基本的运算opencv都提供了,这里以图片相减为例说明。

[java]  view plain  copy
  1. package swing_interface;  
  2.   
  3. import org.opencv.core.Core;  
  4. import org.opencv.core.CvType;  
  5. import org.opencv.core.Mat;  
  6. import org.opencv.highgui.Highgui;  
  7.   
  8. /** 
  9.  * 执行两个图片的相减操作,用于抓取用户所画的部分,去除本来就有的部分 
  10.  */  
  11. public class ImageSub {  
  12.   
  13.     public static void sub(String two_path, String one_path, String des_path) {  
  14.         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);  
  15.   
  16.         // 读取图像,不改变图像的原始信息  
  17.         Mat image1 = Highgui.imread(two_path, Highgui.CV_LOAD_IMAGE_COLOR);  
  18.         Mat image2 = Highgui.imread(one_path, Highgui.CV_LOAD_IMAGE_COLOR);  
  19.               
  20.         Mat image3 = new Mat(image1.size(), CvType.CV_64F);  
  21.         Core.subtract(image2, image1, image3);  
  22.   
  23.         Mat image5 = new Mat(image1.size(), CvType.CV_64F);  
  24.         Core.bitwise_not(image3, image5);  
  25.   
  26.         Highgui.imwrite(des_path, image5);  
  27.     }  
  28.       
  29.     public static void main(String[] args) {  
  30.         String dir = System.getProperty("user.dir");  
  31.         ImageSub.sub(dir+"/r_pic/user_paint_two.jpg", dir+"/r_pic/user_paint_one.jpg", dir+"/res.jpg");  
  32.     }  
  33.   
  34. }  

另外附上用java合并两张图片的程序,当然用opencv实现起来更方便。

[java]  view plain  copy
  1. package merge;  
  2. import java.awt.Graphics2D;  
  3. import java.awt.image.BufferedImage;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6.   
  7. import javax.imageio.ImageIO;  
  8.   
  9. /** 
  10.  * 图片拼接 
  11.  * 把多张宽度一样的图片拼接成一张大图片 
  12.  */  
  13. public class Merge {  
  14.       
  15.     public static String path = System.getProperty("user.dir");  
  16.       
  17.     public static void main(String[] args) {  
  18.         callMergeSimple();  
  19.     }  
  20.       
  21.     public static void callMergeSimple(){  
  22.           
  23.         File file1 = new File(path+"/merge1/1.jpg");  
  24.         File file2 = new File(path+"/merge1/2.jpg");  
  25.         File file3 = new File(path+"/merge2/res.jpg");  
  26.         BufferedImage image1 = null,image2 = null;  
  27.         try {  
  28.             image1 = ImageIO.read(file1);   
  29.             image2 = ImageIO.read(file2);  
  30.         } catch (IOException e) {  
  31.             e.printStackTrace();  
  32.         }   
  33.         mergeSimple(image1,image2,100,300,file3);  
  34.     }  
  35.       
  36.     public static boolean mergeSimple(BufferedImage image1, BufferedImage image2, int posw, int posh, File fileOutput) {  
  37.   
  38.           //合并两个图像  
  39.           int w1 = image1.getWidth();  
  40.           int h1 = image1.getHeight();  
  41.           int w2 = image2.getWidth();  
  42.           int h2 = image2.getHeight();  
  43.   
  44.           BufferedImage imageSaved = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_ARGB);  
  45.           Graphics2D g2d = imageSaved.createGraphics();  
  46.           g2d.drawImage(image1, null00);  
  47.             
  48.           for (int i = 0; i < w2; i++) {  
  49.               for (int j = 0; j < h2; j++) {  
  50.                   int rgb1 = image1.getRGB(i + posw, j + posh);  
  51.                   int rgb2 = image2.getRGB(i, j);  
  52.                     
  53.                   if (rgb1 != rgb2) {  
  54.                       rgb2 = rgb1 & rgb2;  
  55.                   }  
  56.                   imageSaved.setRGB(i + posw, j + posh, rgb2);  
  57.               }  
  58.           }  
  59.   
  60.           boolean b = false;  
  61.           try {  
  62.               b = ImageIO.write(imageSaved, "png", fileOutput);  
  63.           } catch (IOException ie) {  
  64.               ie.printStackTrace();  
  65.           }  
  66.               return b;  
  67.          }  
  68. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值