Repository navigation
Expand file tree
/
Copy pathSegmentationReport.c
More file actions
107 lines (89 loc) · 3.79 KB
/
Copy pathSegmentationReport.c
File metadata and controls
107 lines (89 loc) · 3.79 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
#include "mex.h"
#include <stdio.h>
/* Generates a report of features and segmentation boundaries for ingestion into a database. */
/* Format is slide \t globalcentroid-x \t globalcentroid-y \t {features} \t globalx1,globaly1; globalx2,globaly2... */
/* input: */
/* Output - (string) Path and filename of output file */
/* Slide - (string) Base slide name (without extensions) for generating output. */
/* X - (N-length float) Horizontal coordinates of segmented object centroid in global frame. */
/* Y - (N-length float) Vertical coordinates of segmented object centroid in global frame. */
/* Features - (N x D float) Features of the segmented objects. */
/* Names - (D-length cell array of strings) Feature names. */
/* bX - (N-length cell array of float) Global horizontal coordinates of segmented object boundaries. */
/* Each element is a K_i-length vector. */
/* Bounds should be filtered to first to remove colinear points and compress size. */
/* bY - (N-length cell array of float) Global vertical coordinates of segmented object boundaries. */
/* Each element is a K_i-length vector. */
/* Bounds should be filtered to first to remove colinear points and compress size. */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* variables */
FILE *file;
int i, j; /* loop iterators */
char *Output, *Slide, *Name; /*pointer to input string denoting slide*/
double *X, *Y, *Features, *bX, *bY; /* pointers to input matrices */
mxArray *Names, *BoundsX, *BoundsY; /* cell array of feature names */
size_t N, D, Length; /* number of objects, feature dimension, # boundary points */
/* capture pointers to input arrays */
X = (double*)mxGetPr(prhs[2]);
Y = (double*)mxGetPr(prhs[3]);
Features = (double*)mxGetPr(prhs[4]);
Names = prhs[5];
BoundsX = prhs[6];
BoundsY = prhs[7];
/* get number of objects */
if(mxGetM(prhs[2]) > mxGetN(prhs[2])) {
N = mxGetM(prhs[2]);
}
else {
N = mxGetN(prhs[2]);
}
/* get feature dimension */
if(mxGetM(prhs[4]) != N) {
D = mxGetM(prhs[4]);
}
else {
D = mxGetN(prhs[4]);
}
/* copy strings for slide name and output filename */
Output = mxCalloc(mxGetM(prhs[0])*mxGetN(prhs[0])+1, sizeof(char));
Slide = mxCalloc(mxGetM(prhs[1])*mxGetN(prhs[1])+1, sizeof(char));
mxGetString(prhs[0], Output, mxGetM(prhs[0])*mxGetN(prhs[0])+1);
mxGetString(prhs[1], Slide, mxGetM(prhs[1])*mxGetN(prhs[1])+1);
/* open file, write contents */
file = fopen(Output, "w");
if(file != NULL) {
/* print header */
fprintf(file, "Slide\tX\tY\t");
for(i = 0; i < D; i++) {
mxArray *Cell = mxGetCell(Names,i);
Name = mxCalloc(mxGetM(Cell)*mxGetN(Cell)+1, sizeof(char));
mxGetString(Cell, Name, mxGetM(Cell)*mxGetN(Cell)+1);
fprintf(file, "%s\t", Name);
mxFree(Name);
}
fprintf(file, "Boundaries: x1,y1;x2,y2;...\n");
/* print data */
for(i = 0; i < N; i++){
/* slide name, centroids */
fprintf(file, "%s\t%.1f\t%.1f\t", Slide, X[i], Y[i]);
/* features */
for(j = 0; j < D; j++){
fprintf(file, "%f\t", Features[i+ j*N]);
}
/* boundaries */
bX = mxGetPr(mxGetCell(BoundsX,i));
bY = mxGetPr(mxGetCell(BoundsY,i));
Length = mxGetM(mxGetCell(BoundsX,i)) * mxGetN(mxGetCell(BoundsX,i));
for(j = 0; j < Length; j++) {
fprintf(file, "%.1f,%.1f;", bX[j], bY[j]);
}
fprintf(file, "\n");
}
}
/* close file */
fclose(file);
/* free mem */
mxFree(Output);
mxFree(Slide);
}