From c668840c7fc4f15b40faa64803b6e26aa6d2867e Mon Sep 17 00:00:00 2001 From: bsdnoobz Date: Tue, 18 Dec 2012 00:56:33 +0700 Subject: Improve the algorithm in shape-detect.cpp --- shape-detect.cpp | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/shape-detect.cpp b/shape-detect.cpp index f373adf..7d70d08 100644 --- a/shape-detect.cpp +++ b/shape-detect.cpp @@ -6,6 +6,7 @@ #include #include #include +#include /** * Helper function to find a cosine of angle between vectors @@ -40,7 +41,8 @@ void setLabel(cv::Mat& im, const std::string label, std::vector& cont int main() { - cv::Mat src = cv::imread("basic-shapes-2.png"); + //cv::Mat src = cv::imread("polygon.png"); + cv::Mat src = cv::imread("assets/basic-shapes-2.png"); if (src.empty()) return -1; @@ -70,22 +72,34 @@ int main() continue; if (approx.size() == 3) + { setLabel(dst, "TRI", contours[i]); // Triangles - else if (approx.size() == 5) - setLabel(dst, "PENTA", contours[i]); // Pentagons - else if (approx.size() == 6) - setLabel(dst, "HEXA", contours[i]); // Hexagons - else if (approx.size() == 4) + } + else if (approx.size() >= 4 && approx.size() <= 6) { - // Detect and label rectangles - double maxcos = 0; - for (int j = 2; j < 5; j++) - { - double cos = std::fabs(angle(approx[j%4], approx[j-2], approx[j-1])); - maxcos = std::max(maxcos, cos); - } - if (maxcos < 0.3) + // Number of vertices of polygonal curve + int vtc = approx.size(); + + // Get the cosines of all corners + std::vector cos; + for (int j = 2; j < vtc+1; j++) + cos.push_back(angle(approx[j%vtc], approx[j-2], approx[j-1])); + + // Sort ascending the cosine values + std::sort(cos.begin(), cos.end()); + + // Get the lowest and the highest cosine + double mincos = cos.front(); + double maxcos = cos.back(); + + // Use the degrees obtained above and the number of vertices + // to determine the shape of the contour + if (vtc == 4 && mincos >= -0.1 && maxcos <= 0.3) setLabel(dst, "RECT", contours[i]); + else if (vtc == 5 && mincos >= -0.34 && maxcos <= -0.27) + setLabel(dst, "PENTA", contours[i]); + else if (vtc == 6 && mincos >= -0.55 && maxcos <= -0.45) + setLabel(dst, "HEXA", contours[i]); } else { -- cgit v1.2.3