/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. // Copyright (C) 2009, Willow Garage Inc., all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of the copyright holders may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #ifndef CIRCLESGRID_HPP_ #define CIRCLESGRID_HPP_ #include #include #include #include #include #include "precomp.hpp" class CirclesGridClusterFinder { CirclesGridClusterFinder& operator=(const CirclesGridClusterFinder&); CirclesGridClusterFinder(const CirclesGridClusterFinder&); public: CirclesGridClusterFinder(bool _isAsymmetricGrid) { isAsymmetricGrid = _isAsymmetricGrid; squareSize = 1.0f; maxRectifiedDistance = (float)(squareSize / 2.0); } void findGrid(const std::vector points, cv::Size patternSize, std::vector& centers); //cluster 2d points by geometric coordinates void hierarchicalClustering(const std::vector points, const cv::Size &patternSize, std::vector &patternPoints); private: void findCorners(const std::vector &hull2f, std::vector &corners); void findOutsideCorners(const std::vector &corners, std::vector &outsideCorners); void getSortedCorners(const std::vector &hull2f, const std::vector &corners, const std::vector &outsideCorners, std::vector &sortedCorners); void rectifyPatternPoints(const std::vector &patternPoints, const std::vector &sortedCorners, std::vector &rectifiedPatternPoints); void parsePatternPoints(const std::vector &patternPoints, const std::vector &rectifiedPatternPoints, std::vector ¢ers); float squareSize, maxRectifiedDistance; bool isAsymmetricGrid; cv::Size patternSize; }; class Graph { public: typedef std::set Neighbors; struct Vertex { Neighbors neighbors; }; typedef std::map Vertices; Graph(size_t n); void addVertex(size_t id); void addEdge(size_t id1, size_t id2); void removeEdge(size_t id1, size_t id2); bool doesVertexExist(size_t id) const; bool areVerticesAdjacent(size_t id1, size_t id2) const; size_t getVerticesCount() const; size_t getDegree(size_t id) const; const Neighbors& getNeighbors(size_t id) const; void floydWarshall(cv::Mat &distanceMatrix, int infinity = -1) const; private: Vertices vertices; }; struct Path { int firstVertex; int lastVertex; int length; std::vector vertices; Path(int first = -1, int last = -1, int len = -1) { firstVertex = first; lastVertex = last; length = len; } }; struct CirclesGridFinderParameters { CirclesGridFinderParameters(); cv::Size2f densityNeighborhoodSize; float minDensity; int kmeansAttempts; int minDistanceToAddKeypoint; int keypointScale; float minGraphConfidence; float vertexGain; float vertexPenalty; float existingVertexGain; float edgeGain; float edgePenalty; float convexHullFactor; float minRNGEdgeSwitchDist; enum GridType { SYMMETRIC_GRID, ASYMMETRIC_GRID }; GridType gridType; }; class CirclesGridFinder { public: CirclesGridFinder(cv::Size patternSize, const std::vector &testKeypoints, const CirclesGridFinderParameters ¶meters = CirclesGridFinderParameters()); bool findHoles(); static cv::Mat rectifyGrid(cv::Size detectedGridSize, const std::vector& centers, const std::vector< cv::Point2f> &keypoint, std::vector &warpedKeypoints); void getHoles(std::vector &holes) const; void getAsymmetricHoles(std::vector &holes) const; cv::Size getDetectedGridSize() const; void drawBasis(const std::vector &basis, cv::Point2f origin, cv::Mat &drawImg) const; void drawBasisGraphs(const std::vector &basisGraphs, cv::Mat &drawImg, bool drawEdges = true, bool drawVertices = true) const; void drawHoles(const cv::Mat &srcImage, cv::Mat &drawImage) const; private: void computeRNG(Graph &rng, std::vector &vectors, cv::Mat *drawImage = 0) const; void rng2gridGraph(Graph &rng, std::vector &vectors) const; void eraseUsedGraph(std::vector &basisGraphs) const; void filterOutliersByDensity(const std::vector &samples, std::vector &filteredSamples); void findBasis(const std::vector &samples, std::vector &basis, std::vector &basisGraphs); void findMCS(const std::vector &basis, std::vector &basisGraphs); size_t findLongestPath(std::vector &basisGraphs, Path &bestPath); float computeGraphConfidence(const std::vector &basisGraphs, bool addRow, const std::vector &points, const std::vector &seeds); void addHolesByGraph(const std::vector &basisGraphs, bool addRow, cv::Point2f basisVec); size_t findNearestKeypoint(cv::Point2f pt) const; void addPoint(cv::Point2f pt, std::vector &points); void findCandidateLine(std::vector &line, size_t seedLineIdx, bool addRow, cv::Point2f basisVec, std::vector< size_t> &seeds); void findCandidateHoles(std::vector &above, std::vector &below, bool addRow, cv::Point2f basisVec, std::vector &aboveSeeds, std::vector &belowSeeds); static bool areCentersNew(const std::vector &newCenters, const std::vector > &holes); bool isDetectionCorrect(); static void insertWinner(float aboveConfidence, float belowConfidence, float minConfidence, bool addRow, const std::vector &above, const std::vector &below, std::vector > &holes); struct Segment { cv::Point2f s; cv::Point2f e; Segment(cv::Point2f _s, cv::Point2f _e); }; //if endpoint is on a segment then function return false static bool areSegmentsIntersecting(Segment seg1, Segment seg2); static bool doesIntersectionExist(const std::vector &corner, const std::vector > &segments); void getCornerSegments(const std::vector > &points, std::vector > &segments, std::vector &cornerIndices, std::vector &firstSteps, std::vector &secondSteps) const; size_t getFirstCorner(std::vector &largeCornerIndices, std::vector &smallCornerIndices, std::vector &firstSteps, std::vector &secondSteps) const; static double getDirection(cv::Point2f p1, cv::Point2f p2, cv::Point2f p3); std::vector keypoints; std::vector > holes; std::vector > holes2; std::vector > *largeHoles; std::vector > *smallHoles; const cv::Size_ patternSize; CirclesGridFinderParameters parameters; CirclesGridFinder& operator=(const CirclesGridFinder&); CirclesGridFinder(const CirclesGridFinder&); }; #endif /* CIRCLESGRID_HPP_ */