-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_postgres_functions.cpp
More file actions
242 lines (177 loc) · 7.42 KB
/
my_postgres_functions.cpp
File metadata and controls
242 lines (177 loc) · 7.42 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* UNIVERSITY OF SAO PAULO - ICMC - GBDI http://www.gbdi.icmc.usp.br/
* Author: Jonathan Ramos, [email protected]
* File: my_postgres_functions.cpp
*
*/
#include "cplusplus_header.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
// ---------------------------------------------------------------------------
PG_FUNCTION_INFO_V1(canny_detector);
/**
* Returns with canny edge detection applied
* <p>
* This method always assume the image is in RGB, i.e., there are three channels
*
* @param bytea from the database
* @return bytea composed of the arg0 (bytea - image) image after applying
* canny edge detection
*/
Datum canny_detector(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0)) {
ereport(ERROR, (errmsg("Null arrays not accepted")));
}
bytea *imagem = PG_GETARG_BYTEA_P(0);
cv::Mat image = ByteArray2Mat(imagem);
if (image.empty()) {
ereport(ERROR, (errmsg("ByteArray decoding failed")));
}
cv::Mat edge = cannyEdgeDetection(image);
bytea* bytes = Mat2ByteArray(edge);
// Construct the array to be returned back to the database client
// ArrayType *res = construct_array(imgSizeArray, image.cols * image.rows, INT4OID, 4, true, 'i');
PG_RETURN_BYTEA_P(bytes);
}
// ---------------------------------------------------------------------------
PG_FUNCTION_INFO_V1(get_imgsize);
/**
* Returns the image size (nxm) in pixels based on the bytea data
* <p>
* This method always assume the image is in RGB, i.e., there are three channels
*
* @param bytea from the database
* @return the image in Integer[] type.
*/
Datum get_imgsize(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0)) {
ereport(ERROR, (errmsg("Null arrays not accepted")));
}
bytea* imagem = PG_GETARG_BYTEA_P(0);
cv::Mat image = ByteArray2Mat(imagem);
if (image.empty()) {
ereport(ERROR, (errmsg("ByteArray decoding failed")));
}
Datum* imgSizeArray = (Datum*) palloc(sizeof (Datum) * 2);
imgSizeArray[0] = image.cols;
imgSizeArray[1] = image.rows;
// Construct the array to be returned back to the database client
ArrayType *res = construct_array(imgSizeArray, 2, INT4OID, 4, true, 'i');
PG_RETURN_ARRAYTYPE_P(res);
}
PG_FUNCTION_INFO_V1(euclidean_distance);
Datum euclidean_distance(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
ereport(ERROR, (errmsg("Null arrays not accepted")));
}
bytea *byte_array0 = PG_GETARG_BYTEA_P(0);
bytea *byte_array1 = PG_GETARG_BYTEA_P(1);
Signature features0 = ByteArray2BasicArrayObject(byte_array0);
Signature features1 = ByteArray2BasicArrayObject(byte_array1);
double *res = (double *) palloc(sizeof (double));
try {
EuclideanDistance<Signature> euclidean;
*res = euclidean.getDistance(features0, features1);
} catch (std::length_error) {
ereport(ERROR, (errmsg("Features vectors must have same length")));
}
PG_RETURN_FLOAT8((double) *res);
}
PG_FUNCTION_INFO_V1(manhattan_distance);
Datum manhattan_distance(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
ereport(ERROR, (errmsg("Null arrays not accepted")));
}
bytea *byte_array0 = PG_GETARG_BYTEA_P(0);
bytea *byte_array1 = PG_GETARG_BYTEA_P(1);
Signature features0 = ByteArray2BasicArrayObject(byte_array0);
Signature features1 = ByteArray2BasicArrayObject(byte_array1);
double *result = (double *) (palloc(sizeof (double)));
try {
ManhattanDistance< Signature > manhattan;
*result = manhattan.getDistance(features0, features1);
} catch (std::length_error) {
ereport(ERROR, (errmsg("Features vectors must have same length")));
}
PG_RETURN_FLOAT8(*result);
}
PG_FUNCTION_INFO_V1(chebyshev_distance);
Datum chebyshev_distance(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
ereport(ERROR, (errmsg("Null arrays not accepted")));
}
const bytea *byte_array0 = PG_GETARG_BYTEA_P(0);
const bytea *byte_array1 = PG_GETARG_BYTEA_P(1);
Signature features0 = ByteArray2BasicArrayObject(byte_array0);
Signature features1 = ByteArray2BasicArrayObject(byte_array1);
double *result = (double *) (palloc(sizeof (double)));
try {
ChebyshevDistance< Signature > chebyshev;
*result = chebyshev.getDistance(features0, features1);
} catch (std::length_error) {
ereport(ERROR, (errmsg("Features vectors must have same length")));
}
PG_RETURN_FLOAT8((double) *result);
}
PG_FUNCTION_INFO_V1(lbp_extractor);
Datum lbp_extractor(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0)) {
ereport(ERROR, (errmsg("[ERROR] Null array not accepted.\n")));
}
bytea * inputByteArray = PG_GETARG_BYTEA_P(0);
// ByteArray to Image
Image * image = ByteArray2Image(inputByteArray);
Signature imgSignature;
LocalBinaryPatternExtractor<Signature, Image> *descriptor;
// Extract
descriptor = new LocalBinaryPatternExtractor<Signature, Image>();
descriptor->setNumFeatures(256); // TODO: WHY 256?
descriptor->generateSignature(*image, imgSignature);
// Signature to ByteArray
bytea * characteristics = Signature2ByteArray(imgSignature);
PG_RETURN_BYTEA_P(characteristics);
}
PG_FUNCTION_INFO_V1(color_layout_extractor);
Datum color_layout_extractor(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0)) {
ereport(ERROR, (errmsg("[ERROR] Null array not accepted.\n")));
}
Signature imgSignature;
ColorLayoutExtractor<Signature, Image> *descriptor;
// ByteArray to Image
bytea * inputByteArray = PG_GETARG_BYTEA_P(0);
Image * image = ByteArray2Image(inputByteArray);
// Extract
descriptor = new ColorLayoutExtractor<Signature, Image>();
descriptor->setNumFeatures(256); // TODO: WHY 256?
descriptor->setNumBlocks(256); // TODO: WHY 256?
descriptor->generateSignature(*image, imgSignature);
// Signature to ByteArray
bytea * characteristics = Signature2ByteArray(imgSignature);
PG_RETURN_BYTEA_P(characteristics);
}
PG_FUNCTION_INFO_V1(color_structure_extractor);
Datum color_structure_extractor(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0)) {
ereport(ERROR, (errmsg("[ERROR] Null array not accepted.\n")));
}
Signature imgSignature;
ColorStructureExtractor<Signature, Image> *descriptor;
// ByteArray to Image
bytea * inputByteArray = PG_GETARG_BYTEA_P(0);
Image * image = ByteArray2Image(inputByteArray);
// Extract
descriptor = new ColorStructureExtractor<Signature, Image>();
// descriptor->setNumFeatures(256); // TODO: WHY 256?
descriptor->generateSignature(*image, imgSignature);
// Signature to ByteArray
bytea * characteristics = Signature2ByteArray(imgSignature);
PG_RETURN_BYTEA_P(characteristics);
}
#ifdef __cplusplus
}
#endif