mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2024-11-30 23:49:05 +08:00
023e1b340e
* 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>
99 lines
3.5 KiB
C++
99 lines
3.5 KiB
C++
/**********************************************************************
|
|
* File: dppoint.cpp
|
|
* Description: Simple generic dynamic programming class.
|
|
* Author: Ray Smith
|
|
* Created: Wed Mar 25 19:08:01 PDT 2009
|
|
*
|
|
* (C) Copyright 2009, Google Inc.
|
|
** 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 "dppoint.h"
|
|
#include "tprintf.h"
|
|
|
|
namespace tesseract {
|
|
|
|
// Solve the dynamic programming problem for the given array of points, with
|
|
// the given size and cost function.
|
|
// Steps backwards are limited to being between min_step and max_step
|
|
// inclusive.
|
|
// The return value is the tail of the best path.
|
|
DPPoint* DPPoint::Solve(int min_step, int max_step, bool debug,
|
|
CostFunc cost_func, int size, DPPoint* points) {
|
|
if (size <= 0 || max_step < min_step || min_step >= size)
|
|
return NULL; // Degenerate, but not necessarily an error.
|
|
ASSERT_HOST(min_step > 0); // Infinite loop possible if this is not true.
|
|
if (debug)
|
|
tprintf("min = %d, max=%d\n",
|
|
min_step, max_step);
|
|
// Evaluate the total cost at each point.
|
|
for (int i = 0; i < size; ++i) {
|
|
for (int offset = min_step; offset <= max_step; ++offset) {
|
|
DPPoint* prev = offset <= i ? points + i - offset : NULL;
|
|
int64_t new_cost = (points[i].*cost_func)(prev);
|
|
if (points[i].best_prev_ != NULL && offset > min_step * 2 &&
|
|
new_cost > points[i].total_cost_)
|
|
break; // Find only the first minimum if going over twice the min.
|
|
}
|
|
points[i].total_cost_ += points[i].local_cost_;
|
|
if (debug) {
|
|
tprintf("At point %d, local cost=%d, total_cost=%d, steps=%d\n",
|
|
i, points[i].local_cost_, points[i].total_cost_,
|
|
points[i].total_steps_);
|
|
}
|
|
}
|
|
// Now find the end of the best path and return it.
|
|
int best_cost = points[size - 1].total_cost_;
|
|
int best_end = size - 1;
|
|
for (int end = best_end - 1; end >= size - min_step; --end) {
|
|
int cost = points[end].total_cost_;
|
|
if (cost < best_cost) {
|
|
best_cost = cost;
|
|
best_end = end;
|
|
}
|
|
}
|
|
return points + best_end;
|
|
}
|
|
|
|
// A CostFunc that takes the variance of step into account in the cost.
|
|
int64_t DPPoint::CostWithVariance(const DPPoint* prev) {
|
|
if (prev == NULL || prev == this) {
|
|
UpdateIfBetter(0, 1, NULL, 0, 0, 0);
|
|
return 0;
|
|
}
|
|
|
|
int delta = this - prev;
|
|
int32_t n = prev->n_ + 1;
|
|
int32_t sig_x = prev->sig_x_ + delta;
|
|
int64_t sig_xsq = prev->sig_xsq_ + delta * delta;
|
|
int64_t cost = (sig_xsq - sig_x * sig_x / n) / n;
|
|
cost += prev->total_cost_;
|
|
UpdateIfBetter(cost, prev->total_steps_ + 1, prev, n, sig_x, sig_xsq);
|
|
return cost;
|
|
}
|
|
|
|
// Update the other members if the cost is lower.
|
|
void DPPoint::UpdateIfBetter(int64_t cost, int32_t steps, const DPPoint* prev,
|
|
int32_t n, int32_t sig_x, int64_t sig_xsq) {
|
|
if (cost < total_cost_) {
|
|
total_cost_ = cost;
|
|
total_steps_ = steps;
|
|
best_prev_ = prev;
|
|
n_ = n;
|
|
sig_x_ = sig_x;
|
|
sig_xsq_ = sig_xsq;
|
|
}
|
|
}
|
|
|
|
} // namespace tesseract.
|
|
|