2007-03-08 04:03:40 +08:00
|
|
|
/* -*-C-*-
|
|
|
|
********************************************************************************
|
|
|
|
*
|
|
|
|
* File: pieces.c (Formerly pieces.c)
|
|
|
|
* Description:
|
|
|
|
* Author: Mark Seaman, OCR Technology
|
|
|
|
* Created: Fri Oct 16 14:37:00 1987
|
|
|
|
* Modified: Mon May 20 12:12:35 1991 (Mark Seaman) marks@hpgrlt
|
|
|
|
* Language: C
|
|
|
|
* Package: N/A
|
|
|
|
* Status: Reusable Software Component
|
|
|
|
*
|
|
|
|
* (c) Copyright 1987, Hewlett-Packard Company.
|
|
|
|
** 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.
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
I n c l u d e s
|
|
|
|
----------------------------------------------------------------------*/
|
|
|
|
#include "pieces.h"
|
2009-07-11 10:46:01 +08:00
|
|
|
|
2007-03-08 04:03:40 +08:00
|
|
|
#include "blobs.h"
|
2009-07-11 10:46:01 +08:00
|
|
|
#include "freelist.h"
|
|
|
|
#include "hideedge.h"
|
2007-03-08 04:03:40 +08:00
|
|
|
#include "matchtab.h"
|
2009-07-11 10:46:01 +08:00
|
|
|
#include "ndminx.h"
|
|
|
|
#include "plotseg.h"
|
|
|
|
#include "ratngs.h"
|
|
|
|
#include "wordclass.h"
|
|
|
|
#include "wordrec.h"
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
M a c r o s
|
|
|
|
----------------------------------------------------------------------*/
|
|
|
|
/**********************************************************************
|
|
|
|
* set_bounds_entry
|
|
|
|
*
|
|
|
|
* Set the value of the entry in an array of bounds elements.
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#define set_bounds_entry(array,index,top_left,bot_right) \
|
|
|
|
((array)[index].topleft = (top_left), \
|
|
|
|
(array)[index].botright = (bot_right)) \
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* get_bounds_entry
|
|
|
|
*
|
|
|
|
* Get the value of the entry in an array of bounds elements.
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#define get_bounds_entry(array,index,top_left,bot_right) \
|
|
|
|
((top_left) = (array)[index].topleft, \
|
|
|
|
(bot_right) = (array)[index].botright) \
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
F u n c t i o n s
|
|
|
|
----------------------------------------------------------------------*/
|
|
|
|
/**********************************************************************
|
|
|
|
* break_pieces
|
|
|
|
*
|
|
|
|
* Break up the blobs in this chain so that they are all independent.
|
|
|
|
* This operation should undo the affect of join_pieces.
|
|
|
|
**********************************************************************/
|
2008-04-22 08:35:16 +08:00
|
|
|
void break_pieces(TBLOB *blobs, SEAMS seams, inT16 start, inT16 end) {
|
2007-03-08 04:03:40 +08:00
|
|
|
TESSLINE *outline = blobs->outlines;
|
|
|
|
TBLOB *next_blob;
|
2008-04-22 08:35:16 +08:00
|
|
|
inT16 x;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
for (x = start; x < end; x++)
|
|
|
|
reveal_seam ((SEAM *) array_value (seams, x));
|
|
|
|
|
|
|
|
next_blob = blobs->next;
|
|
|
|
|
|
|
|
while (outline && next_blob) {
|
|
|
|
if (outline->next == next_blob->outlines) {
|
|
|
|
outline->next = NULL;
|
|
|
|
outline = next_blob->outlines;
|
|
|
|
next_blob = next_blob->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
outline = outline->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* join_pieces
|
|
|
|
*
|
|
|
|
* Join a group of base level pieces into a single blob that can then
|
|
|
|
* be classified.
|
|
|
|
**********************************************************************/
|
2008-04-22 08:35:16 +08:00
|
|
|
void join_pieces(TBLOB *piece_blobs, SEAMS seams, inT16 start, inT16 end) {
|
2007-03-08 04:03:40 +08:00
|
|
|
TBLOB *next_blob;
|
|
|
|
TBLOB *blob;
|
2008-04-22 08:35:16 +08:00
|
|
|
inT16 x;
|
2007-03-08 04:03:40 +08:00
|
|
|
TESSLINE *outline;
|
|
|
|
SEAM *seam;
|
|
|
|
|
|
|
|
for (x = 0, blob = piece_blobs; x < start; x++)
|
|
|
|
blob = blob->next;
|
|
|
|
next_blob = blob->next;
|
|
|
|
outline = blob->outlines;
|
|
|
|
if (!outline)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (x < end) {
|
|
|
|
seam = (SEAM *) array_value (seams, x);
|
|
|
|
if (x - seam->widthn >= start && x + seam->widthp < end)
|
2008-04-22 08:35:16 +08:00
|
|
|
hide_seam(seam);
|
2007-03-08 04:03:40 +08:00
|
|
|
while (outline->next)
|
|
|
|
outline = outline->next;
|
|
|
|
outline->next = next_blob->outlines;
|
|
|
|
next_blob = next_blob->next;
|
|
|
|
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* hide_seam
|
|
|
|
*
|
|
|
|
* Change the edge points that are referenced by this seam to make
|
|
|
|
* them hidden edges.
|
|
|
|
**********************************************************************/
|
2008-04-22 08:35:16 +08:00
|
|
|
void hide_seam(SEAM *seam) {
|
2007-03-08 04:03:40 +08:00
|
|
|
if (seam == NULL || seam->split1 == NULL)
|
|
|
|
return;
|
|
|
|
hide_edge_pair (seam->split1->point1, seam->split1->point2);
|
|
|
|
|
|
|
|
if (seam->split2 == NULL)
|
|
|
|
return;
|
|
|
|
hide_edge_pair (seam->split2->point1, seam->split2->point2);
|
|
|
|
|
|
|
|
if (seam->split3 == NULL)
|
|
|
|
return;
|
|
|
|
hide_edge_pair (seam->split3->point1, seam->split3->point2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* hide_edge_pair
|
|
|
|
*
|
|
|
|
* Change the edge points that are referenced by this seam to make
|
|
|
|
* them hidden edges.
|
|
|
|
**********************************************************************/
|
2008-04-22 08:35:16 +08:00
|
|
|
void hide_edge_pair(EDGEPT *pt1, EDGEPT *pt2) {
|
2007-03-08 04:03:40 +08:00
|
|
|
EDGEPT *edgept;
|
|
|
|
|
|
|
|
edgept = pt1;
|
|
|
|
do {
|
2008-04-22 08:35:16 +08:00
|
|
|
hide_edge(edgept);
|
2007-03-08 04:03:40 +08:00
|
|
|
edgept = edgept->next;
|
|
|
|
}
|
|
|
|
while (!exact_point (edgept, pt2) && edgept != pt1);
|
|
|
|
if (edgept == pt1) {
|
|
|
|
/* cprintf("Hid entire outline at (%d,%d)!!\n",
|
|
|
|
edgept->pos.x,edgept->pos.y); */
|
|
|
|
}
|
|
|
|
edgept = pt2;
|
|
|
|
do {
|
2008-04-22 08:35:16 +08:00
|
|
|
hide_edge(edgept);
|
2007-03-08 04:03:40 +08:00
|
|
|
edgept = edgept->next;
|
|
|
|
}
|
|
|
|
while (!exact_point (edgept, pt1) && edgept != pt2);
|
|
|
|
if (edgept == pt2) {
|
|
|
|
/* cprintf("Hid entire outline at (%d,%d)!!\n",
|
|
|
|
edgept->pos.x,edgept->pos.y); */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* reveal_seam
|
|
|
|
*
|
|
|
|
* Change the edge points that are referenced by this seam to make
|
|
|
|
* them hidden edges.
|
|
|
|
**********************************************************************/
|
2008-04-22 08:35:16 +08:00
|
|
|
void reveal_seam(SEAM *seam) {
|
2007-03-08 04:03:40 +08:00
|
|
|
if (seam == NULL || seam->split1 == NULL)
|
|
|
|
return;
|
|
|
|
reveal_edge_pair (seam->split1->point1, seam->split1->point2);
|
|
|
|
|
|
|
|
if (seam->split2 == NULL)
|
|
|
|
return;
|
|
|
|
reveal_edge_pair (seam->split2->point1, seam->split2->point2);
|
|
|
|
|
|
|
|
if (seam->split3 == NULL)
|
|
|
|
return;
|
|
|
|
reveal_edge_pair (seam->split3->point1, seam->split3->point2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* reveal_edge_pair
|
|
|
|
*
|
|
|
|
* Change the edge points that are referenced by this seam to make
|
|
|
|
* them hidden edges.
|
|
|
|
**********************************************************************/
|
2008-04-22 08:35:16 +08:00
|
|
|
void reveal_edge_pair(EDGEPT *pt1, EDGEPT *pt2) {
|
2007-03-08 04:03:40 +08:00
|
|
|
EDGEPT *edgept;
|
|
|
|
|
|
|
|
edgept = pt1;
|
|
|
|
do {
|
2008-04-22 08:35:16 +08:00
|
|
|
reveal_edge(edgept);
|
2007-03-08 04:03:40 +08:00
|
|
|
edgept = edgept->next;
|
|
|
|
}
|
|
|
|
while (!exact_point (edgept, pt2) && edgept != pt1);
|
|
|
|
if (edgept == pt1) {
|
|
|
|
/* cprintf("Hid entire outline at (%d,%d)!!\n",
|
|
|
|
edgept->pos.x,edgept->pos.y); */
|
|
|
|
}
|
|
|
|
edgept = pt2;
|
|
|
|
do {
|
2008-04-22 08:35:16 +08:00
|
|
|
reveal_edge(edgept);
|
2007-03-08 04:03:40 +08:00
|
|
|
edgept = edgept->next;
|
|
|
|
}
|
|
|
|
while (!exact_point (edgept, pt1) && edgept != pt2);
|
|
|
|
if (edgept == pt2) {
|
|
|
|
/* cprintf("Hid entire outline at (%d,%d)!!\n",
|
|
|
|
edgept->pos.x,edgept->pos.y); */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* bounds_of_piece
|
|
|
|
*
|
|
|
|
* Find the bounds of the piece that will be created by joining the
|
|
|
|
* requested collection of pieces together.
|
|
|
|
**********************************************************************/
|
|
|
|
void bounds_of_piece(BOUNDS_LIST bounds,
|
2008-04-22 08:35:16 +08:00
|
|
|
inT16 start,
|
|
|
|
inT16 end,
|
2007-03-08 04:03:40 +08:00
|
|
|
TPOINT *extreme_tl,
|
|
|
|
TPOINT *extreme_br) {
|
|
|
|
TPOINT topleft;
|
|
|
|
TPOINT botright;
|
2008-04-22 08:35:16 +08:00
|
|
|
inT16 x;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2008-04-22 08:35:16 +08:00
|
|
|
get_bounds_entry(bounds, start, *extreme_tl, *extreme_br);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
for (x = start + 1; x <= end; x++) {
|
2008-04-22 08:35:16 +08:00
|
|
|
get_bounds_entry(bounds, x, topleft, botright);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2009-07-11 10:46:01 +08:00
|
|
|
extreme_tl->x = MIN (topleft.x, extreme_tl->x);
|
|
|
|
extreme_tl->y = MAX (topleft.y, extreme_tl->y);
|
|
|
|
extreme_br->x = MAX (botright.x, extreme_br->x);
|
|
|
|
extreme_br->y = MIN (botright.y, extreme_br->y);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* classify_piece
|
|
|
|
*
|
|
|
|
* Create a larger piece from a collection of smaller ones. Classify
|
|
|
|
* it and return the results. Take the large piece apart to leave
|
|
|
|
* the collection of small pieces un modified.
|
|
|
|
**********************************************************************/
|
2009-07-11 10:46:01 +08:00
|
|
|
namespace tesseract {
|
|
|
|
BLOB_CHOICE_LIST *Wordrec::classify_piece(TBLOB *pieces,
|
|
|
|
SEAMS seams,
|
|
|
|
inT16 start,
|
|
|
|
inT16 end) {
|
2007-03-08 04:03:40 +08:00
|
|
|
STATE current_state;
|
2009-07-11 10:46:01 +08:00
|
|
|
BLOB_CHOICE_LIST *choices;
|
2007-03-08 04:03:40 +08:00
|
|
|
TBLOB *pblob;
|
|
|
|
TBLOB *blob;
|
|
|
|
TBLOB *nblob;
|
2008-04-22 08:35:16 +08:00
|
|
|
inT16 x;
|
2007-03-08 04:03:40 +08:00
|
|
|
SEARCH_STATE chunk_groups;
|
|
|
|
|
|
|
|
set_n_ones (¤t_state, array_count (seams));
|
|
|
|
|
2008-04-22 08:35:16 +08:00
|
|
|
join_pieces(pieces, seams, start, end);
|
2007-03-08 04:03:40 +08:00
|
|
|
for (blob = pieces, pblob = NULL, x = 0; x < start; x++) {
|
|
|
|
pblob = blob;
|
|
|
|
blob = blob->next;
|
|
|
|
}
|
|
|
|
for (nblob = blob->next; x < end; x++)
|
|
|
|
nblob = nblob->next;
|
2009-07-11 10:46:01 +08:00
|
|
|
choices = classify_blob (pblob, blob, nblob, NULL, "pieces:", White);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2008-04-22 08:35:16 +08:00
|
|
|
break_pieces(blob, seams, start, end);
|
2007-03-08 04:03:40 +08:00
|
|
|
#ifndef GRAPHICS_DISABLED
|
2009-07-11 10:46:01 +08:00
|
|
|
if (wordrec_display_segmentations > 2) {
|
2007-03-08 04:03:40 +08:00
|
|
|
chunk_groups = bin_to_chunks (¤t_state, array_count (seams));
|
2008-04-22 08:35:16 +08:00
|
|
|
display_segmentation(pieces, chunk_groups);
|
|
|
|
window_wait(segm_window);
|
|
|
|
memfree(chunk_groups);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (choices);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* get_piece_rating
|
|
|
|
*
|
|
|
|
* Check to see if this piece has already been classified. If it has
|
|
|
|
* return that rating. Otherwise build the piece from the smaller
|
|
|
|
* pieces, classify it, store the rating for later, and take the piece
|
|
|
|
* apart again.
|
|
|
|
**********************************************************************/
|
2009-07-11 10:46:01 +08:00
|
|
|
BLOB_CHOICE_LIST *Wordrec::get_piece_rating(MATRIX *ratings,
|
|
|
|
TBLOB *blobs,
|
|
|
|
SEAMS seams,
|
|
|
|
inT16 start,
|
|
|
|
inT16 end) {
|
|
|
|
BLOB_CHOICE_LIST *choices = ratings->get(start, end);
|
2007-03-08 04:03:40 +08:00
|
|
|
if (choices == NOT_CLASSIFIED) {
|
2009-07-11 10:46:01 +08:00
|
|
|
choices = classify_piece(blobs,
|
|
|
|
seams,
|
|
|
|
start,
|
|
|
|
end);
|
|
|
|
ratings->put(start, end, choices);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
return (choices);
|
|
|
|
}
|
2009-07-11 10:46:01 +08:00
|
|
|
} // namespace tesseract
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* record_blob_bounds
|
|
|
|
*
|
|
|
|
* Set up and initialize an array that holds the bounds of a set of
|
|
|
|
* blobs.
|
|
|
|
**********************************************************************/
|
2008-04-22 08:35:16 +08:00
|
|
|
BOUNDS_LIST record_blob_bounds(TBLOB *blobs) {
|
2007-03-08 04:03:40 +08:00
|
|
|
TBLOB *blob;
|
|
|
|
BOUNDS_LIST bounds;
|
|
|
|
TPOINT topleft;
|
|
|
|
TPOINT botright;
|
2008-04-22 08:35:16 +08:00
|
|
|
inT16 x = 0;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
bounds = (BOUNDS_LIST) memalloc (count_blobs (blobs) * sizeof (BOUNDS));
|
|
|
|
|
2008-04-22 08:35:16 +08:00
|
|
|
iterate_blobs(blob, blobs) {
|
|
|
|
blob_bounding_box(blob, &topleft, &botright);
|
|
|
|
set_bounds_entry(bounds, x, topleft, botright);
|
2007-03-08 04:03:40 +08:00
|
|
|
x++;
|
|
|
|
}
|
|
|
|
return (bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* record_piece_ratings
|
|
|
|
*
|
|
|
|
* Save the choices for all the pieces that have been classified into
|
|
|
|
* a matrix that can be used to look them up later. A two dimensional
|
|
|
|
* matrix is created. The indices correspond to the starting and
|
|
|
|
* ending initial piece number.
|
|
|
|
**********************************************************************/
|
2009-07-11 10:46:01 +08:00
|
|
|
MATRIX *record_piece_ratings(TBLOB *blobs) {
|
2007-03-08 04:03:40 +08:00
|
|
|
BOUNDS_LIST bounds;
|
2008-04-22 08:35:16 +08:00
|
|
|
inT16 num_blobs;
|
|
|
|
inT16 x;
|
|
|
|
inT16 y;
|
2007-03-08 04:03:40 +08:00
|
|
|
TPOINT tp_topleft;
|
|
|
|
TPOINT tp_botright;
|
|
|
|
unsigned int topleft;
|
|
|
|
unsigned int botright;
|
2009-07-11 10:46:01 +08:00
|
|
|
MATRIX *ratings;
|
|
|
|
BLOB_CHOICE_LIST *choices;
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
bounds = record_blob_bounds (blobs);
|
|
|
|
num_blobs = count_blobs (blobs);
|
2009-07-11 10:46:01 +08:00
|
|
|
ratings = new MATRIX(num_blobs);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
for (x = 0; x < num_blobs; x++) {
|
|
|
|
for (y = x; y < num_blobs; y++) {
|
2008-04-22 08:35:16 +08:00
|
|
|
bounds_of_piece(bounds, x, y, &tp_topleft, &tp_botright);
|
2007-03-08 04:03:40 +08:00
|
|
|
topleft = *(unsigned int *) &tp_topleft;
|
|
|
|
botright = *(unsigned int *) &tp_botright;
|
|
|
|
choices = get_match_by_bounds (topleft, botright);
|
2009-07-11 10:46:01 +08:00
|
|
|
if (choices != NULL) {
|
|
|
|
ratings->put(x, y, choices);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-22 08:35:16 +08:00
|
|
|
memfree(bounds);
|
2007-03-08 04:03:40 +08:00
|
|
|
return (ratings);
|
|
|
|
}
|