Guest User

Untitled

a guest
Dec 11th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include "opencv2/opencv.hpp"
  2. #include "opencv2/xfeatures2d.hpp"
  3. #include <istream>
  4. #include <string>
  5. #include <stdint.h>
  6.  
  7. using namespace std;
  8. using namespace cv;
  9.  
  10. /*
  11. بسم الله الرحمن الرحيم
  12. */
  13. /* TESTED CONNECTED COMPONENT ANALYSIS OPENCV WRAPPER */
  14.  
  15. class cca_opencv_wrapper{
  16.  
  17. private:
  18.  
  19. Mat inp;
  20. Mat labels;
  21. Mat stats;
  22. Mat centroids;
  23.  
  24. public:
  25.  
  26. cca_opencv_wrapper( Mat input )
  27. {
  28. inp = input.clone();
  29. labels = Mat(input.size(), CV_16U);
  30. if(inp.channels() == 3)
  31. cvtColor(inp, inp, cv::COLOR_BGR2GRAY);
  32. connectedComponentsWithStats(inp, labels, stats, centroids, 8, CV_16U);
  33. }
  34.  
  35. uint32_t get_most_left_of_centroid(int centroid_index)
  36. {
  37. return stats.at<uint32_t>(centroid_index, CC_STAT_LEFT);
  38. }
  39.  
  40. uint32_t get_most_top_of_centroid(int centroid_index)
  41. {
  42. return stats.at<uint32_t>(centroid_index, CC_STAT_TOP);
  43. }
  44.  
  45. uint32_t get_width_of_centroid(int centroid_index)
  46. {
  47. return stats.at<uint32_t>(centroid_index, CC_STAT_WIDTH);
  48. }
  49.  
  50. uint32_t get_heigth_of_centroid(int centroid_index)
  51. {
  52. return stats.at<uint32_t>(centroid_index, CC_STAT_HEIGHT);
  53. }
  54.  
  55. uint32_t get_area_of_component(int centroid_index)
  56. {
  57. return stats.at<uint32_t>(centroid_index, CC_STAT_AREA);
  58. }
  59.  
  60. uint16_t get_label_of_pixel(int x_pos, int y_pos)
  61. {
  62. return labels.at<uint16_t>(x_pos, y_pos);
  63. }
  64.  
  65. uint8_t get_centroid_x(int index)
  66. {
  67. return centroids.at<uint8_t>(index, 0);
  68. }
  69.  
  70. uint8_t get_centroid_y(int index)
  71. {
  72. return centroids.at<uint8_t>(index, 1);
  73. }
  74.  
  75. size_t get_connected_component_count()
  76. {
  77. return centroids.rows;
  78. }
  79.  
  80. uint32_t get_max_component_area()
  81. {
  82. uint32_t max_ = 0;
  83. for(auto i = 0; i < get_connected_component_count(); i++)
  84. {
  85. if(get_area_of_component(i) > max_)
  86. max_ = get_area_of_component(i);
  87. }
  88. return max_;
  89. }
  90.  
  91. uint32_t get_min_component_area()
  92. {
  93. uint32_t min_ = inp.rows * inp.cols;
  94. for(auto i = 0; i < get_connected_component_count(); i++)
  95. {
  96. if(get_area_of_component(i) < min_)
  97. min_ = get_area_of_component(i);
  98. }
  99. return min_;
  100. }
  101.  
  102. float get_average_component_area()
  103. {
  104. uint32_t sum = 0;
  105. for(auto i = 0; i < get_connected_component_count(); i++)
  106. {
  107. sum = sum + get_area_of_component(i);
  108. }
  109. return ((float) sum) / (float) get_connected_component_count();
  110. }
  111.  
  112. float get_standard_deviation_of_connected_compenent_areas()
  113. {
  114. uint32_t sum = 0;
  115. for(auto i = 0; i < get_connected_component_count(); i++)
  116. {
  117. sum = sum + abs( get_area_of_component(i) - get_average_component_area() );
  118. }
  119. return ((float) sum) / (float) get_connected_component_count();
  120. }
  121.  
  122. };
Add Comment
Please, Sign In to add comment