📄 bestfirst.cpp
字号:
bin_to_pieces (the_search->this_state, the_search->num_joints, widths); LogNewSegmentation(widths); rating_limit = class_probability (the_search->best_choice); char_choices = evaluate_chunks (chunks_record, chunk_groups, the_search->this_state, best_state, pass); if (char_choices != NULL) { permute_characters (char_choices, rating_limit, the_search->best_choice, the_search->raw_choice); if (AcceptableChoice (char_choices, the_search->best_choice, the_search->raw_choice, fixpt)) keep_going = FALSE; array_free(char_choices); }#ifndef GRAPHICS_DISABLED if (display_segmentations) { display_segmentation (chunks_record->chunks, chunk_groups); if (display_segmentations > 1) window_wait(segm_window); }#endif if (rating_limit != class_probability (the_search->best_choice)) { the_search->before_best = the_search->num_states; the_search->best_state->part1 = the_search->this_state->part1; the_search->best_state->part2 = the_search->this_state->part2; replace_char_widths(chunks_record, chunk_groups); } else if (char_choices != NULL) fixpt->index = -1; memfree(chunk_groups); return (keep_going);}/********************************************************************** * rebuild_current_state * * Evaluate the segmentation that is represented by this state in the * best first search. Add this state to the "states_seen" list. **********************************************************************/CHOICES_LIST rebuild_current_state(TBLOB *blobs, SEAMS seam_list, STATE *state, CHOICES_LIST old_choices, int fx) { CHOICES_LIST char_choices; SEARCH_STATE search_state; int i; int num_joints = array_count (seam_list); int x = 0; int blobindex; /*current blob */ TBLOB *p_blob; TBLOB *blob; TBLOB *next_blob; int y; search_state = bin_to_chunks (state, num_joints); char_choices = new_choice_list (); /* Iterate sub-paths */ for (i = 1; i <= search_state[0]; i++) { y = x + search_state[i]; x = y + 1; char_choices = array_push (char_choices, NULL); } char_choices = array_push (char_choices, NULL); y = count_blobs (blobs) - 1; for (i = search_state[0]; i >= 0; i--) { if (x == y) { /*single fragment */ array_value (char_choices, i) = array_value (old_choices, x); /*grab the list */ array_value (old_choices, x) = NULL; } else { join_pieces(blobs, seam_list, x, y); for (blob = blobs, blobindex = 0, p_blob = NULL; blobindex < x; blobindex++) { p_blob = blob; blob = blob->next; } while (blobindex < y) { next_blob = blob->next; blob->next = next_blob->next; oldblob(next_blob); /*junk dead blobs */ blobindex++; } array_value (char_choices, i) = (char *) classify_blob (p_blob, blob, blob->next, NULL, fx, "rebuild", Orange, NULL, NULL, 0, 0); } y = x - 1; x = y - search_state[i]; } memfree(search_state); free_all_choices(old_choices, x); return (char_choices);}/********************************************************************** * expand_node * * Create the states that are attached to this one. Check to see that * each one has not already been visited. If not add it to the priority * queue. **********************************************************************/void expand_node(CHUNKS_RECORD *chunks_record, SEARCH_RECORD *the_search) { STATE old_state; int x; int mask = 1 << (the_search->num_joints - 1 - 32); old_state.part1 = the_search->this_state->part1; old_state.part2 = the_search->this_state->part2; for (x = the_search->num_joints; x > 32; x--) { the_search->this_state->part1 = mask ^ old_state.part1; if (!hash_lookup (the_search->closed_states, the_search->this_state)) push_queue (the_search->open_states, the_search->this_state, prioritize_state (chunks_record, the_search, &old_state)); mask >>= 1; } if (the_search->num_joints > 32) { mask = 1 << 31; } else { mask = 1 << (the_search->num_joints - 1); } while (x--) { the_search->this_state->part2 = mask ^ old_state.part2; if (!hash_lookup (the_search->closed_states, the_search->this_state)) push_queue (the_search->open_states, the_search->this_state, prioritize_state (chunks_record, the_search, &old_state)); mask >>= 1; }}/********************************************************************** * new_search * * Create and initialize a new search record. **********************************************************************/SEARCH_RECORD *new_search(CHUNKS_RECORD *chunks_record, int num_joints, A_CHOICE *best_choice, A_CHOICE *raw_choice, STATE *state) { SEARCH_RECORD *this_search; this_search = (SEARCH_RECORD *) memalloc (sizeof (SEARCH_RECORD)); this_search->open_states = MakeHeap (num_seg_states * 20); this_search->closed_states = new_hash_table (); if (state) this_search->this_state = new_state (state); else cprintf ("error: bad initial state in new_search\n"); this_search->first_state = new_state (this_search->this_state); this_search->best_state = new_state (this_search->this_state); this_search->best_choice = best_choice; this_search->raw_choice = raw_choice; this_search->num_joints = num_joints; this_search->num_states = 0; this_search->before_best = 0; return (this_search);}/********************************************************************** * pop_queue * * Get this state from the priority queue. It should be the state that * has the greatest urgency to be evaluated. **********************************************************************/STATE *pop_queue(HEAP *queue) { HEAPENTRY entry; if (GetTopOfHeap (queue, &entry) == OK) {#ifndef GRAPHICS_DISABLED if (display_segmentations) { cprintf ("eval state: %8.3f ", entry.Key); print_state ("", (STATE *) entry.Data, num_joints); }#endif return ((STATE *) entry.Data); } else { return (NULL); }}/********************************************************************** * push_queue * * Add this state into the priority queue. **********************************************************************/void push_queue(HEAP *queue, STATE *state, FLOAT32 priority) { HEAPENTRY entry; if (SizeOfHeap (queue) < MaxSizeOfHeap (queue) && priority < worst_state) { entry.Data = (char *) new_state (state); num_pushed++; entry.Key = priority; HeapStore(queue, &entry); }}/********************************************************************** * replace_char_widths * * Replace the value of the char_width field in the chunks_record with * the updated width measurements from the last_segmentation. **********************************************************************/void replace_char_widths(CHUNKS_RECORD *chunks_record, SEARCH_STATE state) { WIDTH_RECORD *width_record; int num_blobs; int i; free_widths (chunks_record->char_widths); num_blobs = state[0] + 1; width_record = (WIDTH_RECORD *) memalloc (sizeof (int) * num_blobs * 2); width_record->num_chars = num_blobs; for (i = 0; i < num_blobs; i++) { width_record->widths[2 * i] = last_segmentation[i].width; if (i + 1 < num_blobs) width_record->widths[2 * i + 1] = last_segmentation[i].gap; } chunks_record->char_widths = width_record;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -