/* --------------------------------------------------------------------- */ /* - p1.C - */ /* --------------------------------------------------------------------- */ /* - This programs tests the various functions in vks101.c and - */ /* - the ITEX calls. - */ /* --------------------------------------------------------------------- */ /* - Some necessary includes... - */ /* --------------------------------------------------------------------- */ #include #include #include #include #include #include #define NO 0 #define YES 1 #define esc 27 struct sgttyb tab6; struct sgttyb *addr = &tab6; int result; int x,y; int values[1024]; int harray[1024]; int c; int fp; int result; char *name; short done = 0; main() { int cmd; int mode; char buffer[80]; int base, i, result, r; char req; int x1, y1, x2, y2; int dx, threshold; int length, xvals[1024], yvals[1024]; mode = 2; name = "/dev/tty"; system (buffer); fp = open(name, mode); if (fp != -1) do { initialize_vision_system(); fflush(stdin); menu(); set_raw_mode(); cmd = toupper(getchar()); fflush(stdin); set_normal_mode(); fflush(stdin); switch (cmd) { case 'A' : system("clear"); optimal_binary_theshold(); break; case 'B' : system("clear"); night_vision2(); break; case 'E' : done = 1; break; } }while (done == 0); } menu() { int test; system("clear"); GotoXY(10,14);printf("A : Find optimal binary threshold."); GotoXY(14,14); printf("B : Run night vision simulator."); GotoXY(18,14); printf("E : Exit."); GotoXY(22,14); printf("Please pick a function : "); } /*************************************************************************** THIS FUNCTION IS USED BY VIS$OPTIMAL_THRESHOLD(); TO SMOOTH OUT THE FUNCTION THAT IS RETURNED BY VIS$CS455_HISTOGRAM(); . This function is the same as VIS$SMOOTH_FUNCTION() but without graphics *************************************************************************/ void Smooth_Function(y, smooth_factor) int *y, smooth_factor; { int i,j,t; int base = 455; /* must be same as CS455-histogram */ int original[256]; int temp[256]; int how_many = 5; /* default is 5 */ int sum = 0; int largest; float scale; how_many = smooth_factor; for (i=0;i<=255;i++) original[i] = y[i]; t = 0; for (i=0; i<=255;i++) /* was 246 RWW */ temp[i] = 0; for (i=how_many;i < (255 - how_many);i++) /* was 246 RWW */ { sum = 0; for (j=i-how_many;j <= i+how_many; j++) { sum += y[j]; } /* end for j */ temp[i] = sum / (how_many * 2 + 1); } /* end for i */ largest = 0; for (i=0;i <= 255;i++) if (original[i] > largest) largest = original[i]; scale = 1.0/((float)largest/((float)base-25.0)); for (i=0;i <= 254;i++) temp[i] = temp[i] * scale; for (i=0;i <= 254;i++) original[i] = original[i] * scale; for (i=0; i <= 255;i++) /* was 246 RWW */ { y[i] = temp[i]; } } /* end function */ optimal_binary_theshold() { int value,peak,i; int command=1; int harray[256]; while (command > 0) { system("clear"); initialize_vision_system(); GotoXY(14,14);printf("Enter step value for smoothing funtion : "); scanf("%d",&command); if (command == 0) break; fflush(stdin); VIS$FREEZE(); HISTOGRAM(0,0,640,480,NO,harray,2); Smooth_Function(harray,command); peak = 0; i = 1; while (i < 255) { while ((harray[i-1]<=harray[i]) && (harray[i]<= harray[i+1]) && (i<255)) i++; i++; peak+=1; while ((harray[i-1] >= harray[i])&& (harray[i] >= harray[i+1]) && (i<255)) i++; i++; } printf("peaks = %d",peak); if (peak == 2) { if (command >0) value = VIS$OPTIMAL_THRESHOLD(0,0,640,480,NO,9,command); GotoXY(25,14);printf("Optimal Binary Threshold = %d",value); } /* end if */ getkey(); getkey(); } /* end while */ } night_vision2() { int harray[255]; VIS$FREEZE(); HISTOGRAM(0,0,640,480,YES,harray,2); getkey(); } /************ Function sets keyboard input to unbuffered ****************/ set_raw_mode() { /* addr->sg_flags = 0300; /* MVAXII was 0310 in octal RWW fixed 8/9/90 */ result = ioctl(fp, TIOCGETP, addr); addr->sg_flags = O_RAW; /* RWW fixed 7/21/91for SUN */ addr->sg_ispeed = 13; /* 7 is 300 baud 13 is 9600 baud*/ addr->sg_ospeed = 13; /* 7 is 300 baud 13 is 9600 baud*/ result = ioctl(fp, TIOCSETP, addr); } /************ Function sets keyboard input back to buffered ************/ set_normal_mode() { /* addr->sg_flags = 0300; /* MVAXII was 0310 in octal RWW fixed 8/9/90 */ addr->sg_flags = O_ALLDELAY | ECHO | CRMOD; /* RWW fixed 7/21/91for SUN */ addr->sg_ispeed = 13; /* 7 is 300 baud 13 is 9600 baud*/ addr->sg_ospeed = 13; /* 7 is 300 baud 13 is 9600 baud*/ result = ioctl(fp, TIOCSETP, addr); } /************** Function to wait for key to be pressed ********/ getkey() { int cmd; printf("\n\nHit any key to continue"); set_raw_mode(); cmd = toupper(getchar()); fflush(stdin); set_normal_mode(); fflush(stdin); } /*********** Function moves the cursor to the screen coordinates X ( lines down from top), Y ( spaces in from right )***********/ GotoXY( x, y ) int x, y; { printf("\n"); printf( "%c%c%d%c%d%c", (char)esc, '[', x, ';', y, 'H' ); } initialize_vision_system() { VIS$OPEN(); VIS$INIT_FB(); VIS$INIT_LUTS(); VIS$VIEW(); setcamera(0); } /****************** functions from vks101.c ************************/ /*** histogram is the same as VIS$CS455HISTOGRAM but with out graphics */ HISTOGRAM(x1,y1,x2,y2,equalize,harray, color) int x1,y1,x2,y2, equalize, *harray, color; { int i,x; int offset,base,largest,grpfact,barwidth,area,X1,X2; int values[641]; float scale,eos; /* CONSTANTS */ grpfact=4; barwidth=8; base= 455 /* was 475 RWW */ ; area=(y2-y1)*(x2-x1); eos=256/grpfact-1; /* End Of Scale */ /* INITIALIZE ARRAYS */ for (i=0;i<=255;i++) { harray[i] = 0; } /* FILL HARRAY WITH READ_HORIZONTAL_LINE this is the histogram */ for (i=y1;i<=y2;i++) { rhline(x1,i,x2-x1,values); for (x=0;x value[i])&& (value[i] > value[i+1])) i++; done = YES; } VIS$BLOCK (i*2 - width,300,i*2 + width,base,color); return(i); /* return the threshold */ } equalize_histogram_dave(harray,area) int harray[255]; int area; { int values[256]; float t[255],sum[255],increment,tolerance; int i,x,cmdd ; int earray[255]; /* for histogram */ /* INITIALIZE ARRAYS */ for (i=0;i<255;i++) { t[i]=0.0; sum[i]=0.0; earray[i]=0; } /* TRANSFORM HARRAY INTO T */ for (i=0;i<255;i++) t[i] = (float)harray[i]/(float)area; /* SUMMATION OF T */ for (i=0;i<255;i++) { if (i==0) sum[i] = t[0]; else sum[i] = sum[i-1] + t[i]; } mapsums(sum,values); } mapsums(sum,values) int values[256]; float sum[255]; { float temp[255]; int i,x; for (i=0;i<255;i++) { if (i==0) temp[i] = 0; else temp[i] = i*1.0/255.0 + 1/510; } for (i=0;i<255;i++) { x=0; while ( temp[x] < sum[i]) x++; values[i] = x; } VIS$LOAD_LUT (0, 14, values); VIS$SET_MODE(1); VIS$FREEZE(); VIS$SET_MODE(0); }