1: #include <stdlib.h>
2: #include <stdio.h>
3: #include <string.h>
4: #include <math.h>
5: #include <GL/glut.h>
6:
7: #include "pgm.h"
8: #include "chaincode.h"
9:
10: #define Round(v) ((int)(v+0.5))
11:
12: /*minimal size to consider an "object" as length of chaincode*/
13: #define DECENT_SIZED_OBJECT 100
14:
15: #define ONE_THIRD (1.0 / 3.0)
16: #define ONE_HALF (1.0 / 2.0)
17: #define ONE_SIXTH (1.0 / 6.0)
18: #define ONE_TWELTH (1.0 / 12.0)
19:
20: typedef struct
21: {
22: coord corner1;
23: coord corner2;
24: coord corner3;
25: coord corner4;
26: }object;
27:
28: typedef struct
29: {
30: /*The corners of the board. corner1 and corner2 are always opposite
31: (diagnol), just like corner3 and corner4 too.*/
32: object corners;
33:
34: /*The sides. The numbers represent the to corners that the point is
35: between. The first number is the corner that it is closest to.*/
36: coord side14, side13, side23, side24;
37: coord side31, side32, side41, side42;
38:
39: /*The points surrounding the middle square. The number indicates the
40: closest corner.*/
41: coord middle1, middle2, middle3, middle4;
42: }ttt;
43:
44: /*Evil globals, but not do-able otherwise.*/
45: static PGMImage *img_cur; /*current*/
46: static PGMImage *img_original; /*original*/
47: static PGMImage *img_pers_corr; /*perspective correction*/
48: static PGMImage *img_grayscale;
49: static PGMImage *img_moravec; /*moravec*/
50: static int HSIZE;
51: static int VSIZE;
52: static int MVAL;
53:
54: static ttt *ttt_board; /*pointer to a ttt structer containing the board info*/
55:
56: const RGB_INT white = {255, 255, 255};
57: const RGB_INT yellow = {255, 255, 0};
58: const RGB_INT magenta = {255, 0, 255};
59: const RGB_INT cyan = {0, 255, 255};
60: const RGB_INT red = {255, 0, 0};
61: const RGB_INT green = {0, 255, 0};
62: const RGB_INT blue = {0, 0, 255};
63: const RGB_INT black = {0, 0, 0};
64:
65: const RGB_INT gray = {128, 128, 128};
66: const RGB_INT lt_yellow = {255, 255, 128};
67: const RGB_INT lt_magenta = {255, 128, 255};
68: const RGB_INT lt_cyan = {128, 255, 255};
69:
70: const RGB_INT lt_red = {255, 128, 128};
71: const RGB_INT lt_green = {128, 255, 128};
72: const RGB_INT lt_blue = {128, 128, 255};
73:
74: const RGB_INT dk_yellow = {128, 128, 0};
75: const RGB_INT dk_magenta = {128, 0, 128};
76: const RGB_INT dk_cyan = { 0, 128, 128};
77:
78: const RGB_INT dk_red = {128, 0, 0};
79: const RGB_INT dk_green = { 0, 128, 0};
80: const RGB_INT dk_blue = { 0, 0, 128};
81:
82: /*pointer to an array of pointers which point to the first nodes in each of
83: the chain codes.*/
84: list_info* chain_codes;
85:
86: /*hold the points farthest away from each other for each object*/
87: /*coord corner1[MAX_CHAINS], corner2[MAX_CHAINS];
88: coord corner3[MAX_CHAINS], corner4[MAX_CHAINS];*/
89: /*object all_objects[MAX_CHAINS];*/
90: static object *all_objects;
91:
92: /*use double buffered output and keep running enable idle callback to call
93: detect_corners() over and over simulating realtime response.
94: Is set to TRUE or FALSE.*/
95: int is_buffered;
96:
97: /*used to determine if abstract lines should be drawn to the screen.
98: Set to <0 if no abstract lines are to be drawn. Otherwise
99: is set to the number of objects found (AKA number of chaincodes).*/
100: int draw_abstract_lines = -1;
101:
102: /*used to draw the single object most likely to be considered the
103: tic-tac-toe board. Should be <0 if this should not be drawn.
104: Should be equal to the chain-code number (from 0 to MAX_CHAINS - 1)
105: if it is to be drawn.*/
106: int draw_abstract_board = -1;
107:
108:
109: /**************Drawing funcitions************************************/
110: /********************************************************************/
111:
112: void setCPixel(int ix, int iy, RGB_INT color)
113: /*Same as setIPixel except that the last parameter is an RGB color*/
114: {
115: float x = (ix*2.0)/HSIZE - 1.0;
116: float y = (iy*2.0)/VSIZE - 1.0;
117:
118: float red = (float)color.red/(float)MVAL;
119: float green = (float)color.green/(float)MVAL;
120: float blue = (float)color.blue/(float)MVAL;
121:
122: glColor3f(red, green, blue);
123:
124: glBegin(GL_POINTS);
125: glVertex2f (x, y);
126: glEnd();
127: }
128:
129: void setCLines(int ix1, int iy1, int ix2, int iy2, RGB_INT color)
130: /*Similar as setIPixel except that this one draws a line between the first set
131: of points given and the second set in the RGB color specified*/
132: {
133: float x1 = (ix1*2.0)/HSIZE - 1.0;
134: float y1 = (iy1*2.0)/VSIZE - 1.0;
135: float x2 = (ix2*2.0)/HSIZE - 1.0;
136: float y2 = (iy2*2.0)/VSIZE - 1.0;
137:
138: float red = (float)color.red/(float)MVAL;
139: float green = (float)color.green/(float)MVAL;
140: float blue = (float)color.blue/(float)MVAL;
141:
142: glColor3f(red, green, blue);
143:
144: glBegin(GL_LINES);
145: glVertex2f (x1, y1);
146: glVertex2f (x2, y2);
147: glEnd();
148: }
149:
150: void setCRect(int ix1, int iy1, int ix2, int iy2, RGB_INT color)
151: /*Similar as setIPixel except that this one draws a line between the first set
152: of points given and the second set in the RGB color specified*/
153: {
154: float x1 = (ix1*2.0)/HSIZE - 1.0;
155: float y1 = (iy1*2.0)/VSIZE - 1.0;
156: float x2 = (ix2*2.0)/HSIZE - 1.0;
157: float y2 = (iy2*2.0)/VSIZE - 1.0;
158:
159: float red = (float)color.red/(float)MVAL;
160: float green = (float)color.green/(float)MVAL;
161: float blue = (float)color.blue/(float)MVAL;
162:
163: glColor3f(red, green, blue);
164:
165: glBegin(GL_POLYGON);
166: glVertex2f (x1, y1);
167: glVertex2f (x1, y2);
168: glVertex2f (x2, y2);
169: glVertex2f (x2, y1);
170: glEnd();
171: }
172:
173: /* =================================================================
174: * drawString - outputs a string of characters to the graphics port
175: *
176: * x, y: defines the starting location to draw the text
177: * note: this point is the lower left anchor of
178: * the first character - a character's decending
179: * portion would be drawn below this point.
180: * theFont: points to the glut font to be used
181: * theString: holds the string to be output -- up to 255 ch
182: * ----------------------------------------------------------------- */
183: void drawString(int ix, int iy, void *theFont, char theString[256],
184: RGB_INT color)
185: {
186: float x = (ix*2.0)/HSIZE - 1.0;
187: float y = (iy*2.0)/VSIZE - 1.0;
188: int i;
189:
190: float red = (float)color.red/(float)MVAL;
191: float green = (float)color.green/(float)MVAL;
192: float blue = (float)color.blue/(float)MVAL;
193:
194: glColor3f(red, green, blue);
195:
196: glRasterPos2f(x, y);
197: for (i = 0; theString[i] != '\0'; i++) /* draw the chars one at a time */
198: glutBitmapCharacter(theFont, theString[i]);
199: }
200:
201: void showColor (PGMImage *img)
202: {
203: int i, j; /*loop counting: i = y, j = x*/
204:
205: GLubyte checkImage[(*img).height][(*img).width][3];
206:
207: for(i = 0; i < (*img).height; i++)
208: {
209: for(j = 0; j < (*img).width; j++)
210: {
211: checkImage[i][j][0] = (GLubyte) (*img).data[i][j].red;
212: checkImage[i][j][1] = (GLubyte) (*img).data[i][j].green;
213: checkImage[i][j][2] = (GLubyte) (*img).data[i][j].blue;
214: }
215: }
216: /*draw the current image*/
217: glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
218: glRasterPos2f(-1, -1);
219: glDrawPixels((*img).width, (*img).height, GL_RGB,
220: GL_UNSIGNED_BYTE, checkImage);
221:
222: glFlush();
223: }
224:
225: void display_labels(int x_coord, int y_coord)
226: {
227: char text[40]; /*for displaying the locations in text form*/
228: int offset = 8; /*offset text # pixels from location*/
229:
230: sprintf(text, "(%d, %d)\0", x_coord, y_coord);
231: drawString(x_coord + offset, y_coord + offset,
232: GLUT_BITMAP_TIMES_ROMAN_10, text, blue);
233: }
234:
235: /*display the global abstract data*/
236: void showAbstract(object* object_list, ttt *ttt_data)
237: {
238: int i;
239:
240: /* coord hor_top1, hor_top2;
241: coord hor_bot1, hor_bot2;
242: coord ver_rgt1, ver_rgt2;
243: coord ver_lft1, ver_lft2;
244: */
245: list_info temp;
246:
247: glPointSize(2); /*make points more visible, if desired*/
248: /*glLineWidth(4);*/
249:
250: /*draw the chaincodes*/
251: /*chain_codes is a global pointer to an array of list_info types*/
252: for(i = 0; (i < MAX_CHAINS) && chain_codes && chain_codes[i].cur; i++)
253: {
254: memcpy(&temp, &chain_codes[i], sizeof(list_info));
255: while(RetrieveNextNode(&temp).cur)
256: {
257: setCPixel(RetrieveInfo(&temp).location.x,
258: RetrieveInfo(&temp).location.y, gray);
259: Advance(&temp);
260: }
261: }
262:
263: /*first check for non-null pointer, next check for dereferenced pointers
264: in the array of head pointers and lastly make sure things stay in
265: bound of the max incase all MAX_CHAINS number of chains are used.*/
266: /*draw_abstract_lines is the global that holds the number of "objects"
267: to draw abstract information for*/
268: for(i = 0; i < draw_abstract_lines && i < MAX_CHAINS; i++)
269: {
270: setCLines(object_list[i].corner1.x, object_list[i].corner1.y,
271: object_list[i].corner3.x, object_list[i].corner3.y,yellow);
272: setCLines(object_list[i].corner4.x, object_list[i].corner4.y,
273: object_list[i].corner2.x, object_list[i].corner2.y,yellow);
274: setCLines(object_list[i].corner3.x, object_list[i].corner3.y,
275: object_list[i].corner2.x, object_list[i].corner2.y,yellow);
276: setCLines(object_list[i].corner4.x, object_list[i].corner4.y,
277: object_list[i].corner1.x, object_list[i].corner1.y,yellow);
278:
279: setCPixel(object_list[i].corner1.x, object_list[i].corner1.y,dk_red);
280: setCPixel(object_list[i].corner2.x, object_list[i].corner2.y,dk_blue);
281: setCPixel(object_list[i].corner3.x, object_list[i].corner3.y,white);
282: setCPixel(object_list[i].corner4.x, object_list[i].corner4.y,dk_green);
283:
284: /*labels for middle points*/
285: display_labels(ttt_data->corners.corner1.x, ttt_data->corners.corner1.y);
286: display_labels(ttt_data->corners.corner2.x, ttt_data->corners.corner2.y);
287: display_labels(ttt_data->corners.corner3.x, ttt_data->corners.corner3.y);
288: display_labels(ttt_data->corners.corner4.x, ttt_data->corners.corner4.y);
289: }
290:
291: /*if there is board to draw and just make sure there isn't a NULL pointer*/
292: if((draw_abstract_board > -1) && ttt_data)
293: {
294: /*This code should draw the bounding box*/
295: /*setCLines(ttt_data->corners.corner1.x,
296: ttt_data->corners.corner1.y,
297: ttt_data->corners.corner3.x,
298: ttt_data->corners.corner3.y,blue);
299: setCLines(ttt_data->corners.corner4.x,
300: ttt_data->corners.corner4.y,
301: ttt_data->corners.corner2.x,
302: ttt_data->corners.corner2.y,blue);
303: setCLines(ttt_data->corners.corner3.x,
304: ttt_data->corners.corner3.y,
305: ttt_data->corners.corner2.x,
306: ttt_data->corners.corner2.y,blue);
307: setCLines(ttt_data->corners.corner4.x,
308: ttt_data->corners.corner4.y,
309: ttt_data->corners.corner1.x,
310: ttt_data->corners.corner1.y,blue);
311: setCPixel(ttt_data->corners.corner1.x,
312: ttt_data->corners.corner1.y, red);
313: setCPixel(ttt_data->corners.corner2.x,
314: ttt_data->corners.corner2.y, red);
315: setCPixel(ttt_data->corners.corner3.x,
316: ttt_data->corners.corner3.y, red);
317: setCPixel(ttt_data->corners.corner4.x,
318: ttt_data->corners.corner4.y, red);
319: */
320:
321: setCLines(ttt_data->side13.x,
322: ttt_data->side13.y,
323: ttt_data->side42.x,
324: ttt_data->side42.y,blue);
325: setCLines(ttt_data->side31.x,
326: ttt_data->side31.y,
327: ttt_data->side24.x,
328: ttt_data->side24.y,blue);
329: setCLines(ttt_data->side23.x,
330: ttt_data->side23.y,
331: ttt_data->side41.x,
332: ttt_data->side41.y,blue);
333: setCLines(ttt_data->side32.x,
334: ttt_data->side32.y,
335: ttt_data->side14.x,
336: ttt_data->side14.y,blue);
337:
338: setCPixel(ttt_data->middle1.x, ttt_data->middle1.y, red);
339: setCPixel(ttt_data->middle2.x, ttt_data->middle2.y, red);
340: setCPixel(ttt_data->middle3.x, ttt_data->middle3.y, red);
341: setCPixel(ttt_data->middle4.x, ttt_data->middle4.y, red);
342:
343: /*labels for middle points*/
344: display_labels(ttt_data->middle1.x, ttt_data->middle1.y);
345: display_labels(ttt_data->middle2.x, ttt_data->middle2.y);
346: display_labels(ttt_data->middle3.x, ttt_data->middle3.y);
347: display_labels(ttt_data->middle4.x, ttt_data->middle4.y);
348:
349: /*lables for side points*/
350: display_labels(ttt_data->side13.x, ttt_data->side13.y);
351: display_labels(ttt_data->side14.x, ttt_data->side14.y);
352: display_labels(ttt_data->side23.x, ttt_data->side23.y);
353: display_labels(ttt_data->side24.x, ttt_data->side24.y);
354: display_labels(ttt_data->side31.x, ttt_data->side31.y);
355: display_labels(ttt_data->side32.x, ttt_data->side32.y);
356: display_labels(ttt_data->side41.x, ttt_data->side41.y);
357: display_labels(ttt_data->side42.x, ttt_data->side42.y);
358: }
359: glFlush();
360: }
361:
362: /**********************Support functions*******************************/
363: /***********************************************************************/
364:
365: void pxlcpy(PGMImage *dest, int dest_row, int dest_col,
366: PGMImage *src, int src_row, int src_col)
367: {
368: /*make sure values are within bounds*/
369: if(dest_col > 0 && dest_col < (*dest).width
370: && dest_row > 0 && dest_row < (*dest).height
371: && src_col > 0 && src_col < (*src).width
372: && src_row > 0 && src_row < (*src).height)
373: {
374: (*dest).data[dest_row][dest_col].red =
375: (*src).data[src_row][src_col].red;
376:
377: (*dest).data[dest_row][dest_col].green =
378: (*src).data[src_row][src_col].green;
379:
380: (*dest).data[dest_row][dest_col].blue =
381: (*src).data[src_row][src_col].blue;
382: }
383: }
384:
385: int rgb_avg(RGB_INT cur_pxl)
386: {
387: /*convert each RGB to the average of the original*/
388: return ((cur_pxl.red + cur_pxl.green + cur_pxl.blue) / 3);
389: }
390:
391: /*Returns average (with RGB avg) pixel value for the image passed in.*/
392: int img_pxl_avg(PGMImage* img)
393: {
394: int i, j; /*loop counting*/
395: int sum = 0;
396: int temp;
397:
398: for(i = 0; i < (*img).height; i++)/*collumn*/
399: for(j = 0; j < (*img).width; j++)/*row*/
400: sum += rgb_avg((*img).data[i][j]);
401:
402: printf("sum: %d height: %d width: %d\n", sum, (*img).height, (*img).width);
403: temp = (sum / ((*img).height * (*img).width));
404: printf("temp: %d\n", temp);
405: return temp;
406: }
407:
408: /*1st: pixel one of type RGB_INT
409: 2nd: pixel one of type RGB_INT
410: 3rd: differnce allowed to be considered "equal" or close enough*/
411: int pxlcmp (RGB_INT pxl1, RGB_INT pxl2, int range)
412: {
413: return ((abs((rgb_avg(pxl1) - rgb_avg(pxl2)))) < range);
414: }
415:
416: /*return >0 if number of pixels is greater than img. pxl. avg., return <0 if
417: number of pixesl is less than img. pxl. avg. and return zero of equal*/
418: int background(int treash_value, PGMImage* img)
419: {
420: int i, j; /*loop counting*/
421: int pxl_less = 0, pxl_more = 0;
422:
423: for(i = 0; i < (*img).height; i++)/*collumn*/
424: for(j = 0; j < (*img).width; j++)/*row*/
425: {
426: if(rgb_avg((*img).data[i][j]) < treash_value)
427: pxl_less++;
428:
429: if(rgb_avg((*img).data[i][j]) > treash_value)
430: pxl_more++;
431: }
432:
433: if(pxl_less > pxl_more)
434: return -1;
435: else if(pxl_less < pxl_more)
436: return 1;
437: else
438: return 0;
439: }
440:
441: /*Used by showColor() and detect_pieces()*/
442: coord find_dividers(coord point1, coord point2, float t)
443: {
444: coord temp;
445:
446: temp.x = (int)(((1.0 - t) * point1.x) + (t * point2.x));
447: temp.y = (int)(((1.0 - t) * point1.y) + (t * point2.y));
448:
449: return temp;
450: }
451:
452: /******Perspective correction*******************************************
453: ***********************************************************************/
454: void pers_corr(PGMImage* new_img, PGMImage* org_img)
455: {
456: /*i and j are the left half, k and l are the right half*/
457: float i, k; /*loop counting*/
458: int j, l, row; /*loop counting*/
459: int old_i, old_k;
460:
461: float ins_s = 2.0; /*insert constant starting value*/
462: float ins_k = ins_s; /*insert constant*/
463:
464: /*The halfway marks in the width.*/
465: int mid_width_left = ((*new_img).width / 2) - 1;
466: int mid_width_right = ((*new_img).width / 2);
467:
468: /*just to be thourough clear the memory and reset maxes*/
469: memset(new_img, 0, sizeof(PGMImage));
470: (*new_img).height = (*org_img).height;
471: (*new_img).width = (*org_img).width;
472: (*new_img).maxVal = (*org_img).maxVal;
473:
474: /****x direction correction******/
475:
476: /*Loop through each row from top to bottom...*/
477: for(row = ((*new_img).height - 1); row >= 0; row--)
478: {
479: /*...reset moire interference removal counter...*/
480: old_i = ((*new_img).width / 2) - 1;
481: old_k = ((*new_img).width / 2);
482:
483: /*...so each half is ajusted to remove perspective effect...*/
484: for(i = j = mid_width_left, k = l = mid_width_right
485: ; i >= 0, j >= 0, k < (*new_img).width, l < (*new_img).width
486: ; i -= ins_k, j--, k += ins_k, l++)
487: {
488: for(;old_i >= (int)i; old_i--) /*...in the left half...*/
489: pxlcpy(new_img, row, old_i, org_img, row, j);
490: for(;old_k <= (int)k; old_k++) /*...in the right half.*/
491: pxlcpy(new_img, row, old_k, org_img, row, l);
492: }
493: /*Move the new image x_coord pixel counter to next new image pixel*/
494: ins_k -= ((ins_s - 1.0) / (*new_img).height);
495: }
496: }
497:
498: /*****convert color to grayscale****************************************
499: ***********************************************************************/
500: void color_to_gray(PGMImage* new_img, PGMImage* org_img)
501: {
502: int row, col; /*loop counting*/
503: RGB_INT cur_pxl; /*current pixel*/
504:
505: (*new_img).height = (*org_img).height;
506: (*new_img).width = (*org_img).width;
507: (*new_img).maxVal = (*org_img).maxVal;
508:
509: /*Starting with the top row...*/
510: for(row = (*new_img).height - 1; row >= 0; row--)
511: for(col = 0; col < (*new_img).width - 1; col++)
512: {
513: cur_pxl = (*org_img).data[row][col]; /*more readable*/
514:
515: /*convert each RGB to the average of the original*/
516: (*new_img).data[row][col].red = rgb_avg(cur_pxl);
517: (*new_img).data[row][col].green = rgb_avg(cur_pxl);
518: (*new_img).data[row][col].blue = rgb_avg(cur_pxl);
519: }
520: }
521:
522: /*******Edge highlighting**********************************************
523: **********************************************************************/
524: void moravec(PGMImage* new_img, PGMImage* org_img)
525: {
526: int row, col; /*loop counting*/
527: int i, j, k, l; /*Sanka, Hlavac & Boyle; p. 97 f. 4.73*/
528: int running_sum;
529: float K = .5; /*.125 according to org. formula, but .5 is brighter*/
530: int max_val = 0, row_max, col_max; /* max porportion value in image*/
531: printf("width: %d\n", (*org_img).height);
532: memset(new_img, 0, sizeof(PGMImage));
533: (*new_img).height = (*org_img).height;
534: (*new_img).width = (*org_img).width;
535: (*new_img).maxVal = (*org_img).maxVal;
536:
537: /*starting at the top row*/
538: for(row = (*new_img).height - 1 - 1; row > 0; row--)
539: for(col = 1; col < (*new_img).width - 1; col++) /*left col start*/
540: {
541: i = row;
542: j = col;
543: running_sum = 0;
544:
545: /*Sanka, Hlavac & Boyle; p. 97 f. 4.73*/
546: for(k = i - 1; k <= i + 1; k++) /*row*/
547: for(l = j - 1; l <= j + 1; l++) /*column*/
548: running_sum += abs(rgb_avg((*org_img).data[k][l]) -
549: rgb_avg((*org_img).data[i][j]));
550:
551: /*assign the new pixel value*/
552: /*since it all the data is initalized to 0, we only worry when it
553: shouldn't be*/
554: if((int)(K * running_sum) >= 128)
555: {
556: (*new_img).data[row][col].red = 255;
557: (*new_img).data[row][col].green = 255;
558: (*new_img).data[row][col].blue = 255;
559: }
560: /*this is the original code...*/
561: /*(*new_img).data[row][col].red = (int)(K * running_sum);
562: (*new_img).data[row][col].green = (int)(K * running_sum);
563: (*new_img).data[row][col].blue = (int)(K * running_sum);*/
564: }
565: }
566:
567: /*******Corners*****************************************************
568: *******************************************************************/
569:
570: /*takes two coordinates as x and y pairs and returns the distence betweem them
571: as a decimal*/
572: float findDist(int x1, int y1, int x2, int y2)
573: {
574: return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
575: }
576:
577: /*1st param: The array of coords that the first point of the major axis is
578: returned in.
579: 2nd: The array of coords that the second point of the major axis is
580: returned in.
581: 3rd: The array of chain codes that are searched through to find the major
582: axes.*/
583: /* Note: the ending condition is when a NULL chainCodes value is reached.
584: Thusly, it works like a string requiring it to have the last legal value
585: followed by a null value.*/
586: void findFirstTwoCorners(object *objects_array, list_info* chainCodes)
587: {
588: int i; /*loop counting*/
589: list_info temp, search;
590: double max_dist, test_dist;
591:
592: /*printf("\nFinding first 2 corners.\n");*/
593:
594: /*as long as there are codes to check, keep checking. Note: the ending
595: condition is when a NULL chainCodes value is reached. Thusly, it works
596: like a string requiring it to have the last legal value followed by a
597: null value.*/
598: for(i = 0; ((i < MAX_CHAINS) && chainCodes[i].cur); i++)
599: {
600: memcpy(&temp, &chainCodes[i], sizeof(list_info));
601:
602: max_dist = 0.0; /*reset this for each iteration*/
603:
604: /*printf("checking list: %d\n", i);*/
605:
606: while(RetrieveNextNode(&temp).cur) /*while there are nodes to check*/
607: {
608: /*set the faster moving search pointer to temp,
609: this increases the effiecency a lot compared to
610: setting it equal to the first node..*/
611: memcpy(&search, &temp, sizeof(list_info));
612:
613: while(RetrieveNextNode(&search).cur)
614: {
615: /*setCPixel(RetrieveInfo(&temp).location.x,
616: RetrieveInfo(&temp).location.y, green);*/
617:
618: /*determine if found a new maximum distance between two locations*/
619: if((test_dist = findDist(RetrieveInfo(&search).location.x,
620: RetrieveInfo(&search).location.y,
621: RetrieveInfo(&temp).location.x,
622: RetrieveInfo(&temp).location.y))>max_dist)
623: {
624: max_dist = test_dist;
625: objects_array[i].corner1.x = RetrieveInfo(&temp).location.x;
626: objects_array[i].corner1.y = RetrieveInfo(&temp).location.y;
627: objects_array[i].corner2.x = RetrieveInfo(&search).location.x;
628: objects_array[i].corner2.y = RetrieveInfo(&search).location.y;
629: }
630: Advance(&search);
631: }
632: Advance(&temp);
633: }
634: /*printf("point1: %d %d\n", max1[i].x, max1[i].y);
635: printf("point2: %d %d\n", max2[i].x, max2[i].y);*/
636: }
637: }
638:
639: /*1st param: Array of coords for the first corner of each chain code.
640: 2nd param: Array of coords for the second corner of each chain code.
641: The first two parameters should equal the first two parameters "returned"
642: from the findFirstTwoCorners() function.
643: 3rd: Array of coords "returned" with the third corners.
644: 4th: Array of coords "returned" with the fourth corners.
645: 5th: Pointer pointing to the array of chaincode pointers, obtained from
646: showChain().*/
647: void findSecondTwoCorners(object *objects_array, list_info* chain_code_array)
648: {
649: int i; /*loop counting*/
650: list_info temp;
651: float temp_dist1, temp_dist2; /*distance between point and each corner*/
652: coord canidate_coord1, temp_coord;
653: float canidate_dist1, max_dist;
654: int corner_count;
655:
656: /*printf("\nFinding last 2 corners.\n\n");*/
657:
658: /*for each chain code find the corners*/
659: for(i = 0; (i < MAX_CHAINS) && chain_code_array[i].cur; i++)
660: {
661: memcpy(&temp, &chain_code_array[i], sizeof(list_info));
662:
663: /*reset these for the next chain code*/
664: max_dist = 0.0;
665: corner_count = 1;
666:
667: /*while there are nodes in the chain code to check*/
668: /*if there isn't a next node cur is NULL, which is checked*/
669: while(RetrieveNextNode(&temp).cur)
670: {
671: /*setCPixel(RetrieveInfo(&temp).location.x,
672: RetrieveInfo(&temp).location.y, color1);*/
673:
674: /*determine the first canidate coord for corner 3/4*/
675: if(((RetrieveInfo(&temp).location.x == objects_array[i].corner1.x)
676: && (RetrieveInfo(&temp).location.y == objects_array[i].corner1.y))
677: || ((RetrieveInfo(&temp).location.x == objects_array[i].corner2.x)
678: &&(RetrieveInfo(&temp).location.y == objects_array[i].corner2.y)))
679: {
680: /*if this corner found is the first of the two allready known
681: corners, then set the first canidate coord data and reset data
682: to find the next canidate corner point*/
683: if(corner_count == 1)
684: {
685: canidate_coord1.x = temp_coord.x;
686: canidate_coord1.y = temp_coord.y;
687: canidate_dist1 = max_dist;
688:
689: corner_count = 2; /*set for next corner*/
690: max_dist = 0.0;
691: }
692: else if(corner_count == 2)
693: {
694: /*the second canidate is always a corner*/
695: all_objects[i].corner4.x = temp_coord.x;
696: all_objects[i].corner4.y = temp_coord.y;
697:
698: max_dist = 0.0; /*set for next corner canidate*/
699: }
700: }
701:
702: /*calculate the distance between the current point being checked and
703: each corner point*/
704: temp_dist1 = findDist(all_objects[i].corner1.x,
705: all_objects[i].corner1.y,
706: RetrieveInfo(&temp).location.x,
707: RetrieveInfo(&temp).location.y);
708: temp_dist2 = findDist(all_objects[i].corner2.x,
709: all_objects[i].corner2.y,
710: RetrieveInfo(&temp).location.x,
711: RetrieveInfo(&temp).location.y);
712:
713: /*if the current point is the furthest away sofar, store this point
714: untill it is overridden or becomes a canidate point*/
715: if((temp_dist1 + temp_dist2) > max_dist)
716: {
717: temp_coord.x = RetrieveInfo(&temp).location.x;
718: temp_coord.y = RetrieveInfo(&temp).location.y;
719:
720: max_dist = (temp_dist1 + temp_dist2);
721: }
722:
723: Advance(&temp);
724: }
725:
726: /*from the three canidate coords find the two real corners.*/
727: /*the second canidate will always be a corner, must test 1 vs 3, where
728: three is in the variables temp_coord and max_dist.*/
729: if(canidate_dist1 > max_dist) /*first canidate*/
730: {
731: all_objects[i].corner3.x = canidate_coord1.x;
732: all_objects[i].corner3.y = canidate_coord1.y;
733: }
734: else /*third canidate*/
735: {
736: all_objects[i].corner3.x = temp_coord.x;
737: all_objects[i].corner3.y = temp_coord.y;
738: }
739: /*printf("corner3: (%d, %d) corner4: (%d, %d)\n", corner3[i].x,
740: corner3[i].y, corner4[i].x, corner4[i].y);*/
741: }
742: }
743:
744: /*takes a pointer to an image, and a pointer pointing to an array of
745: chain codes pointers, here each chainCode pointer needs to be accessed
746: by calculating the memory address.*/
747: void showBound(/*PGMImage *original,*/ list_info* chainCodes, object *all_objects)
748: {
749: int i;
750:
751: /*find the first two corners. they will be across a diagnal.*/
752: findFirstTwoCorners(all_objects, chainCodes);
753: /*find the second two corners. they will be across a diagnal too.*/
754: findSecondTwoCorners(all_objects, chainCodes);
755: /*
756: for(i = 0; chainCodes[i] && i < MAX_CHAINS; i++)
757: {
758: setCLines(corner1[i].x, corner1[i].y, corner3[i].x, corner3[i].y,yellow);
759: setCLines(corner4[i].x, corner4[i].y, corner2[i].x, corner2[i].y,yellow);
760: setCLines(corner3[i].x, corner3[i].y, corner2[i].x, corner2[i].y,yellow);
761: setCLines(corner4[i].x, corner4[i].y, corner1[i].x, corner1[i].y,yellow);
762: setCPixel(corner1[i].x, corner1[i].y, magenta);
763: setCPixel(corner2[i].x, corner2[i].y, magenta);
764: setCPixel(corner3[i].x, corner3[i].y, magenta);
765: setCPixel(corner4[i].x, corner4[i].y, magenta);
766: }*/
767: }
768:
769: /*returns the number of objects found*/
770: /*places the corner info in the global corners data*/
771: int detect_corners(list_info* current_chaincodes,
772: object *objects_array)
773: {
774: int i; /*temporarily holds number of chaincodes*/
775:
776: showBound(/*org_img,*/ current_chaincodes, objects_array);
777:
778: /*when this stops, i holds the number of chaincodes*/
779: for(i = 0; (i < MAX_CHAINS) && chain_codes && chain_codes[i].cur; i++);
780:
781: return i;
782: }
783:
784: /*******Find the game board********************************************
785: **********************************************************************/
786:
787: float compare_length(float length1, float length2)
788: {
789: float denom_check = fabs((length1 + length2) / 2);
790: if(denom_check == 0)
791: {
792: /*the only way to possibly pull this off is if one point is
793: is considered more than one corner. This is most likely
794: to happen where the chain was only two or three nodes long.*/
795: /*Just set the error to the maximum value obtained from float.h,
796: since such a small chain is not what we are looking for.*/
797:
798: /*error24to13[i] =*/ return FLT_MAX;
799: }
800: else
801: /*error24to13[i] =*/return fabs(length1 - length2) / denom_check;
802: }
803:
804: /*determines which object is the game board and determine moves made in
805: the game.*/
806: /*takes the number of object corners (equal to # of chaincodes) as param.*/
807: int detect_game(list_info *current_chaincodes, object *object_array)
808: {
809: float length2to4[MAX_CHAINS], length1to3[MAX_CHAINS]; /*side pairs*/
810: float length1to4[MAX_CHAINS], length2to3[MAX_CHAINS]; /*side pairs*/
811: float length1to2[MAX_CHAINS], length3to4[MAX_CHAINS]; /*diagnaols*/
812:
813: float error24to13[MAX_CHAINS], error14to23[MAX_CHAINS];/*opp. sides*/
814: float error14to13[MAX_CHAINS], error23to24[MAX_CHAINS];/*share corner*/
815: float error31to32[MAX_CHAINS], error41to42[MAX_CHAINS];/*share corner*/
816: float error12to34[MAX_CHAINS];/*diagnaols*/
817:
818: float error_avg;/*average of the errors stored in error##to## variables*/
819:
820: int i, k; /*loop counting*/
821: /*float denom_check; /*make sure denominators are not zero*/
822: list* temp;
823:
824: /*the most likely object (0 to num_of_corners) that is to be considered
825: as the board. The float is the error associated with this object.*/
826: int most_likely = -1;
827: float ml_error = FLT_MAX; /*just to make sure*/
828: printf("here inside detect game\n");
829: /*for each chaincode*/
830: for(i = 0; (i < MAX_CHAINS) && current_chaincodes &&
831: current_chaincodes[i].cur; i++)
832: {
833: printf("length: %d\n", Length(¤t_chaincodes[i]));
834: /*count the number of nodes in the chaincode. Unless the size is
835: considered long enough, skip it and move on.*/
836: if(Length(¤t_chaincodes[i]) < DECENT_SIZED_OBJECT)
837: continue;
838: printf("here inside detect game for loop\n");
839: /*since points 1 & 2 are at a diagnal, and 3 & 4 are at a diagnol,
840: then the dist between 2 and 4 & 1 and 3 should be close
841: in value.*/
842: length2to4[i] = findDist(object_array[i].corner2.x,
843: object_array[i].corner2.y,
844: object_array[i].corner4.x,
845: object_array[i].corner4.y);
846: length1to3[i] = findDist(object_array[i].corner1.x,
847: object_array[i].corner1.y,
848: object_array[i].corner3.x,
849: object_array[i].corner3.y);
850:
851: /*the other side pair*/
852: length1to4[i] = findDist(object_array[i].corner1.x,
853: object_array[i].corner1.y,
854: object_array[i].corner4.x,
855: object_array[i].corner4.y);
856: length2to3[i] = findDist(object_array[i].corner2.x,
857: object_array[i].corner2.y,
858: object_array[i].corner3.x,
859: object_array[i].corner3.y);
860:
861: /*diagnols... always will be 1 & 2 and 3 & 4*/
862: length1to2[i] = findDist(object_array[i].corner1.x,
863: object_array[i].corner1.y,
864: object_array[i].corner2.x,
865: object_array[i].corner2.y);
866: length3to4[i] = findDist(object_array[i].corner3.x,
867: object_array[i].corner3.y,
868: object_array[i].corner4.x,
869: object_array[i].corner4.y);
870:
871: /*calculate percent errors for all edge (and diagnal) combinations*/
872: error24to13[i] = compare_length(length2to4[i], length1to3[i]);/*op.side*/
873: error14to23[i] = compare_length(length1to4[i], length2to3[i]);
874: error14to13[i] = compare_length(length1to4[i], length1to3[i]);/*1 crn.*/
875: error23to24[i] = compare_length(length2to3[i], length2to4[i]);
876: error31to32[i] = compare_length(length1to3[i], length2to3[i]);
877: error41to42[i] = compare_length(length1to4[i], length2to4[i]);
878: error12to34[i] = compare_length(length1to2[i], length3to4[i]);/*diag.*/
879:
880: /*average all of the error values together*/
881: error_avg = ((error24to13[i] + error14to23[i] + error14to13[i] +
882: error23to24[i] + error31to32[i] + error41to42[i] +
883: error12to34[i]) / 7);
884:
885: printf("error avg: %f\n\n", error_avg);
886: /*determine if the current object is considered the most likely to
887: be the ttt board so far. Average of all the error##to##'s.
888: If the current is */
889: if(ml_error > error_avg)
890: {
891: most_likely = i;
892: ml_error = error_avg;
893: }
894: }
895:
896: printf("Object %d is most likely the board with %f\%% error.\n",
897: most_likely, ml_error * 100);
898:
899: return most_likely; /*return the object number that is the board*/
900: }
901:
902: coord search_moravec(coord point1, coord point2, PGMImage *moravec)
903: {
904: int i, j; /*loop counting*/
905: float t;
906: coord temp;
907: float dist = findDist(point1.x, point1.y, point2.x, point2.y);
908: int search_size = 5;
909:
910: coord most_likely_corner = {-1, -1};
911: int most_likely_sum = 0;
912: int sum_moravec_points = 0;
913:
914: /*calculate the coordinates where the divider edges should be.*/
915: for(t = ONE_HALF; t > ONE_SIXTH; t -= (1 / dist))
916: {
917: temp = find_dividers(point1, point2, t);
918: /* search_size = findDist(point1.x, point1.y, point2.x, point2.y) / 10;*/
919: sum_moravec_points = 0; /*clear this before next iteration*/
920:
921: for(i = -(search_size); i <= search_size; i++)
922: {
923: for(j = -(search_size); j <= search_size; j++)
924: {
925: if(rgb_avg((*moravec).data[temp.y + i][temp.x + j]) > 115)
926: {
927: /*setCPixel(temp.x + j, temp.y + i, red);*/
928: sum_moravec_points++;
929: }
930: }
931: }
932:
933: /*if the current point in the search is the best, store it*/
934: if(most_likely_sum <= sum_moravec_points)
935: {
936: most_likely_corner.x = temp.x;
937: most_likely_corner.y = temp.y;
938: most_likely_sum = sum_moravec_points;
939: }
940: }
941:
942: return most_likely_corner;
943: }
944:
945:
946: coord find_intersection(coord line1_point1, coord line1_point2,
947: coord line2_point1, coord line2_point2)
948: {
949: float line1_slope, line2_slope;
950: coord target = {-1, -1};
951: int *temp = NULL;
952: /*find slope for first line*/
953: if((line1_point1.x - line1_point2.x) != 0)
954: {
955: line1_slope = ((float)(line1_point1.y - line1_point2.y)) /
956: ((float)(line1_point1.x - line1_point2.x));
957: }
958: else /*otherwise handle the undefined slope*/
959: {
960: /*find slope for secon line when first is undefined*/
961: if((line2_point1.x - line2_point2.x) != 0)
962: {
963: line2_slope = ((float)(line2_point1.y - line2_point2.y)) /
964: ((float)(line2_point1.x - line2_point2.x));
965: }
966: else /*this should never happen, but could if someone specifed the same
967: line twice*/
968: return target; /*target is initalized to (-1, -1)*/
969:
970: /*since the slope is undefined the x coord in known*/
971: target.x = line1_point1.x;
972: target.y = line2_slope * target.x + line2_point1.y; /*y = mx + b*/
973: printf("line one has undefined slope\n");
974: return target;
975: }
976:
977: /*find slope for second line*/
978: if((line2_point1.x - line2_point2.x) != 0)
979: {
980: line2_slope = ((float)(line2_point1.y - line2_point2.y)) /
981: ((float)(line2_point1.x - line2_point2.x));
982: }
983: else
984: {
985: /*since the slope is undefined the x coord in known*/
986: target.x = line1_point1.x;
987: target.y = line1_slope * target.x + line1_point1.y; /*y = mx + b*/
988: printf("line two has undefined slope\n");
989: return target;
990: }
991:
992: /*if both lines have defined slopes find the target point*/
993: /* slope is m and defined by:
994: (y1 - y2)
995: m = ---------
996: (x1 - x2)
997:
998: target calculated by setting Yt = M1*(Xt - Xa) + Ya equal to
999: Yt = M2 * (Xt - Xc) + Yc to get
1000:
1001: (Xa*M1 - M2*Xc + Yc - Ya)
1002: Xt = -------------------------
1003: M1 - M2
1004:
1005: then to get the y coordinate sub Xt into:
1006:
1007: Yt = M1 * (Xt - Xa) + Ya
1008:
1009: Where line1_point1 = a
1010: line1_point2 = b
1011: line2_point1 = c
1012: line12point2 = d
1013: and are indicated as subscripts of their X or Y coordinate.
1014: M1 = line1_slope
1015: M2 = line2_slope
1016: */
1017:
1018:
1019:
1020: target.x = (((float)(line1_point1.x * line1_slope)
1021: - (float)(line2_point1.x * line2_slope)
1022: + (float)line2_point1.y - (float)line1_point1.y))
1023: / (float)(line1_slope - line2_slope);
1024:
1025: target.y = line1_slope * ((float)target.x - (float)line1_point1.x)
1026: + (float)line1_point1.y;
1027: /*target.y = line2_slope * ((float)target.x - (float)line2_point1.x)
1028: + (float)line2_point1.y;*/
1029:
1030: /*printf("%d\n", *temp); *//*for gdb*/
1031: return target;
1032: }
1033:
1034: /*anchor - the corner closest to the side divider point being looked for.
1035: common - the corner that the line between itself and the anchor corner contains
1036: the target divider point.
1037: anchor_opposite - the corner adjacent to the anchor corner & oppsoite common.
1038: common_opposite - the corner adjacent to the common corner & oppposite anchor.
1039: */
1040: /*return the divider point*/
1041: coord find_side_points(coord anchor, coord common, coord anchor_opposite,
1042: coord common_opposite, PGMImage *moravec)
1043: {
1044: coord temp1, temp2;
1045:
1046: temp1 = find_dividers(anchor, anchor_opposite, ONE_TWELTH);
1047: temp2 = find_dividers(common, common_opposite, ONE_TWELTH);
1048:
1049: return search_moravec(temp1, temp2, moravec);
1050: }
1051:
1052:
1053: /*find the ttt boards cross lines coordinates*/
1054: void detect_ttt_board(object board_object, ttt *board_details,
1055: PGMImage *moravec)
1056: {
1057: /*copy the corners into the ttt datatype*/
1058: memcpy(&(board_details->corners), &board_object, sizeof(object));
1059:
1060: board_details->side13 = find_side_points(board_object.corner1,
1061: board_object.corner3,
1062: board_object.corner2,
1063: board_object.corner4, moravec);
1064:
1065: board_details->side14 = find_side_points(board_object.corner1,
1066: board_object.corner4,
1067: board_object.corner2,
1068: board_object.corner3, moravec);
1069:
1070: board_details->side23 = find_side_points(board_object.corner2,
1071: board_object.corner3,
1072: board_object.corner1,
1073: board_object.corner4, moravec);
1074:
1075: board_details->side24 = find_side_points(board_object.corner2,
1076: board_object.corner4,
1077: board_object.corner1,
1078: board_object.corner3, moravec);
1079:
1080: board_details->side31 = find_side_points(board_object.corner3,
1081: board_object.corner1,
1082: board_object.corner4,
1083: board_object.corner2, moravec);
1084:
1085: board_details->side32 = find_side_points(board_object.corner3,
1086: board_object.corner2,
1087: board_object.corner4,
1088: board_object.corner1, moravec);
1089:
1090: board_details->side41 = find_side_points(board_object.corner4,
1091: board_object.corner1,
1092: board_object.corner3,
1093: board_object.corner2, moravec);
1094:
1095: board_details->side42 = find_side_points(board_object.corner4,
1096: board_object.corner2,
1097: board_object.corner3,
1098: board_object.corner1, moravec);
1099:
1100: board_details->side13 = find_side_points(board_object.corner1,
1101: board_object.corner3,
1102: board_object.corner2,
1103: board_object.corner4, moravec);
1104:
1105: board_details->side14 = find_side_points(board_object.corner1,
1106: board_object.corner4,
1107: board_object.corner2,
1108: board_object.corner3, moravec);
1109:
1110: board_details->side23 = find_side_points(board_object.corner2,
1111: board_object.corner3,
1112: board_object.corner1,
1113: board_object.corner4, moravec);
1114:
1115: board_details->side24 = find_side_points(board_object.corner2,
1116: board_object.corner4,
1117: board_object.corner1,
1118: board_object.corner3, moravec);
1119:
1120: /*correct the length of the lines that is shortend to calculate them
1121: in the first place above. simply find the intersection of the *short*
1122: line with that of the outside line between the corners.*/
1123: board_details->side13 = find_intersection(board_details->side13,
1124: board_details->side42,
1125: board_details->corners.corner1,
1126: board_details->corners.corner3);
1127: board_details->side14 = find_intersection(board_details->side14,
1128: board_details->side32,
1129: board_details->corners.corner1,
1130: board_details->corners.corner4);
1131: board_details->side23 = find_intersection(board_details->side23,
1132: board_details->side41,
1133: board_details->corners.corner2,
1134: board_details->corners.corner3);
1135: board_details->side24 = find_intersection(board_details->side24,
1136: board_details->side31,
1137: board_details->corners.corner2,
1138: board_details->corners.corner4);
1139: board_details->side31 = find_intersection(board_details->side31,
1140: board_details->side24,
1141: board_details->corners.corner3,
1142: board_details->corners.corner1);
1143: board_details->side32 = find_intersection(board_details->side32,
1144: board_details->side14,
1145: board_details->corners.corner3,
1146: board_details->corners.corner2);
1147: board_details->side41 = find_intersection(board_details->side41,
1148: board_details->side23,
1149: board_details->corners.corner4,
1150: board_details->corners.corner1);
1151: board_details->side42 = find_intersection(board_details->side42,
1152: board_details->side13,
1153: board_details->corners.corner4,
1154: board_details->corners.corner2);
1155:
1156: /*now that the sides are found, find the intersections to find the
1157: middle points*/
1158:
1159: board_details->middle1 = find_intersection(board_details->side14,
1160: board_details->side32,
1161: board_details->side13,
1162: board_details->side42);
1163:
1164: board_details->middle2 = find_intersection(board_details->side23,
1165: board_details->side41,
1166: board_details->side24,
1167: board_details->side31);
1168:
1169: board_details->middle3 = find_intersection(board_details->side31,
1170: board_details->side24,
1171: board_details->side32,
1172: board_details->side14);
1173: printf("middle3: (%d, %d)\n", board_details->middle3.x, board_details->middle3.y);
1174: board_details->middle4 = find_intersection(board_details->side41,
1175: board_details->side23,
1176: board_details->side42,
1177: board_details->side13);
1178:
1179: }
1180:
1181: /*****Find the pieces***************************************************
1182: ***********************************************************************/
1183:
1184: /*Takes four points that must be in order going around the region. Also,
1185: needs a pointer to an image with the moravec filter applied.*/
1186: int search_area(coord point1, coord point2, coord point3, coord point4,
1187: PGMImage *moravec)
1188: {
1189: /*The following variables are used to calculate the existence of a marble
1190: at a location on the board or not. Uses the following basic formula
1191: new_point = ((t-1) * 1st point) + (t * 2nd point)
1192: where the start & end coords are opposite sides of the area being
1193: searched. If you take the entire area of a single position on a ttt
1194: board, the area that is searched is the region that is bouned by the
1195: points halfway between those passed in to this function.*/
1196:
1197: float t, s, r; /*hold values incrementing from 0 to 1*/
1198: coord t_start, t_end, s_start, s_end;
1199: coord temp_t, temp_s, temp_r;
1200: float dist_t, dist_s, dist_r;
1201:
1202: /*pixel count of those over the threashhold and total pixels*/
1203: int pixel_count = 0, moravec_count = 0;
1204:
1205: t_start = find_dividers(point1, point2, ONE_HALF);
1206: t_end = find_dividers(point1, point4, ONE_HALF);
1207:
1208: s_start = find_dividers(point3, point2, ONE_HALF);
1209: s_end = find_dividers(point3, point4, ONE_HALF);
1210:
1211: dist_t = findDist(t_start.x, t_start.y, t_end.x, t_end.y);
1212: dist_s = findDist(s_start.x, s_start.y, s_end.x, s_end.y);
1213: printf("%f %f\n", dist_t, dist_s);
1214:
1215: /*march two points along that parallel each other in the search area*/
1216: for(t = 0.0, s = 0.0; t <= 1.0; t += (1.0 / dist_t), s += (1.0 / dist_s))
1217: {
1218: temp_t = find_dividers(t_start, t_end, t);
1219:
1220: temp_s = find_dividers(s_start, s_end, s);
1221:
1222: dist_r = findDist(temp_t.x, temp_t.y, temp_s.x, temp_s.y);
1223:
1224: /*march a single point along that starts at temp_s and ends
1225: at temp_t. This fills in the region for the search.*/
1226: for(r = 0.0; r <= 1.0; r += (1 / dist_r))
1227: {
1228: temp_r = find_dividers(temp_s, temp_t, r);
1229:
1230: /*talley the number of edge points found (rgb. avg. >= 128)*/
1231: if(rgb_avg((*moravec).data[temp_r.y][temp_r.x]) >= 128)
1232: {
1233: setCPixel(temp_r.x, temp_r.y, blue);
1234: moravec_count++;
1235: }
1236: pixel_count++; /*increment the number of pixels*/
1237: }
1238:
1239: }
1240:
1241: printf("total: %d edge: %d\n", pixel_count, moravec_count);
1242:
1243: /*if the number of edge pixels is greater than 10% of the total, then
1244: this is an edge of marble. The percent is there because there could be
1245: a few small blobs that get counted when the shouldn't that otherwise
1246: would give a positive hit that a marble was found*/
1247: if(moravec_count >= (pixel_count / 10))
1248: return TRUE;
1249:
1250: /*otherwise a marble isn't here*/
1251: return FALSE;
1252: }
1253:
1254: /*takes the object number that is determined to be the board*/
1255: void detect_pieces(ttt *board_data, PGMImage* moravec)
1256: {
1257: if(search_area(board_data->corners.corner1, board_data->side13,
1258: board_data->middle1, board_data->side14, moravec))
1259: printf("corner 1 has a piece\n");
1260: else
1261: printf("corner 1 has no piece\n");
1262:
1263:
1264: if(search_area(board_data->middle1, board_data->middle3,
1265: board_data->middle2, board_data->middle4, moravec))
1266: printf("middle has a piece\n");
1267: else
1268: printf("middle has no piece\n");
1269:
1270:
1271: if(search_area(board_data->corners.corner2, board_data->side23,
1272: board_data->middle2, board_data->side24, moravec))
1273: printf("corner 2 has a piece\n");
1274: else
1275: printf("corner 2 has no piece\n");
1276:
1277: setCPixel(board_data->middle1.x, board_data->middle1.y, red);
1278: setCPixel(board_data->middle2.x, board_data->middle2.y, green);
1279: setCPixel(board_data->middle3.x, board_data->middle3.y, blue);
1280: setCPixel(board_data->middle4.x, board_data->middle4.y, yellow);
1281: }
1282:
1283:
1284: /* =================================================================
1285: * Callback functions.
1286: *
1287: * color = displayed graphics in window
1288: * menu = menu event handling
1289: * keyboard = deyboard event handling
1290: * ----------------------------------------------------------------- */
1291: void color(void)
1292: {
1293: /*glClear (GL_COLOR_BUFFER_BIT);*/
1294:
1295: /*show the current image*/
1296: showColor(img_cur);
1297:
1298: /*show the current abstract information*/
1299: showAbstract(all_objects, ttt_board);
1300:
1301: }
1302:
1303: void buffer()
1304: {
1305: /*if the drawing state of all objects is enabled, recalculate*/
1306: if(draw_abstract_lines >= 0)
1307: draw_abstract_lines = detect_corners(chain_codes, all_objects);
1308:
1309: /*if the drawing state of the object most likely to be the board is
1310: found, recalulate*/
1311: if(draw_abstract_board >= 0)
1312: draw_abstract_board = detect_game(chain_codes, all_objects);
1313:
1314: glutPostRedisplay();
1315: }
1316:
1317: #define RESTART 0
1318: #define PERS_CORR 3
1319: #define COLOR_TO_GRAY 4
1320: #define MORAVEC 5
1321: #define EDGES 6
1322: #define CORNERS 7
1323: #define BUFFERS 8
1324: #define GAME 9
1325: #define PIECES 10
1326:
1327: /*this is not a callback function, but is used inside menu() for setting
1328: new states of execution*/
1329: void reset_state(PGMImage* new_current)
1330: {
1331: img_cur = new_current;
1332: draw_abstract_lines = -1;
1333: draw_abstract_board = -1;
1334: free_chaincodes(&chain_codes);
1335: }
1336:
1337: void menu(int selection)
1338: {
1339: if(selection == RESTART)
1340: {
1341: reset_state(img_original);
1342: }
1343: if(selection == PERS_CORR)
1344: {
1345: pers_corr(img_pers_corr, img_cur);
1346: reset_state(img_pers_corr);
1347: }
1348: if(selection == COLOR_TO_GRAY)
1349: {
1350: color_to_gray(img_grayscale, img_cur);
1351: reset_state(img_grayscale);
1352: }
1353: if(selection == MORAVEC)
1354: {
1355: moravec(img_moravec, img_cur);
1356: reset_state(img_moravec);
1357: }
1358: if(selection == EDGES)
1359: {
1360: /*if there is a chaincode already in memory, free it before a new one
1361: is created.*/
1362: showChain(img_cur, &chain_codes);
1363: }
1364: if(selection == CORNERS)
1365: {
1366: /*if there are chaincodes already in memory, free it before a new one
1367: is created.*/
1368: showChain(img_cur, &chain_codes);
1369:
1370: draw_abstract_lines = detect_corners(chain_codes, all_objects);
1371: }
1372: if(selection == BUFFERS)
1373: {
1374: if(is_buffered == FALSE)
1375: {
1376: glutIdleFunc(buffer);
1377: is_buffered = TRUE;
1378: }
1379: else
1380: {
1381: glutIdleFunc(NULL);
1382: is_buffered = FALSE;
1383: }
1384: }
1385: if(selection == GAME)
1386: {
1387: /*need an image with the moravec filter applied*/
1388: /*don't calculating this if it is the current image!!!
1389: Bad things happen like memseting to zero the current image.*/
1390: if(img_cur != img_moravec)
1391: {
1392: moravec(img_moravec, img_cur);
1393: /*img_cur = img_moravec;*/
1394: }
1395:
1396: /*if there are chaincodes already in memory, free it before a new one
1397: is created.*/
1398: showChain(img_moravec, &chain_codes);
1399:
1400: detect_corners(chain_codes, all_objects);
1401: draw_abstract_board = detect_game(chain_codes, all_objects);
1402: detect_ttt_board(all_objects[draw_abstract_board], ttt_board,
1403: img_moravec);
1404: return;
1405: }
1406: if(selection == PIECES)
1407: {
1408: /*need an image with the moravec filter applied*/
1409: /*don't calculating this if it is the current image!!!
1410: Bad things happen like memseting to zero the current image.*/
1411: if(img_cur != img_moravec)
1412: {
1413: moravec(img_moravec, img_cur);
1414: /*img_cur = img_moravec;*/
1415: }
1416:
1417: /*if there are chaincodes already in memory, they are freed before a
1418: new one is created.*/
1419: showChain(img_moravec, &chain_codes);
1420:
1421: /*detect the ttt game board*/
1422: detect_corners(chain_codes, all_objects);
1423: draw_abstract_board = detect_game(chain_codes, all_objects);
1424: detect_ttt_board(all_objects[draw_abstract_board], ttt_board,
1425: img_moravec);
1426:
1427: /*detect the locations of the pieces*/
1428: detect_pieces(ttt_board, img_moravec);
1429:
1430: glFlush();
1431: return;
1432: }
1433:
1434: glutPostRedisplay(); /*redraw the image*/
1435: }
1436:
1437: void keyboard(unsigned char key, int x, int y)
1438: {
1439: switch (key)
1440: {
1441: case 27:
1442: exit(0);
1443: break;
1444: }
1445: }
1446:
1447: void mouse(int button, int state, int x, int y)
1448: {
1449: char temp[50];
1450:
1451: if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
1452: {
1453: sprintf(temp, "(x, y): (%d, %d) red: %d green: %d blue: %d\n",
1454: x, VSIZE - y, (*img_cur).data[VSIZE - y][x].red,
1455: (*img_cur).data[VSIZE - y][x].green,
1456: (*img_cur).data[VSIZE - y][x].blue);
1457: setCRect(0, 0, 200, 12, black);
1458: glColor3f(1.0, 0.0, 0.0);
1459: drawString(0, 0, GLUT_BITMAP_TIMES_ROMAN_10, temp, red);
1460: glFlush();
1461: }
1462: }
1463:
1464:
1465: /* =================================================================
1466: * init() & parse_command_line()
1467: * initalize none-OpenGL related things.
1468: * ----------------------------------------------------------------- */
1469:
1470: /*set global flag variables from things specified at commandline.*/
1471: /*return the filename specified, if none specified exit().*/
1472: char* parse_command_line(int argc, char** argv)
1473: {
1474: /*parse the command line*/
1475: if(argc == 1)
1476: {
1477: printf("To few parameters.\n");
1478: printf("Usage: research <file.pgm>\n");
1479: exit(1);
1480: }
1481: else if(argc == 2)
1482: {
1483: return argv[1];
1484: }
1485: else
1486: {
1487: printf("To many parameters.\n");
1488: printf("Usage: research <file.pgm>\n");
1489: exit(1);
1490: }
1491: }
1492:
1493: char* init (int argc, char** argv)
1494: {
1495: char* PGMfileName;/*pointer to the string containing the filename*/
1496:
1497: /*parse the command line*/
1498: PGMfileName = parse_command_line(argc, argv);
1499:
1500: /*
1501: * Read in image file. - note: sets our global values, too.
1502: * ----------------------------------------------------------------- */
1503:
1504: /*allocate memory for original image*/
1505: img_original = (PGMImage*) malloc(sizeof(PGMImage));
1506: getPGMfile(PGMfileName, img_original);
1507: HSIZE = (*img_original).width;
1508: VSIZE = (*img_original).height;
1509: MVAL = (*img_original).maxVal;
1510:
1511: img_cur = img_original; /*VERY IMPORTANT to set this*/
1512:
1513: /*allocate memory for pers. corr. image*/
1514: img_pers_corr = (PGMImage*) malloc(sizeof(PGMImage));
1515: (*img_pers_corr).width = HSIZE;
1516: (*img_pers_corr).height = VSIZE;
1517: (*img_pers_corr).maxVal = 255;
1518:
1519: img_grayscale = (PGMImage*) malloc(sizeof(PGMImage));
1520: (*img_grayscale).width = HSIZE;
1521: (*img_grayscale).height = VSIZE;
1522: (*img_grayscale).maxVal = 255;
1523:
1524: img_moravec = (PGMImage*) malloc(sizeof(PGMImage));
1525: (*img_moravec).width = HSIZE;
1526: (*img_moravec).height = VSIZE;
1527: (*img_moravec).maxVal = 255;
1528:
1529:
1530: all_objects = (object*) malloc(sizeof(object) * MAX_CHAINS);
1531: memset(all_objects, 0, sizeof(object) * MAX_CHAINS);
1532:
1533: ttt_board = (ttt*) malloc(sizeof(ttt));
1534: memset(ttt_board, 0, sizeof(ttt));
1535:
1536: return PGMfileName;
1537: }
1538:
1539:
1540: int main(int argc, char** argv)
1541: {
1542: char* PGMfileName;
1543: int WindowID; /*unique window id, there is only one used*/
1544: int i; /*looping variable*/
1545:
1546: /*
1547: * Call our init function to define non-OpenGL related things.
1548: * Have init return a pointer to the filename, used to display the
1549: * filename in the titlebar of the window.
1550: * ----------------------------------------------------------------- */
1551: PGMfileName = init (argc, argv);
1552:
1553: /*
1554: * Initialize the glut package.
1555: * ----------------------------------------------------------------- */
1556: glutInit(&argc, argv);
1557: glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
1558:
1559: /*
1560: * Define a new window (its size, position and title).
1561: * ----------------------------------------------------------------- */
1562: glutInitWindowSize (HSIZE, VSIZE); /*size*/
1563: glutInitWindowPosition (10, 10); /*position*/
1564: WindowID = glutCreateWindow (PGMfileName); /*title*/
1565: glutSetWindow(WindowID);
1566: glutDisplayFunc(color);
1567:
1568: /*
1569: * select clearing color - white
1570: */
1571: glClearColor (1.0, 1.0, 1.0, 0.0);
1572:
1573: /*
1574: * initialize viewport values
1575: */
1576: glMatrixMode(GL_PROJECTION);
1577: glLoadIdentity();
1578: glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
1579:
1580: /*add menus*/
1581: glutCreateMenu(menu);
1582: glutAddMenuEntry("Restart", RESTART);
1583: glutAddMenuEntry("perspective correction", PERS_CORR);
1584: glutAddMenuEntry("color to gray", COLOR_TO_GRAY);
1585: glutAddMenuEntry("moravec", MORAVEC);
1586: glutAddMenuEntry("edges", EDGES);
1587: glutAddMenuEntry("corners", CORNERS);
1588: glutAddMenuEntry("detect game", GAME);
1589: glutAddMenuEntry("detect pieces", PIECES);
1590: glutAddMenuEntry("toggle buffering", BUFFERS);
1591: glutAttachMenu(GLUT_RIGHT_BUTTON);
1592:
1593: glutMouseFunc(mouse);
1594: glutKeyboardFunc(keyboard);
1595: glutMainLoop();
1596:
1597: /*
1598: * When we reach here, we've left the event loop and are ready to
1599: * exit.
1600: * ----------------------------------------------------------------- */
1601: return 0;
1602: }
1603: