Example #1
0
        /// <summary>
        /// Adds a child to the node.
        /// </summary>
        /// <param name="node">
        /// A <see cref="SegmentedNode"/>
        /// </param>
        public void AddSegmentedChild(SegmentedNode node)
        {
            Application.Invoke(this,
                               new AddNodeArgs(node),
                               delegate(object sender, EventArgs arg)
            {
                AddNodeArgs a = arg as AddNodeArgs;
                this.AddChild(a.Node);

                if (this.Parent == null)
                {
                    // Si no tiene padre
                    a.Node.name = String.Format("Img. {0}", this.ChildCount);
                }
                else
                {
                    a.Node.name = String.Format("{0}.{1}", this.name, this.ChildCount);
                }

                if (view != null)
                {
                    view.ExpandAll();
                    view.ColumnsAutosize();
                }
            });
        }
		/// <summary>
		/// <c>SymbolLabelEditorDialog</c>'s constructor method.
		/// </summary>
		/// <param name="parent">
		/// The new dialog's parent window.
		/// </param>
		/// <param name="node">
		/// The node which label we want to edit.
		/// </param>
		public SymbolLabelEditorDialog(Window parent, SegmentedNode node)
		{
			XML gxml = new XML("mathtextrecognizer.glade",
			                   "symbolLabelEditorDialog");
			
			gxml.Autoconnect(this);
			
			symbolLabelEditorDialog.TransientFor = parent;
			
			InitializeWidgets(node);
			
			
			symbolLabelEditorDialog.ShowAll();
		}
		private void OnTreeviewButtonPress(object sender, 
		                                   ButtonPressEventArgs args)
		{
			// Nos quedamos unicamente con los clicks derechos
			if(recognizementFinished 
			   && args.Event.Button == 3)
			{
				TreePath path = new TreePath();
                // Obtenemos el treepath con las coordenadas del cursor.
                treeview.GetPathAtPos(System.Convert.ToInt16 (args.Event.X), 
				                      System.Convert.ToInt16 (args.Event.Y),				              
				                      out path);
                
				if( path != null)
				{
					// We try only if a node was found.			
					SegmentedNode node =  
						(SegmentedNode)(treeview.NodeStore.GetNode(path));	
					
					selectedNode = node;				
					
					segmentedNodeMenu.Popup();
                }                        
			}
		}
		/// <summary>
		/// Establece la imagen inicial para segmentar y reconocer sus
		/// caracteres. 
		/// </summary>
		/// <param name="image">
		/// La imagen inicial.
		/// </param>
		private void SetInitialImage(string  filename)
		{
			
			imageOriginal = new Pixbuf(filename);
			imageAreaOriginal.Image=imageOriginal;				
			store.Clear();
			
			// Generamos el MaxtTextBitmap inical, y lo añadimos como
			// un nodo al arbol.
			MathTextBitmap mtb = new MathTextBitmap(imageOriginal);			
			SegmentedNode node = 
				new SegmentedNode(System.IO.Path.GetFileNameWithoutExtension(filename),
				                  mtb,
				                  treeview);
			    
			store.AddNode(node);
			
			rootNode = node;
			
			controller.StartNode = node;

			Log("¡Archivo de imagen «{0}» cargado correctamente!", filename);
			
			alignNextButtons.Sensitive=true;
			segmentBtn.Sensitive = true;
			gotoTokenizerBtn.Sensitive = false;
			buttonsNB.Page = 0;
		}
		private void OnUnassistedProcessBtnClicked(object sender, EventArgs args)
		{
			// We try to free memory.
			GC.Collect();
			segmentationStore.Clear();
			
			
			MainRecognizerWindow.ProcessItemsSensitive = true;
			unassistedControlHBB.Sensitive = false;
			unassistedTaskNameLabel.Text = "Segmentación y OCR";
			
			unassistedGlobalProgressBar.Fraction = 0;
			
			// We create the image to be recognized.
			
			Bitmap  bitmap = handwritingArea.Bitmap;
			
			FloatBitmap floatBitmap = 
				new FloatBitmap(bitmap.Width, bitmap.Height);
			
			for(int i=0; i< bitmap.Width; i++)
			{
				for(int j = 0; j < bitmap.Height; j++)
				{
					floatBitmap[i,j] = bitmap.GetPixel(i,j).GetBrightness();
				}
			}
			
			// The original image is set.
			originalImage = floatBitmap.CreatePixbuf();
			
			MathTextBitmap mtb = new MathTextBitmap(originalImage);
			
			SegmentedNode node = new SegmentedNode("Raíz",
			                                       mtb,
			                                       null);
			    
			segmentationStore.AddNode(node);
			
			ocrController.StartNode = node;
			ocrController.SearchDatabase = false;
			ocrController.Databases =  
				Config.RecognizerConfig.Instance.Databases;
			ocrController.Next(ControllerStepMode.UntilEnd);
		}
			/// <summary>
			/// <c>AddNodeArgs</c>'s constructor.
			/// </summary>
			public AddNodeArgs(SegmentedNode node)
			{
				this.node= node;
			}
		/// <summary>
		/// Adds a child to the node.
		/// </summary>
		/// <param name="node">
		/// A <see cref="SegmentedNode"/>
		/// </param>
		public void AddSegmentedChild(SegmentedNode node)
		{		
			Application.Invoke(this, 
			                   new AddNodeArgs(node),
			                   delegate(object sender, EventArgs arg)
			{
				AddNodeArgs a = arg as AddNodeArgs;
				this.AddChild(a.Node);
				
				if(this.Parent ==null)
				{
					// Si no tiene padre
					a.Node.name = String.Format("Img. {0}",this.ChildCount);
				}
				else
				{
					a.Node.name = String.Format("{0}.{1}",this.name, this.ChildCount);
				}
				
				if(view != null)
				{
					view.ExpandAll();
					view.ColumnsAutosize();
				}
				
			});
			
		}
		/// <summary>
		/// Con este metodo cramos un arbol de imagenes, de forma recursiva.
		/// Primero intentamos reconocer la imagen como un caracter, si no es 
		/// posible, la intentamos segmentar. Si ninguno de estos procesos es 
		/// posible, la imagen nopudo ser reconocida.
		/// </summary>
		/// <param name="node">
		/// El nodo donde esta la imagen sobre la que trabajaremos.
		/// </param>
		private void RecognizerTreeBuild(SegmentedNode node)
		{			
			// Seleccionamos el nodo.
			
			NodeBeingProcessedInvoker(node);
			SuspendByNode();
		
			node.Select();
			
			while(node.ChildCount>0)
				node.RemoveChild((SegmentedNode)node[0]);
			
			MathTextBitmap bitmap = node.MathTextBitmap;
			MessageLogSentInvoker("=======================================");
			MessageLogSentInvoker("Tratando la subimagen «{0}»",
			                      node.Name);
			
			// Lanzamos el reconocedor de caracteres para cada una de
			// las bases de datos.
			List<MathSymbol> associatedSymbols = new List<MathSymbol>();
			if(searchDatabase)
			{
				foreach(MathTextDatabase database in databases)
				{
					MessageLogSentInvoker("---------- «{0}» ------------",
					                      database.ShortDescription);
					                      
					
					bitmap.ProcessImage(database.Processes);
					BitmapProcessedByDatabaseInvoker(bitmap);
					
					// Añadimos los caracteres reconocidos por la base de datos
					foreach (MathSymbol symbol in database.Recognize(bitmap))
					{
						if(!associatedSymbols.Contains(symbol))
						{
							// Solo añadimos si no esta ya el simbolo.
							associatedSymbols.Add(symbol);
						}
					}
				}
			}
		
						
			// We associate all symbols to the node, so we can postargate
			// the decission step.
			node.Symbols = associatedSymbols;
			
			//Si no hemos reconocido nada, pues intentaremos segmentar el caracter.
			if(associatedSymbols.Count == 0)
			{	
				if(searchDatabase)
				{
					MessageLogSentInvoker("La imagen no pudo ser reconocida como un "
				                      + "simbolo por la base de datos, se tratará de "
				                      + "segmentar");
				}
				else
				{
					MessageLogSentInvoker("Se procede directamente a segmentar la imagen");
					this.SearchDatabase = true;
				}
					
				List<MathTextBitmap> children = CreateChildren(bitmap);
				
				if(children.Count > 1)
				{
					MessageLogSentInvoker("La imagen se ha segmentado correctamente");
					
					//Si solo conseguimos un hijo, es la propia imagen, asi que nada
					
					List<SegmentedNode> nodes = new List<SegmentedNode>();
					
					foreach(MathTextBitmap child in children)
					{
						SegmentedNode childNode = new SegmentedNode(child, node.View);	
						node.AddSegmentedChild(childNode);
						nodes.Add(childNode);
					}
					
					foreach(SegmentedNode childNode in nodes)
					{
						RecognizerTreeBuild(childNode);	
					}
				}
				else
				{
					MessageLogSentInvoker("La imagen no pudo ser segmentada, el "
					                      + "símbolo queda sin reconocer");
				}
			}
			else
			{
				// We prepare the string.
				string text ="";
				foreach(MathSymbol s in associatedSymbols)
				{
					text += String.Format("«{0}», ", s.Text);
				}
				
				text = text.TrimEnd(',',' ');
				MessageLogSentInvoker("Símbolo reconocido por la base de datos como {0}.",
				                      text);
			}
		}
		/// <summary>
		/// Retrieves the leaf nodes of a given node.
		/// </summary>
		/// <param name="node">
		/// The node to check;
		/// </param>
		/// <returns>
		/// A list with the leafs.
		/// </returns>
		private List<SegmentedNode> GetLeafs(SegmentedNode node)
		{
			List<SegmentedNode> leafs = new List<SegmentedNode>();
			
			if(node.ChildCount == 0)
			{
				leafs.Add(node);
			}
			else
			{
				for(int i=0; i< node.ChildCount; i++)
				{
					leafs.AddRange(GetLeafs((SegmentedNode)node[i]));
				}
			}
			
			return leafs;
		}
		/// <summary>
		/// Sets the image to be processed.
		/// </summary>
		/// <param name="image">
		/// The initial image's path.
		/// </param>
		private void SetInitialImage(string  filename)
		{
			segmentationStore.Clear();
			
			Gdk.Pixbuf imageOriginal = new Gdk.Pixbuf(filename);
			originalImageArea.Image=imageOriginal;				
			
			
			// Generamos el MaxtTextBitmap inicial, y lo añadimos como
			// un nodo al arbol.
			MathTextBitmap mtb = new MathTextBitmap(imageOriginal);			
			SegmentedNode node = 
				new SegmentedNode(System.IO.Path.GetFileNameWithoutExtension(filename),
				                  mtb,
				                  null);
			    
			segmentationStore.AddNode(node);
			
			ocrController.StartNode = node;

			Log("¡Archivo de imagen «{0}» cargado correctamente!", filename);
			
			unassistedProcessBtn.Sensitive = true;
			
		}
Example #11
0
 /// <summary>
 /// <c>AddNodeArgs</c>'s constructor.
 /// </summary>
 public AddNodeArgs(SegmentedNode node)
 {
     this.node = node;
 }
		/// <summary>
		/// Initializes the dialogs' widgets.
		/// </summary>
		/// <param name="node">
		/// A <see cref="SegmentedNode"/>
		/// </param>
		private void InitializeWidgets(SegmentedNode node)
		{			
			imageNameLabel.Text = String.Format(imageNameLabel.Text, node.Name);
			
			labelEditor = new SymbolLabelEditorWidget();
			if(node.Symbols.Count ==0)
			{
				labelEditor.Label = node.Label;
			}
			else
			{
				// If we have various posibilities, we add radio buttons.
				RadioButton group = new RadioButton("group");
				foreach(MathSymbol symbol in node.Symbols)
				{		
					RadioButton rb = new RadioButton(group, symbol.Text);
					Alignment rbAlign = new Alignment(0,0.5f,0,0);
					rbAlign.Add(rb);
					symbolEditorPlaceholder.Add(rbAlign);
					
					rb.Clicked += new EventHandler(OnLabelOptionClicked);
					
					
				}
				
				Alignment rbOtherAlign = new Alignment(0,0.5f,0,0);
				otherLabelRB = new RadioButton(group, "Otra:");
				otherLabelRB.Clicked += new EventHandler(OnLabelOptionClicked);
				
				rbOtherAlign.Add(otherLabelRB);
				symbolEditorPlaceholder.Add(rbOtherAlign);
				labelEditor.Sensitive = false;
			}
			
			symbolEditorPlaceholder.Add(labelEditor);
			
			nodeImage = new ImageArea();
			nodeImage.Image = node.MathTextBitmap.Pixbuf;
			nodeImage.ImageMode = ImageAreaMode.Zoom;
			
			imagePlaceholder.Add(nodeImage);
		}