/// <summary> /// Con este metodos almacenamos un nuevo simbolo en la base de /// datos. /// /// Lanza <c>DuplicateSymbolException</c> si ya existe un simbolo /// con la misma etiqueta y caracteristicas binarias en la base de datos. /// </summary> /// <param name="bitmap"> /// La imagen cuyas caracteristicas aprenderemos. /// </param> /// <param name="symbol"> /// El simbolo que representa a la imagen. /// </param> public override bool Learn(MathTextBitmap bitmap, MathSymbol symbol) { if (characteristics == null) { characteristics = CharacteristicFactory.CreateCharacteristicList(); } FloatBitmap processedBitmap = bitmap.LastProcessedImage; CheckVector vector = CreateVector(processedBitmap); int position = symbolsDict.IndexOf(vector); if (position >= 0) { // The vector exists in the list List <MathSymbol> symbols = symbolsDict[position].Symbols; if (symbols.Contains(symbol)) { return(false); } else { symbols.Add(symbol); } } else { vector.Symbols.Add(symbol); symbolsDict.Add(vector); } return(true); }
/// <summary> /// Este metodo intenta recuperar los simbolos de la base de datos /// correspondiente a una imagen. /// </summary> /// <param name="image"> /// La imagen cuyos simbolos asociados queremos encontrar. /// </param> /// <returns> /// Los simbolos que se puedieron asociar con la imagen. /// </returns> public override List <MathSymbol> Match(MathTextBitmap image) { if (characteristics == null) { characteristics = CharacteristicFactory.CreateCharacteristicList(); } List <MathSymbol> res = new List <MathSymbol>(); FloatBitmap processedImage = image.LastProcessedImage; CheckVector vector = CreateVector(processedImage); // We have this image vector, now we will compare with the stored ones. // We consider a threshold. int threshold = (int)(characteristics.Count * epsilon); foreach (CheckVector storedVector in symbolsDict) { // If the distance is below the threshold, we consider it valid. if (vector.Distance(storedVector) <= threshold) { foreach (MathSymbol symbol in storedVector.Symbols) { // We don't want duplicated symbols. if (!res.Contains(symbol)) { res.Add(symbol); } } string msg = String.Format("Distancia({0}, {1}) <= {2}", vector.ToString(), storedVector.ToString(), threshold); msg += "\nSe añadieron los símbolos almacenados."; StepDoneInvoker(new StepDoneArgs(msg)); } else { string msg = String.Format("Distancia({0}, {1}) > {2}", vector.ToString(), storedVector.ToString(), threshold); StepDoneInvoker(new StepDoneArgs(msg)); } } return(res); }
/// <summary> /// Crea una lista con todas las caracteristicas disponibles en /// el sistema. /// </summary> /// <returns>Lista de todas las clases que implementan /// <c>IBinaryCharacteristic</c></returns> public static List <BinaryCharacteristic> CreateCharacteristicList() { List <BinaryCharacteristic> characteristics = new List <BinaryCharacteristic>(); Assembly a = Assembly.GetAssembly(typeof(BinaryCharacteristic)); foreach (Type t in a.GetTypes()) { if (t.BaseType == typeof(BinaryCharacteristic)) { characteristics.Add(CharacteristicFactory.CreateCharacteristic(t)); } } characteristics.Sort(); return(characteristics); }
/// <summary> /// Con este metodos almacenamos un nuevo simbolo en la base de /// datos. /// /// Lanza <c>DuplicateSymbolException</c> si ya existe un simbolo /// con la misma etiqueta y caracteristicas binarias en la base de datos. /// </summary> /// <param name="bitmap"> /// La imagen cuyas caracteristicas aprenderemos. /// </param> /// <param name="symbol"> /// El simbolo que representa a la imagen. ///</param> public override bool Learn(MathTextBitmap bitmap, MathSymbol symbol) { if (characteristics == null) { characteristics = CharacteristicFactory.CreateCharacteristicList(); } CharacteristicNode node = rootNode; bool characteristicValue; FloatBitmap processedBitmap = bitmap.LastProcessedImage; // Recorremos las caracteristicas, y vamos creando el arbol segun // vamos necesitando nodos. foreach (BinaryCharacteristic bc in characteristics) { if (characteristicValue = bc.Apply(processedBitmap)) { if (node.TrueTree == null) { node.TrueTree = new CharacteristicNode(); } node = node.TrueTree; } else { if (node.FalseTree == null) { node.FalseTree = new CharacteristicNode(); } node = node.FalseTree; } StepDoneArgs a = CreateStepDoneArgs(bc, characteristicValue, null); this.StepDoneInvoker(a); } return(node.AddSymbol(symbol)); }
/// <summary> /// Este metodo intenta recuperar los simbolos de la base de datos /// correspondiente a una imagen. /// </summary> /// <param name="image"> /// La imagen cuyos simbolos asociados queremos encontrar. /// </param> /// <returns> /// Los simbolos que se puedieron asociar con la imagen. /// </returns> public override List <MathSymbol> Match(MathTextBitmap image) { if (characteristics == null) { characteristics = CharacteristicFactory.CreateCharacteristicList(); } List <MathSymbol> res = new List <MathSymbol>(); CharacteristicNode nodo = rootNode; BinaryCharacteristic bc; bool exists = true; bool characteristicValue; List <bool> vector = new List <bool>(); FloatBitmap processedImage = image.LastProcessedImage; for (int i = 0; i < characteristics.Count && exists; i++) { bc = (BinaryCharacteristic)(characteristics[i]); if (characteristicValue = bc.Apply(processedImage)) { if (nodo.TrueTree == null) { exists = false; } nodo = nodo.TrueTree; } else { if (nodo.FalseTree == null) { exists = false; } nodo = nodo.FalseTree; } StepDoneArgs args; //Avisamos de que hemos dado un paso if (nodo != null) { args = CreateStepDoneArgs(bc, characteristicValue, nodo.ChildrenSymbols); } else { args = CreateStepDoneArgs(bc, characteristicValue, null); } StepDoneInvoker(args); } if (exists) { res = nodo.Symbols; } return(res); }