原图:
背景:
最终效果:
代码:
package com.machao;
import com.machao.util.OpenCvUtils;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.io.File;
import java.io.FilenameFilter;
public class Picture extends OpenCvUtils {
public static void main(String[] args) {
Long begin = System.currentTimeMillis();
String path = "F:\\opencv_test\\back";
File file = new File(path);
String[] files = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith("jpg");
}
});
Mat src,logo = null;
for (String s : files) {
System.out.println("处理中:" + s);
// 获取背景图片
src = Imgcodecs.imread("F:\\opencv_test\\bg_img\\back1.png", 1);
// 读取需要处理的原图
logo = Imgcodecs.imread(path + "//" + s,1);
// 将文本原图二值处理,让文本不出现灰白色,不需要则注释
Imgproc.threshold(logo,logo, 140, 255, Imgproc.THRESH_BINARY);
// 设置图片背景
setBackground(src, logo, s);
}
Long end = System.currentTimeMillis();
System.out.println("处理完成,共耗时:"+((end-begin)/1000.0)+"s,平均每张图片耗时:"+((end-begin)/files.length/1000.0)+"/s");
// JOptionPane.showMessageDialog( null , "转换完成!" );
}
/**
* 设置图片背景
* @param src 背景图片
* @param logo 当前图片
* @param name 图片名称
*/
public static void setBackground(Mat src, Mat logo, String name){
Mat mask = null;
// 将背景大小重置为logo大小
Imgproc.resize(src,src,logo.size());
mask = logo.clone();
Mat imgROI = src.submat(new Rect(0, 0, logo.cols(), logo.rows())); //Rect方法定义ROI,注意宽是:cols;高是rows。
Core.bitwise_not(mask, mask);//对mask图像取反,白色(255)变成黑色(0)
// Imgproc.threshold(mask,mask, 80, 255, Imgproc.THRESH_BINARY);//对mask进行二值化,threshold处理彩色图是将rgb3值分别按照灰度值分割,只保留纯色
// Imgcodecs.imwrite("F:\\opencv_test\\back\\"+name.substring(0,name.indexOf("."))+"-mask.jpg", mask);
logo.copyTo(imgROI, mask);//将logo拷贝到imgROI上,rgb为不为0的部分保留,为0的被置换为目标图的rgb
Imgcodecs.imwrite("F:\\opencv_test\\back\\"+name.substring(0,name.indexOf("."))+"-result.jpg", src);
// 电脑垃圾,手动gc一下
System.gc();
}
}
package com.machao.util;
import com.machao.TextAreaFind;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.springframework.util.ResourceUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
/**
*
* @类功能说明: openCv公共方法操作类型
*
* @作者:mayiwen
* @创建时间:2019年10月16日 下午3:06:52 @版本:V1.0
*/
public class OpenCvUtils{
/**
* 加载动态库
*/
public static void loadLibraries() {
try {
String osName = System.getProperty("os.name");
String opencvpath = "dll/";
// windows
if (osName.startsWith("Windows")) {
// int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
// // 32位系统
// if (bitness == 32) {
// opencvpath += "opencv_java320_x86.dll";
// }
// // 64位系统
// else if (bitness == 64) {
opencvpath += "opencv_java412.dll";
// } else {
// opencvpath += "opencv_java320_x64.dll";
// }
}
File dll = ResourceUtils.getFile("classpath:"+opencvpath);
opencvpath = dll.getAbsolutePath();
System.load(opencvpath);
} catch (Exception e) {
throw new RuntimeException("Failed to load opencv native library", e);
}
}
static {
// 加载动态库
loadLibraries();
}
}