0

I'm trying to develop a program that counts the number of contourAreas as function of size and display it to the user.

I was able to create drawContours to all the areas but I would like to add a text label under each contouArea and display there respective size.

0

2 Answers 2

0

This should get you started. To go through all the contours you have to use the for loop with h_next below. If you want to find out more I really recommend Gary Bradski's book Learning OpenCv. THere are some great examples on contour finding in the book.

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));
}
1
  • Hi diip_thomas, Thank you very much for your reply and advice. I tried you bite of code and it works. :) I'll buy the book as I really need to learn this library.
    – antrofite_
    Aug 31, 2012 at 20:24
0

You can use the open cv function putText

I guess that you know how to retrieve the position of your contour center, isn' it ?

1
  • Hi abarral, Thank you for replying.Unfortunately English isn't my first language and I think my question isn't well formulated. Sorry. What I'm trying to achieve is to measure each contourArea and display with a surrounding circle there respective location and under each circle there respective area and a text label. My questions are the following: How I can display individually a contourArea (countours[0][1]???) and there respective circle using (drawContour)? How I can add a text label under each circle? I had e a view in Opencv Help for puText but I'm not sure how to implement?
    – antrofite_
    Aug 27, 2012 at 15:15

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.