/// <summary> /// Cast the specified glyph. Return true if glyph is not null. /// </summary> /// <param name="glyph">Glyph.</param> public bool Cast(Glyph glyph) { //Debug.Log("Cast Glyph: "+ glyph.ToString()); castedGlyph = glyph; if (castedGlyph == null) { return(false); } if (method == null) { Method = Method; } if (method != null && targetGlyphSet != null) { GlyphMatch match; int index = method.MultiMatch(castedGlyph, targetGlyphSet, out match); currentMatch = (index != -1 ? match : null); if (OnGlyphCast != null) { OnGlyphCast.Invoke(index, match); } } return(true); }
/// <summary> /// Try to match the specified glyphs. Returns null if the match fails. /// </summary> /// <param name="src">Source.</param> /// <param name="tgt">Target.</param> public override GlyphMatch Match(Glyph src, Glyph tgt) { // Debug.Log("GetDifference: " + src.name + " ~ " + tgt.name); int[] indexMatch = MatchStrokes(src, tgt); if (indexMatch == null) { return(null); //failed } GlyphMatch.StrokeMatch[] matches = new GlyphMatch.StrokeMatch[indexMatch.Length]; for (int i = 0; i < indexMatch.Length; i++) { int j = indexMatch[i]; matches[i] = matchMatrix[i, j]; } float costSum = 0, weight = 0; foreach (GlyphMatch.StrokeMatch sm in matches) { costSum += sm.cost; weight += sm.weight; } GlyphMatch result = new GlyphMatch(src, tgt, matches, Mathf.Sqrt(costSum / weight), threshold); src = null; tgt = null; //matches = null; matchMatrix = null; error = null; return(result); }
/// <summary> /// Try to match a glyph with the current targets and get the best match. /// </summary> /// <returns>The index of the best match, or -1 if there is no match.</returns> /// <param name="src">Source.</param> /// <param name="bestMatch">Best match info, or null if there is no match.</param> public virtual int MultiMatch(Glyph src, out GlyphMatch bestMatch) { bestMatch = null; if (targets == null || targetGlyphCoefficients == null || targetGlyphCoefficients.Length != targets.Length) { return(-1); } // Debug.Log("Src coeffs:"); Vector2[][] srcGlyphCoeffs = new Vector2[src.Length][]; for (int s = 0; s < src.Length; s++) { srcGlyphCoeffs[s] = legendreGenerator.Compute(src[s]); } float bestDiff = float.PositiveInfinity; int bestIndex = -1; for (int targetIndex = 0; targetIndex < targets.Length; targetIndex++) { if (targetGlyphCoefficients[targetIndex] == null) { continue; } int[] indexMatch = MatchStrokes(srcGlyphCoeffs, targetGlyphCoefficients[targetIndex]); GlyphMatch match = FinalizeMatch(src, targets[targetIndex], indexMatch); if (match != null && match.Cost < bestDiff) { bestDiff = match.Cost; bestIndex = targetIndex; bestMatch = match; } } return(bestIndex); }
/// <summary> /// Set targets, then try to match a glyph with them and get the best match. /// </summary> /// <returns>The index of the best match, or -1 if there is no match.</returns> /// <param name="src">Source.</param> /// <param name="targets">Targets.</param> /// <param name="bestMatch">Best match info, or null if there is no match.</param> public override int MultiMatch(Glyph src, Glyph[] targets, out GlyphMatch bestMatch) { if (this.targets != targets) { SetTargets(targets); } return(MultiMatch(src, out bestMatch)); }
public void GlyphCastResult(int index, AdVd.GlyphRecognition.GlyphMatch match) { if (match == null || match.Cost > match.Threshold) { result.text = "Match not found"; } else { result.text = match.target.name; } }
protected virtual GlyphMatch FinalizeMatch(Glyph src, Glyph tgt, int[] indexMatch) { if (indexMatch == null) { return(null); //failed } GlyphMatch.StrokeMatch[] matches = new GlyphMatch.StrokeMatch[indexMatch.Length]; for (int i = 0; i < indexMatch.Length; i++) { int j = indexMatch[i]; srcStroke = src[i]; tgtStroke = tgt[j]; matches[i] = GetStrokeMatch(error[i, j], directMatch[i, j]);// matchMatrix[i, j]; if (matches[i] == null) { return(null); } // if (matches[i]==null){ // Debug.Log ("Match "+i+"-"+j+" failed."); // return null; // } // else{ // float meanCost = (float)Math.Sqrt(matches[i].cost / matches[i].weight); // Debug.Log("MeanCost(" + i + "," + j + "): " + matches[i].cost + " / " + matches[i].weight + " (" + meanCost + ")"); // } } srcStroke = null; tgtStroke = null; float costSum = 0, weight = 0; foreach (GlyphMatch.StrokeMatch sm in matches) { costSum += sm.cost; weight += sm.weight; } GlyphMatch result = new GlyphMatch(src, tgt, matches, Mathf.Sqrt(costSum / weight), threshold); src = null; tgt = null; directMatch = null; error = null; return(result); }
/// <summary> /// Try to match a glyph with multiple targets and get the best match. /// </summary> /// <returns>The index of the best match, or -1 if there is no match.</returns> /// <param name="src">Source.</param> /// <param name="targets">Targets.</param> /// <param name="bestMatch">Best match info, or null if there is no match.</param> public virtual int MultiMatch(Glyph src, Glyph[] targets, out GlyphMatch bestMatch) { float bestDiff = float.PositiveInfinity; int bestIndex = -1; bestMatch = null; for (int targetIndex = 0; targetIndex < targets.Length; targetIndex++) { Glyph target = targets[targetIndex]; if (target == null) { continue; } GlyphMatch match = Match(src, target); if (match != null && match.Cost < bestDiff) { bestDiff = match.Cost; bestIndex = targetIndex; bestMatch = match; } } return(bestIndex); }