⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Unicode Optical Character Recognition
💻 CS
📖 第 1 页 / 共 4 页
字号:
				image_file_name=Path.GetFileNameWithoutExtension(openFileDialog1.FileName );
				image_file_path=Path.GetDirectoryName(openFileDialog1.FileName);				
				image_file_stream.Close();
				input_image_height=input_image.Height ;
				input_image_width=input_image.Width ;
				if(input_image_width>pictureBox1.Width ) 
					pictureBox1.SizeMode= PictureBoxSizeMode.StretchImage;
				else
					pictureBox1.SizeMode= PictureBoxSizeMode.Normal;
				right=1;
				image_start_pixel_x=0;
				image_start_pixel_y=0;
				identify_lines();
				current_line=0;
				character_present=true;
				character_valid=true;
				output_string="";
				label5.Text ="Input Image : ["+image_file_name+".bmp]";
			}
		}
		public void detect_next_character()
		{			
			number_of_input_sets=1;
			get_next_character();
			if(character_present)
			{
				for(int i=0;i<10;i++)
					for(int j=0;j<15;j++)
						input_set[i*15+j,0]=ann_input_value[i*2+1,j*2+1];
				get_inputs(0);
				calculate_outputs();
				comboBox3.Items.Clear ();
				comboBox3.BeginUpdate ();
				for(int i=0;i<number_of_output_nodes;i++)
				{
					output_bit[i]=threshold(node_output[number_of_layers-1,i]);
					comboBox3.Items.Add ("bit["+(i).ToString()+"] " + output_bit[i].ToString ());
				}
				comboBox3.EndUpdate ();
				char character=unicode_to_character();
				output_string=output_string+character.ToString ();
				textBox8.Text =" "+character.ToString ();
				string hexadecimal=binary_to_hex();
				label11.Text =hexadecimal+" h"; label11.Update ();
				richTextBox1.Text =output_string;			
				textBox8.Update ();
				richTextBox1.Update ();
			}
		}
		public void get_next_character()
		{			
			image_start_pixel_x=right+2;
			image_start_pixel_y=line_top[current_line];
			analyze_image();
		}
		public void analyze_image()
		{			
			int analyzed_line=current_line;
			comboBox1.Items.Clear ();
			comboBox2.Items.Clear ();
			get_character_bounds();
			if(character_present)
			{				
				map_character_image_pixel_matrix();
				create_character_image();
				map_ann_input_matrix();			
			}
			else
				MessageBox.Show ("Character Recognition Complete!", "Unicode OCR", 
					MessageBoxButtons.OK , MessageBoxIcon.Exclamation);
			
		}
		private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{								
		}
		public void identify_lines()
		{			
			int y=image_start_pixel_y;
			int x=image_start_pixel_x;
			bool no_black_pixel;
			int line_number=0;
			line_present=true;
			while(line_present)
			{				
				x=image_start_pixel_x;
				while(Convert.ToString (input_image.GetPixel (x,y))=="Color [A=255, R=255, G=255, B=255]")
				{
					x++;
					if(x==input_image_width)
					{
						x=image_start_pixel_x;
						y++;
					}
					if(y>=input_image_height)
					{
						line_present=false;
						break;
					}
				}
				if(line_present)
				{
					line_top[line_number]=y;
					no_black_pixel=false;
					while(no_black_pixel==false)
					{				
						y++;
						no_black_pixel=true;
						for(x=image_start_pixel_x;x<input_image_width;x++)				
							if((Convert.ToString (input_image.GetPixel (x,y))=="Color [A=255, R=0, G=0, B=0]"))					
								no_black_pixel=false;														
					}			
					line_bottom[line_number]=y-1;
					line_number++;
				}
			}
			number_of_lines=line_number;			
		}
		public void get_character_bounds()
		{			
			int x=image_start_pixel_x;
			int y=image_start_pixel_y;			
			bool no_black_pixel=false;
			if(y<=input_image_height && x<=input_image_width)
			{
				while(Convert.ToString (input_image.GetPixel (x,y))=="Color [A=255, R=255, G=255, B=255]")
				{
					x++;
					if(x==input_image_width)
					{
						x=image_start_pixel_x;
						y++;
					}
					if(y>=line_bottom[current_line])
					{
						character_present=false;
						break;
					}
				}
				if(character_present)
				{
					top=y;
			
					x=image_start_pixel_x; y=image_start_pixel_y;	
					while(Convert.ToString (input_image.GetPixel (x,y))=="Color [A=255, R=255, G=255, B=255]")
					{
						y++;
						if(y==line_bottom[current_line])
						{
							y=image_start_pixel_y;
							x++;
						}
						if(x>input_image_width)
							break;
					}
					if(x<input_image_width)			
						left=x;

					no_black_pixel=true;
					y=line_bottom[current_line]+2;
					while(no_black_pixel==true)
					{				
						y--;						
						for(x=image_start_pixel_x;x<input_image_width;x++)				
							if((Convert.ToString (input_image.GetPixel (x,y))=="Color [A=255, R=0, G=0, B=0]"))					
								no_black_pixel=false;														
					}			
					bottom=y;

					no_black_pixel=false;
					x=left+10;
					while(no_black_pixel==false)
					{
						x++;
						no_black_pixel=true;
						for(y=image_start_pixel_y;y<line_bottom[current_line];y++)				
							if((Convert.ToString (input_image.GetPixel (x,y))=="Color [A=255, R=0, G=0, B=0]"))					
								no_black_pixel=false;														
					}		
					right=x-1;
					top=confirm_top();
					bottom=confirm_bottom();					
				
					character_height=bottom-top+1;
					character_width=right-left+1;
					confirm_dimensions();
					if(left-prev_right>=20)
						output_string=output_string+" ";

					prev_right=right;

					textBox1.Text = Convert.ToString (top ,10); textBox1.Update ();
					textBox2.Text = Convert.ToString (left ,10); textBox2.Update ();
					textBox3.Text = Convert.ToString (bottom ,10); textBox3.Update ();
					textBox4.Text = Convert.ToString (right ,10); textBox4.Update ();
					textBox6.Text = Convert.ToString (character_width ,10); textBox6.Update ();
					textBox7.Text = Convert.ToString (character_height ,10); textBox7.Update ();
				}
				else if(current_line<number_of_lines-1)
				{					
					current_line++;
					image_start_pixel_y=line_top[current_line];
					image_start_pixel_x=0;
					prev_right=20;
					output_string=output_string + "\n";
					character_present=true;
					get_character_bounds();																									
				}
			}
			else
				character_present=false;
		}
		public int confirm_top()
		{
			int local_top=top;
			for(int j=top;j<=bottom;j++)
				for(int i=left;i<=right;i++)
					if(Convert.ToString (input_image.GetPixel (i,j))=="Color [A=255, R=0, G=0, B=0]")
					{
						local_top=j;
						return local_top;
					}
			return local_top;
		}
		public int confirm_bottom()
		{
			int local_bottom=bottom;
			for(int j=bottom;j>=0;j--)
				for(int i=left;i<=right;i++)
					if(Convert.ToString (input_image.GetPixel (i,j))!="Color [A=255, R=255, G=255, B=255]")
					{
						local_bottom=j;
						return local_bottom;
					}			
			return local_bottom;
		}
		public void confirm_dimensions()
		{
			if(character_width<20)
			{
				left=left-5; right=right+5;				
			}
			if(character_height<30)
			{
				top=top-15; bottom=bottom+15;				
			}
			character_height=bottom-top+1;
			character_width=right-left+1;
		}
		public void pick_sampling_pixels()
		{			
			int step=(int)(character_height/matrix_height);
			if(step<1) step=1;

			sample_pixel_y[0]=0;			
			sample_pixel_y[29]=character_height-1;
			sample_pixel_y[19]=(int)(2*sample_pixel_y[29]/3);			
			sample_pixel_y[9]=(int)(sample_pixel_y[29]/3);
			
			sample_pixel_y[4]=(int)(sample_pixel_y[9]/2);
			sample_pixel_y[5]=sample_pixel_y[4]+step;
			sample_pixel_y[2]=(int)(sample_pixel_y[4]/2);
			sample_pixel_y[3]=sample_pixel_y[2]+step;
			sample_pixel_y[1]=sample_pixel_y[0]+step;
			sample_pixel_y[6]=sample_pixel_y[1]+sample_pixel_y[5];
			sample_pixel_y[7]=sample_pixel_y[2]+sample_pixel_y[5];
			sample_pixel_y[8]=sample_pixel_y[3]+sample_pixel_y[5];
			for(int i=10;i<19;i++)
				sample_pixel_y[i]=sample_pixel_y[i-10]+sample_pixel_y[9];
			for(int i=20;i<29;i++)
				sample_pixel_y[i]=sample_pixel_y[i-20]+sample_pixel_y[19];

			step=(int)(character_width/matrix_width);
			if(step<1) step=1;
			
			sample_pixel_x[0]=0;			
			sample_pixel_x[19]=character_width-1;			
			sample_pixel_x[9]=(int)(sample_pixel_x[19]/2);						
			
			sample_pixel_x[4]=(int)(sample_pixel_x[9]/2);
			sample_pixel_x[5]=sample_pixel_x[4]+step;
			sample_pixel_x[2]=(int)(sample_pixel_x[4]/2);
			sample_pixel_x[3]=sample_pixel_x[2]+step;
			sample_pixel_x[1]=sample_pixel_x[0]+step;
			sample_pixel_x[6]=sample_pixel_x[1]+sample_pixel_x[5];
			sample_pixel_x[7]=sample_pixel_x[2]+sample_pixel_x[5];
			sample_pixel_x[8]=sample_pixel_x[3]+sample_pixel_x[5];
			for(int i=10;i<19;i++)
				sample_pixel_x[i]=sample_pixel_x[i-10]+sample_pixel_x[9];			

			comboBox2.BeginUpdate ();
			for(int i=0;i<20;i++)
				comboBox2.Items.Add ("["+(i+1).ToString () +"]  "+ sample_pixel_x[i].ToString ());
			comboBox2.EndUpdate ();
			comboBox1.BeginUpdate ();
			for(int i=0;i<30;i++)
				comboBox1.Items.Add ("["+(i+1).ToString () +"]  "+ sample_pixel_y[i].ToString ());
			comboBox1.EndUpdate ();
		}
		public void map_character_image_pixel_matrix()
		{												
			for(int j=0;j<character_height;j++)			
				for(int i=0;i<character_width;i++)				
					character_image_pixel[i,j]=input_image.GetPixel (i+left,j+top);																
		}
		public void create_character_image()
		{
			character_image = new System.Drawing.Bitmap(character_width, character_height);
			for(int j=0;j<character_height;j++)
				for(int i=0;i<character_width;i++)
					character_image.SetPixel (i,j,character_image_pixel[i,j]);
			pictureBox2.Image =character_image;
			pictureBox2.Update ();	
		}
		public void map_ann_input_matrix()
		{			
			pick_sampling_pixels();			
			for( int j = 0; j < matrix_height; j++)			
				for(int i= 0; i < matrix_width; i++ )	
				{														
					ann_input_pixel[i,j]=character_image.GetPixel(sample_pixel_x[i],sample_pixel_y[j]);	
					if(ann_input_pixel[i,j].ToString()=="Color [A=255, R=0, G=0, B=0]")
						ann_input_value[i,j]=1;
					else
						ann_input_value[i,j]=0;
				}
			groupBox6.Invalidate ();
			groupBox6.Update ();
		}
		private void groupBox6_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			SolidBrush blueBrush = new SolidBrush(Color.Blue);
			Pen blackpen=new Pen(Color.Black, 1);
			for(int j=0;j<matrix_height;j++)
				for(int i=0;i<matrix_width;i++)
				{					
					e.Graphics.DrawRectangle(blackpen, (x_org+rec_width*i),(y_org+rec_height*j),(rec_width),(rec_height));
					if(ann_input_value[i,j]==1)
						e.Graphics.FillRectangle(blueBrush, x_org+rec_width*i,y_org+rec_height*j,rec_width,rec_height);			
				}			
		}
		private void button6_Click(object sender, System.EventArgs e)
		{
			if(trainer_thread_created==true)
			{
				if(trainer_thread.ThreadState ==System.Threading.ThreadState.Suspended)
					trainer_thread.Resume ();			
				trainer_thread.Abort();		
			}
			Application.Exit ();
		}
		private void button5_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show ("Unicode Optical Character Recognizer Ver.1.0.0. 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -