110,571
社区成员
发帖
与我相关
我的任务
分享
//////连通域相关参数定义
List<List<Point>> list = new List<List<Point>>();
List<Point> close = new List<Point>(); //处理过的点放入close集合,不再访问
int listCount = 0;
Bitmap a = new Bitmap(pictureBox1.Image);
int w = a.Width;
int h = a.Height;
bool[,] B = new bool[w, h];
//////定义结束
//////用true/false有无标示轨迹点,true:有轨迹点,flase:无轨迹点
for (int x = 0; x < w; x++ )
for (int y = 0; y < h; y++ )
{
var color = a.GetPixel(x, y);
if ((int)color.R == 0 && (int)color.G == 0 && (int)color.B == 0)
{
B[x, y] = true;
}
else
B[x, y] = false;
}
////下一功能
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++) //循环扫描每个点,发现一个点为true时,停止循环,并以该点为基地,
{ //广度优先搜索所有可以到达的点
if (B[x, y] && !close.Contains(new Point(x, y)))
{
listCount++;
list.Add(new List<Point>());
List<Point> queue = new List<Point>();
queue.Add(new Point(x, y));
while (queue.Count != 0)
{
Point current = queue[0];
if (current == null)
{
break;
}
queue.RemoveAt(0);
//可以到达的点
Point[] reach = new Point[8] { new Point(current.X - 1, current.Y - 1), new Point(current.X, current.Y - 1),
new Point(current.X + 1, current.Y - 1), new Point(current.X - 1, current.Y),
new Point(current.X + 1, current.Y), new Point(current.X - 1, current.Y + 1),
new Point(current.X, current.Y + 1), new Point(current.X + 1, current.Y + 1) };
for (int k = 0; k < 8; k++)
{
if (reach[k].X >= 0 && reach[k].X < w && reach[k].Y >= 0 && reach[k].Y < h)//没有超出范围
{
if (B[reach[k].X, reach[k].Y] && !close.Contains(new Point(reach[k].X, reach[k].Y)))
{
list[listCount].Add(new Point(reach[k].X, reach[k].Y));
queue.Add(new Point(reach[k].X, reach[k].Y));
close.Add(new Point(reach[k].X, reach[k].Y));
}
}
}
}
close.Add(new Point(x, y));
}
}
}
//////连通域相关参数定义
List<List<Point>> list = new List<List<Point>>();
List<Point> close = new List<Point>(); //处理过的点放入close集合,不再访问
int listCount = 0;
Bitmap a = new Bitmap(pictureBox1.Image);
int w = a.Width;
int h = a.Height;
bool[,] B = new bool[w, h];
//////定义结束
//////用true/false有无标示轨迹点,true:有轨迹点,flase:无轨迹点
for (int x = 0; x < w; x++ )
for (int y = 0; y < h; y++ )
{
var color = a.GetPixel(x, y);
if ((int)color.R == 0 && (int)color.G == 0 && (int)color.B == 0)
{
B[x, y] = true;
}
else
B[x, y] = false;
}
////下一功能
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++) //循环扫描每个点,发现一个点为true时,停止循环,并以该点为基地,
{ //广度优先搜索所有可以到达的点
if (B[x, y] && !close.Contains(new Point(x, y)))
{
listCount++;
list.Add(new List<Point>());
List<Point> queue = new List<Point>();
queue.Add(new Point(x, y));
while (queue.Count != 0)
{
Point current = queue[0];
if (current == null)
{
break;
}
queue.RemoveAt(0);
//可以到达的点
Point[] reach = new Point[8] { new Point(current.X - 1, current.Y - 1), new Point(current.X, current.Y - 1),
new Point(current.X + 1, current.Y - 1), new Point(current.X - 1, current.Y),
new Point(current.X + 1, current.Y), new Point(current.X - 1, current.Y + 1),
new Point(current.X, current.Y + 1), new Point(current.X + 1, current.Y + 1) };
for (int k = 0; k < 8; k++)
{
if (reach[k].X >= 0 && reach[k].X < w && reach[k].Y >= 0 && reach[k].Y < h)//没有超出范围
{
if (B[reach[k].X, reach[k].Y] && !close.Contains(new Point(reach[k].X, reach[k].Y)))
{
list[listCount].Add(new Point(reach[k].X, reach[k].Y));
queue.Add(new Point(reach[k].X, reach[k].Y));
close.Add(new Point(reach[k].X, reach[k].Y));
}
}
}
}
close.Add(new Point(x, y));
}
}
}
参考了别人的程序,但是读list的count总是等于0,感觉我读List<List<Point>>的方法有误
for (int i = 0; i < list.Count - 1; i++)
{
sw.WriteLine("var num pointxy{" + 2 * list[i].Count + "}:=[");//输出i 轮廓的点数
for (int kkk = 0; kkk < list[i].Count-1;kkk++ )
{
sw.WriteLine(string.Format("{0}, {1},", list[i][kkk].X, list[i][kkk].Y));//输出i 轮廓下的所有坐标
}
}