PROGRAMS CREATED WITH LOVE BY Z AND J



| zjprogram | | zjframeclip | | zjcov |

zjprogram

NAME
zjframeclip - Extracts a segment from an image sequence.

SYNOPSIS
zjframeclip
if={infile} of={outfile} s={start frame index} e={end frame index}

DESCRIPTION
Zjframeclip extracts a segment of consecutive frames from an
image sequence. The start and end indices are specified by the
parameters.


/**********************************************************************************/
/* zjprogram: Binary Image Preprocessing */
/**********************************************************************************/
#include "VisXV4.h" /* VisX structure include file */
#include "Vutil.h" /* VisX utility header files */
VXparam_t par[] =
{
{ "if=", 0, " input file vssum: compute temporal iamge running average"},
{ "of=", 0, " output file "},

{ 0, 0}
};
#define IVAL par[0].val
#define OVAL par[1].val

VisXfile_t *VXin, /* input file structure */
*VXout; /* output file structure */
VisXelem_t *VXlist,*VXpt; /* VisX data structure */

main(argc, argv)
int argc;
char *argv[];
{
VisXimage_t *ib; /* multiframe image buffer */
VisXimage_t tm,crop,crop2; /* temp image structures */
int i,j,k; /* index counters */
int n; /* number of frames to average */
int told; /* temp integer */
int first;
int m00; /* zeroth order moment: area of region */
int m01; /* 1st order moment: (x-coordinate of centre of mass)X(region area) */
int m10; /* 1st order moment: (y-coordinate of centre of mass)X(region area) */
int COMx, COMy; /* coordinates for centre of mass (COM) */
/* initialize dimensions of frame centred at region's COM (prior to downsampling) */
float box[] = {0, 250, 0, 455};
/* initialize downsampled frame dimensions */
float box2[] = {0, 125, 0, 227};

n=1; /* read 1 frame */

VXparse(&argc, &argv, par); /* parse the command line */
VXin = VXopen(IVAL, 0); /* open input file */
VXout = VXopen(OVAL, 1); /* open the output file */
ib = VXsbufimage(VXin, n); /* allocate buffer and read n-1 frames */

first = 1;
while(VXrbufimage(ib, VXin, n)) /* every frame */
{
VXfupdate(VXout, VXin); /* update global constants */
/********************* Application specific section ***************************/
/* make temp int image */
VXmakeimage(&tm, VX_PINT, ib[0].bbx, ib[0].chan);

/* make output image of new size to contain moving figure */
VXmakeimage(&crop, VX_PBYTE, box , 1);

/* make output image of new size to contain downsampled moving figure */
VXmakeimage(&crop2, VX_PBYTE, box2 , 1);

/* Remove fringes caused by interlacing of video frames via */
/* linear interpolation. */
/* Replace pixel values in every other column by the average */
/* of the 2 vertically adjacent pixel values */
for (i = tm.ylo+1; i <= tm.yhi; i=i+2)
{
for (j = tm.xlo; j <= tm.xhi; j++)
{
ib[0].u[i][j]=(ib[0].u[i-1][j]+ib[0].u[i+1][j])/2 ;
}
}


/* Thresholding: Pixel values above 65 are set to 255 */
for (i = tm.ylo; i <= tm.yhi; i=i++)
{
for (j = tm.xlo; j <= tm.xhi; j++)
{

if (ib[0].u[i][j]>65) ib[0].u[i][j] = 255;
else ib[0].u[i][j] = 0;

first=0;
}
}


/* 3X3 median filter */
for (i = tm.ylo+2; i <= tm.yhi-2; i=i++)
{
for (j = tm.xlo+2; j <= tm.xhi-2; j++)
{
if((ib[0].u[i-1][j+1]+ib[0].u[i][j+1]+ib[0].u[i+1][j+1]+
ib[0].u[i-1][j]+ib[0].u[i][j]+ib[0].u[i+1][j]+
ib[0].u[i-1][j-1]+ib[0].u[i][j-1]+ib[0].u[i+1][j-1]) >=1020)
tm.i[i][j]=255;
else tm.i[i][j]=0;

}
}



m00=0;
m01=0;
m10=0;

/* compute moments */
for (i = tm.ylo; i <= tm.yhi; i=i++)
{
for (j = tm.xlo; j <= tm.xhi; j++)
{
ib[0].u[i][j]=tm.i[i][j];
if(tm.i[i][j]==0){
m00++;
m10 += j;
m01 += i;
}
}
}

/+ compute COM using moments */
COMx = m10/m00;
COMy = m01/m00;


/* Crop moving figure within a 455X250 window centred around the COM */
for (i=0; i<455; i++){
for (j=0; j< 250; j++){
// crop.u[i][j]=ib[0].u[i][COMx-125+j];

if ((COMy-227+i)<455 && (COMy-227+i)>0){
crop.u[i][j]=ib[0].u[COMy-227+i][COMx-125+j];
}
else crop.u[i][j]=255;

}
}

/* Downsample by factor of 4: take every other pixel value */
for (i=0; i<227; i++){
for (j=0; j< 125; j++){
crop2.u[i][j]=crop.u[2*i][2*j];
}
}

VXwriteframe(VXout,crop2.list); /* write oldest frame */

/******************* End of the Application specific section ******************/
} /* end of every frame section */
if (first)
{
fprintf(stderr,"vssum: not enough frames in image set\n");
exit(1);
}
VXclose(VXin); /* close files */
VXclose(VXout);
exit(0);
}




zjframeclip

NAME
zjprogram - Binary Image Preprocessing

SYNOPSIS
zjprogram
if={infile} of={outfile}

DESCRIPTION
There are six things that zjprogram accomplishes.
1) Removal of fringe caused by video interlacing via linear interpolation.
2) Thresholding at pixel value of 65.
3) 3X3 median filtering to remove salt and pepper noise.
4) Compute figure's COM.
5) Contain figure within a 455X250 window centered at the COM.
6) Downsample cropped frames to 227X125.


/**********************************************************************************/
/* zjclipframe: Extracts a segment of consecutive frames from an image sequence */
/**********************************************************************************/
#include "VisXV4.h" /* VisX structure include file */
#include "Vutil.h" /* VisX utility header files */
VXparam_t par[] =
{
{ "if=", 0, " input file vssum: compute temporal iamge running average"},
{ "of=", 0, " output file "},
{ "s=", 0, "start frame"},
{ "e=", 0, "end frame"},
{ 0, 0}
};
#define IVAL par[0].val
#define OVAL par[1].val
#define SVAL par[2].val
#define EVAL par[3].val

VisXfile_t *VXin, /* input file structure */
*VXout; /* output file structure */
VisXelem_t *VXlist,*VXpt; /* VisX data structure */

main(argc, argv)
int argc;
char *argv[];
{
VisXimage_t *ib; /* multiframe image buffer */
VisXimage_t tm; /* temp image structure */
int i,j,k, counter; /* index counters */
int n; /* number of frames to read in */
int start, end; /* start and end frame values */
int first;

n=1;
VXparse(&argc, &argv, par); /* parse the command line */
VXin = VXopen(IVAL, 0); /* open input file */
VXout = VXopen(OVAL, 1); /* open the output file */
ib = VXsbufimage(VXin, n); /* allocate buffer and read n-1 frames */

first = 1;


start = (SVAL ? atoi(SVAL) : 1);
end = (EVAL ? atoi(EVAL) : 30);
while(VXrbufimage(ib, VXin, n) && start<=end )
/* every frame within desired segment*/
{
VXfupdate(VXout, VXin); /* update global constants */
/********************* Application specific section ***************************/
/* make temp int image */
VXmakeimage(&tm, VX_PFLOAT, ib[0].bbx, ib[0].chan);

start++;


first=0;
/* write desired frames into temporary image sequence */
for (i = tm.ylo; i <= tm.yhi; i++)
{
for (j = tm.xlo; j <= tm.xhi; j++)
{tm.f[i][j]=ib[0].f[i][j];
}
}


VXwriteframe(VXout,tm.list); /* write desired frames */

/******************* End of the Application specific section ******************/
} /* end of every frame section */
if (first)
{
fprintf(stderr,"vssum: not enough frames in image set\n");
exit(1);
}
VXclose(VXin); /* close files */
VXclose(VXout);
exit(0);
}




zjcov

NAME
zjcov - Finds the covariance matrices corresponding to image sequences.

SYNOPSIS
zjcov
if1={image sequence 1} if2={image sequence 2} if3={image sequence 3}
if4={image sequence 4} if5={image sequence 5} of={image sequence of covariance matrices}

DESCRIPTION
Performs frame by frame averaging over the input sequences.
Calculates the frame by frame covariance matrices of the input sequences.
Then Calculates the eigenvalues for each frame of the covariance matrices.
NOTE: Only works on 227X125 matrices.


/**********************************************************************************/
/* zjcov: Prepares Covariance matrices of image sequences */
/**********************************************************************************/
#include "VisXV4.h" /* VisX structure include file */
#include "Vutil.h" /* VisX utility header files */
#include "nrutil.h" /* Numerical Recipes Header Files */
#include "nr.h"
VXparam_t par[] =
{
{ "if1=", 0, " input file: image sequence used for cov matrix calculation"},
{ "if2=", 0, " input file: image sequence used for cov matrix calculation"},
{ "if3=", 0, " input file: image sequence used for cov matrix calculation"},
{ "if4=", 0, " input file: image sequence used for cov matrix calculation"},
{ "if5=", 0, " input file: image sequence used for cov matrix calculation"},
{ "of=", 0, " output file: image sequence of covariance matrices "},
{ 0, 0, 0}
};
#define I1VAL par[0].val
#define I2VAL par[1].val
#define I3VAL par[2].val
#define I4VAL par[3].val
#define I5VAL par[4].val
#define OVAL par[5].val

VisXfile_t *VXin1, *VXin2, *VXin3, *VXin4, *VXin5, /* input file structures */
*VXout; /* output file structure */
VisXelem_t *VXlist,*VXpt; /* VisX data structure */

/*routine to find eigenvalues of a tridiagonal matrix (from numerical recipes in c) */
void zjli(float d[], float e[],int n, float z[][]){
int m,l,iter,i,k;
float s,r,p,g,f,dd,c,b;
void nrerror();


for (i=2; i<=n; i++) e[i-1]=e[i];
e[n] = 0.0;
for (l=1; l<=n; l++){
iter=0;
do{
for (m=l; m<=n-1; m++){
dd=fabs(d[m])+fabs(d[m+1]);
if((float)(fabs(e[m])+dd) == dd) break;
}
if (m!=l) {
if(iter++ == 1000) nrerror("too many iterations");
g=(d[l+1]-d[l])/(2.0*e[l]);
r=sqrt((g*g)+1.0);
g=d[m]-d[l]+e[l]/(g+SIGN(r,g));
s=c=1.0;
p=0.0;
for (i=m-1; i>=l; i--){
f=s*e[i];
b=c*e[i];
if (fabs(f) >= fabs(g)) {
c=g/f;
r=sqrt((c*c)+1.0);
e[i+1]=f*r;
c *= (s=1.0/r);
} else{
s=f/g;
r=sqrt((s*s)+1.0);
e[i+1]=g*r;
s *= (c=1.0/r);
}
g=d[i+1]-p;
r=(d[i]-g)*s+2.0*c*b;
p=s*r;
d[i+1]=g+p;
g=c*r-b;
}
d[l]=d[l]-p;


e[l]=g;
e[m]=0.0;
}
} while(m != l);
}
}


main(argc, argv)
int argc;
char *argv[];
{
VisXimage_t *ib1, *ib2, *ib3, *ib4, *ib5; /* multiframe image buffers */
VisXimage_t tm1, tm2, tm3, tm4, tm5, tm6; /* temporary image structures */
int i,j,k; /* index counters */
int n; /* number of frames to average */
int first; /* set off error message if not enough frames in image set */
float box[] = {0, 227, 0, 227}; /* bounding box for output image */


float **a;
float d[227];
float e[227];
int order=227;
a =(float*) malloc(227*sizeof(float*));

for(i=1;i<=227;i++)
a[i] = (float*) malloc(227*sizeof(float));

n=1; /* read 1 frame each loop */




VXparse(&argc, &argv, par); /* parse the command line */
VXin1 = VXopen(I1VAL, 0); /* open input file */
VXin2 = VXopen(I2VAL, 0); /* open input file */
VXin3 = VXopen(I2VAL, 0); /* open input file */
VXin4 = VXopen(I2VAL, 0); /* open input file */
VXin5 = VXopen(I2VAL, 0); /* open input file */
VXout = VXopen(OVAL, 1); /* open the output file */
ib1 = VXsbufimage(VXin1, n); /* allocate buffer and read n-1 frames */
ib2 = VXsbufimage(VXin2, n); /* allocate buffer and read n-1 frames */
ib3 = VXsbufimage(VXin3, n); /* allocate buffer and read n-1 frames */
ib4 = VXsbufimage(VXin4, n); /* allocate buffer and read n-1 frames */
ib5 = VXsbufimage(VXin5, n); /* allocate buffer and read n-1 frames */

first = 1;
while(VXrbufimage(ib1, VXin1, n)&& VXrbufimage(ib2, VXin2,n)
&& VXrbufimage(ib3, VXin3,n) && VXrbufimage(ib4, VXin4,n) && VXrbufimage(ib5, VXin5,n)) /* every frame */
{
VXfupdate(VXout, VXin2); /* update global constants */
/********************* Application specific section ***************************/
/* Make temp int images */
VXmakeimage(&tm1, VX_PFLOAT, ib1[0].bbx, ib1[0].chan);
VXmakeimage(&tm2, VX_PFLOAT, ib1[0].bbx, ib1[0].chan);
VXmakeimage(&tm3, VX_PFLOAT, ib1[0].bbx, ib1[0].chan);
VXmakeimage(&tm4, VX_PFLOAT, ib1[0].bbx, ib1[0].chan);
VXmakeimage(&tm5, VX_PFLOAT, ib1[0].bbx, ib1[0].chan);
VXmakeimage(&tm6, VX_PFLOAT, box, 1);

/* Calculate (x-m_x) where x = image sequence, m_x = mean image sequence */
for (i = tm1.ylo; i <= tm1.yhi; i++)
{
for (j = tm1.xlo; j <= tm1.xhi; j++)
{
tm1.f[i][j] = ib1[0].f[i][j]-(ib1[0].f[i][j]+ib2[0].f[i][j]+ib3[0].f[i][j]+
ib4[0].f[i][j]+ ib5[0].f[i][j])/5;
tm2.f[i][j] = ib2[0].f[i][j]-(ib1[0].f[i][j]+ib2[0].f[i][j]+ib3[0].f[i][j]+
ib4[0].f[i][j]+ ib5[0].f[i][j])/5;
tm3.f[i][j] = ib3[0].f[i][j]-(ib1[0].f[i][j]+ib2[0].f[i][j]+ib3[0].f[i][j]+
ib4[0].f[i][j]+ ib5[0].f[i][j])/5;
tm4.f[i][j] = ib4[0].f[i][j]-(ib1[0].f[i][j]+ib2[0].f[i][j]+ib3[0].f[i][j]+
ib4[0].f[i][j]+ ib5[0].f[i][j])/5;
tm5.f[i][j] = ib5[0].f[i][j]-(ib1[0].f[i][j]+ib2[0].f[i][j]+ib3[0].f[i][j]+
ib4[0].f[i][j]+ ib5[0].f[i][j])/5 ;
}
}

/* Compute (x-m_x)(x-m_x)^T of each sequence and sum all to obtain Cov matrix */
for (k = tm1.ylo; k <= tm1.yhi; k++)
for (i = tm1.ylo ; i <= tm1.yhi ; i++)
for (j = tm1.xlo; j <= tm1.xhi; j++)
tm6.f[i][226-k] += tm1.f[i][j]*tm1.f[k][j] + tm2.f[i][j]*tm2.f[k][j] +
tm3.f[i][j]*tm2.f[k][j] + tm4.f[i][j]*tm2.f[k][j] + tm5.f[i][j]*tm2.f[k][j] ;

for (i = tm1.ylo ; i <= tm1.yhi ; i++)
for (j = tm1.xlo; j <= tm1.xhi; j++)
a[i+1][j+1] = tm3.f[i][j];


/* Tridiagonalize the matrices (necessary for zjli)*/
tred2(a,order,d,e);

/* Compute the eigenvalues*/
zjli(d,e,order,a);


if (first == 30){
for(i=1; i<=227; i++)
printf("%4.3f\t",d[i]);
}


first++;
// VXwriteframe(VXout,tm3.list); /* write oldest frame */

/******************* End of the Application specific section ******************/
}
first =0; /* end of every frame section */
if (first)
{
fprintf(stderr,"vssum: not enough frames in image set\n");
exit(1);
}
VXclose(VXin1); /* close files */
VXclose(VXin2); /* close files */
VXclose(VXout);
exit(0);
}