-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathImageSketch.java
96 lines (86 loc) · 3.02 KB
/
ImageSketch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.biubiu.example;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
/**
* @author :张音乐
* @date :Created in 2021/4/26 下午4:14
* @description:素描特效
* @email: [email protected]
* @version: 1.0
*/
public class ImageSketch {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
String path = "D:\\upload\\hanfu.jpg";
Mat img = Imgcodecs.imread(path);
if(img.empty()) {
System.out.println("cannot open file");
return;
}
Mat origin = img.clone();
HighGui.imshow("原始图", origin);
// 素描特效
Mat result = toSketch(img);
HighGui.imshow("素描图", result);
HighGui.waitKey(0);
// 释放所有的窗体资源
HighGui.destroyAllWindows();
}
private static Mat toSketch(Mat img) {
Mat gray = new Mat(img.size(), CvType.CV_8UC3);
// 去色
for(int i = 0; i < img.rows(); i++) {
for(int j = 0; j < img.cols(); j++) {
double b = img.get(i, j)[0];
double g = img.get(i, j)[1];
double r = img.get(i, j)[2];
double max = Math.max(Math.max(b, g), r);
double min = Math.min(Math.min(b, g), r);
double []arr = new double[img.get(i, j).length];
for(int k = 0; k < 3; k++) {
arr[k] = (min + max) / 2;
}
gray.put(i, j, arr);
}
}
// 反色
Mat reverse = new Mat(img.size(), CvType.CV_8UC3);
for(int i = 0; i < gray.rows(); i++) {
for (int j = 0; j < gray.cols(); j++) {
double [] grayArr = gray.get(i, j);
double []arr = new double[grayArr.length];
for(int k = 0; k < 3; k++) {
arr[k] = 255 - grayArr[k];
}
reverse.put(i, j, arr);
}
}
// 高斯模糊
Mat thresh = new Mat();
Imgproc.GaussianBlur(reverse, thresh, new Size(7, 7), 3, 3);
Mat result = new Mat(gray.size(), CvType.CV_8UC3);
// 模糊后的图像叠加模式选择颜色减淡效果。
for(int i = 0; i < gray.rows(); i++) {
for (int j = 0; j < gray.cols(); j++) {
double []grayArr = gray.get(i, j);
double []threshArr = thresh.get(i, j);
double []arr = new double[gray.get(i, j).length];
for(int k = 0; k < 3; k++) {
double a = grayArr[k];
double b = threshArr[k];
double c = Math.min(a + (a * b) / (255 - b + 1), 255);
arr[k] = c;
}
result.put(i, j, arr);
}
}
return result;
}
}