- Notifications
You must be signed in to change notification settings - Fork5.9k
Test alphamatte#2247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
muskaankularia wants to merge82 commits intoopencv:4.xChoose a base branch frommuskaankularia:test_alphamatte
base:4.x
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+3,915 −0
Open
Test alphamatte#2247
Changes fromall commits
Commits
Show all changes
82 commits Select commitHold shift + click to select a range
17207fa Add files via upload
muskaankularia41ceff9 Add files via upload
muskaankularia5e3e999 Add files via upload
muskaankularia0847836 Delete doll.png
muskaankulariac67a32f Delete donkey.png
muskaankulariad782987 Delete elephant.png
muskaankularia158e938 Delete net.png
muskaankularia2477942 Delete pineapple.png
muskaankularia3fe589e Delete plant.png
muskaankulariab056e35 Delete plasticbag.png
muskaankulariaf994020 Delete troll.png
muskaankularia4359adc Delete elephant.png
muskaankularia491b51f Delete net.png
muskaankulariafac36ae Delete pineapple.png
muskaankularia0b06ed5 Delete plant.png
muskaankularia8cb8c13 Delete plasticbag.png
muskaankularia8dca8a5 Delete alphamat.bib
muskaankularia1a38add Delete summary_Information_Flow.docx
muskaankularia2727519 Update README.md
muskaankularia9b2e395 Update README.md
muskaankularia09db622 Update README.md
muskaankulariac5056ca Update README.md
muskaankularia0e55f0e Update README.md
muskaankularia7a775a7 Create CMakeLists.txt
muskaankularia08d9f30 removed bits/stdc++.h header
muskaankulariaf13088e updated headers in precomp.h
muskaankulariaf22a069 edit headers
muskaankulariadb41d5b updated header
muskaankularia4127d49 updated infoflow
muskaankulariab6b2c49 Delete alphac.cpp
muskaankularia0c38534 Eigen added for compiling
muskaankulariad7a2ef9 Merge branch 'test_alphamatte' of https://github.com/muskaankularia/o…
muskaankulariac16e565 test file header updated
muskaankulariac5cd238 test file header updated
muskaankularia2459b38 code changed acc to codeing style
muskaankularia6fba3df added include and doc
muskaankularia2b90c69 edit in namespace
muskaankularia3e49d7c namespace added
muskaankularia5035eb9 cv::cv - corrected, dim - corrected
muskaankularia95a8c8c compilation errors removed
muskaankularia2146461 header file incl in .hpp
muskaankularia18b14b2 path to Eigen added
muskaankularia0f8fcf2 test error function added
muskaankularia18b248e edit in test file
muskaankularia504533c test update
muskaankularia93db368 edit test
muskaankularia0ceff2e edit test
muskaankulariacb59bf0 edit test
muskaankularia873beb2 edit test
muskaankularia5d30eaa edit cmake
muskaankularia0cf5ae8 cv removed
muskaankularia46e66ae warning in KtoU removed
muskaankularia66e80cb warnings removed
muskaankularia1be1340 remove warnings and errors
muskaankulariaf5b51b9 ifndef conditions addded
muskaankularia7be17ed globals moved from header to cpp
muskaankulariaaefdf7e typedef moved to header
muskaankularia3ca82b1 error in test resolved
muskaankularia26cfa9c added path to opencv_extra
muskaankularia0509d25 comment last 4 lines
muskaankulariab02c158 Eigen removed
muskaankularia77f5ac4 cmake updated
muskaankulariacd9d3ee include Eigen updated
muskaankularia26458a6 Removed whitespaces and updated CMakeLists.txt
sunitanyk00c1b87 Removed empty line at EOF of CMakeLists.txt
sunitanykd2d2f97 Merge pull request #2 from sunitanyk/test_alphamatte_sn
muskaankularia9232b73 added samples and tutorials
sunitanyk3de7d53 included opencv.hpp
sunitanyke7e793b included headers
sunitanyk0ea3bce included headers
sunitanykdb50a05 edited cpp sample
sunitanyk2c0b495 edited markdown files
sunitanyk61d2b18 Merge pull request #3 from sunitanyk/test_alphamatte_sn
muskaankularia0c16f1e uncommented test_infoflow
muskaankularia22ae638 tutorials update
sunitanykea342d6 removed blank line
sunitanyka39bd46 included tutorial images
sunitanyk4551de6 removed whitespaces
sunitanyk07dafff added paper info
sunitanyk393f752 Merge pull request #4 from sunitanyk/test_alphamatte_sn
muskaankulariaccb6c9e Update README.md
muskaankulariabb477a2 Update README.md
muskaankulariaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletionsmodules/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletionsmodules/alphamat/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| if(NOT HAVE_EIGEN) | ||
| set(DISABLE_MSG "Module opencv_alphamat disabled because the following dependencies are not found:") | ||
| set(DISABLE_MSG "${DISABLE_MSG} Eigen") | ||
| message(STATUS ${DISABLE_MSG}) | ||
| ocv_module_disable(alphamat) | ||
| endif() | ||
| cmake_minimum_required (VERSION 3.0) | ||
| project (myproject) | ||
| set (CMAKE_CXX_STANDARD 11) | ||
| ocv_define_module(alphamat opencv_core | ||
| opencv_imgproc | ||
| opencv_objdetect | ||
| opencv_imgcodecs | ||
| opencv_photo | ||
| ) |
60 changes: 60 additions & 0 deletionsmodules/alphamat/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Designing Effective Inter-Pixel Information Flow for Natural Image Matting: | ||
| Alphamatting is the problem of extracting the foreground from an image. Given the input of image and its corresponding trimap, we try to extract the foreground from the background. Following is an example - | ||
| Input Image | Input trimap | Ouput Alpha matte | ||
| :-------------------------:|:-------------------------:|:-------------------------: | ||
| <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/img/net.png" alt="alt text" width="300" height="200"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/trimap/net.png" alt="alt text" width="300" height="200"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/Result/result_net.png" alt="alt text" width="300" height="200"> | ||
| This project is implementation of Information-Flow Matting [Yağız Aksoy, Tunç Ozan Aydın, Marc Pollefeys] [1]. It required implementation of some parts of other papers [2,3]. | ||
| This is a pixel-affinity based alpha matting algorithm which solves a linear system of equations using preconditioned conjugate gradient method. Affinity-based methods operate by propagating opacity information from known opacity regions(K) into unknown opacity regions(U) using a variety of affinity definitions mentioned as - | ||
| * Color mixture information flow - Opacity transitions in a matte occur as a result of the original colors in the image getting mixed with each other due to transparency or intricate parts of an object. They make use of this fact by representing each pixel in U as a mixture of similarly-colored pixels and the difference is the energy term ECM, which is to be reduced. This is coded in **cm.hpp** | ||
| * K-to-U information flow - Connections from every pixel in U to both F(foreground pixels) and B(background pixels) are made to facilitate direct information flow from known-opacity regions to even the most remote opacity-transition regions in the image. This is coded in **KtoU.hpp** | ||
| * Intra U information flow - They distribute the information inside U effectively by encouraging pixels with similar colors inside U to have similar opacity. This is coded in **intraU.hpp** | ||
| * Local information flow - Spatial connectivity is one of the main cues for information flow which is achieved by connecting each pixel in U to its immediate neighbors to ensure spatially smooth mattes. This is coded in **local_info.hpp** | ||
| Using these information flow, energy/error(E) is obtained as a weighted local composite of E<sub>CM</sub>, E<sub>KU</sub>(K-to-U information flow), E<sub>UU</sub>(Intra U information flow), E<sub>L</sub>(Local information flow). | ||
| E represents the deviation of unknown pixels opacity or colour from what we predict it to be using other pixels. So, the algorithm aims at minimizing this error. This is coded in **alphac.cpp** | ||
| Pre-processing and post-processing is implemented in **trimming.hpp** | ||
| Compile the module using - | ||
| ``` | ||
| cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -Dopencv_alphamat=ON <opencv_source_dir> | ||
| make | ||
| ``` | ||
| ## Results | ||
| Results for input_lowres are available here - | ||
| https://docs.google.com/document/d/1BJG4633_U5K-Z0QLp3RTi43q25NI0hrTw-Q4w_85NrA/edit?usp=sharing | ||
| Input Image | Ouput Alpha matte | ||
| :-------------------------:|:-------------------------: | ||
| <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/img/net.png" alt="alt text" width="200" height="155"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/Result/result_net.png" alt="alt text" width="200" height="155"> | ||
| <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/img/doll.png" alt="alt text" width="200" height="155"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/Result/result_doll.png" alt="alt text" width="200" height="155"> | ||
| <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/img/donkey.png" alt="alt text" width="200" height="155"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/Result/result_donkey.png" alt="alt text" width="200" height="155"> | ||
| <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/img/elephant.png" alt="alt text" width="200" height="155"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/Result/result_elephant.png" alt="alt text" width="200" height="155"> | ||
| <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/img/pineapple.png" alt="alt text" width="200" height="155"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/Result/result_pineapple.png" alt="alt text" width="200" height="155"> | ||
| <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/img/plant.png" alt="alt text" width="200" height="155"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/Result/result_plant.png" alt="alt text" width="200" height="155"> | ||
| <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/img/plasticbag.png" alt="alt text" width="200" height="155"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/Result/result_plasticbag.png" alt="alt text" width="200" height="155"> | ||
| <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/img/troll.png" alt="alt text" width="200" height="155"> | <img src="https://github.com/muskaankularia/opencv_contrib/blob/alphamat/modules/alphamat/Result/result_troll.png" alt="alt text" width="200" height="155"> | ||
| Average time taken to compute the different flows is 40s, but solving of linear equations using preconditioned conjugate gradient method takes another 2-3 min, which can be lessened by allowing lesser iterations. | ||
| ## TO DO | ||
| * Results need to be improved by extensively comparing each flow's matrix with yaksoy MATLAB implementation [4]. | ||
| * Runtime needs improvement. | ||
| * Third part library(Eigen, nanoflann) dependencies can be removed. | ||
| ## References | ||
| [1] Yagiz Aksoy, Tunc Ozan Aydin, Marc Pollefeys, "Designing Effective Inter-Pixel Information Flow for Natural Image Matting", CVPR, 2017. [[link](http://people.inf.ethz.ch/aksoyy/ifm/)] | ||
| [2] Roweis, Sam T., and Lawrence K. Saul. "Nonlinear dimensionality reduction by locally linear embedding." science 290.5500 (2000): 2323-2326.[[link](https://science.sciencemag.org/content/290/5500/2323)] | ||
| [3] Ehsan Shahrian, Deepu Rajan, Brian Price, Scott Cohen, "Improving Image Matting using Comprehensive Sampling Sets", CVPR 2013 [[paper](http://www.cv-foundation.org/openaccess/content_cvpr_2013/papers/Shahrian_Improving_Image_Matting_2013_CVPR_paper.pdf)] | ||
| [4] Affinity Based Matting Toolbox by Yagiz Aksoy[[link](https://github.com/yaksoy/AffinityBasedMattingToolbox)] |
26 changes: 26 additions & 0 deletionsmodules/alphamat/doc/alphamat.bib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| @inproceedings{aksoy2017designing, | ||
| title={Designing effective inter-pixel information flow for natural image matting}, | ||
| author={Aksoy, Yagiz and Ozan Aydin, Tunc and Pollefeys, Marc}, | ||
| booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition}, | ||
| pages={29--37}, | ||
| year={2017} | ||
| } | ||
| @article{roweis2000nonlinear, | ||
| title={Nonlinear dimensionality reduction by locally linear embedding}, | ||
| author={Roweis, Sam T and Saul, Lawrence K}, | ||
| journal={science}, | ||
| volume={290}, | ||
| number={5500}, | ||
| pages={2323--2326}, | ||
| year={2000}, | ||
| publisher={American Association for the Advancement of Science} | ||
| } | ||
| @inproceedings{shahrian2013improving, | ||
| title={Improving image matting using comprehensive sampling sets}, | ||
| author={Shahrian, Ehsan and Rajan, Deepu and Price, Brian and Cohen, Scott}, | ||
| booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition}, | ||
| pages={636--643}, | ||
| year={2013} | ||
| } |
Binary file addedmodules/alphamat/doc/summary_Information_Flow.docx
Binary file not shown.
31 changes: 31 additions & 0 deletionsmodules/alphamat/include/opencv2/infoflow.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // This file is part of OpenCV project. | ||
| // It is subject to the license terms in the LICENSE file found in the top-level directory | ||
| // of this distribution and at http://opencv.org/license.html. | ||
| #ifndef _OPENCV_ALPHAMAT_INFOFLOW_HPP_ | ||
| #define _OPENCV_ALPHAMAT_INFOFLOW_HPP_ | ||
| /** Information Flow algorithm implementaton for alphamatting | ||
| This module contains functionality for extracting the | ||
| The following four models are implemented: | ||
| - EDSR <https://arxiv.org/abs/1707.02921> | ||
| - ESPCN <https://arxiv.org/abs/1609.05158> | ||
| - FSRCNN <https://arxiv.org/abs/1608.00367> | ||
| - LapSRN <https://arxiv.org/abs/1710.01992> | ||
| */ | ||
| #include <Eigen/Sparse> | ||
| using namespace Eigen; | ||
| namespace cv{ namespace alphamat{ | ||
| void solve(SparseMatrix<double> Wcm,SparseMatrix<double> Wuu,SparseMatrix<double> Wl,SparseMatrix<double> Dcm, | ||
| SparseMatrix<double> Duu,SparseMatrix<double> Dl,SparseMatrix<double> H,SparseMatrix<double> T, | ||
| Mat& ak, Mat& wf, bool useKU, Mat& alpha); | ||
| CV_EXPORTS_W void infoFlow(Mat& image, Mat& tmap, Mat& result, bool useKU, bool trim); | ||
| }} | ||
| #endif |
81 changes: 81 additions & 0 deletionsmodules/alphamat/samples/information_flow_matting.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // This file is part of OpenCV project. | ||
| // It is subject to the license terms in the LICENSE file found in the top-level directory | ||
| // of this distribution and at http://opencv.org/license.html. | ||
| #include <iostream> | ||
| #include "opencv2/highgui.hpp" | ||
| #include <opencv2/core/base.hpp> | ||
| #include <opencv2/core/utility.hpp> | ||
| #include <opencv2/imgproc.hpp> | ||
| #include <opencv2/infoflow.hpp> | ||
| using namespace std; | ||
| using namespace cv; | ||
| using namespace cv::alphamat; | ||
| const char* keys = | ||
| { | ||
| "{img || input image name}" | ||
| "{tri || input trimap image name}" | ||
| "{out || output image name}" | ||
| }; | ||
| int main(int argc, char *argv[]) | ||
| { | ||
| bool show_help = (argc == 1); | ||
| show_help = show_help || (argc == 2 && string(argv[1]) == "--help"); | ||
| show_help = show_help || (argc == 2 && string(argv[1]) == "-h"); | ||
| if (show_help) | ||
| { | ||
| printf("\nThis sample demonstrates Information Flow alpha matting\n" | ||
| "Call:\n" | ||
| " alphamat_information_flow -img=<string> -tri=<string> [-out=<string>]\n\n"); | ||
| return 0; | ||
| } | ||
| CommandLineParser parser(argc, argv, keys); | ||
| if (!parser.check()) | ||
| { | ||
| parser.printErrors(); | ||
| return -1; | ||
| } | ||
| string img_path = parser.get<std::string>("img"); | ||
| string trimap_path = parser.get<std::string>("tri"); | ||
| string result_path = parser.get<std::string>("out"); | ||
| Mat image, tmap; | ||
| image = imread(img_path, IMREAD_COLOR); // Read the input image file | ||
| if (image.empty()) | ||
| { | ||
| printf("Cannot read image file: %s\n", img_path.c_str()); | ||
| return -1; | ||
| } | ||
| tmap = imread(trimap_path, IMREAD_GRAYSCALE); | ||
| if (tmap.empty()) | ||
| { | ||
| printf("Cannot read trimap file: %s\n", trimap_path.c_str()); | ||
| return -1; | ||
| } | ||
| Mat result; | ||
| infoFlow(image, tmap, result, false, true); | ||
| if (result_path.empty()) | ||
| { | ||
| namedWindow("result alpha matte", WINDOW_NORMAL); | ||
| imshow("result alpha matte", result); | ||
| waitKey(0); | ||
| } | ||
| else | ||
| { | ||
| imwrite(result_path, result); | ||
| } | ||
| imshow("Result Matte", result); | ||
| return 0; | ||
| } |
Binary file addedmodules/alphamat/samples/input_images/elephant.png
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedmodules/alphamat/samples/input_images/net.png
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedmodules/alphamat/samples/trimaps/elephant.png
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedmodules/alphamat/samples/trimaps/net.png
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
116 changes: 116 additions & 0 deletionsmodules/alphamat/src/KDTreeVectorOfVectorsAdaptor.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /*********************************************************************** | ||
| * Software License Agreement (BSD License) | ||
| * | ||
| * Copyright 2011-16 Jose Luis Blanco (joseluisblancoc@gmail.com). | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions | ||
| * are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * 2. Redistributions 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. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. | ||
| *************************************************************************/ | ||
| #pragma once | ||
| #include "nanoflann.hpp" | ||
| #include <vector> | ||
| // ===== This example shows how to use nanoflann with these types of containers: ======= | ||
| //typedef std::vector<std::vector<double> > my_vector_of_vectors_t; | ||
| //typedef std::vector<Eigen::VectorXd> my_vector_of_vectors_t; // This requires #include <Eigen/Dense> | ||
| // ===================================================================================== | ||
| /** A simple vector-of-vectors adaptor for nanoflann, without duplicating the storage. | ||
| * The i'th vector represents a point in the state space. | ||
| * | ||
| * \tparam DIM If set to >0, it specifies a compile-time fixed dimensionality for the points in the data set, allowing more compiler optimizations. | ||
| * \tparam num_t The type of the point coordinates (typically, double or float). | ||
| * \tparam Distance The distance metric to use: nanoflann::metric_L1, nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. | ||
| * \tparam IndexType The type for indices in the KD-tree index (typically, size_t of int) | ||
| */ | ||
| template <class VectorOfVectorsType, typename num_t = double, int DIM = -1, class Distance = nanoflann::metric_L2, typename IndexType = size_t> | ||
| struct KDTreeVectorOfVectorsAdaptor | ||
| { | ||
| typedef KDTreeVectorOfVectorsAdaptor<VectorOfVectorsType, num_t, DIM,Distance> self_t; | ||
| typedef typename Distance::template traits<num_t, self_t>::distance_t metric_t; | ||
| typedef nanoflann::KDTreeSingleIndexAdaptor< metric_t, self_t, DIM, IndexType> index_t; | ||
| index_t* index; //! The kd-tree index for the user to call its methods as usual with any other FLANN index. | ||
| /// Constructor: takes a const ref to the vector of vectors object with the data points | ||
| KDTreeVectorOfVectorsAdaptor(const size_t /* dimensionality */, const VectorOfVectorsType &mat, const int leaf_max_size = 10) : m_data(mat) | ||
| { | ||
| assert(mat.size() != 0 && mat[0].size() != 0); | ||
| const size_t dims = mat[0].size(); | ||
| if (DIM>0 && static_cast<int>(dims) != DIM) | ||
| throw std::runtime_error("Data set dimensionality does not match the 'DIM' template argument"); | ||
| index = new index_t( static_cast<int>(dims), *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leaf_max_size ) ); | ||
| index->buildIndex(); | ||
| } | ||
| ~KDTreeVectorOfVectorsAdaptor() { | ||
| delete index; | ||
| } | ||
| const VectorOfVectorsType &m_data; | ||
| /** Query for the \a num_closest closest points to a given point (entered as query_point[0:dim-1]). | ||
| * Note that this is a short-cut method for index->findNeighbors(). | ||
| * The user can also call index->... methods as desired. | ||
| * \note nChecks_IGNORED is ignored but kept for compatibility with the original FLANN interface. | ||
| */ | ||
| inline void query(const num_t *query_point, const size_t num_closest, IndexType *out_indices, num_t *out_distances_sq, const int nChecks_IGNORED = 10) const | ||
| { | ||
| nanoflann::KNNResultSet<num_t, IndexType> resultSet(num_closest); | ||
| resultSet.init(out_indices, out_distances_sq); | ||
| index->findNeighbors(resultSet, query_point, nanoflann::SearchParams()); | ||
| } | ||
| /** @name Interface expected by KDTreeSingleIndexAdaptor | ||
| * @{ */ | ||
| const self_t & derived() const { | ||
| return *this; | ||
| } | ||
| self_t & derived() { | ||
| return *this; | ||
| } | ||
| // Must return the number of data points | ||
| inline size_t kdtree_get_point_count() const { | ||
| return m_data.size(); | ||
| } | ||
| // Returns the dim'th component of the idx'th point in the class: | ||
| inline num_t kdtree_get_pt(const size_t idx, const size_t dim) const { | ||
| return m_data[idx][dim]; | ||
| } | ||
| // Optional bounding-box computation: return false to default to a standard bbox computation loop. | ||
| // Return true if the BBOX was already computed by the class and returned in "bb" so it can be avoided to redo it again. | ||
| // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds) | ||
| template <class BBOX> | ||
| bool kdtree_get_bbox(BBOX & /*bb*/) const { | ||
| return false; | ||
| } | ||
| /** @} */ | ||
| }; // end of KDTreeVectorOfVectorsAdaptor |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.