///<summary>Finds the best matching labels for a given piece of heuristics.</summary> public IEnumerable<RecognizedSegment> PerformLookup(HeuristicSet heuristics, IProgressReporter progress = null) { if (heuristics.Label != null) throw new ArgumentException("PerformLookup expects an unidentified segment", "heuristics"); var results = GetMatchesIterator(heuristics, progress).OrderBy(i => i.Certainty); return results; }
private HeuristicSet ExtractHeursitics(int midpoint) { HeuristicSet heuristics = new HeuristicSet(); int idx = midpoint - 6; int width = Boards.First().Matrix[0].Length; heuristics.GoThroughBoards(Boards, new Rectangle(idx, 0, Segmentation.Segmentator.WidthOfCanvas, width)); return heuristics; }
public static IEnumerable<HeuristicSet> Segment(this IterateBoards boards) { int width = boards.Boards.First().Matrix.Length; int height = boards.Boards.First().Matrix[0].Length; for (int idx = 0; idx < width - WidthOfCanvas; idx++) { var heuristics = new HeuristicSet { Bounds = new Rectangle(idx, 0, Segmentator.WidthOfCanvas, Segmentator.HeightOfCanvas) }; heuristics.GoThroughBoards(boards.Boards, heuristics.Bounds); yield return heuristics; } }
public HeuristicSet GetLetterHeuristics(BoundedCharacter ch) { var rect = ch.Bounds.ToGdi(); int midpoint = rect.X + (int)Math.Round(rect.Width / 2d); Rectangle smallerRect = new Rectangle(midpoint - Segmentator.PointerOffset, 0, Segmentator.WidthOfCanvas, Segmentator.HeightOfCanvas); HeuristicSet heursitics = new HeuristicSet { Bounds = rect, Label = ch.Character.ToString() }; heursitics.GoThroughBoards(Boards, smallerRect); Bitmap b = Boards.First().Matrix.ExtractRectangularContentArea(smallerRect).ConvertDoubleArrayToBitmap(Color.White); b.Log(ch.Character.ToString(), smallerRect); return heursitics; }
public HeuristicSet GetSpaceHeuristics(BoundedCharacter ch1, BoundedCharacter ch2) { var rect1 = ch1.Bounds.ToGdi(); var rect2 = ch2.Bounds.ToGdi(); int midpoint = rect1.Right + (rect2.X - rect1.Right) / 2; Rectangle smallerRect = new Rectangle(midpoint - Segmentator.PointerOffset, 0, Segmentator.WidthOfCanvas, Segmentator.HeightOfCanvas); HeuristicSet heursitics = new HeuristicSet { Bounds = smallerRect, Label = "whitespace" }; heursitics.GoThroughBoards(Boards, smallerRect); Bitmap b = Boards.First().Matrix.ExtractRectangularContentArea(smallerRect).ConvertDoubleArrayToBitmap(Color.White); b.Log("whitespace", smallerRect); return heursitics; }
public IEnumerable<RecognizedSegment> PerformWhitespaceLookup(HeuristicSet unlabledHeuristic, IProgressReporter progress = null) { progress = progress ?? new EmptyProgressReporter(); int heuristicCount = unlabledHeuristic.Heuristics.Count; Dictionary<ReferenceLabel, double[]> probabilityFromEachHeuristic = new Dictionary<ReferenceLabel, double[]>(); Dictionary<ReferenceLabel, double[]> lblComparisonResults = new Dictionary<ReferenceLabel, double[]>(); Dictionary<ReferenceLabel, double> labelProbability = new Dictionary<ReferenceLabel, double>(); ReferenceLabel whitespace = null, allLabels = null; for (int i = 0; i < Library.Count; i++) { if (Library[i].Label == "whitespace") whitespace = Library[i]; else if (Library[i].Label == "AllLabels") allLabels = Library[i]; if (whitespace != null && allLabels != null) break; } var sampleCount = whitespace.Samples.Count + allLabels.Samples.Count; progress.Maximum = heuristicCount * sampleCount + 2 * heuristicCount; probabilityFromEachHeuristic[whitespace] = new double[heuristicCount]; probabilityFromEachHeuristic[allLabels] = new double[heuristicCount]; lblComparisonResults[whitespace] = new double[heuristicCount]; lblComparisonResults[allLabels] = new double[heuristicCount]; for (int heurIdx = 0; heurIdx < heuristicCount; heurIdx++) { foreach (var item in whitespace.Samples) { progress.Progress++; if (unlabledHeuristic.GetAtIndex(heurIdx) == item.Heuristics[heurIdx]) { lblComparisonResults[whitespace][heurIdx]++; } } foreach (var item in allLabels.Samples) { progress.Progress++; if (unlabledHeuristic.GetAtIndex(heurIdx) == item.Heuristics[heurIdx]) { lblComparisonResults[allLabels][heurIdx]++; } } lblComparisonResults[whitespace][heurIdx] = lblComparisonResults[whitespace][heurIdx] / (double)whitespace.Samples.Count; lblComparisonResults[allLabels][heurIdx] = lblComparisonResults[allLabels][heurIdx] / (double)allLabels.Samples.Count; } Debug.Assert(progress.Progress == heuristicCount * sampleCount); double heuristicProbabilisticIndication; double multiplicativeOffset; double aprioriProb = 1.0 / (double)2; double factorIncrease = (1.0 - aprioriProb) / aprioriProb; foreach (var label in new[] { whitespace, allLabels }) { labelProbability[label] = 1.0 / (double)2; for (int heurIdx = 0; heurIdx < heuristicCount; heurIdx++) { progress.Progress++; double comparisonToThisLabel = lblComparisonResults[label][heurIdx]; double comparisonToOtherLabels = lblComparisonResults.Sum(h => h.Value[heurIdx]) - comparisonToThisLabel; if (comparisonToThisLabel + comparisonToOtherLabels != 0) { heuristicProbabilisticIndication = comparisonToThisLabel / (comparisonToThisLabel + comparisonToOtherLabels); heuristicsControl.buildHeuristicProbabilityHistorgram(heuristicProbabilisticIndication, Library.IndexOf(label), heurIdx); multiplicativeOffset = label.Variances.Append(heuristicProbabilisticIndication); multiplicativeOffset += aprioriProb / (double)label.Variances.Count; if (multiplicativeOffset < double.MaxValue) labelProbability[label] *= (factorIncrease * (heuristicProbabilisticIndication + multiplicativeOffset)) / (1 - heuristicProbabilisticIndication + multiplicativeOffset); if (double.IsInfinity(labelProbability[label]) || labelProbability[label] == 0) { progress.Progress += heuristicCount - heurIdx - 1; break; } } } if (label.Samples.Count > 0) yield return new RecognizedSegment(unlabledHeuristic.Bounds, label.Label, labelProbability[label]); } Debug.Assert(progress.Progress == progress.Maximum); }
private IEnumerable<RecognizedSegment> GetMatchesIterator(HeuristicSet unlabledHeuristic, IProgressReporter progress) { int whitespaceIdx = int.MinValue, allLabelsIdx = int.MinValue; for (int i = 0; i < Library.Count; i++) { if (Library[i].Label == "whitespace") { whitespaceIdx = i; } if (Library[i].Label == "AllLabels") { allLabelsIdx = i; } if (whitespaceIdx != int.MinValue && allLabelsIdx != int.MinValue) { i = Library.Count; } } HashSet<int> dontCheck = new HashSet<int>() { whitespaceIdx, allLabelsIdx }; progress = progress ?? new EmptyProgressReporter(); int heuristicCount = unlabledHeuristic.Heuristics.Count; double[][] probabilityFromEachHeuristic = new double[Library.Count][]; double[][] lblComparisonResults = new double[Library.Count][]; double[] labelProbability; var totalSampleCount = Library.Where((_, i) => !dontCheck.Contains(i)).Sum(rl => rl.Samples.Count); progress.Maximum = heuristicCount * totalSampleCount + heuristicCount * (Library.Count - 2); //double[] totalComparison_test = new double[numberOfLabelsToCount]; for (int i = 0; i < Library.Count; i++) { probabilityFromEachHeuristic[i] = new double[heuristicCount]; lblComparisonResults[i] = new double[heuristicCount]; } for (int heurIdx = 0; heurIdx < heuristicCount; heurIdx++) { for (int lblIdx = 0; lblIdx < Library.Count; lblIdx++) { while (dontCheck.Contains(lblIdx)) lblIdx++; if (lblIdx == Library.Count) break; var current = Library[lblIdx]; foreach (var item in current.Samples) { progress.Progress++; if (unlabledHeuristic.GetAtIndex(heurIdx) == item.Heuristics[heurIdx]) lblComparisonResults[lblIdx][heurIdx]++; } } for (int labelIndex = 0; labelIndex < Library.Count; labelIndex++) { while (dontCheck.Contains(labelIndex)) { labelIndex++; } if (labelIndex != Library.Count) { lblComparisonResults[labelIndex][heurIdx] = lblComparisonResults[labelIndex][heurIdx] / (double)Library[labelIndex].Samples.Count; } } } Debug.Assert(progress.Progress == heuristicCount * totalSampleCount); //We are working to produce two DSs: lblComparisonResults[][], totalComparison_test[] double heuristicProbabilisticIndication; double multiplicativeOffset; labelProbability = new double[Library.Count]; double aprioriProb = 1.0 / ((double)Library.Count - dontCheck.Count()); double factorIncrease = (1.0 - aprioriProb) / aprioriProb; //factorIncrease+=10; for (int inspectionLbl = 0; inspectionLbl < Library.Count; inspectionLbl++) { while (dontCheck.Contains(inspectionLbl)) inspectionLbl++; if (inspectionLbl == Library.Count) break; labelProbability[inspectionLbl] = 1.0 / ((double)Library.Count - dontCheck.Count()); for (int heurIdx = 0; heurIdx < heuristicCount; heurIdx++) { progress.Progress++; double comparisonToThisLabel = lblComparisonResults[inspectionLbl][heurIdx]; double comparisonToOtherLabels = lblComparisonResults.Sum(h => h[heurIdx]) - comparisonToThisLabel; if (comparisonToThisLabel + comparisonToOtherLabels != 0) { heuristicProbabilisticIndication = comparisonToThisLabel / (comparisonToThisLabel + comparisonToOtherLabels); heuristicsControl.buildHeuristicProbabilityHistorgram(heuristicProbabilisticIndication, inspectionLbl, heurIdx); multiplicativeOffset = Library[inspectionLbl].Variances.Append(heuristicProbabilisticIndication); multiplicativeOffset += aprioriProb / (double)Library[inspectionLbl].Variances.Count; if (multiplicativeOffset < double.MaxValue) labelProbability[inspectionLbl] *= (factorIncrease * heuristicProbabilisticIndication + multiplicativeOffset) / (1 - heuristicProbabilisticIndication + multiplicativeOffset); if (double.IsInfinity(labelProbability[inspectionLbl]) || labelProbability[inspectionLbl] == 0) { progress.Progress += heuristicCount - heurIdx - 1; break; } } } if (Library[inspectionLbl].Samples.Count > 0) { yield return new RecognizedSegment(unlabledHeuristic.Bounds, Library[inspectionLbl].Label, labelProbability[inspectionLbl]); } } Debug.Assert(progress.Progress == progress.Maximum); }