Skip to content

Instantly share code, notes, and snippets.

@hkhojasteh
Last active January 30, 2019 05:39
Show Gist options
  • Save hkhojasteh/ce2a6d5ea72d2480143a8c2bc8ab4f8f to your computer and use it in GitHub Desktop.
Save hkhojasteh/ce2a6d5ea72d2480143a8c2bc8ab4f8f to your computer and use it in GitHub Desktop.
bool less_left(const Vec4i& lhs, const Vec4i& rhs){
return lhs[0] < rhs[0];
}
bool less_right(const Vec4i& lhs, const Vec4i& rhs){
return lhs[0] > rhs[0];
}
vector<Vec4i> rightls, leftls;
//Calculating the slope and group lines
float slope = (float)(l[3] - l[1]) / (l[2] - l[0]);
if (slope > 0.40){
rightls.push_back(l);
}else if (slope < -0.40){
leftls.push_back(l);
}
//Find regions
Point left_b, left_t, right_b, right_t;
if (leftls.size() > 0){
auto lmmx = minmax_element(leftls.begin(), leftls.end(), less_left);
left_b = Point(get<0>(lmmx)[0][0], get<0>(lmmx)[0][1]);
left_t = Point(get<0>(lmmx)[0][2], get<0>(lmmx)[0][3]);
}
if (rightls.size() > 0){
auto rmmx = minmax_element(rightls.begin(), rightls.end(), less_right);
right_t = Point(get<0>(rmmx)[0][0], get<0>(rmmx)[0][1]);
right_b = Point(get<0>(rmmx)[0][2], get<0>(rmmx)[0][3]);
}
Mat poly = img_hlines.clone();
vector<Point> vertices{ left_b, left_t, right_t, right_b };
vector<vector<Point>> pts{ vertices };
fillPoly(poly, pts, Scalar(58, 190, 37, 0));
addWeighted(poly, 0.50, img_hlines, 0.50, 0, img_hlines);
//Find reference line points by average top and bottom middle
float rx = (((right_b.x - left_b.x) / 2 + left_b.x) + ((right_t.x - left_t.x) / 2 + left_t.x)) / 2, ry = img_hlines.rows;
//Draw dashed reference line
Point p1(rx, 0), p2(rx, ry);
LineIterator itl(img_hlines, p1, p2, 8);
for (int i = 0; i < itl.count; i++, itl++){
if (i % 5 != 0){
(*itl)[1] = 80;
(*itl)[2] = 75;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment