diff options
-rw-r--r-- | quad-segmentation.cpp | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/quad-segmentation.cpp b/quad-segmentation.cpp index bb341cd..17428a1 100644 --- a/quad-segmentation.cpp +++ b/quad-segmentation.cpp @@ -37,17 +37,20 @@ void sortCorners(std::vector<cv::Point2f>& corners, else bot.push_back(corners[i]); } - - cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0]; - cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1]; - cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0]; - cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1]; - corners.clear(); - corners.push_back(tl); - corners.push_back(tr); - corners.push_back(br); - corners.push_back(bl); + + if (top.size() == 2 && bot.size() == 2){ + cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0]; + cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1]; + cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0]; + cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1]; + + + corners.push_back(tl); + corners.push_back(tr); + corners.push_back(br); + corners.push_back(bl); + } } int main() @@ -93,14 +96,17 @@ int main() std::cout << "The object is not quadrilateral!" << std::endl; return -1; } - + // Get mass center for (int i = 0; i < corners.size(); i++) center += corners[i]; center *= (1. / corners.size()); sortCorners(corners, center); - + if (corners.size() == 0){ + std::cout << "The corners were not sorted correctly!" << std::endl; + return -1; + } cv::Mat dst = src.clone(); // Draw lines |