Path: blob/master/modules/calib3d/src/circlesgrid.hpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#ifndef CIRCLESGRID_HPP_43#define CIRCLESGRID_HPP_4445#include <fstream>46#include <set>47#include <list>48#include <numeric>49#include <map>5051class CirclesGridClusterFinder52{53CirclesGridClusterFinder& operator=(const CirclesGridClusterFinder&);54CirclesGridClusterFinder(const CirclesGridClusterFinder&);55public:56CirclesGridClusterFinder(const cv::CirclesGridFinderParameters ¶meters)57{58isAsymmetricGrid = parameters.gridType == cv::CirclesGridFinderParameters::ASYMMETRIC_GRID;59squareSize = parameters.squareSize;60maxRectifiedDistance = parameters.maxRectifiedDistance;61}62void findGrid(const std::vector<cv::Point2f> &points, cv::Size patternSize, std::vector<cv::Point2f>& centers);6364//cluster 2d points by geometric coordinates65void hierarchicalClustering(const std::vector<cv::Point2f> &points, const cv::Size &patternSize, std::vector<cv::Point2f> &patternPoints);66private:67void findCorners(const std::vector<cv::Point2f> &hull2f, std::vector<cv::Point2f> &corners);68void findOutsideCorners(const std::vector<cv::Point2f> &corners, std::vector<cv::Point2f> &outsideCorners);69void getSortedCorners(const std::vector<cv::Point2f> &hull2f, const std::vector<cv::Point2f> &corners, const std::vector<cv::Point2f> &outsideCorners, std::vector<cv::Point2f> &sortedCorners);70void rectifyPatternPoints(const std::vector<cv::Point2f> &patternPoints, const std::vector<cv::Point2f> &sortedCorners, std::vector<cv::Point2f> &rectifiedPatternPoints);71void parsePatternPoints(const std::vector<cv::Point2f> &patternPoints, const std::vector<cv::Point2f> &rectifiedPatternPoints, std::vector<cv::Point2f> ¢ers);7273float squareSize, maxRectifiedDistance;74bool isAsymmetricGrid;7576cv::Size patternSize;77};7879class Graph80{81public:82typedef std::set<size_t> Neighbors;83struct Vertex84{85Neighbors neighbors;86};87typedef std::map<size_t, Vertex> Vertices;8889Graph(size_t n);90void addVertex(size_t id);91void addEdge(size_t id1, size_t id2);92void removeEdge(size_t id1, size_t id2);93bool doesVertexExist(size_t id) const;94bool areVerticesAdjacent(size_t id1, size_t id2) const;95size_t getVerticesCount() const;96size_t getDegree(size_t id) const;97const Neighbors& getNeighbors(size_t id) const;98void floydWarshall(cv::Mat &distanceMatrix, int infinity = -1) const;99private:100Vertices vertices;101};102103struct Path104{105int firstVertex;106int lastVertex;107int length;108109std::vector<size_t> vertices;110111Path(int first = -1, int last = -1, int len = -1)112{113firstVertex = first;114lastVertex = last;115length = len;116}117};118119class CirclesGridFinder120{121public:122CirclesGridFinder(cv::Size patternSize, const std::vector<cv::Point2f> &testKeypoints,123const cv::CirclesGridFinderParameters ¶meters = cv::CirclesGridFinderParameters());124bool findHoles();125static cv::Mat rectifyGrid(cv::Size detectedGridSize, const std::vector<cv::Point2f>& centers, const std::vector<126cv::Point2f> &keypoint, std::vector<cv::Point2f> &warpedKeypoints);127128void getHoles(std::vector<cv::Point2f> &holes) const;129void getAsymmetricHoles(std::vector<cv::Point2f> &holes) const;130cv::Size getDetectedGridSize() const;131132void drawBasis(const std::vector<cv::Point2f> &basis, cv::Point2f origin, cv::Mat &drawImg) const;133void drawBasisGraphs(const std::vector<Graph> &basisGraphs, cv::Mat &drawImg, bool drawEdges = true,134bool drawVertices = true) const;135void drawHoles(const cv::Mat &srcImage, cv::Mat &drawImage) const;136private:137void computeRNG(Graph &rng, std::vector<cv::Point2f> &vectors, cv::Mat *drawImage = 0) const;138void rng2gridGraph(Graph &rng, std::vector<cv::Point2f> &vectors) const;139void eraseUsedGraph(std::vector<Graph> &basisGraphs) const;140void filterOutliersByDensity(const std::vector<cv::Point2f> &samples, std::vector<cv::Point2f> &filteredSamples);141void findBasis(const std::vector<cv::Point2f> &samples, std::vector<cv::Point2f> &basis,142std::vector<Graph> &basisGraphs);143void findMCS(const std::vector<cv::Point2f> &basis, std::vector<Graph> &basisGraphs);144size_t findLongestPath(std::vector<Graph> &basisGraphs, Path &bestPath);145float computeGraphConfidence(const std::vector<Graph> &basisGraphs, bool addRow, const std::vector<size_t> &points,146const std::vector<size_t> &seeds);147void addHolesByGraph(const std::vector<Graph> &basisGraphs, bool addRow, cv::Point2f basisVec);148149size_t findNearestKeypoint(cv::Point2f pt) const;150void addPoint(cv::Point2f pt, std::vector<size_t> &points);151void findCandidateLine(std::vector<size_t> &line, size_t seedLineIdx, bool addRow, cv::Point2f basisVec, std::vector<152size_t> &seeds);153void findCandidateHoles(std::vector<size_t> &above, std::vector<size_t> &below, bool addRow, cv::Point2f basisVec,154std::vector<size_t> &aboveSeeds, std::vector<size_t> &belowSeeds);155static bool areCentersNew(const std::vector<size_t> &newCenters, const std::vector<std::vector<size_t> > &holes);156bool isDetectionCorrect();157158static void insertWinner(float aboveConfidence, float belowConfidence, float minConfidence, bool addRow,159const std::vector<size_t> &above, const std::vector<size_t> &below, std::vector<std::vector<160size_t> > &holes);161162struct Segment163{164cv::Point2f s;165cv::Point2f e;166Segment(cv::Point2f _s, cv::Point2f _e);167};168169//if endpoint is on a segment then function return false170static bool areSegmentsIntersecting(Segment seg1, Segment seg2);171static bool doesIntersectionExist(const std::vector<Segment> &corner, const std::vector<std::vector<Segment> > &segments);172void getCornerSegments(const std::vector<std::vector<size_t> > &points, std::vector<std::vector<Segment> > &segments,173std::vector<cv::Point> &cornerIndices, std::vector<cv::Point> &firstSteps,174std::vector<cv::Point> &secondSteps) const;175size_t getFirstCorner(std::vector<cv::Point> &largeCornerIndices, std::vector<cv::Point> &smallCornerIndices,176std::vector<cv::Point> &firstSteps, std::vector<cv::Point> &secondSteps) const;177static double getDirection(cv::Point2f p1, cv::Point2f p2, cv::Point2f p3);178179std::vector<cv::Point2f> keypoints;180181std::vector<std::vector<size_t> > holes;182std::vector<std::vector<size_t> > holes2;183std::vector<std::vector<size_t> > *largeHoles;184std::vector<std::vector<size_t> > *smallHoles;185186const cv::Size_<size_t> patternSize;187cv::CirclesGridFinderParameters parameters;188189CirclesGridFinder& operator=(const CirclesGridFinder&);190CirclesGridFinder(const CirclesGridFinder&);191};192193#endif /* CIRCLESGRID_HPP_ */194195196