Before we start this section, I'd like to fix a bug in 'EngiDisplayModel'. Please file in the patch in Appendix 15. 'EngiDisplayModel' is a super class of 'EngiFreehandElement' which I create in this section, so be sure to do it.
I will show you a program which retrieves pictures by matching a hand written diagram. EngiFreehandElement class analyzes hand written pictures or letters. The program is in Appendix 16. The EngiFreehandModel class supports matching pictures created with 'EngiFreehandElement'.
Now file in Appendix 16 and execute example 1 and 2 (EngiFreehandElement is in category Engi-Figure.) Example 1 creates a window labeled 'clear (gizagiza)'. 'gizagiza' is a Japanese word meaning notches. Example 2 creates a window labeled 'a (hiragana)'. 'hiragana' is a type of Japanese letters, and you see one 'hiragana' character. Its in color, so if you have a monochrome screen its a little harder to see.
Three types of data are presented on a picture. Diamonds are start points of lines; squares are end points, and circles are turning points. In the top left corner of the window there is a normalized image of the picture, and horizontal and vertical spectra. The spectra is created by counting the number of pixels (horizontally or vertically). The nine numbers at the top are the density of each of nine areas (imagine the figure divided like a tic-tac-toe board. 'EngiFreehandModel' in the next section recognizes a hand written picture using this data.
Let's examine 'EngiFreehandElement' more carefully. It has 8 instance variables:
Table 31-1 ------------------------------------------------------------------- (1) polylines an array of point arrays (each of which will be called a locus) (2) area the rectangular size of the picture (3) starts a set of associations of line segments and the index of the point in the line-segment array which starts the line (4) bends A set of pivot points: a set of associations (5) ends A set of end points: a set of associations (6) image The normalized image of the picture: a 50 @ 50 pixel black and white image (7) density the density of nine parts of the normalized image (8) spectra horizontal and vertical spectra of the normalized image: two 50 @ 50 size black and white images ---------------------------------------------------------------------
The 'value' (an inherited instance variable) holds the name of the picture.
Program 31-1 creates an instance of 'EngiFreehandElement'.
Program-31-1: (EngiFreehandElement; polylines:) ------------------------------------------------------- | polylines freehandElement locus1 locus2 locus3| polylines := OrderedCollection new. locus1 := OrderedCollection new. locus2 := OrderedCollection new. locus3 := OrderedCollection new. locus1 add: 100 @ 100; add: 105 @ 150. locus2 add: 105 @ 100; add: 180 @ 100; add: 175 @ 150. locus3 add: 110 @ 150; add: 170 @ 150. polylines add: locus1. polylines add: locus2. polylines add: locus3. freehandElement := EngiFreehandElement new. freehandElement value: 'kuchi (kanji)'. freehandElement polylines: polylines. freehandElement open. ^freehandElement -------------------------------------------------------
Evaluate the program to see a window which has an up-side-down trapezoid which is the Chinese character ('kanji') meaning mouth and is pronounced 'kuchi' in Japanese.
The program creates three loci (the first and last have just two points, the middle one has three points) which represent the Chinese character 'kuchi', then sends the loci as 'polylines:' with a window label as 'value:' to an instance of 'EngiFreehandElement'. In this case, the 'kuchi' Chinese character is constructed by the program instead of being hand written, however 'EngiFreehandElement' behaves the same.
An instance of 'EngiFreehandElement' calculates start points, end points, turn points, a normalized image, horizontal and vertical spectra of the normalized image, density of nine parts of the normalized image whenever it receives a set of poly-lines.
A start point is indexed by the first point of a segment, and end point is indexed by last point. A turning point is considered to be any point whose two lines are at less than 40 degrees. This data is used as a filter finding similar shapes.
A normalized image is generated by:
Horizontal spectra of the normalized image are made by counting the black pixels of the image horizontally. Density of nine parts is calculated similarly by region. This data is also used in filtering images to find similar shapes.
This program was written in 1987. It was not usable then because computers were too slow. However today's machines are fast enough to make this code usable. In the next section I will introduce a program which uses mouse or tablet drawn pictures to search for similar pictures.