Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct WEAK_LEARNER
- {
- int feature; //can be 0 or 1 (indicates the column number in feature vector X
- double threshold;
- int class_label;
- double error;
- int classifiy(Mat X)//X is a row vector having all the features
- {
- if (X.at<double>(feature) < threshold)
- return class_label;
- else
- return -class_label;
- }
- };
- struct CLASSIFIER
- {
- int T;
- double alphas[1000];
- WEAK_LEARNER hs[1000];
- int classify(Mat X) {//X is a row vector having all the features
- double s = 0.;
- for (int t = 0; t < T; t++)
- {
- s += alphas[t] * (double)(hs[t].classifiy(X));
- }
- if (s > 0)
- return 1;
- else
- return -1;
- }
- };
- void drawBoundary(Mat *img, CLASSIFIER classifier)
- {
- const int nr_of_features = 2;
- for (int i = 0; i < (*img).rows; i++)
- for (int j = 0; j < (*img).cols; j++)
- {
- //pixel is white => color it depending on its class
- if ((*img).at<Vec3b>(i, j) == Vec3b{ 255, 255, 255 })
- {
- Mat row_X(1, nr_of_features, CV_64FC1);
- row_X.at<double>(0, 0) = i; //y coordinate
- row_X.at<double>(0, 1) = j; //x coordinate
- //blue = -1, red = 1
- if (classifier.classify(row_X) < 0)
- {
- //blue class
- (*img).at<Vec3b>(i, j) = Vec3b{ 255, 255, 0 };
- }
- else
- {
- //red class
- (*img).at<Vec3b>(i, j) = Vec3b{ 0, 255, 255 };
- }
- }
- }
- }
- WEAK_LEARNER findWeakLearner(Mat X, Mat Y, Mat W, int width, int height)
- {
- WEAK_LEARNER best_h;
- double best_err = 1;
- for (int j = 0; j < X.cols; j++)
- {
- // X n x {y, x} - first column = y coordinate, second column = x coordinate
- int size = j == 0 ? height : width;
- for (int threshold = 0; threshold < size; threshold++)
- {
- for (int class_label = -1; class_label <= 1; class_label += 2) // class_label can be {-1, +1}
- {
- double e = 0;
- for (int i = 0; i < X.rows; i++)
- {
- double z;
- if (X.at<double>(i, j) < (double)(threshold))
- {
- z = class_label;
- }
- else
- {
- z = -class_label;
- }
- if (z * Y.at<double>(i, 0) < 0)
- {
- e += W.at<double>(i, 0);
- }
- }
- if (e < best_err)
- {
- best_err = e;
- best_h = WEAK_LEARNER{ j, (double)(threshold), class_label, best_err };
- }
- }
- }
- }
- return best_h;
- }
- CLASSIFIER adaBoost(Mat X, Mat Y, const int T, const int width, const int height)
- {
- //initialise the weight vector with 1/n, where n is the number of examples
- Mat W(X.rows, 1, CV_64FC1, double(1. / X.rows)); // weight vector
- CLASSIFIER classifier;
- classifier.T = T;
- for (int t = 0; t < T; t++)
- {
- WEAK_LEARNER weakLearner = findWeakLearner(X, Y, W, width, height);
- double alpha_t = 0.5 * log((1 - weakLearner.error) / weakLearner.error);
- double s = 0.;
- for (int i = 0; i < W.rows; i++)
- {
- W.at<double>(i, 0) *= exp(-alpha_t * Y.at<double>(i, 0) * (double)(weakLearner.classifiy(X.row(i))));
- s += W.at<double>(i, 0);
- }
- //normalize the weights
- for (int i = 0; i < W.rows; i++)
- W.at<double>(i, 0) /= s;
- classifier.alphas[t] = alpha_t;
- classifier.hs[t] = weakLearner;
- }
- return classifier;
- }
- void AdaBoostCaller()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src = imread(fname, CV_LOAD_IMAGE_COLOR);
- const int nr_elements_feature_vector = 2;
- Mat X(0, nr_elements_feature_vector, CV_64FC1);
- Mat Y(0, 1, CV_64FC1);
- // read the coordinates: x = [1 x_coordinate y_coordinate]
- for (int i = 0; i < src.rows; i++)
- for (int j = 0; j < src.cols; j++)
- {
- //blue: green == 0 && red == 0
- if (src.at<Vec3b>(i, j)[1] == 0 && src.at<Vec3b>(i, j)[2] == 0)
- {
- Mat row_X(1, nr_elements_feature_vector, CV_64FC1);
- row_X.at<double>(0, 0) = i; //y coordinate
- row_X.at<double>(0, 1) = j; //x coordinate
- Mat row_Y(1, 1, CV_64FC1);
- row_Y.at<double>(0, 0) = -1;
- X.push_back(row_X);
- Y.push_back(row_Y);
- }
- //red: blue == 0 && green == 0
- else if (src.at<Vec3b>(i, j)[0] == 0 && src.at<Vec3b>(i, j)[1] == 0)
- {
- Mat row_X(1, nr_elements_feature_vector, CV_64FC1);
- row_X.at<double>(0, 0) = i; //y coordinate
- row_X.at<double>(0, 1) = j; //x coordinate
- Mat row_Y(1, 1, CV_64FC1);
- row_Y.at<double>(0, 0) = 1;
- X.push_back(row_X);
- Y.push_back(row_Y);
- }
- }
- CLASSIFIER classifier = adaBoost(X, Y, 200, src.cols, src.rows);
- drawBoundary(&src, classifier);
- cv::imshow("src", src);
- cv::waitKey(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement