-
Notifications
You must be signed in to change notification settings - Fork 5
/
Img.java
146 lines (121 loc) · 3.81 KB
/
Img.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.genericsystem.cv;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class Img {
private final Mat src = new Mat();
public Mat getSrc() {
return src;
}
public Img(Mat src) {
src.copyTo(this.src);
}
public Img sobel(int ddepth, int dx, int dy, int ksize, double scale, double delta, int borderType) {
Mat result = new Mat();
Imgproc.Sobel(src, result, ddepth, dx, dy, ksize, scale, delta, borderType);
return new Img(result);
}
public Img thresHold(double thresh, double maxval, int type) {
Mat result = new Mat();
Imgproc.threshold(src, result, thresh, maxval, type);
return new Img(result);
}
public Img morphologyEx(int morphOp, StructuringElement structuringElement) {
Mat result = new Mat();
Imgproc.morphologyEx(src, result, morphOp, structuringElement.getSrc());
return new Img(result);
}
public List<MatOfPoint> findContours(Img[] hierarchy, int mode, int method) {
Mat mat = new Mat();
List<MatOfPoint> result = new ArrayList<>();
Imgproc.findContours(src, result, mat, mode, method);
hierarchy[0] = new Img(mat);
return result;
}
public List<MatOfPoint> findContours(Img[] hierarchy, int mode, int method, Point point) {
Mat mat = new Mat();
List<MatOfPoint> result = new ArrayList<>();
Imgproc.findContours(src, result, mat, mode, method, point);
hierarchy[0] = new Img(mat);
return result;
}
public Img canny(double threshold1, double threshold2) {
Mat result = new Mat();
Imgproc.Canny(src, result, threshold1, threshold2);
return new Img(result);
}
public Size size() {
return src.size();
}
public int height() {
return src.height();
}
public int width() {
return src.width();
}
public double[] get(int row, int col) {
return src.get(row, col);
}
public Img cvtColor(int code) {
Mat result = new Mat();
Imgproc.cvtColor(src, result, code);
return new Img(result);
}
public ImageIcon getImageIcon() {
return new ImageIcon(Tools.mat2bufferedImage(src));
}
public void rectangle(Rect rect, Scalar color, int thickNess) {
Imgproc.rectangle(src, rect.br(), rect.tl(), color, thickNess);
}
public Node getImageView() {
Mat conv = new Mat();
src.convertTo(conv, CvType.CV_8UC1);
Mat target = new Mat();
Imgproc.resize(conv, target, new Size(AbstractApp.displayWidth, Math.floor((AbstractApp.displayWidth / conv.width()) * conv.height())));
MatOfByte buffer = new MatOfByte();
Imgcodecs.imencode(".png", target, buffer);
ImageView imageView = new ImageView(new Image(new ByteArrayInputStream(buffer.toArray())));
imageView.setPreserveRatio(true);
imageView.setFitWidth(AbstractApp.displayWidth);
return imageView;
}
public int channels() {
return src.channels();
}
public Img range(Scalar scalar, Scalar scalar2, boolean hsv) {
Img ranged = this;
if (hsv)
ranged = ranged.cvtColor(Imgproc.COLOR_BGR2HSV);
Mat result = new Mat(ranged.size(), ranged.type(), new Scalar(0, 0, 0));
Mat mask = new Mat();
Core.inRange(ranged.getSrc(), scalar, scalar2, mask);
ranged.getSrc().copyTo(result, mask);
Img resultImg = new Img(result);
if (hsv)
resultImg = resultImg.cvtColor(Imgproc.COLOR_HSV2BGR);
return resultImg;
}
public int type() {
return src.type();
}
public Img gaussianBlur(Size size) {
Mat result = new Mat();
Imgproc.GaussianBlur(src, result, size, 0);
return new Img(result);
}
}