-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageLibrary.pde
118 lines (85 loc) · 2.84 KB
/
ImageLibrary.pde
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
import processing.core.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.awt.image.Raster;
import java.awt.image.DataBufferByte;
import org.opencv.core.Point;
import org.opencv.core.Point3;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;
public class ImageLibrary {
PApplet parent;
public ImageLibrary(PApplet parent) {
this.parent = parent;
parent.registerMethod("dispose", this);
}
public void dispose()
{
//nothing yet
}
public PVector toP5(Point pt) {
return new PVector( (float) pt.x, (float) pt.y);
}
public PVector toP5(Point3 pt) {
return new PVector( (float) pt.x, (float) pt.y, (float) pt.z);
}
/**
* We support here only 3 bytes BGR images
*/
public PImage toP5(Mat mat) {
int cols = mat.cols();
int rows = mat.rows();
int elemSize = (int) mat.elemSize();
byte[] data = new byte[cols * rows * elemSize];
int type;
//println ("elem size = " + elemSize);
mat.get(0, 0, data);
switch (mat.channels()) {
case 1:
type = BufferedImage.TYPE_BYTE_GRAY;
break;
case 3:
byte b;
for(int i=0; i<data.length; i=i+3) {
b = data[i];
data[i] = data[i+2];
data[i+2] = b;
}
type = BufferedImage.TYPE_3BYTE_BGR;
break;
default:
type = BufferedImage.TYPE_3BYTE_BGR;
//return null;
}
BufferedImage image = new BufferedImage(cols, rows, type);
image.getRaster().setDataElements(0, 0, cols, rows, data);
PImage pimage = new PImage(image.getWidth(), image.getHeight(), PConstants.ARGB);
// now copy to the image
image.getRGB(0, 0, pimage.width, pimage.height, pimage.pixels, 0, pimage.width);
pimage.updatePixels();
return pimage;
}
////////////////////////////////////////////////////////////////////////////////////////
// toCV
////////////////////////////////////////////////////////////////////////////////////////
public Mat toCV(PImage image) {
BufferedImage bm = new BufferedImage(image.width, image.height, BufferedImage.TYPE_4BYTE_ABGR);
bm.setRGB(0, 0, image.width, image.height, image.pixels, 0, image.width);
Raster rr = bm.getRaster();
byte [] pixels = new byte[image.width * image.height * 4];
rr.getDataElements(0, 0, image.width, image.height, pixels);
/*
byte[] pixels = ((DataBufferByte) bm.getRaster().getDataBuffer()).getData();
*/
Mat m1 = new Mat(image.height, image.width, CvType.CV_8UC4); // CvType.CV_8UC4
m1.put(0, 0, pixels);
return m1;
}
public Point toPoint(PVector pt) {
return new Point(pt.x, pt.y);
}
public Point3 toPoint3(PVector pt) {
return new Point3(pt.x, pt.y, pt.z);
}
}