29 #ifndef TESSERACT_CCSTRUCT_MATRIX_H__ 30 #define TESSERACT_CCSTRUCT_MATRIX_H__ 39 class BLOB_CHOICE_LIST;
41 #define NOT_CLASSIFIED reinterpret_cast<BLOB_CHOICE_LIST*>(0) 87 int new_size = size1 * size2;
98 void Resize(
int size1,
int size2,
const T& empty) {
107 int new_size = size1 * size2;
108 T* new_array =
new T[new_size];
109 for (
int col = 0; col < size1; ++col) {
110 for (
int row = 0; row < size2; ++row) {
111 int old_index = col *
dim2() + row;
112 int new_index = col * size2 + row;
114 new_array[new_index] =
array_[old_index];
116 new_array[new_index] =
empty_;
131 for (
int i = 0; i < total_size; ++i)
139 if (fwrite(&
empty_,
sizeof(
empty_), 1, fp) != 1)
return false;
141 if (fwrite(
array_,
sizeof(*
array_), size, fp) != size)
return false;
157 if (fread(&
empty_,
sizeof(
empty_), 1, fp) != 1)
return false;
160 if (fread(
array_,
sizeof(*
array_), size, fp) != size)
return false;
162 for (
int i = 0; i < size; ++i)
174 for (
int i = 0; i < size; ++i)
184 if (!
empty_.Serialize(fp))
return false;
186 for (
int i = 0; i < size; ++i) {
197 if (!
empty_.DeSerialize(swap, fp))
return false;
199 for (
int i = 0; i < size; ++i) {
215 virtual int index(
int column,
int row)
const {
216 return (column *
dim2_ + row);
223 void put(
int column,
int row,
const T& thing) {
231 T
get(
int column,
int row)
const {
255 for (
int i = 0; i < size; ++i) {
259 for (
int x = 0; x <
dim1_; x++) {
260 for (
int y = 0; y <
dim2_; y++) {
261 (*this)(x, y) += addend(x, y);
271 for (
int i = 0; i < size; ++i) {
275 for (
int x = 0; x <
dim1_; x++) {
276 for (
int y = 0; y <
dim2_; y++) {
277 (*this)(x, y) -= minuend(x, y);
285 for (
int i = 0; i < size; ++i) {
292 for (
int i = 0; i < size; ++i) {
297 void Clip(
const T& rangemin,
const T& rangemax) {
299 for (
int i = 0; i < size; ++i) {
307 for (
int i = 0; i < size; ++i) {
308 const T& value =
array_[i];
309 if (value < rangemin || rangemax < value)
317 if (size <= 0)
return 0.0;
320 for (
int i = 0; i < size; ++i) {
326 for (
int i = 0; i < size; ++i) {
327 double normed =
array_[i] - mean;
329 sd += normed * normed;
331 sd = sqrt(sd / size);
334 for (
int i = 0; i < size; ++i) {
344 if (size <= 0)
return empty_;
347 for (
int i = 1; i < size; ++i) {
348 const T& value =
array_[i];
349 if (value > max_value) max_value = value;
357 if (size <= 0)
return empty_;
359 T max_abs =
static_cast<T
>(0);
360 for (
int i = 0; i < size; ++i) {
361 T value =
static_cast<T
>(fabs(
array_[i]));
362 if (value > max_abs) max_abs = value;
370 for (
int i = 0; i < size; ++i) {
379 for (
int i = 0; i < size; ++i) {
386 for (
int i = 0; i < size; ++i) {
417 int max_d =
MAX(src_dim, dest_dim);
418 int min_d =
MIN(src_dim, dest_dim);
427 int num_replicas = 1, move_size = 1, src_step = 1;
428 for (
int d = 0; d < min_d; ++d) num_replicas *= dims[d];
429 for (
int d = max_d + 1; d < num_dims; ++d) move_size *= dims[d];
430 for (
int d = src_dim + 1; d < num_dims; ++d) src_step *= dims[d];
431 if (src_dim > dest_dim) src_step *= dims[src_dim];
434 int wrap_size = move_size;
435 for (
int d = min_d; d <= max_d; ++d) wrap_size *= dims[d];
440 for (
int replica = 0; replica < num_replicas; ++replica) {
441 for (
int start = 0; start < src_step; start += move_size) {
442 for (
int pos = start; pos < wrap_size; pos += src_step) {
443 memcpy(dest, src + pos,
sizeof(*dest) * move_size);
454 for (
int i = 0; i < size; ++i) {
455 T matrix_cell =
array_[i];
456 if (matrix_cell !=
empty_)
465 if (fwrite(&size,
sizeof(size), 1, fp) != 1)
return false;
467 if (fwrite(&size,
sizeof(size), 1, fp) != 1)
return false;
472 if (fp->
FWrite(&size,
sizeof(size), 1) != 1)
return false;
474 if (fp->
FWrite(&size,
sizeof(size), 1) != 1)
return false;
481 if (fread(&size1,
sizeof(size1), 1, fp) != 1)
return false;
482 if (fread(&size2,
sizeof(size2), 1, fp) != 1)
return false;
492 if (fp->
FRead(&size1,
sizeof(size1), 1) != 1)
return false;
493 if (fp->
FRead(&size2,
sizeof(size2), 1) != 1)
return false;
537 virtual int index(
int column,
int row)
const {
540 return column * this->dim2_ + row - column;
550 T* new_array =
new T[new_dim1 * new_dim2];
551 for (
int col = 0; col < new_dim1; ++col) {
552 for (
int j = 0; j < new_dim2; ++j) {
553 int new_index = col * new_dim2 + j;
555 new_array[new_index] = this->
get(col, col + j);
556 }
else if (col >= this->
dim1_ && j < array2->
dim2_) {
557 new_array[new_index] = array2->
get(col - this->
dim1_,
558 col - this->
dim1_ + j);
561 new_array[new_index] = this->
empty_;
567 this->
dim1_ = new_dim1;
568 this->
dim2_ = new_dim2;
578 bool Classified(
int col,
int row,
int wildcard_id)
const;
635 #endif // TESSERACT_CCSTRUCT_MATRIX_H__ T * operator[](int column)
void Clip(const T &rangemin, const T &rangemax)
bool SerializeSize(tesseract::TFile *fp) const
void Resize(int size1, int size2, const T &empty)
void IncreaseBandSize(int bandwidth)
T & operator()(int column, int row)
MATRIX_COORD(int c, int r)
virtual int num_elements() const
bool DeSerializeClasses(bool swap, FILE *fp)
int FWrite(const void *buffer, int size, int count)
void ResizeNoInit(int size1, int size2)
void operator+=(const GENERIC_2D_ARRAY< T > &addend)
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty, T *array)
void RotatingTranspose(const int *dims, int num_dims, int src_dim, int dest_dim, GENERIC_2D_ARRAY< T > *result) const
static void Delete(void *arg)
const T & operator()(int column, int row) const
void MapForSplit(int ind)
bool DeSerializeSize(bool swap, FILE *fp)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
GENERIC_2D_ARRAY(const GENERIC_2D_ARRAY< T > &src)
bool DeSerialize(bool swap, tesseract::TFile *fp)
bool Classified(int col, int row, int wildcard_id) const
void AssertFinite() const
const T * operator[](int column) const
bool Valid(const MATRIX &m) const
bool WithinBounds(const T &rangemin, const T &rangemax) const
void operator=(const GENERIC_2D_ARRAY< T > &src)
virtual int index(int column, int row) const
bool Serialize(tesseract::TFile *fp) const
MATRIX * ConsumeAndMakeBigger(int ind)
MATRIX(int dimension, int bandwidth)
bool SerializeClasses(FILE *fp) const
void put(ICOORD pos, const T &thing)
int FRead(void *buffer, int size, int count)
inT16 x() const
access function
bool DeSerialize(bool swap, FILE *fp)
MATRIX * DeepCopy() const
void SumSquares(const GENERIC_2D_ARRAY< T > &src)
bool DeSerializeSize(bool swap, tesseract::TFile *fp)
virtual ~GENERIC_2D_ARRAY()
bool SerializeSize(FILE *fp) const
void delete_matrix_pointers()
void operator*=(const T &factor)
void ResizeWithCopy(int size1, int size2)
void AttachOnCorner(BandTriMatrix< T > *array2)
tesseract::KDPairInc< float, MATRIX_COORD > MatrixCoordPair
virtual int index(int column, int row) const
void put(int column, int row, const T &thing)
void AdaGradScaling(const GENERIC_2D_ARRAY< T > &sqsum, int num_samples)
void print(const UNICHARSET &unicharset) const
void ReverseN(void *ptr, int num_bytes)
BandTriMatrix(int dim1, int dim2, const T &empty)
bool Serialize(FILE *fp) const
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty)
void operator+=(const T &addend)
inT16 y() const
access_function
void operator-=(const GENERIC_2D_ARRAY< T > &minuend)