Skip to content

Commit

Permalink
Update Region class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir55 committed Dec 2, 2017
1 parent 4d52dd5 commit 3137bcc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 32 deletions.
74 changes: 45 additions & 29 deletions src/LineSegmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,36 @@ Valley::comp(const Valley *a, const Valley *b) {
return a->position < b->position;
}

Region::Region(cv::Mat a, vector<int> ro) {
region = a.clone();
row_offset = ro;
Region::Region(Line *top, Line *bottom) {
this->top = top;
this->bottom = bottom;
}

bool
Region::update_region(Mat &binary_image, int region_id) {
this->region_id = region_id;

int min_region_row = row_offset = (top == nullptr) ? 0 : top->min_row_position;
int max_region_row = (bottom == nullptr) ? binary_image.rows : bottom->max_row_position;

region = Mat::ones(max_region_row - min_region_row, binary_image.cols, CV_8U) * 255;

// Fill region.
if (bottom != nullptr) {
for (int c = 0; c < binary_image.cols; c++) {
int start = ((top == nullptr) ? 0 : top->points[c].x);
for (int i = start; i < bottom->points[c].x; i++) {
region.at<uchar>(i - min_region_row, c) = binary_image.at<uchar>(i, c);
}
}
}
calculate_mean();
calculate_covariance();

imwrite("out/" + string("Region") + to_string(region_id) + ".jpg",
region);

return countNonZero(region) == region.cols * region.rows;
}

void
Expand All @@ -494,9 +519,9 @@ Region::calculate_mean() {
if (region.at<uchar>(i, j) == 255) continue;
if (n == 0) {
n = n + 1;
mean = Vec2f(i + row_offset[j], j);
mean = Vec2f(i + row_offset, j);
} else {
mean = (n - 1.0) / n * mean + 1.0 / n * Vec2f(i + row_offset[j], j);
mean = (n - 1.0) / n * mean + 1.0 / n * Vec2f(i + row_offset, j);
n = n + 1;
}
}
Expand All @@ -507,26 +532,31 @@ Region::calculate_mean() {
void
Region::calculate_covariance() {
Mat covariance = Mat::zeros(2, 2, CV_32F);
int n = 0;
float sum_x_squared = 0, sum_y_squared = 0, sum_x_y = 0;

int n = 0; // Total number of considered points so far.
float sum_i_squared = 0, sum_j_squared = 0, sum_i_j = 0;

for (int i = 0; i < region.rows; i++) {
for (int j = 0; j < region.cols; j++) {
// if white pixel continue.
if ((int) region.at<uchar>(i, j) == 255) continue;

float new_i = i + row_offset[j] - mean[0];
float new_i = i + row_offset - mean[0];
float new_j = j - mean[1];
sum_x_squared += new_i * new_i;
sum_x_y += new_i * new_j;
sum_y_squared += new_j * new_j;

sum_i_squared += new_i * new_i;
sum_i_j += new_i * new_j;
sum_j_squared += new_j * new_j;
n++;
}
}
covariance.at<float>(0, 0) = sum_x_squared / n;
covariance.at<float>(0, 1) = sum_x_y / n;
covariance.at<float>(1, 0) = sum_x_y / n;
covariance.at<float>(1, 1) = sum_y_squared / n;
if (n) {
covariance.at<float>(0, 0) = sum_i_squared / n;
covariance.at<float>(0, 1) = sum_i_j / n;
covariance.at<float>(1, 0) = sum_i_j / n;
covariance.at<float>(1, 1) = sum_j_squared / n;

}

this->covariance = covariance.clone();
}
Expand All @@ -539,23 +569,9 @@ Region::bi_variate_gaussian_density(Mat point) {
Mat point_transpose;
transpose(point, point_transpose);


Mat ret = ((point * this->covariance.inv() * point_transpose));
ret *= sqrt(determinant(this->covariance * 2 * M_PI));

// cout << "COVARIANCE " << covariance << endl;
// cout << "INVERSE COVARIANCE " << this->covariance.inv() << endl;
// cout << "DETERMIN. " << determinant(this->covariance * 2 * M_PI) << endl;
// cout << "SQRT DETERMIN. " <<1.0/sqrt(determinant(this->covariance) * 2 * M_PI) << endl;
// cout << "MEAN " << mean << endl;

// cout << "RET " << ret << endl;
// Probability above: 6192 below: 3888
// Mat m = point * this->covariance.inv() * point_transpose;
// cout << "POINT " << point<<endl;
// cout << "M" << (m.at<float>(0,0)) << endl;
// double ret2 = (1.0 / (2 * M_PI * sqrt(determinant(this->covariance)))) * exp(-0.5 * (m.at<float>(0, 0)));
// cout << "RET 2 " << ret2 << endl << endl << endl << endl << endl;
return ret.at<float>(0, 0);
}

Expand Down
21 changes: 18 additions & 3 deletions src/LineSegmentation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,39 @@ class Region {
friend class LineSegmentation;

private:
int region_id;
///< Region id.
cv::Mat region;
///< 2D matrix representing the region.
Line *top;
///< Region top boundaries;
Line *bottom;
///< Region bottom boundaries;
int start_row;
///<
int end_row;
///<
vector<int> row_offset;
///< A vector containing the offset of each col to the original image matrix.
int row_offset;
///< The offset of each col to the original image matrix.
cv::Mat covariance;
///< The covariance of the matrix.
cv::Vec2f mean;
///< The mean of the matrix.

Region(cv::Mat a, vector<int> ro);
Region(Line *top, Line *bottom);

/// Get the region matrix
/// \param img
/// \param region_id
/// \return
bool
update_region(Mat &img, int);

/// Calculate region black pixels mean relative to the whole image dimensions
void
calculate_mean();

/// Calculate region black pixels covariance relative to the whole image dimensions
void
calculate_covariance();

Expand Down

0 comments on commit 3137bcc

Please sign in to comment.