之前发过一篇博文是用python写连通区域标记算法,搜索过程采用的是深度优先搜索,对于一些像素数目较多的目标区域,容易陷入深度递归,造成stack溢出,所以这次用广度优先搜索写了一下,算法中用到了队列数据结构,平台是VS2008+opencv2.3.1,对一副二值图进行连通区域标记。
广度优先搜索的伪代码如下:
连通区域标记的代码如下:
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "highgui.h"
#include <math.h>
typedef unsigned long uint32;
typedef unsigned int uint16;
typedef unsigned char uint8;
#define WHITE 1
#define GRAY 2
#define BLACK 3
#define NIL 0
#define INFINITE 255
typedef CvPoint ElemType;
typedef struct Queue
{
ElemType* data;
int front;
int rear;
int Qsize;
}Queue;
//初始化通过参数传进来创建队列的大小
bool i