/// <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> /// Creates a <c>CharacteristicVector</c> instance for a given /// <c>FloatBitmap</c> object. /// </summary> /// <param name="image"> /// A <see cref="FloatBitmap"/> /// </param> /// <returns> /// A <see cref="CharacteristicVector"/> /// </returns> private CheckVector CreateVector(FloatBitmap image) { // We create the receptors list. if (receptors == null) { receptors = Receptor.GenerateList(40); } CheckVector vector = new CheckVector(); bool checkValue; int width = image.Width; int height = image.Height; foreach (Receptor receptor in receptors) { checkValue = receptor.CheckBressard(image); vector.Values.Add(checkValue); StepDoneArgs args = new StepDoneArgs(String.Format("Comprobando receptor {0}: {1}", String.Format("({0}, {1}) -> ({2}, {3})", receptor.X0, receptor.Y0, receptor.X1, receptor.Y1), checkValue)); StepDoneInvoker(args); Thread.Sleep(20); } foreach (BinaryCharacteristic characteristic in characteristics) { checkValue = characteristic.Apply(image); vector.Values.Add(checkValue); StepDoneArgs args = new StepDoneArgs(String.Format("Comprobando característica {0}: {1}", characteristic.GetType().ToString(), checkValue)); StepDoneInvoker(args); Thread.Sleep(20); } return(vector); }
/// <summary> /// Creates a <c>CharacteristicVector</c> instance for a given /// <c>FloatBitmap</c> object. /// </summary> /// <param name="image"> /// A <see cref="FloatBitmap"/> /// </param> /// <returns> /// A <see cref="CharacteristicVector"/> /// </returns> private CheckVector CreateVector(FloatBitmap image) { CheckVector vector = new CheckVector(); bool characteristicValue; foreach (BinaryCharacteristic bc in characteristics) { characteristicValue = bc.Apply(image); vector.Values.Add(characteristicValue); StepDoneArgs args = new StepDoneArgs(String.Format("Comprobando {0}: {1}", bc.GetType(), characteristicValue)); StepDoneInvoker(args); } return(vector); }
/// <summary> /// Creates a <c>CharacteristicVector</c> instance for a given /// <c>FloatBitmap</c> object. /// </summary> /// <param name="image"> /// A <see cref="FloatBitmap"/> /// </param> /// <returns> /// A <see cref="CharacteristicVector"/> /// </returns> private CheckVector CreateVector(FloatBitmap image) { CheckVector vector = new CheckVector(); bool characteristicValue; foreach(BinaryCharacteristic bc in characteristics) { characteristicValue = bc.Apply(image); vector.Values.Add(characteristicValue); StepDoneArgs args = new StepDoneArgs(String.Format("Comprobando {0}: {1}", bc.GetType(), characteristicValue)); StepDoneInvoker(args); } return vector; }