tesseract  4.00.00dev
cluster.h File Reference
#include "kdtree.h"
#include "oldlist.h"

Go to the source code of this file.

Classes

struct  sample
 
struct  CLUSTERCONFIG
 
union  FLOATUNION
 
struct  PROTOTYPE
 
struct  CLUSTERER
 
struct  SAMPLELIST
 

Macros

#define MINBUCKETS   5
 
#define MAXBUCKETS   39
 
#define InitSampleSearch(S, C)   (((C)==NULL)?(S=NIL_LIST):(S=push(NIL_LIST,(C))))
 
#define ALREADYCLUSTERED   4000
 

Typedefs

typedef struct sample CLUSTER
 
typedef CLUSTER SAMPLE
 

Enumerations

enum  PROTOSTYLE { spherical, elliptical, mixed, automatic }
 
enum  DISTRIBUTION { normal, uniform, D_random, DISTRIBUTION_COUNT }
 

Functions

CLUSTERERMakeClusterer (inT16 SampleSize, const PARAM_DESC ParamDesc[])
 
SAMPLEMakeSample (CLUSTERER *Clusterer, const FLOAT32 *Feature, inT32 CharID)
 
LIST ClusterSamples (CLUSTERER *Clusterer, CLUSTERCONFIG *Config)
 
void FreeClusterer (CLUSTERER *Clusterer)
 
void FreeProtoList (LIST *ProtoList)
 
void FreePrototype (void *arg)
 
CLUSTERNextSample (LIST *SearchState)
 
FLOAT32 Mean (PROTOTYPE *Proto, uinT16 Dimension)
 
FLOAT32 StandardDeviation (PROTOTYPE *Proto, uinT16 Dimension)
 
inT32 MergeClusters (inT16 N, PARAM_DESC ParamDesc[], inT32 n1, inT32 n2, FLOAT32 m[], FLOAT32 m1[], FLOAT32 m2[])
 

Macro Definition Documentation

◆ ALREADYCLUSTERED

#define ALREADYCLUSTERED   4000

Definition at line 133 of file cluster.h.

◆ InitSampleSearch

#define InitSampleSearch (   S,
 
)    (((C)==NULL)?(S=NIL_LIST):(S=push(NIL_LIST,(C))))

Definition at line 105 of file cluster.h.

◆ MAXBUCKETS

#define MAXBUCKETS   39

Definition at line 27 of file cluster.h.

◆ MINBUCKETS

#define MINBUCKETS   5

Definition at line 26 of file cluster.h.

Typedef Documentation

◆ CLUSTER

typedef struct sample CLUSTER

◆ SAMPLE

typedef CLUSTER SAMPLE

Definition at line 42 of file cluster.h.

Enumeration Type Documentation

◆ DISTRIBUTION

Enumerator
normal 
uniform 
D_random 
DISTRIBUTION_COUNT 

Definition at line 58 of file cluster.h.

◆ PROTOSTYLE

enum PROTOSTYLE
Enumerator
spherical 
elliptical 
mixed 
automatic 

Definition at line 44 of file cluster.h.

44  {
46 } PROTOSTYLE;
PROTOSTYLE
Definition: cluster.h:44
Definition: cluster.h:45

Function Documentation

◆ ClusterSamples()

LIST ClusterSamples ( CLUSTERER Clusterer,
CLUSTERCONFIG Config 
)

This routine first checks to see if the samples in this clusterer have already been clustered before; if so, it does not bother to recreate the cluster tree. It simply recomputes the prototypes based on the new Config info.

If the samples have not been clustered before, the samples in the KD tree are formed into a cluster tree and then the prototypes are computed from the cluster tree.

In either case this routine returns a pointer to a list of prototypes that best represent the samples given the constraints specified in Config.

Parameters
Clustererdata struct containing samples to be clustered
Configparameters which control clustering process
Returns
Pointer to a list of prototypes
Note
Exceptions: None
History: 5/29/89, DSJ, Created.

Definition at line 512 of file cluster.cpp.

512  {
513  //only create cluster tree if samples have never been clustered before
514  if (Clusterer->Root == NULL)
515  CreateClusterTree(Clusterer);
516 
517  //deallocate the old prototype list if one exists
518  FreeProtoList (&Clusterer->ProtoList);
519  Clusterer->ProtoList = NIL_LIST;
520 
521  //compute prototypes starting at the root node in the tree
522  ComputePrototypes(Clusterer, Config);
523  // We don't need the cluster pointers in the protos any more, so null them
524  // out, which makes it safe to delete the clusterer.
525  LIST proto_list = Clusterer->ProtoList;
526  iterate(proto_list) {
527  PROTOTYPE *proto = reinterpret_cast<PROTOTYPE *>(first_node(proto_list));
528  proto->Cluster = NULL;
529  }
530  return Clusterer->ProtoList;
531 } // ClusterSamples
LIST ProtoList
Definition: cluster.h:92
void CreateClusterTree(CLUSTERER *Clusterer)
Definition: cluster.cpp:698
#define NIL_LIST
Definition: oldlist.h:126
void FreeProtoList(LIST *ProtoList)
Definition: cluster.cpp:573
#define first_node(l)
Definition: oldlist.h:139
CLUSTER * Cluster
Definition: cluster.h:76
CLUSTER * Root
Definition: cluster.h:91
#define iterate(l)
Definition: oldlist.h:159
void ComputePrototypes(CLUSTERER *Clusterer, CLUSTERCONFIG *Config)
Definition: cluster.cpp:924

◆ FreeClusterer()

void FreeClusterer ( CLUSTERER Clusterer)

This routine frees all of the memory allocated to the specified data structure. It will not, however, free the memory used by the prototype list. The pointers to the clusters for each prototype in the list will be set to NULL to indicate that the cluster data structures no longer exist. Any sample lists that have been obtained via calls to GetSamples are no longer valid.

Parameters
Clustererpointer to data structure to be freed
Returns
None
Note
Exceptions: None
History: 6/6/89, DSJ, Created.

Definition at line 546 of file cluster.cpp.

546  {
547  if (Clusterer != NULL) {
548  free(Clusterer->ParamDesc);
549  if (Clusterer->KDTree != NULL)
550  FreeKDTree (Clusterer->KDTree);
551  if (Clusterer->Root != NULL)
552  FreeCluster (Clusterer->Root);
553  // Free up all used buckets structures.
554  for (int d = 0; d < DISTRIBUTION_COUNT; ++d) {
555  for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c)
556  if (Clusterer->bucket_cache[d][c] != NULL)
557  FreeBuckets(Clusterer->bucket_cache[d][c]);
558  }
559 
560  free(Clusterer);
561  }
562 } // FreeClusterer
BUCKETS * bucket_cache[DISTRIBUTION_COUNT][MAXBUCKETS+1 - MINBUCKETS]
Definition: cluster.h:95
#define MAXBUCKETS
Definition: cluster.h:27
#define MINBUCKETS
Definition: cluster.h:26
PARAM_DESC * ParamDesc
Definition: cluster.h:88
void FreeCluster(CLUSTER *Cluster)
Definition: cluster.cpp:2183
void FreeKDTree(KDTREE *Tree)
Definition: kdtree.cpp:349
CLUSTER * Root
Definition: cluster.h:91
KDTREE * KDTree
Definition: cluster.h:90
void FreeBuckets(BUCKETS *Buckets)
Definition: cluster.cpp:2165

◆ FreeProtoList()

void FreeProtoList ( LIST ProtoList)

This routine frees all of the memory allocated to the specified list of prototypes. The clusters which are pointed to by the prototypes are not freed.

Parameters
ProtoListpointer to list of prototypes to be freed
Returns
None
Note
Exceptions: None
History: 6/6/89, DSJ, Created.

Definition at line 573 of file cluster.cpp.

573  {
574  destroy_nodes(*ProtoList, FreePrototype);
575 } // FreeProtoList
void destroy_nodes(LIST list, void_dest destructor)
Definition: oldlist.cpp:199
void FreePrototype(void *arg)
Definition: cluster.cpp:587

◆ FreePrototype()

void FreePrototype ( void *  arg)

This routine deallocates the memory consumed by the specified prototype and modifies the corresponding cluster so that it is no longer marked as a prototype. The cluster is NOT deallocated by this routine.

Parameters
argprototype data structure to be deallocated
Returns
None
Note
Exceptions: None
History: 5/30/89, DSJ, Created.

Definition at line 587 of file cluster.cpp.

587  { //PROTOTYPE *Prototype)
588  PROTOTYPE *Prototype = (PROTOTYPE *) arg;
589 
590  // unmark the corresponding cluster (if there is one
591  if (Prototype->Cluster != NULL)
592  Prototype->Cluster->Prototype = FALSE;
593 
594  // deallocate the prototype statistics and then the prototype itself
595  free (Prototype->Distrib);
596  free (Prototype->Mean);
597  if (Prototype->Style != spherical) {
598  free (Prototype->Variance.Elliptical);
599  free (Prototype->Magnitude.Elliptical);
600  free (Prototype->Weight.Elliptical);
601  }
602  free(Prototype);
603 } // FreePrototype
DISTRIBUTION * Distrib
Definition: cluster.h:77
FLOAT32 * Elliptical
Definition: cluster.h:64
FLOATUNION Weight
Definition: cluster.h:83
FLOATUNION Variance
Definition: cluster.h:81
unsigned Style
Definition: cluster.h:74
#define FALSE
Definition: capi.h:46
FLOAT32 * Mean
Definition: cluster.h:78
unsigned Prototype
Definition: cluster.h:34
CLUSTER * Cluster
Definition: cluster.h:76
FLOATUNION Magnitude
Definition: cluster.h:82

◆ MakeClusterer()

CLUSTERER* MakeClusterer ( inT16  SampleSize,
const PARAM_DESC  ParamDesc[] 
)

This routine creates a new clusterer data structure, initializes it, and returns a pointer to it.

Parameters
SampleSizenumber of dimensions in feature space
ParamDescdescription of each dimension
Returns
pointer to the new clusterer data structure
Note
Exceptions: None
History: 5/29/89, DSJ, Created.

Definition at line 399 of file cluster.cpp.

399  {
400  CLUSTERER *Clusterer;
401  int i;
402 
403  // allocate main clusterer data structure and init simple fields
404  Clusterer = (CLUSTERER *) Emalloc (sizeof (CLUSTERER));
405  Clusterer->SampleSize = SampleSize;
406  Clusterer->NumberOfSamples = 0;
407  Clusterer->NumChar = 0;
408 
409  // init fields which will not be used initially
410  Clusterer->Root = NULL;
411  Clusterer->ProtoList = NIL_LIST;
412 
413  // maintain a copy of param descriptors in the clusterer data structure
414  Clusterer->ParamDesc =
415  (PARAM_DESC *) Emalloc (SampleSize * sizeof (PARAM_DESC));
416  for (i = 0; i < SampleSize; i++) {
417  Clusterer->ParamDesc[i].Circular = ParamDesc[i].Circular;
418  Clusterer->ParamDesc[i].NonEssential = ParamDesc[i].NonEssential;
419  Clusterer->ParamDesc[i].Min = ParamDesc[i].Min;
420  Clusterer->ParamDesc[i].Max = ParamDesc[i].Max;
421  Clusterer->ParamDesc[i].Range = ParamDesc[i].Max - ParamDesc[i].Min;
422  Clusterer->ParamDesc[i].HalfRange = Clusterer->ParamDesc[i].Range / 2;
423  Clusterer->ParamDesc[i].MidRange =
424  (ParamDesc[i].Max + ParamDesc[i].Min) / 2;
425  }
426 
427  // allocate a kd tree to hold the samples
428  Clusterer->KDTree = MakeKDTree (SampleSize, ParamDesc);
429 
430  // Initialize cache of histogram buckets to minimize recomputing them.
431  for (int d = 0; d < DISTRIBUTION_COUNT; ++d) {
432  for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c)
433  Clusterer->bucket_cache[d][c] = NULL;
434  }
435 
436  return Clusterer;
437 } // MakeClusterer
LIST ProtoList
Definition: cluster.h:92
BUCKETS * bucket_cache[DISTRIBUTION_COUNT][MAXBUCKETS+1 - MINBUCKETS]
Definition: cluster.h:95
#define MAXBUCKETS
Definition: cluster.h:27
KDTREE * MakeKDTree(inT16 KeySize, const PARAM_DESC KeyDesc[])
Definition: kdtree.cpp:182
FLOAT32 MidRange
Definition: ocrfeatures.h:53
void * Emalloc(int Size)
Definition: emalloc.cpp:47
FLOAT32 Range
Definition: ocrfeatures.h:51
#define MINBUCKETS
Definition: cluster.h:26
PARAM_DESC * ParamDesc
Definition: cluster.h:88
#define NIL_LIST
Definition: oldlist.h:126
inT8 Circular
Definition: ocrfeatures.h:47
FLOAT32 Min
Definition: ocrfeatures.h:49
inT32 NumberOfSamples
Definition: cluster.h:89
inT16 SampleSize
Definition: cluster.h:87
inT8 NonEssential
Definition: ocrfeatures.h:48
inT32 NumChar
Definition: cluster.h:93
FLOAT32 Max
Definition: ocrfeatures.h:50
CLUSTER * Root
Definition: cluster.h:91
KDTREE * KDTree
Definition: cluster.h:90
FLOAT32 HalfRange
Definition: ocrfeatures.h:52

◆ MakeSample()

SAMPLE* MakeSample ( CLUSTERER Clusterer,
const FLOAT32 Feature,
inT32  CharID 
)

This routine creates a new sample data structure to hold the specified feature. This sample is added to the clusterer data structure (so that it knows which samples are to be clustered later), and a pointer to the sample is returned to the caller.

Parameters
Clustererclusterer data structure to add sample to
Featurefeature to be added to clusterer
CharIDunique ident. of char that sample came from
Returns
Pointer to the new sample data structure
Note
Exceptions: ALREADYCLUSTERED MakeSample can't be called after ClusterSamples has been called
History: 5/29/89, DSJ, Created.

Definition at line 455 of file cluster.cpp.

456  {
457  SAMPLE *Sample;
458  int i;
459 
460  // see if the samples have already been clustered - if so trap an error
461  if (Clusterer->Root != NULL)
463  "Can't add samples after they have been clustered");
464 
465  // allocate the new sample and initialize it
466  Sample = (SAMPLE *) Emalloc (sizeof (SAMPLE) +
467  (Clusterer->SampleSize -
468  1) * sizeof (FLOAT32));
469  Sample->Clustered = FALSE;
470  Sample->Prototype = FALSE;
471  Sample->SampleCount = 1;
472  Sample->Left = NULL;
473  Sample->Right = NULL;
474  Sample->CharID = CharID;
475 
476  for (i = 0; i < Clusterer->SampleSize; i++)
477  Sample->Mean[i] = Feature[i];
478 
479  // add the sample to the KD tree - keep track of the total # of samples
480  Clusterer->NumberOfSamples++;
481  KDStore (Clusterer->KDTree, Sample->Mean, (char *) Sample);
482  if (CharID >= Clusterer->NumChar)
483  Clusterer->NumChar = CharID + 1;
484 
485  // execute hook for monitoring clustering operation
486  // (*SampleCreationHook)( Sample );
487 
488  return (Sample);
489 } // MakeSample
unsigned Clustered
Definition: cluster.h:33
void KDStore(KDTREE *Tree, FLOAT32 *Key, void *Data)
Definition: kdtree.cpp:218
struct sample * Left
Definition: cluster.h:36
FLOAT32 Mean[1]
Definition: cluster.h:39
void * Emalloc(int Size)
Definition: emalloc.cpp:47
inT32 NumberOfSamples
Definition: cluster.h:89
inT16 SampleSize
Definition: cluster.h:87
inT32 CharID
Definition: cluster.h:38
#define FALSE
Definition: capi.h:46
#define ALREADYCLUSTERED
Definition: cluster.h:133
struct sample * Right
Definition: cluster.h:37
inT32 NumChar
Definition: cluster.h:93
unsigned Prototype
Definition: cluster.h:34
float FLOAT32
Definition: host.h:42
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
CLUSTER * Root
Definition: cluster.h:91
unsigned SampleCount
Definition: cluster.h:35
KDTREE * KDTree
Definition: cluster.h:90
Definition: cluster.h:32

◆ Mean()

FLOAT32 Mean ( PROTOTYPE Proto,
uinT16  Dimension 
)

This routine returns the mean of the specified prototype in the indicated dimension.

Parameters
Protoprototype to return mean of
Dimensiondimension whose mean is to be returned
Returns
Mean of Prototype in Dimension
Note
Exceptions: none
History: 7/6/89, DSJ, Created.

Definition at line 644 of file cluster.cpp.

644  {
645  return (Proto->Mean[Dimension]);
646 } // Mean
FLOAT32 * Mean
Definition: cluster.h:78

◆ MergeClusters()

inT32 MergeClusters ( inT16  N,
PARAM_DESC  ParamDesc[],
inT32  n1,
inT32  n2,
FLOAT32  m[],
FLOAT32  m1[],
FLOAT32  m2[] 
)

This routine merges two clusters into one larger cluster. To do this it computes the number of samples in the new cluster and the mean of the new cluster. The ParamDesc information is used to ensure that circular dimensions are handled correctly.

Parameters
N# of dimensions (size of arrays)
ParamDescarray of dimension descriptions
n1,n2number of samples in each old cluster
marray to hold mean of new cluster
m1,m2arrays containing means of old clusters
Returns
The number of samples in the new cluster.
Note
Exceptions: None
History: 5/31/89, DSJ, Created.

Definition at line 880 of file cluster.cpp.

885  {
886  inT32 i, n;
887 
888  n = n1 + n2;
889  for (i = N; i > 0; i--, ParamDesc++, m++, m1++, m2++) {
890  if (ParamDesc->Circular) {
891  // if distance between means is greater than allowed
892  // reduce upper point by one "rotation" to compute mean
893  // then normalize the mean back into the accepted range
894  if ((*m2 - *m1) > ParamDesc->HalfRange) {
895  *m = (n1 * *m1 + n2 * (*m2 - ParamDesc->Range)) / n;
896  if (*m < ParamDesc->Min)
897  *m += ParamDesc->Range;
898  }
899  else if ((*m1 - *m2) > ParamDesc->HalfRange) {
900  *m = (n1 * (*m1 - ParamDesc->Range) + n2 * *m2) / n;
901  if (*m < ParamDesc->Min)
902  *m += ParamDesc->Range;
903  }
904  else
905  *m = (n1 * *m1 + n2 * *m2) / n;
906  }
907  else
908  *m = (n1 * *m1 + n2 * *m2) / n;
909  }
910  return n;
911 } // MergeClusters
int32_t inT32
Definition: host.h:38

◆ NextSample()

CLUSTER* NextSample ( LIST SearchState)

This routine is used to find all of the samples which belong to a cluster. It starts by removing the top cluster on the cluster list (SearchState). If this cluster is a leaf it is returned. Otherwise, the right subcluster is pushed on the list and we continue the search in the left subcluster. This continues until a leaf is found. If all samples have been found, NULL is returned. InitSampleSearch() must be called before NextSample() to initialize the search.

Parameters
SearchStateptr to list containing clusters to be searched
Returns
Pointer to the next leaf cluster (sample) or NULL.
Note
Exceptions: None
History: 6/16/89, DSJ, Created.

Definition at line 620 of file cluster.cpp.

620  {
621  CLUSTER *Cluster;
622 
623  if (*SearchState == NIL_LIST)
624  return (NULL);
625  Cluster = (CLUSTER *) first_node (*SearchState);
626  *SearchState = pop (*SearchState);
627  while (TRUE) {
628  if (Cluster->Left == NULL)
629  return (Cluster);
630  *SearchState = push (*SearchState, Cluster->Right);
631  Cluster = Cluster->Left;
632  }
633 } // NextSample
#define TRUE
Definition: capi.h:45
struct sample * Left
Definition: cluster.h:36
#define NIL_LIST
Definition: oldlist.h:126
LIST pop(LIST list)
Definition: oldlist.cpp:299
struct sample * Right
Definition: cluster.h:37
#define first_node(l)
Definition: oldlist.h:139
Definition: cluster.h:32
LIST push(LIST list, void *element)
Definition: oldlist.cpp:317

◆ StandardDeviation()

FLOAT32 StandardDeviation ( PROTOTYPE Proto,
uinT16  Dimension 
)

This routine returns the standard deviation of the prototype in the indicated dimension.

Parameters
Protoprototype to return standard deviation of
Dimensiondimension whose stddev is to be returned
Returns
Standard deviation of Prototype in Dimension
Note
Exceptions: none
History: 7/6/89, DSJ, Created.

Definition at line 657 of file cluster.cpp.

657  {
658  switch (Proto->Style) {
659  case spherical:
660  return ((FLOAT32) sqrt ((double) Proto->Variance.Spherical));
661  case elliptical:
662  return ((FLOAT32)
663  sqrt ((double) Proto->Variance.Elliptical[Dimension]));
664  case mixed:
665  switch (Proto->Distrib[Dimension]) {
666  case normal:
667  return ((FLOAT32)
668  sqrt ((double) Proto->Variance.Elliptical[Dimension]));
669  case uniform:
670  case D_random:
671  return (Proto->Variance.Elliptical[Dimension]);
672  case DISTRIBUTION_COUNT:
673  ASSERT_HOST(!"Distribution count not allowed!");
674  }
675  }
676  return 0.0f;
677 } // StandardDeviation
DISTRIBUTION * Distrib
Definition: cluster.h:77
FLOAT32 * Elliptical
Definition: cluster.h:64
#define ASSERT_HOST(x)
Definition: errcode.h:84
FLOATUNION Variance
Definition: cluster.h:81
unsigned Style
Definition: cluster.h:74
float FLOAT32
Definition: host.h:42
Definition: cluster.h:45
FLOAT32 Spherical
Definition: cluster.h:63
Definition: cluster.h:59