OpenCV-bwLabel-实现图像连通组件标记与分析
OpenCV实现图像连通组件标记与分析- matlab bwLabel;
code:

#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; 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)); vector<Vec3b> colors(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<int>(row, col); if (label == 0) continue; dst.at<Vec3b>(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)); vector<Vec3b> colors(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<int>(row, col); if (label == 0) continue; dst.at<Vec3b>(row, col) = colors[label]; } } for (int i = 1; i < num_labels; i++) { Vec2b pt = centroids.at<Vec2d>(i, 0); int x = stats.at<int>(i, CC_STAT_LEFT); int y = stats.at<int>(i, CC_STAT_TOP); int width = stats.at<int>(i, CC_STAT_WIDTH); int height = stats.at<int>(i, CC_STAT_HEIGHT); int area = stats.at<int>(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); }
参考
End
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2016-06-12 使用matlab和ISE 创建并仿真ROM IP核