public string Apply(int tnum, Bitmap srcimg, double size_ratio, double ang_threshold, bool preprocessing, string outImagePath) { this.tnum = tnum; this.width = srcimg.Width; this.height = srcimg.Height; int iteration = 2; this.size_ratio = size_ratio; this.ang_threshold = ang_threshold; //input image: white as forground srcimg = ImageUtils.ConvertGrayScaleToBinary(srcimg, 128); if (preprocessing) { ConnectLinesWithOnePixelGap cg = new ConnectLinesWithOnePixelGap(); srcimg = cg.Apply(srcimg); } //Log.WriteBitmap2Debug(srcimg, "start"); srcimg = ImageUtils.InvertColors(srcimg); minimum_distance_between_CCs_in_string = iteration; MyConnectedComponentsAnalysisFast.MyBlobCounter char_bc = new MyConnectedComponentsAnalysisFast.MyBlobCounter(); this.char_blobs = char_bc.GetBlobs(srcimg); this.char_labels = new short[width * height]; // Log.WriteBitmap2Debug(Printnumb(), "num"); for (int i = 0; i < width * height; i++) { char_labels[i] = (short)(char_bc.objectLabels[i]); } for (int i = 0; i < char_blobs.Count; i++) { char_blob_idx_max_size_table.Add(i, Math.Max(char_blobs[i].bbx.Width, char_blobs[i].bbx.Height)); // original size of the character //narges expendable_char_blob_idx_set.Add(i); connected_char_blob_idx_set_list.Add(new HashSet <int>()); } original_char_labels = char_bc.objectLabels; for (int i = 0; i < width * height; i++) { char_labels[i] = (short)(char_bc.objectLabels[i]); } // first run // Log.WriteBitmap2Debug(Print(), "k1_"); k1(); // Log.WriteBitmap2Debug(Print(), "k1_" + 0); iteration_counter = 1; // while (expendable_char_blob_idx_set.Count > 1 && iteration_counter < iteration_threshold) while (iteration_counter < iteration_threshold) { k1(); iteration_counter++; // Log.WriteBitmap2Debug(Print(), "k1_" + iteration_counter); } Print(outImagePath); return(outImagePath); }
public List <TextString> Apply(Bitmap srcimg, Bitmap dilatedimg) { width = srcimg.Width; height = srcimg.Height; // max_width = width / 2; I commented these two lines // max_height = height / 2; //ashish srcimg = ImageUtils.ConvertGrayScaleToBinary(srcimg, threshold: 128); srcimg = ImageUtils.InvertColors(srcimg); dilatedimg = ImageUtils.ConvertGrayScaleToBinary(dilatedimg, threshold: 128); dilatedimg = ImageUtils.InvertColors(dilatedimg); MyConnectedComponentsAnalysisFast.MyBlobCounter char_bc = new MyConnectedComponentsAnalysisFast.MyBlobCounter(); List <MyConnectedComponentsAnalysisFast.MyBlob> char_blobs = char_bc.GetBlobs(srcimg); ushort[] char_labels = char_bc.objectLabels; MyConnectedComponentsAnalysisFast.MyBlobCounter string_bc = new MyConnectedComponentsAnalysisFast.MyBlobCounter(); List <MyConnectedComponentsAnalysisFast.MyBlob> string_blobs = string_bc.GetBlobs(dilatedimg); ushort[] string_labels = string_bc.objectLabels; List <TextString> initial_string_list = new List <TextString>(); int string_count = string_blobs.Count; for (int i = 0; i < string_count; i++) { initial_string_list.Add(new TextString()); initial_string_list.Last().mass_center = string_blobs[i].mass_center; } for (int i = 0; i < char_blobs.Count; i++) { if (char_blobs[i].bbx.Width > 1 && char_blobs[i].bbx.Height > 1) { char_blobs[i].string_id = string_labels[char_blobs[i].sample_y * width + char_blobs[i].sample_x] - 1; initial_string_list[char_blobs[i].string_id].AddChar(char_blobs[i]); } } for (int i = 0; i < initial_string_list.Count; i++) { if ((initial_string_list[i].char_list.Count == 0) || (initial_string_list[i].bbx.Width < min_width || initial_string_list[i].bbx.Height < min_height) || (initial_string_list[i].bbx.Width > max_width || initial_string_list[i].bbx.Height > max_height)) { initial_string_list.RemoveAt(i); i--; } } for (int i = 0; i < initial_string_list.Count; i++) { PrintSubStringsSmall(char_labels, initial_string_list[i], 0); } return(initial_string_list); }
public void NeighborhoodTextDetection(Bitmap Neighborhood) { MyConnectedComponentsAnalysisFast.MyBlobCounter ConnectedComponent = new MyConnectedComponentsAnalysisFast.MyBlobCounter(); ConnectedComponent.GetBlobs(Neighborhood); }
public void IdentifyExpendableCharBlobs() { short_char_blob_idx_set.Clear(); Bitmap resultimg = Print(); resultimg = ImageUtils.InvertColors(resultimg); MyConnectedComponentsAnalysisFast.MyBlobCounter string_bc = new MyConnectedComponentsAnalysisFast.MyBlobCounter(); List <MyConnectedComponentsAnalysisFast.MyBlob> string_blobs = string_bc.GetBlobs(resultimg); ushort[] string_labels = string_bc.objectLabels; int string_count = string_blobs.Count; List <List <MyConnectedComponentsAnalysisFast.MyBlob> > string_list = new List <List <MyConnectedComponentsAnalysisFast.MyBlob> >(); for (int i = 0; i < string_count; i++) { string_list.Add(new List <MyConnectedComponentsAnalysisFast.MyBlob>()); } for (int i = 0; i < char_blobs.Count; i++) { char_blobs[i].string_id = string_labels[char_blobs[i].sample_y * width + char_blobs[i].sample_x] - 1; string_list[char_blobs[i].string_id].Add(char_blobs[i]); } for (int i = 0; i < string_list.Count; i++) { if (string_list[i].Count > 0) { int avg_size = 0; for (int j = 0; j < string_list[i].Count; j++) { avg_size += Math.Max(string_list[i][j].bbx.Width, string_list[i][j].bbx.Height); } avg_size /= string_list[i].Count; for (int j = 0; j < string_list[i].Count; j++) { char_blob_idx_max_size_table[string_list[i][j].pixel_id - 1] = avg_size; } } } // recomputing expendable char blob idx for (int i = 0; i < char_blobs.Count; i++) { if (connected_char_blob_idx_set_list[i].Count == 2) { expendable_char_blob_idx_set.Remove(i); } else // 0 or 1 connecting CC { int max_dist = (int)((double)((int)char_blob_idx_max_size_table[i]) / 3); // int max_dist = (int)((double)((int)char_blob_idx_max_size_table[i])/2 ); if (max_dist < minimum_distance_between_CCs_in_string * 2) { max_dist = minimum_distance_between_CCs_in_string * 2; } if (iteration_counter > max_dist) { expendable_char_blob_idx_set.Remove(i); } } } }