资源简介
有main函数的C++源码,直接可运行,自己写的二值图像八连通区域,对应matlab中的bwlabel
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef unsigned char uchar;
using namespace std;
/// 图算法相关
typedef struct EDGE{
int start end;
EDGE()
{
start = 0;
end = 0;
}
EDGE(int a int b)
{
start = a;
end = b;
}
}edge;
typedef struct GraphicNode{
int vertex; //顶点数据信息
GraphicNode *nextNode;
}graphNode;
// 依据图连通域分量合并错分的runs,并重新赋值,返回实际的连通域个数
int RunlabelDeleteError(int *runlabel int *outlabel int numRuns vector > sameLabel)
{
for (int i = 0; i < sameLabel.size(); i++)
{
if (sameLabel[i].size() < 2)
{
continue;
}
for (int j = 0; j < sameLabel[i].size(); j++)
{
for (int k = 0; k < numRuns; k++)
{
if (runlabel[k] == sameLabel[i][j])
{
runlabel[k] = sameLabel[i][0];
}
}
}
}
// label 去重,排序
vector temp;
temp.push_back(runlabel[0]);
for (int i = 1; i < numRuns; i++)
{
bool flag = false;
for (int j = 0; j < temp.size(); j++)
{
flag |= runlabel[i] == temp[j];
}
if (!flag)
{
temp.push_back(runlabel[i]);
}
}
int *data = new int[temp.size()];
int *index = new int[temp.size()];
for (int i = 0; i < temp.size(); i++)
{
data[i] = temp[i];
index[i] = i;
}
for (int i = 0; i < temp.size() - 1; i++)
{
for (int j = i + 1; j < temp.size(); j++)
{
if (data[i] > data[j])
{
int temp =data[i];
data[i] = data[j];
data[j] = temp;
temp = index[i];
index[i] = index[j];
index[j] = temp;
}
}
}
for (int i = 0; i < numRuns; i++)
{
int index = 0;
for (int j = 0; j < temp.size(); j++)
{
if (runlabel[i] == data[j])
{
index = j;
break;
}
}
outlabel[i] = index + 1;
}
delete[] data;
delete[] index;
return temp.size();
}
void CreateGpaphic(vector pt GraphicNode *G)
{
for (int i = 0; i < pt.size(); i++)
{
int start = pt[i].start;
int end = pt[i].end;
// create a new node
GraphicNode *node *ptr;
node = new GraphicNode();
node->vertex = end;
node->nextNode = NULL;
ptr = &(G[start]);
while (ptr->nextNode != NULL)
{
ptr = ptr->nextNode;
}
ptr->nextNode = node;
}
}
// 图深度优先搜索算法
void dfn(GraphicNode *G bool *visited int current vector &index)
{
//printf(“vertex = %d\n“ current);
index.push_back(current);
visited[current] = true;
GraphicNode *p = &G[current];
while (p != NULL)
{
if (!visited[p->vertex])
{
dfn(G visited p->vertex index);
}
p = p->nextNode;
}
}
// 图连通域分量
void DfnComponent(vector pt vector > &outdata)
{
// 统计节点个数 以空间消耗替代时间消耗
int nodeNum = max(pt[0].start pt[0].end);
int startIndex = min(pt[0].start pt[0].end);
for (int i = 1; i < pt.size(); i++)
{
if (pt[i].start > nodeNum)
{
nodeNu
评论
共有 条评论