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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/*
* graycoprops
*
* graycoprops in scilab
* Please refer to :
* http://www.cse.unsw.edu.au/~icml2002/workshops/MLCV02/MLCV02-Bevk.pdf, p.3.
*/
// Created by Samiran Roy, mail: samiranroy@cse.iitb.ac.in
// An implementation of graycoprops method of matlab
// Usage:
// graycoprops(GLCM,property) : Calculates the Property of the input Gray level co-occurence matrix
// Known Changes from Matlab:
/*
* 1) None, as of now - Matching exactly - but does not use structures - for properties
*/
#include <numeric>
#include <math.h>
#include <vector>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
extern "C" {
#include "api_scilab.h"
#include "Scierror.h"
#include "BOOL.h"
#include <localization.h>
#include "sciprint.h"
#include "../common.h"
double return_contrast(Mat image)
{
double contrast = 0;
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
contrast = contrast + (pow((i - j), 2) * image.at<double>(i, j));
}
}
return contrast;
}
double return_correlation(Mat image)
{
double correlation = 0;
std::vector<int> mg_rows;
std::vector<int> mg_cols;
// Meshgrid of row and column indices
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
mg_rows.push_back(j);
mg_cols.push_back(i);
}
}
// for (int i=0; i<rows.size();i++)
// sciprint("%d\n", rows[i]);
Mat flat_image = image.reshape(1, 1);
double mean_row = 0;
double mean_col = 0;
double std_row = 0;
double std_col = 0;
for (int i = 0; i < mg_rows.size(); i++)
mean_row = mean_row + mg_rows[i] * flat_image.at<double>(i);
for (int i = 0; i < mg_cols.size(); i++)
mean_col = mean_col + mg_cols[i] * flat_image.at<double>(i);
for (int i = 0; i < mg_rows.size(); i++)
std_row =
std_row + (pow(mg_rows[i] - mean_row, 2) * flat_image.at<double>(i));
std_row = sqrt(std_row);
for (int i = 0; i < mg_cols.size(); i++)
std_col =
std_col + (pow(mg_cols[i] - mean_col, 2) * flat_image.at<double>(i));
std_col = sqrt(std_col);
for (int i = 0; i < mg_rows.size(); i++)
correlation =
correlation + ((mg_rows[i] - mean_row) * (mg_cols[i] - mean_col) *
flat_image.at<double>(i));
double denom = std_col * std_row;
if (denom != 0) {
correlation = correlation / denom;
}
// sciprint("\n");
// sciprint("MR: %f",mean_row);
// sciprint("MC: %f",mean_col);
// sciprint("SR: %f",std_row);
// sciprint("SC: %f",std_col);
return correlation;
}
double return_energy(Mat image)
{
double energy = 0;
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
energy = energy + pow(image.at<double>(i, j), 2);
}
}
return energy;
}
double return_homogeneity(Mat image)
{
double homogeneity = 0;
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
homogeneity = homogeneity + (image.at<double>(i, j) / (1 + abs(i - j)));
}
}
return homogeneity;
}
int opencv_graycoprops(char *fname, unsigned long fname_len) {
SciErr sciErr;
int intErr = 0;
int *piAddr1 = NULL;
int error;
// String holding the second argument
int iRet = 0;
char *pstData = NULL;
// Checking input argument
CheckInputArgument(pvApiCtx, 1, 2);
CheckOutputArgument(pvApiCtx, 1, 1);
// Get input image
Mat image;
retrieveImage(image, 1);
// converting image to float
image.convertTo(image, CV_64FC1, 1, 0);
// for (int i = 0; i < image.rows; i++) {
// for (int j = 0; j < image.cols; j++) {
// sciprint("%f ", image.at<double>(i, j));
// }
// sciprint("\n");
// }
// Error Checks
if (image.channels() == 1) {
// Normalizing image
double s = cv::sum(image)[0];
image = image / s;
sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr1);
if (sciErr.iErr) {
printError(&sciErr, 0);
return 0;
}
if (isStringType(pvApiCtx, piAddr1)) {
if (isScalar(pvApiCtx, piAddr1)) {
iRet = getAllocatedSingleString(pvApiCtx, piAddr1, &pstData);
}
}
if (strcmp(pstData, "contrast") == 0) {
double contrast = return_contrast(image);
intErr =
createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, contrast);
if (intErr) return intErr;
// Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
// Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
else if (strcmp(pstData, "correlation") == 0) {
double correlation = return_correlation(image);
intErr = createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1,
correlation);
if (intErr) return intErr;
// Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
// Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
} else if (strcmp(pstData, "energy") == 0) {
double energy = return_energy(image);
intErr =
createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, energy);
if (intErr) return intErr;
// Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
// Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
else if (strcmp(pstData, "homogeneity") == 0) {
double homogeneity = return_homogeneity(image);
intErr = createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1,
homogeneity);
if (intErr) return intErr;
// Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
// Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
else {
sciprint("\nUnknown Parameter Name: %s\n", pstData);
return 0;
}
}
else if (image.channels() == 3) {
sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr1);
if (sciErr.iErr) {
printError(&sciErr, 0);
return 0;
}
if (isStringType(pvApiCtx, piAddr1)) {
if (isScalar(pvApiCtx, piAddr1)) {
iRet = getAllocatedSingleString(pvApiCtx, piAddr1, &pstData);
}
}
if (strcmp(pstData, "contrast") == 0) {
vector<Mat> rgb;
split(image, rgb);
double *contrast = (double *)malloc(sizeof(double) * 3);
double s;
for (int i = 0; i < 3; i++) {
s = cv::sum(rgb[i])[0];
rgb[i] = rgb[i] / s;
contrast[2-i] = return_contrast(rgb[i]);
}
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx), 1, 3,
contrast);
free(contrast);
if (sciErr.iErr) {
printError(&sciErr, 0);
return 0;
}
// Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx);
// Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
else if (strcmp(pstData, "correlation") == 0) {
vector<Mat> rgb;
split(image, rgb);
double *correlation = (double *)malloc(sizeof(double) * 3);
double s;
for (int i = 0; i < 3; i++) {
s = cv::sum(rgb[i])[0];
rgb[i] = rgb[i] / s;
correlation[2-i] = return_correlation(rgb[i]);
}
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx), 1, 3,
correlation);
free(correlation);
if (sciErr.iErr) {
printError(&sciErr, 0);
return 0;
}
// Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx);
// Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
else if (strcmp(pstData, "energy") == 0) {
vector<Mat> rgb;
split(image, rgb);
double *energy = (double *)malloc(sizeof(double) * 3);
double s;
for (int i = 0; i < 3; i++) {
s = cv::sum(rgb[i])[0];
rgb[i] = rgb[i] / s;
energy[2-i] = return_energy(rgb[i]);
}
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx), 1, 3,
energy);
free(energy);
if (sciErr.iErr) {
printError(&sciErr, 0);
return 0;
}
// Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx);
// Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
else if (strcmp(pstData, "homogeneity") == 0) {
vector<Mat> rgb;
split(image, rgb);
double *homogeneity = (double *)malloc(sizeof(double) * 3);
double s;
for (int i = 0; i < 3; i++) {
s = cv::sum(rgb[i])[0];
rgb[i] = rgb[i] / s;
homogeneity[2-i] = return_homogeneity(rgb[i]);
}
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx), 1, 3,
homogeneity);
free(homogeneity);
if (sciErr.iErr) {
printError(&sciErr, 0);
return 0;
}
// Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx);
// Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
else {
sciprint("\nUnknown Parameter Name: %s\n", pstData);
return 0;
}
} else {
sciprint("Invalid number of channels in the image(must be 1 or 3)\n");
return 0;
}
// sciprint("\n");
// for (int i = 0; i < new_image.rows; i++) {
// for (int j = 0; j < new_image.cols; j++) {
// sciprint("%f ", new_image.at<double>(i,j));
// }
// sciprint("\n");
// }
// new_image is sent to scilab as output
}
/* ==================================================================== */
}
|