当前位置:网站首页>【opencv】图像形态学操作-opencv标记不同连通域的位置
【opencv】图像形态学操作-opencv标记不同连通域的位置
2022-07-06 22:59:00 【GLL_】
【opencv】图像形态学操作-opencv标记不同连通域的位置
/** @overload
@param image the 8-bit single-channel image to be labeled
@param labels destination labeled image
@param stats statistics output for each label, including the background label.
Statistics are accessed via stats(label, COLUMN) where COLUMN is one of
#ConnectedComponentsTypes, selecting the statistic. The data type is CV_32S.
@param centroids centroid output for each label, including the background label. Centroids are
accessed via centroids(label, 0) for x and centroids(label, 1) for y. The data type CV_64F.
@param connectivity 8 or 4 for 8-way or 4-way connectivity respectively
@param ltype output image label type. Currently CV_32S and CV_16U are supported.
*/
CV_EXPORTS_W int connectedComponentsWithStats(InputArray image, OutputArray labels,
OutputArray stats, OutputArray centroids,
int connectivity = 8, int ltype = CV_32S); 
system("color F0");
Mat img = imread("D:/image/ml2.png");
if (img.empty()) {
cout << "img is empty." << endl;
return;
}
//imshow("srcImg", img);
Mat rice, riceBW;
cvtColor(img, rice, COLOR_BGR2GRAY);
imshow("rice", rice);
threshold(rice, riceBW, 50, 255, THRESH_BINARY);
imshow("riceBW", riceBW);
RNG rng(10086);
Mat out, stats, centroids;
//
int number = connectedComponentsWithStats(riceBW, out, stats, centroids, 8, CV_16U);
vector<Vec3b> colors;
for (int i = 0; i < number; i++) {
Vec3b vec3 = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
colors.push_back(vec3);
}
Mat result = Mat::zeros(rice.size(), img.type());
int w = result.cols;
int h = result.rows;
for (int i = 1; i < number; i++) {
int center_x = centroids.at<double>(i, 0);
int center_y = centroids.at<double>(i, 1);
int x = stats.at<int>(i, CC_STAT_LEFT);
int y = stats.at<int>(i, CC_STAT_TOP);
int w = stats.at<int>(i, CC_STAT_WIDTH);
int h = stats.at<int>(i, CC_STAT_HEIGHT);
int area = stats.at<int>(i, CC_STAT_AREA);
circle(img, Point(center_x, center_y), 2, Scalar(0, 255, 0), 2, 8, 0);
Rect rect(x, y, w, h);
rectangle(img, rect, colors[i], 1, 8, 0);
putText(img, format("%d", i), Point(center_x, center_y),
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 1);
cout << "number: " << i << ",area: " << area << endl;
}
imshow("result:", img);
waitKey(0);结果:




边栏推荐
- JS variable case
- [practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
- How to choose an offer and what factors should be considered
- 5G VoNR+之IMS Data Channel概念
- 【最佳网页宽度及其实现】「建议收藏」
- 《五》表格
- Section 1: (3) logic chip process substrate selection
- STM32 encapsulates the one key configuration function of esp8266: realize the switching between AP mode and sta mode, and the creation of server and client
- AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
- CentOS 7.9安装Oracle 21c历险记
猜你喜欢

Meow, come, come: do you really know if, if else

动态生成表格

ThinkPHP关联预载入with

《二》标签

Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
![[Yugong series] go teaching course 005 variables in July 2022](/img/29/2bb30443e1e418556b5e08932f75b4.png)
[Yugong series] go teaching course 005 variables in July 2022

C语言中函数指针与指针函数

Salesforce 容器化 ISV 场景下的软件供应链安全落地实践

offer如何选择该考虑哪些因素

A simple and beautiful regression table is produced in one line of code~
随机推荐
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
U++4 接口 学习笔记
Inventory host list in ansible (I wish you countless flowers and romance)
Common Oracle SQL statements
Leetcode(417)——太平洋大西洋水流问题
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
Read of shell internal value command
Using thread class and runnable interface to realize the difference between multithreading
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
01机器学习相关规定
5G VoNR+之IMS Data Channel概念
NiO related knowledge points (I)
Two methods of chromosome coordinate sequencing
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
【736. Lisp 语法解析】
How to package the parsed Excel data into objects and write this object set into the database?
腾讯云数据库公有云市场稳居TOP 2!
STM32F103实现IAP在线升级应用程序
JS input and output