当前位置:网站首页>[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 :
边栏推荐
- Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
- Longest non descent subsequence (LIS) (dynamic programming)
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- [ArcGIS tutorial] thematic map production - population density distribution map - population density analysis
- PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
- Error: No named parameter with the name ‘foregroundColor‘
- 接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
- Pointer and array are input in function to realize reverse order output
- AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
- 谈谈讲清楚这件事的重要性
猜你喜欢
Leetcode(417)——太平洋大西洋水流问题
Time complexity & space complexity
Leetcode(46)——全排列
ThinkPHP关联预载入with
动态生成表格
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
JS variable plus
LabVIEW在打开一个新的引用,提示内存已满
Techniques d'utilisation de sublime
随机推荐
【PHP SPL笔记】
PMP证书有没有必要续期?
《四》表单
Longest palindrome substring (dynamic programming)
精彩速递|腾讯云数据库6月刊
Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
JS input and output
y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
Talk about the importance of making it clear
Dynamically generate tables
Sublime tips
Inventory host list in ansible (I wish you countless flowers and romance)
JS variable case output user name
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
使用知云阅读器翻译统计遗传学书籍
Error: No named parameter with the name ‘foregroundColor‘
U++ 元数据说明符 学习笔记
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
How to design API interface and realize unified format return?