Lecture notes: **************************************************************************** Game of Life Rules: Live cell: less than two live neighbors, dies (under-population) two or three neighbors, lives four or more neighbors, dies (over-population) Dead cell: exactly three live neighbors, lives State transition table Q (input) --> Q' Q | input (count live) || Q' 1 (live) | 0, 1, live neighbors || 0 (dead) 1 (live) | 2, 3 live neighbors || 1 (live) 1 (live) | 4 - 9 live neighbors || 0 (dead) 0 (dead) | 3 live neighbors || 1 (live) 0 (dead) | 0 - 2, 4 - 9 live neighbors || 0 (dead) An equilavent representation for coding purposes. The following representation uses more space, however just doing look-up (rather than computing a count), makes this data structure faster than the above representation. Since there are nine neighbors, the neighborhood has a total of 2^n patterns (0 0000 0000, 0 0000 0001, ... 1 1111 1110, 1 1111 1111, where 0 is dead, 1 is live) (in hex: 0x000, 0x001, 0x002, 0x003, ... 0x0FE, 0x0FF, 0x100, 0x101, 0x102, ... 0x1FE, 0x1FF) Q | input || Q' 0 | 0 0000 0000 || 0 0 | 0 0000 0001 || 0 0 | 0 0000 0010 || 0 0 | 0 0000 0011 || 0 0 | 0 0000 0100 || 0 0 | 0 0000 0101 || 0 0 | 0 0000 0110 || 0 0 | 0 0000 0111 || 1 0 | 0 0000 1000 || 0 0 | 0 0000 1001 || 0 0 | 0 0000 1010 || 0 0 | 0 0000 1011 || 1 [ elision ] 0 | 1 1111 1100 || 0 0 | 1 1111 1101 || 0 0 | 1 1111 1110 || 0 0 | 1 1111 1111 || 0 1 | 0 0000 0000 || 0 1 | 0 0000 0001 || 0 [ elision ] 1 | 1 0000 0010 || 1 1 | 1 0000 0011 || 1 1 | 1 0000 0100 || 1 1 | 1 0000 0101 || 1 1 | 1 0000 0110 || 1 1 | 1 0000 0111 || 0 [ elision ] 1 | 1 1111 1100 || 0 1 | 1 1111 1101 || 0 1 | 1 1111 1110 || 0 1 | 1 1111 1111 || 0 NOTICE! All together, the above table has 2^10 entries. *************************************************************************** Pseudo code for Game of Life old = initialize_board(); while (more generations) { display_board(old); for (r = 0; r < maxR; r++) for (c = 0; c < maxC; c++) { living = count(r, c); if ( live && (living == 0 || living == 1 ) ) new[r, c] = dead; else if ( live && (living == 2 || living == 3) ) new[r, c] = live; else if (live) new[r, c] = dead; else if (dead && (living == 3) ) new[r, c] = live; else new[r, c] = dead; } // end for old = new; } // end while int count (row, col) { // return the sum of the live neighbors of location (r, c) ... } **************************************************************************** Edge detection (template matching) (extremely simple) edge template (2 X 2): Detect column discontinuity (edge): -1 1 -1 1 Detect row discontinuity (edge): -1 -1 1 1 Consider black & white image: 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 0 0 0 0 When we run the column edge detector over the image, we get: +1 0 0 -1 +2 0 0 -2 +2 0 0 -2 +1 0 0 -1 When we run the row edge detector over the image, we get: +1 +2 +2 +1 0 0 0 0 0 0 0 0 -1 -2 -2 -1 Take the absolute value of the result, pick a threshold (e.g. T >= 2) column edges row edges are detected at are detected at 0 0 0 0 0 0 x x 0 0 x 0 0 x 0 0 0 0 0 0 x 0 0 x 0 0 0 0 0 0 0 0 0 0 0 0 x x 0 0 *************************************************************************** Another example of template matching. Find 'T'. Template: +1 +1 +1 -1 +1 -1 -1 +1 -1 Run template over this image: 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 0 0 0 0 1 0 We get: 0 0 -1 -1 0 +4 0 0 0 +1 -1 0 +1 +2 +1 +5 Pick a threshold (e.g., T >= 3) to detect the 'T' at (row, col) = (1, 1) and (3, 3) I.e., two T's as shown below. 0 0 0 0 0 0 0 * * * 0 0 0 0 * 0 0 0 0 0 * * * * 0 0 0 0 * 0 0 0 0 0 * 0 Note, there is almost a T at row, col = (3, 1) An important property of thresholding: By moving the threshold down, we'll get more T's, but there will be more 'false positives'. By moving the threshold up, we're reducing the number of false positives, but we're more likely to miss some true T's. So to get all the true T's, have a low threshold --- but then the result will have a lot of chaff (false positives). To be sure of only getting true T's, have a high threshold --- then you will probably not detect some true T's.