PageRenderTime 102ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/contours.cpp

https://gitlab.com/vyablokov/opencvtest
C++ | 40 lines | 32 code | 8 blank | 0 comment | 4 complexity | 9ecfc8357fd418a5b7eaa9049caf993e MD5 | raw file
  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv/cv.hpp"
  3. #include <iostream>
  4. int main(int argc, char* argv[])
  5. {
  6. if( argc < 2 )
  7. {
  8. std::cout << " Usage: ./contours <img1>" << std::endl;
  9. return -1;
  10. }
  11. IplImage *img = cvLoadImage(argv[1]);
  12. IplImage *bin_img = cvCreateImage(cvGetSize(img), 8, 1);
  13. cvCvtColor(img, bin_img, CV_RGB2GRAY);
  14. cvSmooth(bin_img, bin_img, CV_GAUSSIAN);
  15. cvCanny(bin_img, bin_img, 50, 150, 3);
  16. CvMemStorage *storage = cvCreateMemStorage(0);
  17. CvContourScanner traverse = cvStartFindContours(bin_img, storage, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
  18. CvSeq *mContour = 0;
  19. for (CvSeq *contour = cvFindNextContour(traverse); contour; contour = cvFindNextContour(traverse))
  20. {
  21. CvRect rect = cvContourBoundingRect(contour);
  22. if ((rect.width < 20) && (rect.height < 20))
  23. cvRectangle(img, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height), CV_RGB(255, 0, 0));
  24. }
  25. cvEndFindContours(&traverse);
  26. cvReleaseMemStorage(&storage);
  27. cvSaveImage("res.jpg", img);
  28. cvSaveImage("contours.png", bin_img);
  29. cvReleaseImage(&bin_img);
  30. cvReleaseImage(&img);
  31. return 0;
  32. }