call from Beam Search to return the alt list corresponding to recognizing the bitmap between two segmentation pts
| 238 | // call from Beam Search to return the alt list corresponding to |
| 239 | // recognizing the bitmap between two segmentation pts |
| 240 | CharAltList * CubeSearchObject::RecognizeSegment(int start_pt, int end_pt) { |
| 241 | // init if necessary |
| 242 | if (!init_ && !Init()) { |
| 243 | fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): could " |
| 244 | "not initialize CubeSearchObject\n"); |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | // validate segment range |
| 249 | if (!IsValidSegmentRange(start_pt, end_pt)) { |
| 250 | fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): invalid " |
| 251 | "segment range (%d, %d)\n", start_pt, end_pt); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | // look for the recognition results in cache in the cache |
| 256 | if (reco_cache_ && reco_cache_[start_pt + 1] && |
| 257 | reco_cache_[start_pt + 1][end_pt]) { |
| 258 | return reco_cache_[start_pt + 1][end_pt]; |
| 259 | } |
| 260 | |
| 261 | // create the char sample corresponding to the blob |
| 262 | CharSamp *samp = CharSample(start_pt, end_pt); |
| 263 | if (!samp) { |
| 264 | fprintf(stderr, "Cube ERROR (CubeSearchObject::RecognizeSegment): could " |
| 265 | "not construct CharSamp\n"); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | // recognize the char sample |
| 270 | CharClassifier *char_classifier = cntxt_->Classifier(); |
| 271 | if (char_classifier) { |
| 272 | reco_cache_[start_pt + 1][end_pt] = char_classifier->Classify(samp); |
| 273 | } else { |
| 274 | // no classifer: all characters are equally probable; add a penalty |
| 275 | // that favors 2-segment characters and aspect ratios (w/h) > 1 |
| 276 | fprintf(stderr, "Cube WARNING (CubeSearchObject::RecognizeSegment): cube " |
| 277 | "context has no character classifier!! Inventing a probability " |
| 278 | "distribution.\n"); |
| 279 | int class_cnt = cntxt_->CharacterSet()->ClassCount(); |
| 280 | CharAltList *alt_list = new CharAltList(cntxt_->CharacterSet(), class_cnt); |
| 281 | int seg_cnt = end_pt - start_pt; |
| 282 | double prob_val = (1.0 / class_cnt) * |
| 283 | exp(-fabs(seg_cnt - 2.0)) * |
| 284 | exp(-samp->Width() / static_cast<double>(samp->Height())); |
| 285 | |
| 286 | for (int class_idx = 0; class_idx < class_cnt; class_idx++) { |
| 287 | alt_list->Insert(class_idx, CubeUtils::Prob2Cost(prob_val)); |
| 288 | } |
| 289 | reco_cache_[start_pt + 1][end_pt] = alt_list; |
| 290 | } |
| 291 | |
| 292 | return reco_cache_[start_pt + 1][end_pt]; |
| 293 | } |
| 294 | |
| 295 | // Perform segmentation of the bitmap by detecting connected components, |
| 296 | // segmenting each connected component using windowed vertical pixel density |
no test coverage detected