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




边栏推荐
- 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
- [Yugong series] go teaching course 005 variables in July 2022
- How to design API interface and realize unified format return?
- Error: No named parameter with the name ‘foregroundColor‘
- Appium practice | make the test faster, more stable and more reliable (I): slice test
- 《四》表单
- pmp真的有用吗?
- 3.基金的类型
- ThinkPHP关联预载入with
- STM32 system timer flashing LED
猜你喜欢

Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“

装饰器基础学习02

HarmonyOS第四次培训

Sublime tips

sublime使用技巧

U++ game learning notes

Decorator basic learning 02

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

U++ metadata specifier learning notes

If you‘re running pod install manually, make sure flutter pub get is executed first.
随机推荐
Why do many people misunderstand technical debt
【PHP SPL笔记】
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
Longest common subsequence (LCS) (dynamic programming, recursive)
Function pointer and pointer function in C language
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
JS variable
If you ask me about R code debugging, I will tell you head, STR, help
指针与数组在函数中输入实现逆序输出
Pointer and array are input in function to realize reverse order output
Servicemesh mainly solves three pain points
Leetcode(417)——太平洋大西洋水流问题
模拟线程通信
JS variable case
R descriptive statistics and hypothesis testing
C语言中函数指针与指针函数