intfx.cpp
来自「一个google的OCR源码」· C++ 代码 · 共 609 行 · 第 1/2 页
CPP
609 行
/****************************************************************************** ** Filename: intfx.c ** Purpose: Integer character normalization & feature extraction ** Author: Robert Moss ** History: Tue May 21 15:51:57 MDT 1991, RWM, Created. ** ** (c) Copyright Hewlett-Packard Company, 1988. ** 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. ******************************************************************************//**---------------------------------------------------------------------------- Include Files and Type Defines----------------------------------------------------------------------------**/#include "intfx.h"#include "intmatcher.h"#include "const.h"#ifdef __UNIX__#include <assert.h>#endif/**---------------------------------------------------------------------------- Private Function Prototypes----------------------------------------------------------------------------**/int SaveFeature();uinT8 TableLookup();uinT8 MySqrt2();void ClipRadius();make_int_var (RadiusGyrMinMan, 255, MakeRadiusGyrMinMan,16, 10, SetRadiusGyrMinMan,"Minimum Radius of Gyration Mantissa 0-255: ");make_int_var (RadiusGyrMinExp, 0, MakeRadiusGyrMinExp,16, 11, SetRadiusGyrMinExp,"Minimum Radius of Gyration Exponent 0-255: ");make_int_var (RadiusGyrMaxMan, 158, MakeRadiusGyrMaxMan,16, 12, SetRadiusGyrMaxMan,"Maximum Radius of Gyration Mantissa 0-255: ");make_int_var (RadiusGyrMaxExp, 8, MakeRadiusGyrMaxExp,16, 13, SetRadiusGyrMaxExp,"Maximum Radius of Gyration Exponent 0-255: ");/**---------------------------------------------------------------------------- Global Data Definitions and Declarations----------------------------------------------------------------------------**/#define ATAN_TABLE_SIZE 64static uinT8 AtanTable[ATAN_TABLE_SIZE];/**---------------------------------------------------------------------------- Public Code----------------------------------------------------------------------------**//*---------------------------------------------------------------------------*/void InitIntegerFX() { int i; for (i = 0; i < ATAN_TABLE_SIZE; i++) AtanTable[i] = (uinT8) (atan ((i / (float) ATAN_TABLE_SIZE)) * 128.0 / PI + 0.5);}/*--------------------------------------------------------------------------*/int ExtractIntFeat(TBLOB *Blob, INT_FEATURE_ARRAY BLFeat, INT_FEATURE_ARRAY CNFeat, INT_FX_RESULT Results) { TESSLINE *OutLine; EDGEPT *Loop, *LoopStart, *Segment; inT16 LastX, LastY, Xmean, Ymean; inT32 NormX, NormY, DeltaX, DeltaY; inT32 Xsum, Ysum; uinT32 Ix, Iy, LengthSum; uinT16 n; uinT8 Theta; uinT16 NumBLFeatures, NumCNFeatures; uinT8 RxInv, RyInv; /* x.xxxxxxx * 2^Exp */ uinT8 RxExp, RyExp; /* sxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxx */ register inT32 pfX, pfY, dX, dY; uinT16 Length; register int i; Results->Length = 0; Results->Xmean = 0; Results->Ymean = 0; Results->Rx = 0; Results->Ry = 0; Results->NumBL = 0; Results->NumCN = 0; /* find Xmean, Ymean */ NumBLFeatures = 0; NumCNFeatures = 0; OutLine = Blob->outlines; Xsum = 0; Ysum = 0; LengthSum = 0; while (OutLine != NULL) { LoopStart = OutLine->loop; Loop = LoopStart; LastX = Loop->pos.x; LastY = Loop->pos.y; /* Check for bad loops */ if ((Loop == NULL) || (Loop->next == NULL) || (Loop->next == LoopStart)) return FALSE; do { Segment = Loop; Loop = Loop->next; NormX = Loop->pos.x; NormY = Loop->pos.y; n = 1; if (!is_hidden_edge (Segment)) { DeltaX = NormX - LastX; DeltaY = NormY - LastY; Length = MySqrt (DeltaX, DeltaY); n = ((Length << 2) + Length + 32) >> 6; if (n != 0) { Xsum += ((LastX << 1) + DeltaX) * (int) Length; Ysum += ((LastY << 1) + DeltaY) * (int) Length; LengthSum += Length; } } if (n != 0) { /* Throw away a point that is too close */ LastX = NormX; LastY = NormY; } } while (Loop != LoopStart); OutLine = OutLine->next; } if (LengthSum == 0) return FALSE; Xmean = (Xsum / (inT32) LengthSum) >> 1; Ymean = (Ysum / (inT32) LengthSum) >> 1; Results->Length = LengthSum; Results->Xmean = Xmean; Results->Ymean = Ymean; /* extract Baseline normalized features, */ /* and find 2nd moments & radius of gyration */ Ix = 0; Iy = 0; NumBLFeatures = 0; OutLine = Blob->outlines; while (OutLine != NULL) { LoopStart = OutLine->loop; Loop = LoopStart; LastX = Loop->pos.x - Xmean; LastY = Loop->pos.y; /* Check for bad loops */ if ((Loop == NULL) || (Loop->next == NULL) || (Loop->next == LoopStart)) return FALSE; do { Segment = Loop; Loop = Loop->next; NormX = Loop->pos.x - Xmean; NormY = Loop->pos.y; n = 1; if (!is_hidden_edge (Segment)) { DeltaX = NormX - LastX; DeltaY = NormY - LastY; Length = MySqrt (DeltaX, DeltaY); n = ((Length << 2) + Length + 32) >> 6; if (n != 0) { Theta = TableLookup (DeltaY, DeltaX); dX = (DeltaX << 8) / n; dY = (DeltaY << 8) / n; pfX = (LastX << 8) + (dX >> 1); pfY = (LastY << 8) + (dY >> 1); Ix += ((pfY >> 8) - Ymean) * ((pfY >> 8) - Ymean); Iy += (pfX >> 8) * (pfX >> 8); if (SaveFeature (BLFeat, NumBLFeatures, (inT16) (pfX >> 8), (inT16) ((pfY >> 8) - 128), Theta) == FALSE) return FALSE; NumBLFeatures++; for (i = 1; i < n; i++) { pfX += dX; pfY += dY; Ix += ((pfY >> 8) - Ymean) * ((pfY >> 8) - Ymean); Iy += (pfX >> 8) * (pfX >> 8); if (SaveFeature (BLFeat, NumBLFeatures, (inT16) (pfX >> 8), (inT16) ((pfY >> 8) - 128), Theta) == FALSE) return FALSE; NumBLFeatures++; } } } if (n != 0) { /* Throw away a point that is too close */ LastX = NormX; LastY = NormY; } } while (Loop != LoopStart); OutLine = OutLine->next; } if (Ix == 0) Ix = 1; if (Iy == 0) Iy = 1; RxInv = MySqrt2 (NumBLFeatures, Ix, &RxExp); RyInv = MySqrt2 (NumBLFeatures, Iy, &RyExp); ClipRadius(&RxInv, &RxExp, &RyInv, &RyExp); Results->Rx = (inT16) (51.2 / (double) RxInv * pow (2.0, (double) RxExp)); Results->Ry = (inT16) (51.2 / (double) RyInv * pow (2.0, (double) RyExp)); Results->NumBL = NumBLFeatures; /* extract character normalized features */ NumCNFeatures = 0; OutLine = Blob->outlines; while (OutLine != NULL) { LoopStart = OutLine->loop; Loop = LoopStart; LastX = (Loop->pos.x - Xmean) * RyInv; LastY = (Loop->pos.y - Ymean) * RxInv; LastX >>= (inT8) RyExp; LastY >>= (inT8) RxExp; /* Check for bad loops */ if ((Loop == NULL) || (Loop->next == NULL) || (Loop->next == LoopStart)) return FALSE; do { Segment = Loop; Loop = Loop->next; NormX = (Loop->pos.x - Xmean) * RyInv; NormY = (Loop->pos.y - Ymean) * RxInv; NormX >>= (inT8) RyExp; NormY >>= (inT8) RxExp; n = 1; if (!is_hidden_edge (Segment)) { DeltaX = NormX - LastX; DeltaY = NormY - LastY; Length = MySqrt (DeltaX, DeltaY); n = ((Length << 2) + Length + 32) >> 6; if (n != 0) { Theta = TableLookup (DeltaY, DeltaX); dX = (DeltaX << 8) / n; dY = (DeltaY << 8) / n; pfX = (LastX << 8) + (dX >> 1); pfY = (LastY << 8) + (dY >> 1); if (SaveFeature (CNFeat, NumCNFeatures, (inT16) (pfX >> 8), (inT16) ((pfY >> 8)), Theta) == FALSE) return FALSE; NumCNFeatures++; for (i = 1; i < n; i++) { pfX += dX; pfY += dY; if (SaveFeature (CNFeat, NumCNFeatures, (inT16) (pfX >> 8), (inT16) ((pfY >> 8)), Theta) == FALSE) return FALSE; NumCNFeatures++; } } } if (n != 0) { /* Throw away a point that is too close */ LastX = NormX; LastY = NormY; } } while (Loop != LoopStart); OutLine = OutLine->next; } Results->NumCN = NumCNFeatures; return TRUE;}/*--------------------------------------------------------------------------*/uinT8 TableLookup(inT32 Y, inT32 X) { inT16 Angle; uinT16 Ratio; uinT32 AbsX, AbsY; assert ((X != 0) || (Y != 0)); if (X < 0) AbsX = -X; else AbsX = X; if (Y < 0) AbsY = -Y; else AbsY = Y; if (AbsX > AbsY) Ratio = AbsY * ATAN_TABLE_SIZE / AbsX; else Ratio = AbsX * ATAN_TABLE_SIZE / AbsY;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?