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




边栏推荐
- 5G VoNR+之IMS Data Channel概念
- DBSync新增对MongoDB、ES的支持
- Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
- torch optimizer小解析
- If you‘re running pod install manually, make sure flutter pub get is executed first.
- Oracle - views and sequences
- 《二》标签
- U++ game learning notes
- 01机器学习相关规定
- JS variable case output user name
猜你喜欢

U++ 元数据说明符 学习笔记
![Local tool [Navicat] connects to remote [MySQL] operation](/img/e8/a7533bac4a70ab5aa3fe15f9b0fcb0.jpg)
Local tool [Navicat] connects to remote [MySQL] operation

JS variable plus

《二》标签

R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators

【问道】编译原理

【二叉树】二叉树寻路

ThinkPHP关联预载入with

Field data acquisition and edge calculation scheme of CNC machine tools

LabVIEW在打开一个新的引用,提示内存已满
随机推荐
Monitoring cannot be started after Oracle modifies the computer name
Why is the salary of test and development so high?
Canteen user dish relationship system (C language course design)
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
[hand torn STL] list
How does vscade use the built-in browser?
DBSync新增对MongoDB、ES的支持
Field data acquisition and edge calculation scheme of CNC machine tools
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
Comparison between thread and runnable in creating threads
IMS data channel concept of 5g vonr+
If you ask me about R code debugging, I will tell you head, STR, help
File upload vulnerability summary
offer如何选择该考虑哪些因素
Techniques d'utilisation de sublime
JS 的 try catch finally 中 return 的执行顺序
National meteorological data / rainfall distribution data / solar radiation data /npp net primary productivity data / vegetation coverage data
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)