当前位置:网站首页>[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 :




边栏推荐
- 使用Thread类和Runnable接口实现多线程的区别
- Using thread class and runnable interface to realize the difference between multithreading
- ServiceMesh主要解决的三大痛点
- HarmonyOS第四次培训
- Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
- Talk about the importance of making it clear
- 【愚公系列】2022年7月 Go教学课程 005-变量
- 2039: [Bluebridge cup 2022 preliminaries] Li Bai's enhanced version (dynamic planning)
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- 【二叉树】二叉树寻路
猜你喜欢

Leetcode(417)——太平洋大西洋水流问题

Weebly mobile website editor mobile browsing New Era

SQL injection - secondary injection and multi statement injection

【问道】编译原理

Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.

The sooner you understand the four rules of life, the more blessed you will be

【二叉树】二叉树寻路

指针与数组在函数中输入实现逆序输出

Pointer and array are input in function to realize reverse order output

【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
随机推荐
动态生成表格
JS input and output
带你遨游银河系的 10 种分布式数据库
Techniques d'utilisation de sublime
window定时计划任务
Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
np.random.shuffle与np.swapaxis或transpose一起时要慎用
Comparison between thread and runnable in creating threads
Leetcode longest public prefix
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
【opencv】图像形态学操作-opencv标记不同连通域的位置
最全常用高数公式
sublime使用技巧
U++ 元数据说明符 学习笔记
【最佳网页宽度及其实现】「建议收藏」
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
Time complexity & space complexity
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
SQL injection - secondary injection and multi statement injection
Analysis -- MySQL statement execution process & MySQL architecture