Advertisement
Guest User

a

a guest
Dec 11th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. struct WEAK_LEARNER
  2. {
  3. int feature; //can be 0 or 1 (indicates the column number in feature vector X
  4. double threshold;
  5. int class_label;
  6. double error;
  7.  
  8. int classifiy(Mat X)//X is a row vector having all the features
  9. {
  10. if (X.at<double>(feature) < threshold)
  11. return class_label;
  12. else
  13. return -class_label;
  14. }
  15. };
  16.  
  17. struct CLASSIFIER
  18. {
  19. int T;
  20. double alphas[1000];
  21.  
  22. WEAK_LEARNER hs[1000];
  23.  
  24. int classify(Mat X) {//X is a row vector having all the features
  25. double s = 0.;
  26.  
  27. for (int t = 0; t < T; t++)
  28. {
  29. s += alphas[t] * (double)(hs[t].classifiy(X));
  30. }
  31.  
  32. if (s > 0)
  33. return 1;
  34. else
  35. return -1;
  36. }
  37. };
  38.  
  39. void drawBoundary(Mat *img, CLASSIFIER classifier)
  40. {
  41. const int nr_of_features = 2;
  42. for (int i = 0; i < (*img).rows; i++)
  43. for (int j = 0; j < (*img).cols; j++)
  44. {
  45. //pixel is white => color it depending on its class
  46. if ((*img).at<Vec3b>(i, j) == Vec3b{ 255, 255, 255 })
  47. {
  48. Mat row_X(1, nr_of_features, CV_64FC1);
  49. row_X.at<double>(0, 0) = i; //y coordinate
  50. row_X.at<double>(0, 1) = j; //x coordinate
  51.  
  52. //blue = -1, red = 1
  53. if (classifier.classify(row_X) < 0)
  54. {
  55. //blue class
  56. (*img).at<Vec3b>(i, j) = Vec3b{ 255, 255, 0 };
  57. }
  58. else
  59. {
  60. //red class
  61. (*img).at<Vec3b>(i, j) = Vec3b{ 0, 255, 255 };
  62. }
  63. }
  64. }
  65. }
  66.  
  67. WEAK_LEARNER findWeakLearner(Mat X, Mat Y, Mat W, int width, int height)
  68. {
  69. WEAK_LEARNER best_h;
  70. double best_err = 1;
  71.  
  72. for (int j = 0; j < X.cols; j++)
  73. {
  74. // X n x {y, x} - first column = y coordinate, second column = x coordinate
  75. int size = j == 0 ? height : width;
  76.  
  77. for (int threshold = 0; threshold < size; threshold++)
  78. {
  79. for (int class_label = -1; class_label <= 1; class_label += 2) // class_label can be {-1, +1}
  80. {
  81. double e = 0;
  82.  
  83. for (int i = 0; i < X.rows; i++)
  84. {
  85. double z;
  86. if (X.at<double>(i, j) < (double)(threshold))
  87. {
  88. z = class_label;
  89. }
  90. else
  91. {
  92. z = -class_label;
  93. }
  94.  
  95. if (z * Y.at<double>(i, 0) < 0)
  96. {
  97. e += W.at<double>(i, 0);
  98. }
  99. }
  100.  
  101. if (e < best_err)
  102. {
  103. best_err = e;
  104. best_h = WEAK_LEARNER{ j, (double)(threshold), class_label, best_err };
  105. }
  106. }
  107. }
  108. }
  109. return best_h;
  110. }
  111.  
  112. CLASSIFIER adaBoost(Mat X, Mat Y, const int T, const int width, const int height)
  113. {
  114.  
  115. //initialise the weight vector with 1/n, where n is the number of examples
  116. Mat W(X.rows, 1, CV_64FC1, double(1. / X.rows)); // weight vector
  117.  
  118. CLASSIFIER classifier;
  119. classifier.T = T;
  120.  
  121. for (int t = 0; t < T; t++)
  122. {
  123. WEAK_LEARNER weakLearner = findWeakLearner(X, Y, W, width, height);
  124.  
  125. double alpha_t = 0.5 * log((1 - weakLearner.error) / weakLearner.error);
  126.  
  127. double s = 0.;
  128.  
  129. for (int i = 0; i < W.rows; i++)
  130. {
  131. W.at<double>(i, 0) *= exp(-alpha_t * Y.at<double>(i, 0) * (double)(weakLearner.classifiy(X.row(i))));
  132.  
  133. s += W.at<double>(i, 0);
  134. }
  135.  
  136. //normalize the weights
  137. for (int i = 0; i < W.rows; i++)
  138. W.at<double>(i, 0) /= s;
  139.  
  140. classifier.alphas[t] = alpha_t;
  141. classifier.hs[t] = weakLearner;
  142. }
  143. return classifier;
  144. }
  145.  
  146. void AdaBoostCaller()
  147. {
  148. char fname[MAX_PATH];
  149. while (openFileDlg(fname))
  150. {
  151. Mat src = imread(fname, CV_LOAD_IMAGE_COLOR);
  152.  
  153. const int nr_elements_feature_vector = 2;
  154.  
  155. Mat X(0, nr_elements_feature_vector, CV_64FC1);
  156. Mat Y(0, 1, CV_64FC1);
  157.  
  158. // read the coordinates: x = [1 x_coordinate y_coordinate]
  159. for (int i = 0; i < src.rows; i++)
  160. for (int j = 0; j < src.cols; j++)
  161. {
  162. //blue: green == 0 && red == 0
  163. if (src.at<Vec3b>(i, j)[1] == 0 && src.at<Vec3b>(i, j)[2] == 0)
  164. {
  165. Mat row_X(1, nr_elements_feature_vector, CV_64FC1);
  166. row_X.at<double>(0, 0) = i; //y coordinate
  167. row_X.at<double>(0, 1) = j; //x coordinate
  168.  
  169. Mat row_Y(1, 1, CV_64FC1);
  170. row_Y.at<double>(0, 0) = -1;
  171.  
  172. X.push_back(row_X);
  173. Y.push_back(row_Y);
  174. }
  175. //red: blue == 0 && green == 0
  176. else if (src.at<Vec3b>(i, j)[0] == 0 && src.at<Vec3b>(i, j)[1] == 0)
  177. {
  178. Mat row_X(1, nr_elements_feature_vector, CV_64FC1);
  179. row_X.at<double>(0, 0) = i; //y coordinate
  180. row_X.at<double>(0, 1) = j; //x coordinate
  181.  
  182. Mat row_Y(1, 1, CV_64FC1);
  183. row_Y.at<double>(0, 0) = 1;
  184.  
  185. X.push_back(row_X);
  186. Y.push_back(row_Y);
  187. }
  188. }
  189.  
  190. CLASSIFIER classifier = adaBoost(X, Y, 200, src.cols, src.rows);
  191.  
  192. drawBoundary(&src, classifier);
  193. cv::imshow("src", src);
  194. cv::waitKey(0);
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement