-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDrawTransparent.java
More file actions
64 lines (51 loc) · 2.11 KB
/
Copy pathDrawTransparent.java
File metadata and controls
64 lines (51 loc) · 2.11 KB
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
package me;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import origami.Origami;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DrawTransparent {
static void drawTransparency(Mat frame, Mat transp, int xPos, int yPos) {
List<Mat> layers = new ArrayList<Mat>();
Core.split(transp, layers);
Mat mask = layers.remove(3);
Core.merge(layers, transp);
Mat submat = frame.submat(yPos, yPos + transp.rows(), xPos, xPos + transp.cols());
transp.copyTo(submat, mask);
}
static void weightedTransparency(Mat frame, Mat transp, int xPos, int yPos) {
List<Mat> layers = new ArrayList<Mat>();
Core.split(transp, layers);
// transparency used as the mask
Mat mask = layers.remove(3);
// we need an inverted mask later on
Core.bitwise_not(mask, mask);
Mat merged = new Mat();
Core.merge(layers, merged);
Mat submat = frame.submat(yPos, yPos + transp.rows(), xPos, xPos + transp.cols());
Mat source = submat.clone();
Core.addWeighted(merged, 0.5, source, 0.5, 0, submat);
source.copyTo(submat, mask);
}
public static void marcelAndWhool() {
Mat marcel = Imgcodecs.imread("data/marcel2019.jpg");
Mat whool = Imgcodecs.imread("data/tp/whool.png", Imgcodecs.IMREAD_UNCHANGED);
drawTransparency(marcel, whool, 50, 50);
Imgcodecs.imwrite("target/marcelandwhool.jpg", marcel);
}
public static void marcelAndSheep() {
Mat marcel = Imgcodecs.imread("data/marcel2019.jpg");
Mat sheep = Imgcodecs.imread("data/tp/sheep.png", Imgcodecs.IMREAD_UNCHANGED);
weightedTransparency(marcel, sheep, 50, 50);
sheep = Imgcodecs.imread("data/tp/sheep.png", Imgcodecs.IMREAD_UNCHANGED);
weightedTransparency(marcel, sheep, 400, 500);
Imgcodecs.imwrite("target/marcelandsheep.jpg", marcel);
}
public static void main(String[] args) throws IOException {
Origami.init();
marcelAndWhool();
marcelAndSheep();
}
}