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
| #include "mex.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#define SCALE 40000
#define IN_ADJ prhs[0]
#define IN_XY prhs[1]
#define min(x,y) ((x) < (y) ? (x) : (y))
#define max(x,y) ((x) > (y) ? (x) : (y))
#define XY(i) (int)((xyOffset + xy[i]) * SCALE)
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
if(nrhs < 2) mexErrMsgTxt("expects two parameters: adjency matrix (sparse, n times n), xy vector (n times 2)");
if(! mxIsSparse(IN_ADJ)) mexErrMsgTxt("Adjency matrix must be sparse");
if(mxGetM(IN_ADJ) != mxGetN(IN_ADJ)) mexErrMsgTxt("Adjency matrix must be square");
int n = mxGetN(IN_ADJ);
int nz = mxGetNzmax(IN_ADJ);
if(mxGetN(IN_XY) != 2 || mxGetM(IN_XY) != n) mexErrMsgTxt("XY vector must be (n times 2)");
double *xy = mxGetPr(IN_XY);
double *adj = mxGetPr(IN_ADJ);
// get dimensions
int i, j;
double xyOffset = 0, maxX = 0, maxY = 0;
for(i = 0; i < n; i++) {
if(xy[i] < xyOffset) xyOffset = xy[i];
if(xy[i] > maxX) maxX = xy[i];
}
for(i = n; i < 2*n; i++) {
if(xy[i] < xyOffset) xyOffset = xy[i];
if(xy[i] > maxY) maxY = xy[i];
}
xyOffset = -xyOffset;
maxX += xyOffset;
maxY += xyOffset;
mexPrintf("img size: %i x %i, xyOffset: %f, maxX: %f, maxY: %f\n", (int)(maxX * SCALE), (int)(maxY * SCALE), xyOffset, maxX, maxY);
IplImage *img = cvCreateImage(cvSize((int)(maxX * SCALE), (int)(maxY * SCALE)), IPL_DEPTH_8U, 3);
int *ir = mxGetIr(IN_ADJ);
int *jc = mxGetJc(IN_ADJ);
int total = 0;
CvPoint p1, p2;
for (i=0; i<n; i++) {
p1.x = XY(i);
p1.y = XY(n + i);
int starting_row_index = jc[i];
int stopping_row_index = jc[i+1];
if (starting_row_index == stopping_row_index) continue;
else {
for (j = starting_row_index; j < stopping_row_index; j++) {
p2.x = XY(ir[j]);
p2.y = XY(n + ir[j]);
cvLine(img, p1, p2, cvScalar(255,128,255,0), 1, CV_AA, 0);
// mexPrintf("\t(%d,%d) = %g || (%i %i) -- (%i %i)\n", ir[j]+1, i+1, adj[total++], p1.x, p1.y, p2.x, p2.y);
}
}
}
if(!cvSaveImage("/tmp/gr.png", img)) mexErrMsgTxt("Unable to write /tmp/gr.png");
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvShowImage("mainWin", img );
cvWaitKey(20);
} |