验证中...
Languages: Java
Categories: 图形图像处理
Latest update 2019-11-09 17:32
WaterMarkUtil.java
Raw Copy
package com.opencv.watermark;
import static org.bytedeco.opencv.global.opencv_imgproc.CV_FONT_HERSHEY_COMPLEX;
import static org.bytedeco.opencv.global.opencv_core.CV_32F;
import static org.bytedeco.opencv.global.opencv_core.CV_8S;
import static org.bytedeco.opencv.global.opencv_core.CV_8U;
import static org.bytedeco.opencv.global.opencv_core.CV_8UC1;
import static org.bytedeco.opencv.global.opencv_core.CV_8UC3;
import static org.bytedeco.opencv.global.opencv_core.NORM_MINMAX;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imwrite;
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.MatVector;
import org.bytedeco.opencv.opencv_core.Point;
import org.bytedeco.opencv.opencv_core.Rect;
import org.bytedeco.opencv.opencv_core.Scalar;
public class WaterMarkUtil {
public static Mat addWaterMark(String src, String text){
Mat source = imread(src, CV_8S);
MatVector colors = readColorChannel(source);
for (int i = 0; i < colors.size(); i++) {
Mat com = dft(colors.get(i));
com = putText(com, text);
com = inverse(com);
colors.put(i, com);
}
Mat result = new Mat();
opencv_core.merge(colors, result);
if (result.rows() != source.rows() || result.cols() != source.cols()) {
result = new Mat(result, new Rect(0, 0, source.size().width(), source.size().height()));
}
result.convertTo(result, CV_8U);
return result;
}
public static Mat getWaterMark(String src) {
Mat source = imread(src, CV_8U);
MatVector newPlanes = readColorChannel(dft(source));
Mat mag = new Mat();
opencv_core.magnitude(newPlanes.get(0), newPlanes.get(1), mag);
opencv_core.add(Mat.ones(mag.size(), CV_32F).asMat(), mag, mag);
opencv_core.log(mag, mag);
mag.convertTo(mag, CV_8UC1);
opencv_core.normalize(mag, mag, 0, 255, NORM_MINMAX, CV_8UC1, null);
return mag;
}
static MatVector readColorChannel(Mat mat) {
MatVector colors = new MatVector(3);
opencv_core.split(mat, colors);
return colors;
}
//傅里叶变换
static Mat dft(Mat color) {
color.convertTo(color, CV_32F);
MatVector planes = new MatVector(2);
Mat com = new Mat();
planes.put(0, color);
planes.put(1, Mat.zeros(color.size(), CV_32F).asMat());
opencv_core.merge(planes, com);
opencv_core.dft(com, com);
return com;
}
//写入文字
static Mat putText(Mat mat, String text) {
int fontFace = CV_FONT_HERSHEY_COMPLEX;
Point point = new Point(20, mat.cols() / 5);
opencv_imgproc.putText(mat, text, point, fontFace, 1.5, Scalar.all(0), 2, 30, false);
opencv_core.flip(mat, mat, -1);
opencv_imgproc.putText(mat, text, point, fontFace, 1.5, Scalar.all(0), 2, 30, false);
opencv_core.flip(mat, mat, -1);
return mat;
}
//收尾并转换回原图片
static Mat inverse(Mat color){
MatVector planes = new MatVector(2);
opencv_core.idft(color, color);
opencv_core.split(color, planes);
opencv_core.normalize(planes.get(0), color, 0, 255, NORM_MINMAX, CV_8UC3, null);
return color;
}
public static void main(String[] args){
imwrite("target.jpg", addWaterMark("source.jpg", "FY07"));//保存加过水印的图片
imwrite("watermark.jpg", getWaterMark("target.jpg"));//保存获取到的水印
}
}
pom.xml
Raw Copy
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.opencv.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<jdk.version>1.8</jdk.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version>0.0.1-SNAPSHOT</version>
</properties>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies>
</project>

Comment list( 0 )

Sign in for post a comment

Help Search

191139_cd20d5fd_5186603 191143_ebef6f8d_5186603