获取连通域 - GetConnectedComponents
函数简介
获取图像中的连通域信息。连通域是指图像中具有相同或相似颜色值的相邻像素组成的区域。此函数可以识别图像中的连通区域并返回其坐标信息。
接口名称
GetConnectedComponents
DLL调用
long GetConnectedComponents(long instance, long ptr, string points, int tolerance)
参数说明
参数名 | 类型 | 说明 |
---|---|---|
instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
ptr | 长整数型 | 图像指针,由图像处理函数返回 |
points | 字符串 | 连通域点数组,格式为JSON,如[{"x":10,"y":10},{"x":20,"y":20}] |
tolerance | 整数型 | 连通域阈值,用于判断像素是否属于同一连通域 |
示例
// 加载图像并获取连通域
long image = LoadImage(ola, "D:\\test\\image.png");
if (image != 0) {
char points[4096]; // 足够大的缓冲区存储结果
int tolerance = 10; // 设置连通域阈值
long result = GetConnectedComponents(ola, image, points, tolerance);
ShowImage(result);
FreeImagePtr(ola, result);
FreeImagePtr(ola, image);
}
// 对截图进行连通域分析
long screen = GetScreenDataPtr(ola, 0, 0, 1920, 1080);
if (screen != 0) {
char points[8192]; // 更大的缓冲区用于复杂图像
int tolerance = 5; // 较小的阈值,更精确的连通域
if (GetConnectedComponents(ola, screen, points, tolerance) == 1) {
printf("屏幕截图连通域分析完成\n");
// 处理连通域数据
}
FreeImagePtr(ola, screen);
}
返回值
长整数型:
OLAImage对象的地址
注意事项
- 返回的连通域为二值化图像,后续可以用做蒙版进行高级操作
- tolerance参数影响连通域的识别精度,值越小连通域越精确
- 建议为points参数分配足够大的缓冲区以存储所有连通域数据
- 连通域坐标基于图像坐标系,原点在左上角
- 此函数适用于图像分割、目标检测等应用场景