连通组件标记算法matlab,OpenCV-bwLabel-实现图像连通组件标记与分析

#include #include

using namespacecv;using namespacestd;

RNG rng(12345);void connected_component_demo(Mat &image);void connected_component_stats_demo(Mat &image);int main(int argc, char**argv)

{

Mat src= imread("./src/rice.png");if (src.empty()) printf("could not load image...\n");

imshow("input", src);

connected_component_stats_demo(src);

connected_component_demo(src);

waitKey(0);return 0;

}void connected_component_demo(Mat &image)

{//binarization.

Mat gray, binary;

cvtColor(image, gray, COLOR_BGR2GRAY);

threshold(gray, binary,0, 255, THRESH_BINARY |THRESH_OTSU);//morphology.

Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));

morphologyEx(binary, binary, MORPH_OPEN, k);

morphologyEx(binary, binary, MORPH_CLOSE, k);

imshow("binary", binary);

imwrite("./ccla_binary.png", binary);

Mat labels=Mat::zeros(image.size(), CV_32S);int num_labels = connectedComponents(binary, labels, 8, CV_32S);

printf("total labels: %d\n", (num_labels-1));

vectorcolors(num_labels);//background color.

colors[0] = Vec3b(0, 0, 0);//object color.

for (int i = 1; i < num_labels; i++)

{

colors[i]= Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));

}//render result.

Mat dst =Mat::zeros(image.size(), image.type());int w =image.cols;int h =image.rows;for (int row = 0; row < h; row++)

{for (int col = 0; col < w; col++)

{int label = labels.at(row, col);if (label == 0) continue;

dst.at(row, col) =colors[label];

}

}

imshow("ccla_demo", dst);

imwrite("./ccla_dst.png", dst);

}void connected_component_stats_demo(Mat &image)

{//binarization.

Mat gray, binary;

cvtColor(image, gray, COLOR_BGR2GRAY);

threshold(gray, binary,0, 255, THRESH_BINARY |THRESH_OTSU);//morphology.

Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));

morphologyEx(binary, binary, MORPH_OPEN, k);

morphologyEx(binary, binary, MORPH_CLOSE, k);

imshow("binary", binary);

Mat labels=Mat::zeros(image.size(), CV_32S);

Mat stats, centroids;int num_labels = connectedComponentsWithStats(binary, labels, stats, centroids, 8, 4);

printf("total labels: %d\n", (num_labels-1));

vectorcolors(num_labels);//background color.

colors[0] = Vec3b(0, 0, 0);//object color.

int b = rng.uniform(0, 256);int g = rng.uniform(0, 256);int r = rng.uniform(0, 256);for (int i = 1; i < num_labels; i++)

{

colors[i]= Vec3b(0, 255, 0);

}//render result.

Mat dst =Mat::zeros(image.size(), image.type());int w =image.cols;int h =image.rows;for (int row = 0; row < h; row++)

{for (int col = 0; col < w; col++)

{int label = labels.at(row, col);if (label == 0) continue;

dst.at(row, col) =colors[label];

}

}for (int i = 1; i < num_labels; i++)

{

Vec2b pt= centroids.at(i, 0);int x = stats.at(i, CC_STAT_LEFT);int y = stats.at(i, CC_STAT_TOP);int width = stats.at(i, CC_STAT_WIDTH);int height = stats.at(i, CC_STAT_HEIGHT);int area = stats.at(i, CC_STAT_AREA);

printf("area: %d, center point(%.2f, %.2f)\n", area, pt[0], pt[1]);

circle(dst, Point(pt[0], pt[1]), 2, Scalar(0, 0, 255), -1, 8, 0);

rectangle(dst, Rect(x, y, width, height), Scalar(255, 0, 255), 1, 8, 0);

}

imshow("ccla-demo", dst);

imwrite("ccla_stats_dst.png", dst);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值