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

📄 connectedcomponents.cs

📁 graycsale,dilatation, erotion morphology
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;			
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing.Imaging;

namespace latihan1
{
	/// <summary>
	/// Summary description for ConnectedComponents.
	/// </summary>
	public class ConnectedComponents
	{
		public static int SAMPLES;				
		private BitmapData data;
		private int stride;
		private System.IntPtr ptr;		
		
		private int nOffset;
		public int line=0;		
		private int startX,finalX;
		private int startY=int.MaxValue;
		private int finalY=int.MinValue;		
		public string[][] urdu;
			
		public Object objects=new Object();
		/** Option for finding 4-connected components. */
		public readonly static int N4 = 1;
		/** Option for finding D-connected components.*/
		public readonly static int ND = 2; 

		/** Option for finding 8-connected components. */
		public readonly static int N8 = 3; 
		
		
		private Bitmap image;
		private Bitmap IntensityImage;   //Gray Scale Image
		public Bitmap BinaryImage;
		private int numRows ; 
		private int numCols; 
		private int label; 
		private int nbhood; 		
		public static System.IO.StreamWriter finalFile;

		public ConnectedComponents(Bitmap b)
		{
			new ConnectedComponents(b,N8);
		}
		public ConnectedComponents(Bitmap b,int nbhood)
		{			
			this.image=b;
			IntensityImage=(Bitmap)b.Clone();
			//this.GrayScale(IntensityImage);
			//Binary(IntensityImage,true);			
			//BinaryImage=(Bitmap)b.Clone();
			//Binary(BinaryImage,true);						
		}
		private void LockImage()
		{			
			data=IntensityImage.LockBits(new Rectangle(0,0,this.numCols,this.numRows),ImageLockMode.ReadWrite,PixelFormat .Format24bppRgb);
			stride=data.Stride;
			ptr=data.Scan0;
			nOffset=stride-IntensityImage.Width*3;						
			label=0;			
		}

		/** 
		 * Returns the labeled image in which the value of each pixel is
		 * the label of its connected component. All background pixels have
		 * value zero.
		 * @return The labeled image
		 */
		public Bitmap getIntensityImage() 
		{
			//IntensityImage.Save(@"c:/");
			return IntensityImage;
		}
		public Bitmap getImage() 
		{
			//IntensityImage.Save(@"c:/");
			return image;
		}

		
		/** 
	* Returns the value of the label of the last connected component
	* found in the image. This is the same as the number of connected
	* components of the image.
	* @return The label of the last connected component
	*/ 
		public int Label
		{
			get
			{
				return label;
			}
		}	
		public void ObjectIsolation(Bitmap b)
		{	
			this.nbhood=N8;
			this.numRows=b.Height;
			this.numCols=b.Width;			
			data=b.LockBits(new Rectangle(0,0,this.numCols,this.numRows),ImageLockMode.ReadWrite,PixelFormat .Format24bppRgb);
			stride=data.Stride;
			ptr=data.Scan0;
			nOffset=stride-b.Width*3;							
			unsafe
			{						
				byte*p;

				p=(byte*)(void*)ptr;
				//p+=stride;
				for (int y=1; y<numRows-1; y++) 
				{
					//p+=3;											
					for (int x=1; x<numCols-3; x++) 
					{										
						if ((p+x*3+stride*y)[0] == 0) 
						{
							label++; 									
							try
							{
								startY=10000;
								finalY=0;
								startX=int.MaxValue;
								finalX=0;
								search(label,y,x); 
								Object singleObject=new Object();
								singleObject.StartX=startX;	
								singleObject.StartY=this.startY;
								singleObject.FinalX=finalX;
								singleObject.FinalY=finalY;
								singleObject.Label=label;																						
								objects.Add(singleObject);
							}
							catch(System.StackOverflowException e) 
							{						
								MessageBox.Show(e.Message.ToString());
								return; 
							}
						}							
						//p+=3;						
					}						
					//p+=stride;									
				}				
				int t=0;
				t++;
			}			
		b.UnlockBits(data);
		}

		private void search (int Label, int r, int c)
		{		 					
			if (r<startY)
				startY=r;

			if (r>finalY)
				finalY=r;
			if (c>finalX)
				finalX=c;			
			if (c<startX)
				startX=c;
			
			try
			{
			
				int [] nb = {r,c}; 
				ArrayList nbList = findNeighbours(nb); 												
				unsafe
				{
					byte*p=(byte*)(void*)ptr;
					(p+r*stride+c*3)[0]=(byte)Label;
					(p+r*stride+c*3)[1]=(byte)Label;
					(p+r*stride+c*3)[2]=(byte)(Label*20);
																			
					for (int i=0; i<nbList.Count; i++) 
					{
						int [] pos  = (int [])nbList[i];          
						if ((p+pos[0]*stride+pos[1]*3)[0] == 0) 
							search(Label,pos[0],pos[1]);
					}
				}				
			}
			catch (System.StackOverflowException e)
			{
				MessageBox.Show(e.Message.ToString());
				return;
			}
		}

		private ArrayList findNeighbours(int [] pos) 
		{
			ArrayList nbList ; 

			if (nbhood == ND) 
				nbList = findDNeighbours(pos); 
			else if (nbhood == N8) 
				nbList = find8Neighbours(pos); 
			else 
				nbList = find4Neighbours(pos); 

			return nbList; 
		}

		private ArrayList  find4Neighbours(int [] pos)
		{
			ArrayList nbList = new ArrayList(); 

			if (pos[0] > 0) 
				addNeighbour(pos[0]-1,pos[1],nbList); 
			if (pos[1] > 0) 
				addNeighbour(pos[0],pos[1]-1,nbList); 
			if (pos[1] < numCols -1) 
				addNeighbour(pos[0],pos[1]+1,nbList); 
			if (pos[0] < numRows -1 )
				addNeighbour(pos[0]+1,pos[1],nbList); 

			return nbList; 
		} 
		
		private ArrayList findDNeighbours(int [] pos) 
		{
			ArrayList nbList = new ArrayList(); 

			if ( (pos[0] > 0) && (pos[1] > 0))
				addNeighbour(pos[0]-1,pos[1]-1,nbList); 
			if ( (pos[0] > 0) && (pos[1] < numCols -1 ) )
				addNeighbour(pos[0]-1,pos[1] +1,nbList);
			if ( (pos[0] < numRows -1) && (pos[1] > 0)) 
				addNeighbour(pos[0]+1,pos[1]-1,nbList); 
			if ((pos[0] < numRows -1) && (pos[1] <numCols -1))
				addNeighbour(pos[0]+1, pos[1] +1 ,nbList); 


     
			return nbList; 
		}


		private ArrayList find8Neighbours(int [] pos) 
		{
			ArrayList nbList = new ArrayList(); 
      
			if ( (pos[0] > 0) && (pos[1] > 0))
				addNeighbour(pos[0]-1,pos[1]-1,nbList); 
			if (pos[0] > 0) 
				addNeighbour(pos[0]-1,pos[1],nbList); 
			if ( (pos[0] > 0) && (pos[1] < numCols -1 ) )
				addNeighbour(pos[0]-1,pos[1] +1,nbList);
			if (pos[1] > 0) 
				addNeighbour(pos[0],pos[1]-1,nbList); 
			if (pos[1] < numCols -1) 
				addNeighbour(pos[0],pos[1]+1,nbList); 
			if ( (pos[0] < numRows -1) && (pos[1] > 0)) 
				addNeighbour(pos[0]+1,pos[1]-1,nbList); 
			if (pos[0] < numRows -1 )
				addNeighbour(pos[0]+1,pos[1],nbList); 
			if ((pos[0] < numRows -1) && (pos[1] <numCols -1))
				addNeighbour(pos[0]+1, pos[1] +1 ,nbList); 

			return nbList;
		}

		private void addNeighbour(int r, int c, ArrayList list)
		{
			
            try
            {
                int[] nb = { r, c };
                list.Add(nb);
            }
            catch (System.StackOverflowException e)
            {
                MessageBox.Show(e.Message.ToString());
                return;
            }
		}
																							
	}
}

⌨️ 快捷键说明

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