Click here to Skip to main content
15,888,195 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have been working on my dissertation lately .Now I get a tough problem to deal with .When I call
the following function to do contour tracking ,I found there are some points cannot be tracked. I use the setpixel function to set the points that has been tracked in white color, but the contour doesn't disappeared as I expected,there are still some points left which make it difficult to track the next contour. Help!

Code:
C#
public void  MyFindContours(Bitmap cBmp)
{ 
   int[,] Direction = new int[,] { { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 }, { -1, 0 } };

   Color PointPix = new Color();
   Point StartPoint = new Point();
   Point CurrentPoint = new Point();

   bool bFindStartPoint = false;
   bool bFindPoint;

   Color bb = Color.FromArgb(0, 0, 0);
   Color ww = Color.FromArgb(255, 255, 255);

   //先找最左上方的边界点
   for (int j = 0; j < cBmp.Height && !bFindStartPoint; j++)
   {
      for (int i = 0; i < cBmp.Width && !bFindStartPoint; i++)
      {
         PointPix = cBmp.GetPixel(i, j);

         if (PointPix == bb)
         {
            bFindStartPoint = true;
            StartPoint.X = i;
            StartPoint.Y = j;
         
            myContour.Clear(); //每次找到一个轮廓边界点,将myContour 清空,用于暂时存放轮廓点
            myContour.Add(StartPoint);//将轮廓起点存入
         }
         else
         {
            if (j == cBmp.Height - 1 && i == cBmp.Width - 1) 
            {
               return;
            }
            else
            {
               continue;
            }
         }
      }
   }

   //由于起始点在左下方,故起始扫描沿着左上方向

   int BeginDirect = 0;
   bFindStartPoint = false;

   CurrentPoint.X = StartPoint.X;
   CurrentPoint.Y = StartPoint.Y;

   while (!bFindStartPoint)
   {
      bFindPoint = false;

      while (!bFindPoint)
      {
         PointPix = cBmp.GetPixel(CurrentPoint.X + Direction[BeginDirect, 0], CurrentPoint.Y + Direction[BeginDirect, 1]);

         if (PointPix == bb)
         {
            bFindPoint = true;

            myContour.Add(CurrentPoint);//当前点为边界点,则将其加入数组

            cBmp.SetPixel(CurrentPoint.X,CurrentPoint.Y, ww);
            this.pictureBox2.Image = cBmp;
            this.pictureBox2.Refresh();

            CurrentPoint.Y = CurrentPoint.Y + Direction[BeginDirect, 1];
            CurrentPoint.X = CurrentPoint.X + Direction[BeginDirect, 0];

            if (CurrentPoint.X == StartPoint.X && CurrentPoint.Y == StartPoint.Y)
            {
               bFindStartPoint = true;
               myContour.Remove(CurrentPoint);//当回到起点,已经把起点加到数组最后了,因此要删除
               gContour.Add(myContour);//把轮廓点加到总轮廓中
            }

            PointPix = cBmp.GetPixel(CurrentPoint.X,CurrentPoint.Y);
            //扫描的方向逆时针旋转两格
            BeginDirect--;

            if (BeginDirect == -1)
            {
               BeginDirect = 7;
            }

            BeginDirect--;

            if (BeginDirect == -1)
            {
               BeginDirect = 7;
            }
         }
         else
         {
            //扫描方向顺时针旋转一格
            BeginDirect++;

            if (BeginDirect == 8)
            {
               BeginDirect = 0;
            }
         }
      }
   }

   for(int i=0;i<gcontour.count;i++)>
   {
      for (int j = 0; j < gContour[i].Count; j++)
      {
         cBmp.SetPixel(gContour[i].ElementAt(j).X, gContour[i].ElementAt(j).Y, ww);
         this.pictureBox2.Image = cBmp;
         this.pictureBox2.Refresh();
      }
   }
        
   MyFindContours(cBmp);
}
Posted
Updated 4-May-12 7:33am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900