generalizedHoughTransform.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. @file generalizedHoughTransform.cpp
  3. @author Markus Heck
  4. @brief Detects an object, given by a template, in an image using GeneralizedHoughBallard and GeneralizedHoughGuil.
  5. */
  6. #include "opencv2/highgui.hpp"
  7. #include "opencv2/imgproc.hpp"
  8. using namespace cv;
  9. using namespace std;
  10. int main() {
  11. //! [generalized-hough-transform-load-and-setup]
  12. // load source image and grayscale template
  13. samples::addSamplesDataSearchSubDirectory("doc/tutorials/imgproc/generalized_hough_ballard_guil");
  14. Mat image = imread(samples::findFile("images/generalized_hough_mini_image.jpg"));
  15. Mat templ = imread(samples::findFile("images/generalized_hough_mini_template.jpg"), IMREAD_GRAYSCALE);
  16. // create grayscale image
  17. Mat grayImage;
  18. cvtColor(image, grayImage, COLOR_RGB2GRAY);
  19. // create variable for location, scale and rotation of detected templates
  20. vector<Vec4f> positionBallard, positionGuil;
  21. // template width and height
  22. int w = templ.cols;
  23. int h = templ.rows;
  24. //! [generalized-hough-transform-load-and-setup]
  25. //! [generalized-hough-transform-setup-parameters]
  26. // create ballard and set options
  27. Ptr<GeneralizedHoughBallard> ballard = createGeneralizedHoughBallard();
  28. ballard->setMinDist(10);
  29. ballard->setLevels(360);
  30. ballard->setDp(2);
  31. ballard->setMaxBufferSize(1000);
  32. ballard->setVotesThreshold(40);
  33. ballard->setCannyLowThresh(30);
  34. ballard->setCannyHighThresh(110);
  35. ballard->setTemplate(templ);
  36. // create guil and set options
  37. Ptr<GeneralizedHoughGuil> guil = createGeneralizedHoughGuil();
  38. guil->setMinDist(10);
  39. guil->setLevels(360);
  40. guil->setDp(3);
  41. guil->setMaxBufferSize(1000);
  42. guil->setMinAngle(0);
  43. guil->setMaxAngle(360);
  44. guil->setAngleStep(1);
  45. guil->setAngleThresh(1500);
  46. guil->setMinScale(0.5);
  47. guil->setMaxScale(2.0);
  48. guil->setScaleStep(0.05);
  49. guil->setScaleThresh(50);
  50. guil->setPosThresh(10);
  51. guil->setCannyLowThresh(30);
  52. guil->setCannyHighThresh(110);
  53. guil->setTemplate(templ);
  54. //! [generalized-hough-transform-setup-parameters]
  55. //! [generalized-hough-transform-run]
  56. // execute ballard detection
  57. ballard->detect(grayImage, positionBallard);
  58. // execute guil detection
  59. guil->detect(grayImage, positionGuil);
  60. //! [generalized-hough-transform-run]
  61. //! [generalized-hough-transform-draw-results]
  62. // draw ballard
  63. for (vector<Vec4f>::iterator iter = positionBallard.begin(); iter != positionBallard.end(); ++iter) {
  64. RotatedRect rRect = RotatedRect(Point2f((*iter)[0], (*iter)[1]),
  65. Size2f(w * (*iter)[2], h * (*iter)[2]),
  66. (*iter)[3]);
  67. Point2f vertices[4];
  68. rRect.points(vertices);
  69. for (int i = 0; i < 4; i++)
  70. line(image, vertices[i], vertices[(i + 1) % 4], Scalar(255, 0, 0), 6);
  71. }
  72. // draw guil
  73. for (vector<Vec4f>::iterator iter = positionGuil.begin(); iter != positionGuil.end(); ++iter) {
  74. RotatedRect rRect = RotatedRect(Point2f((*iter)[0], (*iter)[1]),
  75. Size2f(w * (*iter)[2], h * (*iter)[2]),
  76. (*iter)[3]);
  77. Point2f vertices[4];
  78. rRect.points(vertices);
  79. for (int i = 0; i < 4; i++)
  80. line(image, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0), 2);
  81. }
  82. imshow("result_img", image);
  83. waitKey();
  84. //! [generalized-hough-transform-draw-results]
  85. return EXIT_SUCCESS;
  86. }