当前位置:网站首页>[opencv] image morphological operation opencv marks the positions of different connected domains
[opencv] image morphological operation opencv marks the positions of different connected domains
2022-07-07 05:12:00 【GLL_】
【opencv】 Image morphology operation -opencv Mark the location of different connected domains
/** @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);
result :
边栏推荐
- The sooner you understand the four rules of life, the more blessed you will be
- HarmonyOS第四次培训
- If you‘re running pod install manually, make sure flutter pub get is executed first.
- npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
- Weebly移动端网站编辑器 手机浏览新时代
- Error: No named parameter with the name ‘foregroundColor‘
- 为什么很多人对技术债务产生误解
- Ansible overview and module explanation (you just passed today, but yesterday came to your face)
- Monitoring cannot be started after Oracle modifies the computer name
- Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
猜你喜欢
拿到PMP认证带来什么改变?
The sooner you understand the four rules of life, the more blessed you will be
装饰器基础学习02
[email protected] Mapping relatio"/>
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
U++ game learning notes
[Yugong series] go teaching course 005 variables in July 2022
U++ 游戏类 学习笔记
Weebly mobile website editor mobile browsing New Era
【问道】编译原理
最长回文子串(动态规划)
随机推荐
DBSync新增对MongoDB、ES的支持
与利润无关的背包问题(深度优先搜索)
JS variable case
《四》表单
动态生成表格
使用知云阅读器翻译统计遗传学书籍
The sooner you understand the four rules of life, the more blessed you will be
If you‘re running pod install manually, make sure flutter pub get is executed first.
Common Oracle SQL statements
SQL injection HTTP header injection
app内嵌h5---iphone软键盘遮挡输入文字
QT控件样式系列(一)之QSlider
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
【最佳网页宽度及其实现】「建议收藏」
批量归一化(标准化)处理
局部变量的数组初始化问题
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
一个酷酷的“幽灵”控制台工具
ScheduledExecutorService定时器
AOSP ~Binder 通信原理 (一) - 概要