轮廓计数和标记

Contours counting and labeling

本文关键字:轮廓      更新时间:2023-10-16

我正试图开发一个程序,将contourAreas的数量作为大小的函数进行计数,并将其显示给用户。

我可以为所有区域创建drawContours,但我想在每个contouArea下添加一个文本标签,并在那里显示相应的大小。

这应该会让您开始。要浏览所有轮廓,必须使用下面带有h_next的for循环。如果你想了解更多,我真的推荐加里·布拉德斯基的书《开放式学习》。这本书中有一些关于轮廓发现的好例子。

CvMemStorage* contour_storage = cvCreateMemStorage(0);
CvSeq* contours;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.6f, 0.6f, 0, 2);
cvFindContours(sourceImage, contour_storage, &contours, sizeof (CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
for (CvSeq* d = contours; d != NULL; d = d->h_next) {
 CvRect iconBox = cvBoundingRect(d, 0);
 CvPoint center = cvPoint(iconBox.x + (iconBox.width / 2), iconBox.y + (iconBox.height / 2));
 int area = abs(cvContourArea(d, CV_WHOLE_SEQ));
 cvPutText(sourceImage,"area", center, &font, CV_RGB(255, 255, 255));
}

您可以使用打开的cv函数putText

我想你知道如何检索你的轮廓中心的位置,不是吗?