Skip to content

[GSoC 2019]Alphamatting #2240

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 58 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
82328d2
Add files via upload
muskaankularia Aug 25, 2019
3b48952
Delete CMakeLists.txt
muskaankularia Aug 25, 2019
b859eb2
Add files via upload
muskaankularia Aug 25, 2019
7c0ccad
Add files via upload
muskaankularia Aug 25, 2019
989fdc6
Add files via upload
muskaankularia Aug 25, 2019
b8326df
Add files via upload
muskaankularia Aug 25, 2019
617815c
Add files via upload
muskaankularia Aug 25, 2019
a1e7497
Add files via upload
muskaankularia Aug 25, 2019
495fa11
Add files via upload
muskaankularia Aug 25, 2019
6a5373b
Add files via upload
muskaankularia Aug 25, 2019
2a2f4e8
Add files via upload
muskaankularia Aug 25, 2019
21a5e58
Add files via upload
muskaankularia Aug 25, 2019
6d52824
Add files via upload
muskaankularia Aug 25, 2019
057d9b4
Create abc
muskaankularia Aug 25, 2019
be1990e
Add files via upload
muskaankularia Aug 25, 2019
62ce08f
changing .cpp to .hpp
muskaankularia Aug 30, 2019
42de316
Add files via upload
muskaankularia Aug 30, 2019
d69623a
Create README.md
muskaankularia Aug 30, 2019
a4747f6
Update README.md
muskaankularia Aug 30, 2019
9e5825f
Update README.md
muskaankularia Aug 30, 2019
2a28b67
Update README.md
muskaankularia Aug 30, 2019
98b0fc3
Update README.md
muskaankularia Aug 30, 2019
829d95c
Update README.md
muskaankularia Aug 30, 2019
7a145eb
Update README.md
muskaankularia Aug 30, 2019
9bb1f92
removed compilation err
muskaankularia Aug 31, 2019
a9da5b1
data for testing purpose
muskaankularia Aug 31, 2019
53e1099
taking data from command line
muskaankularia Aug 31, 2019
9ee14f5
Update alphac.cpp
muskaankularia Aug 31, 2019
f945317
Update trimming.hpp
muskaankularia Aug 31, 2019
3bccdb9
Update README.md
muskaankularia Aug 31, 2019
f188752
Update README.md
muskaankularia Aug 31, 2019
a78a58c
more files for testing added
muskaankularia Sep 1, 2019
e56f1bc
added more files for testing
muskaankularia Sep 1, 2019
4484e21
Delete CMakeLists.txt
muskaankularia Sep 1, 2019
5d35cda
Add files via upload
muskaankularia Sep 2, 2019
2087761
Update CMakeLists.txt
muskaankularia Sep 2, 2019
883fee6
Update CMakeLists.txt
muskaankularia Sep 2, 2019
1f066c5
Update CMakeLists.txt
muskaankularia Sep 2, 2019
4ff4474
Update CMakeLists.txt
muskaankularia Sep 2, 2019
4eddbe2
Delete CMakeLists.txt
muskaankularia Sep 2, 2019
be8d3b9
Update README.md
muskaankularia Sep 2, 2019
86055b5
Removed Eigen
muskaankularia Sep 2, 2019
936beae
Update alphac.cpp
muskaankularia Sep 2, 2019
7b49b5e
Update cm.hpp
muskaankularia Sep 2, 2019
618c13e
whitespace err removal
muskaankularia Sep 2, 2019
5876e94
Update intraU.hpp
muskaankularia Sep 2, 2019
2aae058
Update local_info.hpp
muskaankularia Sep 2, 2019
f27bca2
Update trimming.hpp
muskaankularia Sep 2, 2019
f199bdf
Update KtoU.hpp
muskaankularia Sep 2, 2019
a9f45d8
Update infoflow.cpp
muskaankularia Sep 2, 2019
690bbb5
Update KDTreeVectorOfVectorsAdaptor.h
muskaankularia Sep 2, 2019
2b40c70
Update README.md
muskaankularia Sep 2, 2019
6ccd5f3
Update alphac.cpp
muskaankularia Sep 2, 2019
26d3094
Update cm.hpp
muskaankularia Sep 2, 2019
5eec8e5
Update perf_infoflow.cpp
muskaankularia Sep 2, 2019
01c525c
Update perf_infoflow.cpp
muskaankularia Sep 2, 2019
e32784b
Update test_infoflow.cpp
muskaankularia Sep 2, 2019
7263620
Link to results added
muskaankularia Sep 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ To turn off building one of these module repositories, set the names in bold bel
```
$ cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -D BUILD_opencv_<reponame>=OFF <opencv_source_directory>
```
- **alphamat**: Information flow algorithm that estimates alpha value for every pixel using the image and its corresponding trimap.

- **aruco**: ArUco and ChArUco Markers -- Augmented reality ArUco marker and "ChARUco" markers where ArUco markers embedded inside the white areas of the checker board.

Expand Down
22 changes: 22 additions & 0 deletions modules/alphamat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Designing Effective Inter-Pixel Information Flow for Natural Image Matting:
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**

To run the code -
1. **g++ -std=c++11 alphac.cpp \`pkg-config --cflags --libs opencv\`**
1. **./a.out \<path to image> \<path to corresponding trimap>**

Sample image and trimap are in opencv_contrib/modules/alphamat/src/img and opencv_contrib/modules/alphamat/src/trimap

Results for input_lowres are available here -
https://docs.google.com/document/d/1BJG4633_U5K-Z0QLp3RTi43q25NI0hrTw-Q4w_85NrA/edit?usp=sharing

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.
26 changes: 26 additions & 0 deletions modules/alphamat/doc/alphamat.bib
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 not shown.
Binary file added modules/alphamat/img/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 added modules/alphamat/img/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 added modules/alphamat/img/pineapple.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 added modules/alphamat/img/plant.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 added modules/alphamat/img/plasticbag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions modules/alphamat/perf/perf_infoflow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using namespace std;
using namespace cv;
using namespace perf;

#include "perf_precomp.hpp"

namespace opencv_test
{

typedef std::tr1::tuple<Size, MatType, MatDepth> Size_MatType_OutMatDepth_t;
typedef perf::TestBaseWithParam<Size_MatType_OutMatDepth_t> Size_MatType_OutMatDepth;

/* 2. Declare the testsuite */
PERF_TEST_P( Size_MatType_OutMatDepth, integral1,
testing::Combine(
testing::Values( TYPICAL_MAT_SIZES ),
testing::Values( CV_8UC1, CV_8UC4 ),
testing::Values( CV_32S, CV_32F, CV_64F ) ) )
{
string folder = "cv/alphamat/";
string image_path = folder + "img/elephant.png";
string trimap_path = folder + "trimap/elephant.png";
string reference_path = folder + "reference/elephant.png";

Mat image = imread(getDataPath(image_path), IMREAD_COLOR);
Mat trimap = imread(getDataPath(trimap_path), IMREAD_COLOR);
Mat reference = imread(getDataPath(reference_path), IMREAD_GRAYSCALE);

Size sz = get<0>(GetParam());
int inpaintingMethod = get<1>(GetParam());

Mat result;
declare.in(image, trimap).out(result).time(120);

TEST_CYCLE() infoFlow(image, trimap, result, false, true);

SANITY_CHECK_NOTHING();
}
} // namespace
7 changes: 7 additions & 0 deletions modules/alphamat/perf/perf_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "perf_precomp.hpp"

#if defined(HAVE_HPX)
#include <hpx/hpx_main.hpp>
#endif

CV_PERF_TEST_MAIN(stitching)
6 changes: 6 additions & 0 deletions modules/alphamat/perf/perf_precomp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __OPENCV_PERF_PRECOMP_HPP__
#define __OPENCV_PERF_PRECOMP_HPP__

#include "opencv2/ts.hpp"

#endif
116 changes: 116 additions & 0 deletions modules/alphamat/src/KDTreeVectorOfVectorsAdaptor.h
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 ([email protected]).
* 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
Loading