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:
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.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);
}