当前位置:网站首页>【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);结果:




边栏推荐
- Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
- Techniques d'utilisation de sublime
- Error: No named parameter with the name ‘foregroundColor‘
- 【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
- Two methods of chromosome coordinate sequencing
- 5G VoNR+之IMS Data Channel概念
- U++ game learning notes
- How to package the parsed Excel data into objects and write this object set into the database?
- np.random.shuffle与np.swapaxis或transpose一起时要慎用
- The execution order of return in JS' try catch finally
猜你喜欢

LabVIEW在打开一个新的引用,提示内存已满

A row of code r shows the table of Cox regression model

A line of R code draws the population pyramid

Function pointer and pointer function in C language
[email protected]映射关系问题"/>接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题

为什么很多人对技术债务产生误解

Markdown editor

Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market

Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)

ThinkPHP关联预载入with
随机推荐
Monitoring cannot be started after Oracle modifies the computer name
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
Clickhouse (03) how to install and deploy Clickhouse
A line of R code draws the population pyramid
np.random.shuffle与np.swapaxis或transpose一起时要慎用
Thread和Runnable创建线程的方式对比
What is Web3
Redis如何实现多可用区?
《四》表单
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
Two methods of chromosome coordinate sequencing
Basic knowledge of road loss of 3GPP channel model
The most complete learning rate adjustment strategy in history LR_ scheduler
How does vscade use the built-in browser?
Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
File upload vulnerability summary
01机器学习相关规定
一文搞懂常见的网络I/O模型
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)