2007-03-08 04:03:40 +08:00
|
|
|
/******************************************************************************
|
2018-07-02 18:24:35 +08:00
|
|
|
** Filename: cluster.h
|
|
|
|
** Purpose: Definition of feature space clustering routines
|
|
|
|
** Author: Dan Johnson
|
2007-03-08 04:03:40 +08:00
|
|
|
**
|
2018-07-02 18:24:35 +08:00
|
|
|
** (c) Copyright Hewlett-Packard Company, 1988.
|
2007-03-08 04:03:40 +08:00
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
2018-07-02 18:24:35 +08:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CLUSTER_H
|
|
|
|
#define CLUSTER_H
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
#include "kdtree.h"
|
|
|
|
#include "oldlist.h"
|
|
|
|
|
2012-02-02 10:57:42 +08:00
|
|
|
struct BUCKETS;
|
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
#define MINBUCKETS 5
|
|
|
|
#define MAXBUCKETS 39
|
2012-02-02 10:57:42 +08:00
|
|
|
|
2007-03-08 04:03:40 +08:00
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Types
|
|
|
|
----------------------------------------------------------------------*/
|
2010-11-24 02:34:14 +08:00
|
|
|
typedef struct sample {
|
2019-04-04 02:25:48 +08:00
|
|
|
bool Clustered : 1; // true if included in a higher cluster
|
|
|
|
bool Prototype : 1; // true if cluster represented by a proto
|
|
|
|
unsigned SampleCount : 30; // number of samples in this cluster
|
|
|
|
struct sample* Left; // ptr to left sub-cluster
|
|
|
|
struct sample* Right; // ptr to right sub-cluster
|
|
|
|
int32_t CharID; // identifier of char sample came from
|
|
|
|
float Mean[1]; // mean of cluster - SampleSize floats
|
2010-11-24 02:34:14 +08:00
|
|
|
} CLUSTER;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
using SAMPLE = CLUSTER; // can refer to as either sample or cluster
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
typedef enum { spherical, elliptical, mixed, automatic } PROTOSTYLE;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
typedef struct { // parameters to control clustering
|
|
|
|
PROTOSTYLE ProtoStyle; // specifies types of protos to be made
|
|
|
|
float MinSamples; // min # of samples per proto - % of total
|
|
|
|
float MaxIllegal; // max percentage of samples in a cluster which
|
|
|
|
// have more than 1 feature in that cluster
|
|
|
|
float Independence; // desired independence between dimensions
|
|
|
|
double Confidence; // desired confidence in prototypes created
|
|
|
|
int MagicSamples; // Ideal number of samples in a cluster.
|
2010-11-24 02:34:14 +08:00
|
|
|
} CLUSTERCONFIG;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
typedef enum { normal, uniform, D_random, DISTRIBUTION_COUNT } DISTRIBUTION;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
typedef union {
|
2018-07-02 18:24:35 +08:00
|
|
|
float Spherical;
|
2019-04-04 02:25:48 +08:00
|
|
|
float* Elliptical;
|
2010-11-24 02:34:14 +08:00
|
|
|
} FLOATUNION;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
typedef struct {
|
2019-04-04 02:25:48 +08:00
|
|
|
bool Significant : 1; // true if prototype is significant
|
|
|
|
bool Merged : 1; // Merged after clustering so do not output
|
|
|
|
// but kept for display purposes. If it has no
|
|
|
|
// samples then it was actually merged.
|
|
|
|
// Otherwise it matched an already significant
|
|
|
|
// cluster.
|
|
|
|
unsigned Style : 2; // spherical, elliptical, or mixed
|
|
|
|
unsigned NumSamples : 28; // number of samples in the cluster
|
|
|
|
CLUSTER* Cluster; // ptr to cluster which made prototype
|
|
|
|
DISTRIBUTION* Distrib; // different distribution for each dimension
|
|
|
|
float* Mean; // prototype mean
|
|
|
|
float TotalMagnitude; // total magnitude over all dimensions
|
|
|
|
float LogMagnitude; // log base e of TotalMagnitude
|
|
|
|
FLOATUNION Variance; // prototype variance
|
|
|
|
FLOATUNION Magnitude; // magnitude of density function
|
|
|
|
FLOATUNION Weight; // weight of density function
|
2010-11-24 02:34:14 +08:00
|
|
|
} PROTOTYPE;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
typedef struct {
|
2019-04-04 02:25:48 +08:00
|
|
|
int16_t SampleSize; // number of parameters per sample
|
|
|
|
PARAM_DESC* ParamDesc; // description of each parameter
|
|
|
|
int32_t NumberOfSamples; // total number of samples being clustered
|
|
|
|
KDTREE* KDTree; // for optimal nearest neighbor searching
|
|
|
|
CLUSTER* Root; // ptr to root cluster of cluster tree
|
|
|
|
LIST ProtoList; // list of prototypes
|
|
|
|
int32_t NumChar; // # of characters represented by samples
|
2012-02-02 10:57:42 +08:00
|
|
|
// cache of reusable histograms by distribution type and number of buckets.
|
|
|
|
BUCKETS* bucket_cache[DISTRIBUTION_COUNT][MAXBUCKETS + 1 - MINBUCKETS];
|
2010-11-24 02:34:14 +08:00
|
|
|
} CLUSTERER;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-11-24 02:34:14 +08:00
|
|
|
typedef struct {
|
2019-04-04 02:25:48 +08:00
|
|
|
int32_t NumSamples; // number of samples in list
|
|
|
|
int32_t MaxNumSamples; // maximum size of list
|
|
|
|
SAMPLE* Sample[1]; // array of ptrs to sample data structures
|
2010-11-24 02:34:14 +08:00
|
|
|
} SAMPLELIST;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
// low level cluster tree analysis routines.
|
2019-04-04 02:25:48 +08:00
|
|
|
#define InitSampleSearch(S, C) \
|
|
|
|
(((C) == nullptr) ? (S = NIL_LIST) : (S = push(NIL_LIST, (C))))
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------
|
|
|
|
Public Function Prototypes
|
|
|
|
--------------------------------------------------------------------------*/
|
2019-04-04 02:25:48 +08:00
|
|
|
CLUSTERER* MakeClusterer(int16_t SampleSize, const PARAM_DESC ParamDesc[]);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
SAMPLE* MakeSample(CLUSTERER* Clusterer, const float* Feature, int32_t CharID);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
LIST ClusterSamples(CLUSTERER* Clusterer, CLUSTERCONFIG* Config);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
void FreeClusterer(CLUSTERER* Clusterer);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
void FreeProtoList(LIST* ProtoList);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
void FreePrototype(void* arg); // PROTOTYPE *Prototype);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
CLUSTER* NextSample(LIST* SearchState);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
float Mean(PROTOTYPE* Proto, uint16_t Dimension);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2019-04-04 02:25:48 +08:00
|
|
|
float StandardDeviation(PROTOTYPE* Proto, uint16_t Dimension);
|
2008-02-01 08:07:59 +08:00
|
|
|
|
Use POSIX data types and macros (#878)
* api: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* cutil: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* training: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract data types by POSIX data types
Now all Tesseract data types which are no longer needed can be removed
from ccutil/host.h.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* lstm: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Remove the macros which are now unused from ccutil/host.h.
Remove also the obsolete history comments.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* Fix build error caused by ambiguous ClipToRange
Error message vom Appveyor CI:
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2672: 'ClipToRange': no matching overloaded function found [C:\projects\tesseract\build\libtesseract.vcxproj]
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2782: 'T ClipToRange(const T &,const T &,const T &)': template parameter 'T' is ambiguous [C:\projects\tesseract\build\libtesseract.vcxproj]
c:\projects\tesseract\ccutil\helpers.h(122): note: see declaration of 'ClipToRange'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: could be 'char'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: or 'int'
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* unittest: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* arch: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2018-03-14 04:36:30 +08:00
|
|
|
int32_t MergeClusters(int16_t N, PARAM_DESC ParamDesc[], int32_t n1, int32_t n2,
|
2018-07-02 18:24:35 +08:00
|
|
|
float m[], float m1[], float m2[]);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
#endif
|