From 2b35f5130fc1e14a9ac1a699b2b7d75af004539e Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 25 Aug 2019 23:09:32 +0530 Subject: [PATCH 01/71] Add files via upload --- .../include/opencv2/ximgproc/mattingClass.h | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 modules/ximgproc/include/opencv2/ximgproc/mattingClass.h diff --git a/modules/ximgproc/include/opencv2/ximgproc/mattingClass.h b/modules/ximgproc/include/opencv2/ximgproc/mattingClass.h new file mode 100644 index 00000000000..9c18b22330b --- /dev/null +++ b/modules/ximgproc/include/opencv2/ximgproc/mattingClass.h @@ -0,0 +1,161 @@ + +#include "opencv2/imgproc.hpp" +#include "opencv2/highgui.hpp" +#include + +#include +#include +#include + + +using namespace std; +using namespace cv; + +#ifndef GLOBAL_MATTING_H +#define GLOBAL_MATTING_H + + + +class GlobalMatting +{ + private: + + template + inline T sqr(T a) + { + return a * a; + } + + + + std::vector findBoundaryPixels(const cv::Mat_ &trimap, int a, int b); + + // Eq. 2 + float calculateAlpha(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I); + + // Eq. 3 + float colorCost(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I, float alpha); + + // Eq. 4 + float distCost(const cv::Point &p0, const cv::Point &p1, float minDist); + + float colorDist(const cv::Vec3b &I0, const cv::Vec3b &I1); + + float nearestDistance(const std::vector &boundary, const cv::Point &p); + + + // for sorting the boundary pixels according to intensity + struct IntensityComp + { + IntensityComp(const cv::Mat_ &image) : image(image) + { + + } + + bool operator()(const cv::Point &p0, const cv::Point &p1) const + { + const cv::Vec3b &c0 = image(p0.y, p0.x); + const cv::Vec3b &c1 = image(p1.y, p1.x); + + return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); + } + + const cv::Mat_ ℑ + }; + + void expansionOfKnownRegions(const cv::Mat_ &image, + cv::Mat_ &trimap, + int r, float c); + + + // erode foreground and background regions to increase the size of unknown region + void erodeFB(cv::Mat_ &trimap, int r); + + + + struct Sample + { + int fi, bj; + float df, db; + float cost, alpha; + }; + + void calculateAlphaPatchMatch(const cv::Mat_ &image, + const cv::Mat_ &trimap, + const std::vector &foregroundBoundary, + const std::vector &backgroundBoundary, + std::vector > &samples); + + void expansionOfKnownRegionsHelper(const cv::Mat &_image, + cv::Mat &_trimap, + int r, float c); + + + // erode foreground and background regions to increase the size of unknown region + void erodeFB(cv::Mat &_trimap, int r); + + void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter); + + + void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); + + + public: + + GlobalMatting(); + + void globalMatting(cv::InputArray _image, cv::InputArray _trimap, cv::OutputArray _foreground, cv::OutputArray _alpha, cv::OutputArray _conf); + + void getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: Mat &alpha,int niter=9); + + + + + + +}; + +#endif +/* +int main(int argc,char** argv) +{ + if(argc<3) + { + cout<<"Enter the path of image and trimap"<(y, x) == 0) + alpha.at(y, x) = 0; + else if (trimap.at(y, x) == 255) + alpha.at(y, x) = 255; + } + + cv::imwrite("GT04-alpha.png", alpha); + + return 0; +} +*/ From 65d426803d449a4533e24095d24d9ea06046db95 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 25 Aug 2019 23:10:22 +0530 Subject: [PATCH 02/71] Add files via upload --- modules/ximgproc/src/mattingClass.cpp | 601 ++++++++++++++++++++++++++ 1 file changed, 601 insertions(+) create mode 100644 modules/ximgproc/src/mattingClass.cpp diff --git a/modules/ximgproc/src/mattingClass.cpp b/modules/ximgproc/src/mattingClass.cpp new file mode 100644 index 00000000000..da8c714716f --- /dev/null +++ b/modules/ximgproc/src/mattingClass.cpp @@ -0,0 +1,601 @@ + +#include "opencv2/imgproc.hpp" +#include "opencv2/highgui.hpp" +#include + +#include +#include +#include +#include "mattingClass.h" + + +using namespace std; +using namespace cv; + + + + + +std::vector GlobalMatting::findBoundaryPixels(const cv::Mat_ &trimap, int a, int b) +{ + std::vector result; + + for (int x = 1; x < trimap.cols - 1; ++x) + for (int y = 1; y < trimap.rows - 1; ++y) + { + if (trimap(y, x) == a) + { + if (trimap(y - 1, x) == b || + trimap(y + 1, x) == b || + trimap(y, x - 1) == b || + trimap(y, x + 1) == b) + { + result.push_back(cv::Point(x, y)); + } + } + } + + return result; +} + +// Eq. 2 +float GlobalMatting::calculateAlpha(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I) +{ + float result = 0; + float div = 1e-6f; + for (int c = 0; c < 3; ++c) + { + float f = F[c]; + float b = B[c]; + float i = I[c]; + + result += (i - b) * (f - b); + div += (f - b) * (f - b); + } + + return std::min(std::max(result / div, 0.f), 1.f); +} + +// Eq. 3 +float GlobalMatting::colorCost(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I, float alpha) +{ + float result = 0; + for (int c = 0; c < 3; ++c) + { + float f = F[c]; + float b = B[c]; + float i = I[c]; + + result += sqr(i - (alpha * f + (1 - alpha) * b)); + } + + return sqrt(result); +} + +// Eq. 4 +float GlobalMatting::distCost(const cv::Point &p0, const cv::Point &p1, float minDist) +{ + int dist = sqr(p0.x - p1.x) + sqr(p0.y - p1.y); + return sqrt((float)dist) / minDist; +} + +float GlobalMatting::colorDist(const cv::Vec3b &I0, const cv::Vec3b &I1) +{ + int result = 0; + + for (int c = 0; c < 3; ++c) + result += sqr((int)I0[c] - (int)I1[c]); + + return sqrt((float)result); +} + +float GlobalMatting::nearestDistance(const std::vector &boundary, const cv::Point &p) +{ + int minDist2 = INT_MAX; + for (std::size_t i = 0; i < boundary.size(); ++i) + { + int dist2 = sqr(boundary[i].x - p.x) + sqr(boundary[i].y - p.y); + minDist2 = std::min(minDist2, dist2); + } + + return sqrt((float)minDist2); +} + + + + +void GlobalMatting::expansionOfKnownRegions(const cv::Mat_ &image, + cv::Mat_ &trimap, + int r, float c) +{ + int w = image.cols; + int h = image.rows; + + for (int x = 0; x < w; ++x) + for (int y = 0; y < h; ++y) + { + if (trimap(y, x) != 128) + continue; + + const cv::Vec3b &I = image(y, x); + + for (int j = y-r; j <= y+r; ++j) + for (int i = x-r; i <= x+r; ++i) + { + if (i < 0 || i >= w || j < 0 || j >= h) + continue; + + if (trimap(j, i) != 0 && trimap(j, i) != 255) + continue; + + const cv::Vec3b &I2 = image(j, i); + + float pd = sqrt((float)(sqr(x - i) + sqr(y - j))); + float cd = colorDist(I, I2); + + if (pd <= r && cd <= c) + { + if (trimap(j, i) == 0) + trimap(y, x) = 1; + else if (trimap(j, i) == 255) + trimap(y, x) = 254; + } + } + } + + for (int x = 0; x < trimap.cols; ++x) + for (int y = 0; y < trimap.rows; ++y) + { + if (trimap(y, x) == 1) + trimap(y, x) = 0; + else if (trimap(y, x) == 254) + trimap(y, x) = 255; + + } +} + +// erode foreground and background regions to increase the size of unknown region +void GlobalMatting::erodeFB(cv::Mat_ &trimap, int r) +{ + int w = trimap.cols; + int h = trimap.rows; + + cv::Mat_ foreground(trimap.size(), (uchar)0); + cv::Mat_ background(trimap.size(), (uchar)0); + + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) + { + if (trimap(y, x) == 0) + background(y, x) = 1; + else if (trimap(y, x) == 255) + foreground(y, x) = 1; + } + + + cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(r, r)); + + cv::erode(background, background, kernel); + cv::erode(foreground, foreground, kernel); + + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) + { + if (background(y, x) == 0 && foreground(y, x) == 0) + trimap(y, x) = 128; + } +} + + +void GlobalMatting::calculateAlphaPatchMatch(const cv::Mat_ &image, + const cv::Mat_ &trimap, + const std::vector &foregroundBoundary, + const std::vector &backgroundBoundary, + std::vector > &samples) +{ + int w = image.cols; + int h = image.rows; + + samples.resize(h, std::vector(w)); + + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) + { + if (trimap(y, x) == 128) + { + cv::Point p(x, y); + + samples[y][x].fi = rand() % foregroundBoundary.size(); + samples[y][x].bj = rand() % backgroundBoundary.size(); + samples[y][x].df = nearestDistance(foregroundBoundary, p); + samples[y][x].db = nearestDistance(backgroundBoundary, p); + samples[y][x].cost = FLT_MAX; + } + } + + std::vector coords(w * h); + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) + coords[x + y * w] = cv::Point(x, y); + + for (int iter = 0; iter < 10; ++iter) + { + // propagation + std::random_shuffle(coords.begin(), coords.end()); + + for (std::size_t i = 0; i < coords.size(); ++i) + { + const cv::Point &p = coords[i]; + + int x = p.x; + int y = p.y; + + if (trimap(y, x) != 128) + continue; + + const cv::Vec3b &I = image(y, x); + + Sample &s = samples[y][x]; + + for (int y2 = y - 1; y2 <= y + 1; ++y2) + for (int x2 = x - 1; x2 <= x + 1; ++x2) + { + if (x2 < 0 || x2 >= w || y2 < 0 || y2 >= h) + continue; + + if (trimap(y2, x2) != 128) + continue; + + Sample &s2 = samples[y2][x2]; + + const cv::Point &fp = foregroundBoundary[s2.fi]; + const cv::Point &bp = backgroundBoundary[s2.bj]; + + const cv::Vec3b F = image(fp.y, fp.x); + const cv::Vec3b B = image(bp.y, bp.x); + + float alpha = calculateAlpha(F, B, I); + + float cost = colorCost(F, B, I, alpha) + distCost(p, fp, s.df) + distCost(p, bp, s.db); + + if (cost < s.cost) + { + s.fi = s2.fi; + s.bj = s2.bj; + s.cost = cost; + s.alpha = alpha; + } + } + } + + // random walk + int w2 = (int)std::max(foregroundBoundary.size(), backgroundBoundary.size()); + + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) + { + if (trimap(y, x) != 128) + continue; + + cv::Point p(x, y); + + const cv::Vec3b &I = image(y, x); + + Sample &s = samples[y][x]; + + for (int k = 0; ; k++) + { + float r = w2 * pow(0.5f, k); + + if (r < 1) + break; + + int di = r * (rand() / (RAND_MAX + 1.f)); + int dj = r * (rand() / (RAND_MAX + 1.f)); + + int fi = s.fi + di; + int bj = s.bj + dj; + + if (fi < 0 || fi >= foregroundBoundary.size() || bj < 0 || bj >= backgroundBoundary.size()) + continue; + + const cv::Point &fp = foregroundBoundary[fi]; + const cv::Point &bp = backgroundBoundary[bj]; + + const cv::Vec3b F = image(fp.y, fp.x); + const cv::Vec3b B = image(bp.y, bp.x); + + float alpha = calculateAlpha(F, B, I); + + float cost = colorCost(F, B, I, alpha) + distCost(p, fp, s.df) + distCost(p, bp, s.db); + + if (cost < s.cost) + { + s.fi = fi; + s.bj = bj; + s.cost = cost; + s.alpha = alpha; + } + } + } + } +} + +void GlobalMatting::expansionOfKnownRegionsHelper(const cv::Mat &_image, + cv::Mat &_trimap, + int r, float c) +{ + const cv::Mat_ &image = (const cv::Mat_ &)_image; + cv::Mat_ &trimap = (cv::Mat_&)_trimap; + + int w = image.cols; + int h = image.rows; + + for (int x = 0; x < w; ++x) + for (int y = 0; y < h; ++y) + { + if (trimap(y, x) != 128) + continue; + + const cv::Vec3b &I = image(y, x); + + for (int j = y-r; j <= y+r; ++j) + for (int i = x-r; i <= x+r; ++i) + { + if (i < 0 || i >= w || j < 0 || j >= h) + continue; + + if (trimap(j, i) != 0 && trimap(j, i) != 255) + continue; + + const cv::Vec3b &I2 = image(j, i); + + float pd = sqrt((float)(sqr(x - i) + sqr(y - j))); + float cd = colorDist(I, I2); + + if (pd <= r && cd <= c) + { + if (trimap(j, i) == 0) + trimap(y, x) = 1; + else if (trimap(j, i) == 255) + trimap(y, x) = 254; + } + } + } + + for (int x = 0; x < trimap.cols; ++x) + for (int y = 0; y < trimap.rows; ++y) + { + if (trimap(y, x) == 1) + trimap(y, x) = 0; + else if (trimap(y, x) == 254) + trimap(y, x) = 255; + + } +} + +// erode foreground and background regions to increase the size of unknown region +void GlobalMatting::erodeFB(cv::Mat &_trimap, int r) +{ + cv::Mat_ &trimap = (cv::Mat_&)_trimap; + + int w = trimap.cols; + int h = trimap.rows; + + cv::Mat_ foreground(trimap.size(), (uchar)0); + cv::Mat_ background(trimap.size(), (uchar)0); + + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) + { + if (trimap(y, x) == 0) + background(y, x) = 1; + else if (trimap(y, x) == 255) + foreground(y, x) = 1; + } + + + cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(r, r)); + + cv::erode(background, background, kernel); + cv::erode(foreground, foreground, kernel); + + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) + { + if (background(y, x) == 0 && foreground(y, x) == 0) + trimap(y, x) = 128; + } +} + +void GlobalMatting::expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter) +{ + cv::Mat img = _img.getMat(); + cv::Mat &trimap = _trimap.getMatRef(); + + if (img.empty()) + CV_Error(CV_StsBadArg, "image is empty"); + if (img.type() != CV_8UC3) + CV_Error(CV_StsBadArg, "image mush have CV_8UC3 type"); + + if (trimap.empty()) + CV_Error(CV_StsBadArg, "trimap is empty"); + if (trimap.type() != CV_8UC1) + CV_Error(CV_StsBadArg, "trimap mush have CV_8UC1 type"); + + if (img.size() != trimap.size()) + CV_Error(CV_StsBadArg, "image and trimap mush have same size"); + + for (int i = 0; i < niter; ++i) + expansionOfKnownRegionsHelper(img, trimap, i + 1, niter - i); + erodeFB(trimap, 2); +} + + +void GlobalMatting::globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf) +{ + const cv::Mat_ &image = (const cv::Mat_&)_image; + const cv::Mat_ &trimap = (const cv::Mat_&)_trimap; + + std::vector foregroundBoundary = findBoundaryPixels(trimap, 255, 128); + std::vector backgroundBoundary = findBoundaryPixels(trimap, 0, 128); + + int n = (int)(foregroundBoundary.size() + backgroundBoundary.size()); + for (int i = 0; i < n; ++i) + { + int x = rand() % trimap.cols; + int y = rand() % trimap.rows; + + if (trimap(y, x) == 0) + backgroundBoundary.push_back(cv::Point(x, y)); + else if (trimap(y, x) == 255) + foregroundBoundary.push_back(cv::Point(x, y)); + } + + std::sort(foregroundBoundary.begin(), foregroundBoundary.end(), IntensityComp(image)); + std::sort(backgroundBoundary.begin(), backgroundBoundary.end(), IntensityComp(image)); + + std::vector > samples; + calculateAlphaPatchMatch(image, trimap, foregroundBoundary, backgroundBoundary, samples); + + _foreground.create(image.size(), CV_8UC3); + _alpha.create(image.size(), CV_8UC1); + _conf.create(image.size(), CV_8UC1); + + cv::Mat_ &foreground = (cv::Mat_&)_foreground; + cv::Mat_ &alpha = (cv::Mat_&)_alpha; + cv::Mat_ &conf = (cv::Mat_&)_conf; + + for (int y = 0; y < alpha.rows; ++y) + for (int x = 0; x < alpha.cols; ++x) + { + switch (trimap(y, x)) + { + case 0: + alpha(y, x) = 0; + conf(y, x) = 255; + foreground(y, x) = 0; + break; + case 128: + { + alpha(y, x) = 255 * samples[y][x].alpha; + conf(y, x) = 255 * exp(-samples[y][x].cost / 6); + cv::Point p = foregroundBoundary[samples[y][x].fi]; + foreground(y, x) = image(p.y, p.x); + break; + } + case 255: + alpha(y, x) = 255; + conf(y, x) = 255; + foreground(y, x) = image(y, x); + break; + } + } +} + +void GlobalMatting::globalMatting(cv::InputArray _image, cv::InputArray _trimap, cv::OutputArray _foreground, cv::OutputArray _alpha, cv::OutputArray _conf) +{ + cv::Mat image = _image.getMat(); + cv::Mat trimap = _trimap.getMat(); + + if (image.empty()) + CV_Error(CV_StsBadArg, "image is empty"); + if (image.type() != CV_8UC3) + CV_Error(CV_StsBadArg, "image mush have CV_8UC3 type"); + + if (trimap.empty()) + CV_Error(CV_StsBadArg, "trimap is empty"); + if (trimap.type() != CV_8UC1) + CV_Error(CV_StsBadArg, "trimap mush have CV_8UC1 type"); + + if (image.size() != trimap.size()) + CV_Error(CV_StsBadArg, "image and trimap mush have same size"); + + cv::Mat &foreground = _foreground.getMatRef(); + cv::Mat &alpha = _alpha.getMatRef(); + cv::Mat tempConf; + + globalMattingHelper(image, trimap, foreground, alpha, tempConf); + + cv::ximgproc::guidedFilter(image,alpha,alpha,10,1e-5); + + if(_conf.needed()) + tempConf.copyTo(_conf); +} + +void GlobalMatting::getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: Mat &alpha,int niter) +{ + cv::Mat conf; + globalMatting(image,trimap,foreground,alpha,conf); + expansionOfKnownRegions(image,trimap,niter); + for (int x = 0; x < trimap.cols; ++x) + { + for (int y = 0; y < trimap.rows; ++y) + { + if (trimap.at(y, x) == 0) + alpha.at(y, x) = 0; + else if (trimap.at(y, x) == 255) + alpha.at(y, x) = 255; + } + } + +} + +GlobalMatting::GlobalMatting() +{ + cout<<"The Global matting object has been instantiated"<(y, x) == 0) + alpha.at(y, x) = 0; + else if (trimap.at(y, x) == 255) + alpha.at(y, x) = 255; + } + + cv::imwrite("GT04-alpha.png", alpha); + + return 0; +} +*/ From ab0015dde67f83a11b6f9eb4cac3259969f2899b Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 25 Aug 2019 23:14:15 +0530 Subject: [PATCH 03/71] Add files via upload --- modules/ximgproc/test/test_global_matting.cpp | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 modules/ximgproc/test/test_global_matting.cpp diff --git a/modules/ximgproc/test/test_global_matting.cpp b/modules/ximgproc/test/test_global_matting.cpp new file mode 100644 index 00000000000..47cd74755dc --- /dev/null +++ b/modules/ximgproc/test/test_global_matting.cpp @@ -0,0 +1,67 @@ +#include "test_precomp.hpp" + +const std::string INPUT_DIR = "." +const std::string IMAGE_FILENAME = "Input/doll.png" +const std::string TRIMAP_FILENAME = "Trimap/doll.png" + + +class CV_GlobalMattingTest : public cvtest:BaseTest +{ + public: + CV_GlobalMattingTest(); + + protected: + Ptr gm; + void runModel(); +}; + +void CV_GlobalMattingTest::runModel() +{ + std::string img_path = std::string(ts->get_data_path()) + INPUT_DIR + "/" + IMAGE_FILENAME; + std::string trimap_path = std::string(ts->get_data_path()) + INPUT_DIR + "/" + TRIMAP_FILENAME; + + Mat img = imread(img_path); + Mat trimap = imread(trimap_path); + if(img.empty() || trimap.empty()) + { + ts->printf(cvtest::TS::LOG,"Test images not found!\n"); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); + return; + } + if(img.cols!=trimap.cols || img.rows!=trimap.cols) + { + ts->printf(cvtest::TS::LOG,"Dimensions of trimap and the image are not the same"); + ts->set_failed_test_infor(cvtest::TS::FAIL_INVALID_TEST_PATH); + return; + } + + Mat foreground,alpha; + int niter = 9; + this->gm->getMat(img,trimap,foreground,alpha,niter); + + if(alpha.empty()) + { + ts->printf(cvtest::TS::LOG,"Could not find the alpha matte for the image\n"); + ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); + return; + } + + if(alpha.cols!=img.cols || alpha.rows!=img.rows) + { + ts->printf(cvtest::TS::LOG,"The dimensions of the output are not correct"); + ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); + return; + } +} + +CV_GlobalMattingTest::CV_GlobalMattingTest() +{ + gm = makePtr(); +} + +TEST(CV_GlobalMattingTest,accuracy) +{ + CV_GlobalMattingTest test; + test.safe_run(); +} + From 75bb304d6a779a95f3cb0df3182def733877c7f4 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 07:26:37 +0530 Subject: [PATCH 04/71] Update README.md --- modules/ximgproc/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/ximgproc/README.md b/modules/ximgproc/README.md index 59e744e0238..be37294a798 100644 --- a/modules/ximgproc/README.md +++ b/modules/ximgproc/README.md @@ -16,3 +16,4 @@ Extended Image Processing - Pei&Lin Normalization - Ridge Detection Filter - Binary morphology on run-length encoded images +- Global sampling method for matting From 977d5241ea41435810065b898ef3db8520732d47 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 07:27:50 +0530 Subject: [PATCH 05/71] Update and rename mattingClass.cpp to globalmatting.cpp --- .../{mattingClass.cpp => globalmatting.cpp} | 49 +------------------ 1 file changed, 1 insertion(+), 48 deletions(-) rename modules/ximgproc/src/{mattingClass.cpp => globalmatting.cpp} (92%) diff --git a/modules/ximgproc/src/mattingClass.cpp b/modules/ximgproc/src/globalmatting.cpp similarity index 92% rename from modules/ximgproc/src/mattingClass.cpp rename to modules/ximgproc/src/globalmatting.cpp index da8c714716f..850e1fa837d 100644 --- a/modules/ximgproc/src/mattingClass.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "mattingClass.h" +#include "globalmatting.h" using namespace std; @@ -550,52 +550,5 @@ GlobalMatting::GlobalMatting() -#include "globalmatting.h" - -// you can get the guided filter implementation -// from https://github.com/atilimcetin/guided-filter -#include "guidedfilter.h" - -/* -int main(int argc,char** argv) -{ - if(argc<3) - { - cout<<"Enter the path of image and trimap"<(y, x) == 0) - alpha.at(y, x) = 0; - else if (trimap.at(y, x) == 255) - alpha.at(y, x) = 255; - } - cv::imwrite("GT04-alpha.png", alpha); - - return 0; -} -*/ From 0cf5b8046d9b9e246ef959776a2f937d6d606046 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 07:28:40 +0530 Subject: [PATCH 06/71] Rename mattingClass.h to globalmatting.h --- .../include/opencv2/ximgproc/{mattingClass.h => globalmatting.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/ximgproc/include/opencv2/ximgproc/{mattingClass.h => globalmatting.h} (100%) diff --git a/modules/ximgproc/include/opencv2/ximgproc/mattingClass.h b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.h similarity index 100% rename from modules/ximgproc/include/opencv2/ximgproc/mattingClass.h rename to modules/ximgproc/include/opencv2/ximgproc/globalmatting.h From cba44e8d5b8832e8b739a710bbeab418812e02d5 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 07:49:18 +0530 Subject: [PATCH 07/71] Update and rename globalmatting.h to globalmatting.hpp --- .../ximgproc/{globalmatting.h => globalmatting.hpp} | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) rename modules/ximgproc/include/opencv2/ximgproc/{globalmatting.h => globalmatting.hpp} (98%) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.h b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp similarity index 98% rename from modules/ximgproc/include/opencv2/ximgproc/globalmatting.h rename to modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 9c18b22330b..2f0cc73d0bc 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.h +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -8,14 +8,16 @@ #include -using namespace std; -using namespace cv; + #ifndef GLOBAL_MATTING_H #define GLOBAL_MATTING_H - - +namespace cv +{ + +namespace ximgproc +{ class GlobalMatting { private: @@ -114,6 +116,9 @@ class GlobalMatting }; + +} +} #endif /* From 61bfe981feec940b8fc445d7b3ca59f5d48f5165 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 07:52:32 +0530 Subject: [PATCH 08/71] Update globalmatting.cpp --- modules/ximgproc/src/globalmatting.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index 850e1fa837d..773263cb3e6 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -4,18 +4,14 @@ #include #include -#include #include -#include "globalmatting.h" - - -using namespace std; -using namespace cv; - - - +#include "opencv2/ximgproc/globalmatting.hpp" +namespace cv +{ +namespace ximgproc +{ std::vector GlobalMatting::findBoundaryPixels(const cv::Mat_ &trimap, int a, int b) { std::vector result; @@ -546,7 +542,8 @@ GlobalMatting::GlobalMatting() cout<<"The Global matting object has been instantiated"< Date: Mon, 26 Aug 2019 07:56:11 +0530 Subject: [PATCH 09/71] Add files via upload --- .../samples/global_matting_sample.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 modules/ximgproc/samples/global_matting_sample.cpp diff --git a/modules/ximgproc/samples/global_matting_sample.cpp b/modules/ximgproc/samples/global_matting_sample.cpp new file mode 100644 index 00000000000..ca5c00abe80 --- /dev/null +++ b/modules/ximgproc/samples/global_matting_sample.cpp @@ -0,0 +1,42 @@ +#include "mattingClass.h" +#include + +int main(int argc,char** argv) +{ + if(argc<3) + { + cout<<"Enter the path of image and trimap"< Date: Mon, 26 Aug 2019 08:02:28 +0530 Subject: [PATCH 10/71] Update global_matting_sample.cpp --- modules/ximgproc/samples/global_matting_sample.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/modules/ximgproc/samples/global_matting_sample.cpp b/modules/ximgproc/samples/global_matting_sample.cpp index ca5c00abe80..222a990f5f8 100644 --- a/modules/ximgproc/samples/global_matting_sample.cpp +++ b/modules/ximgproc/samples/global_matting_sample.cpp @@ -1,11 +1,16 @@ -#include "mattingClass.h" -#include +#include +#include +using namespace std; +using namespace cv; +using namespace ximgproc; int main(int argc,char** argv) { if(argc<3) { - cout<<"Enter the path of image and trimap"< Date: Mon, 26 Aug 2019 08:10:17 +0530 Subject: [PATCH 11/71] Update test_global_matting.cpp --- modules/ximgproc/test/test_global_matting.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/ximgproc/test/test_global_matting.cpp b/modules/ximgproc/test/test_global_matting.cpp index 47cd74755dc..318424eed4f 100644 --- a/modules/ximgproc/test/test_global_matting.cpp +++ b/modules/ximgproc/test/test_global_matting.cpp @@ -1,8 +1,12 @@ #include "test_precomp.hpp" -const std::string INPUT_DIR = "." -const std::string IMAGE_FILENAME = "Input/doll.png" -const std::string TRIMAP_FILENAME = "Trimap/doll.png" +namespace opencv_test +{ +namespace +{ +const std::string INPUT_DIR = "matting" +const std::string IMAGE_FILENAME = "input/doll.png" +const std::string TRIMAP_FILENAME = "trimap/doll.png" class CV_GlobalMattingTest : public cvtest:BaseTest @@ -64,4 +68,6 @@ TEST(CV_GlobalMattingTest,accuracy) CV_GlobalMattingTest test; test.safe_run(); } - + +} +} From c1b2aaa3bf188195fdf140248042bcb5f5d57184 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 08:30:47 +0530 Subject: [PATCH 12/71] Update globalmatting.cpp --- modules/ximgproc/src/globalmatting.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index 773263cb3e6..6863447b5e7 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -1,6 +1,6 @@ -#include "opencv2/imgproc.hpp" -#include "opencv2/highgui.hpp" +#include +#include #include #include From f71d0ced7dec72290bad7220ee268a7e2c72e3d9 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 08:32:33 +0530 Subject: [PATCH 13/71] Update globalmatting.hpp --- modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 2f0cc73d0bc..4c89944a39e 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -1,10 +1,4 @@ - -#include "opencv2/imgproc.hpp" -#include "opencv2/highgui.hpp" -#include - #include -#include #include From 7679b63e68d9c3cac7613b9145a8d6a4795c2ecb Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 08:34:10 +0530 Subject: [PATCH 14/71] Update globalmatting.hpp --- modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 4c89944a39e..660018c4da6 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -1,3 +1,7 @@ +#include +#include +#include + #include #include From 74155ca5828f6efde65bcac3a5b43ef14e90eb8b Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 08:44:06 +0530 Subject: [PATCH 15/71] Update globalmatting.cpp --- modules/ximgproc/src/globalmatting.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index 6863447b5e7..3de7186fbec 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -1,11 +1,6 @@ - -#include -#include -#include - #include #include -#include "opencv2/ximgproc/globalmatting.hpp" + namespace cv From 3781902b3acedc925d2396f8c7d1f923c3176a2a Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 08:51:16 +0530 Subject: [PATCH 16/71] Update globalmatting.cpp --- modules/ximgproc/src/globalmatting.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index 3de7186fbec..fc2e0f864cd 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -1,6 +1,7 @@ #include #include - +#include +#include "precomp.hpp" namespace cv From dcce8467c92ca9dbf905c062a21fd9200ba66cf5 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 08:57:30 +0530 Subject: [PATCH 17/71] Update globalmatting.hpp --- modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 660018c4da6..fb02dcfe881 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -16,7 +16,7 @@ namespace cv namespace ximgproc { -class GlobalMatting +class CV_EXPORTS GlobalMatting { private: From 16d0c32e4877516b681b9fef37098642edc76d97 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 09:03:21 +0530 Subject: [PATCH 18/71] Update globalmatting.hpp --- modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index fb02dcfe881..0981399bb3d 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -1,3 +1,6 @@ +#ifndef GLOBAL_MATTING_H +#define GLOBAL_MATTING_H + #include #include #include @@ -8,8 +11,7 @@ -#ifndef GLOBAL_MATTING_H -#define GLOBAL_MATTING_H + namespace cv { From 1b22801cb47cb60304964329057fe700f57ce5ed Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 09:04:55 +0530 Subject: [PATCH 19/71] Update ximgproc.hpp --- modules/ximgproc/include/opencv2/ximgproc.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/ximgproc/include/opencv2/ximgproc.hpp b/modules/ximgproc/include/opencv2/ximgproc.hpp index 4632f53127a..5903fedd5d1 100644 --- a/modules/ximgproc/include/opencv2/ximgproc.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc.hpp @@ -59,6 +59,7 @@ #include "ximgproc/run_length_morphology.hpp" #include "ximgproc/edgepreserving_filter.hpp" #include "ximgproc/color_match.hpp" +#include "ximgproc.global_matting.hpp" /** @defgroup ximgproc Extended Image Processing From 2f915e9936706f25b4583f8d7e9a53cc38662f9e Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 09:09:51 +0530 Subject: [PATCH 20/71] Update ximgproc.hpp --- modules/ximgproc/include/opencv2/ximgproc.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc.hpp b/modules/ximgproc/include/opencv2/ximgproc.hpp index 5903fedd5d1..66409daf308 100644 --- a/modules/ximgproc/include/opencv2/ximgproc.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc.hpp @@ -59,7 +59,7 @@ #include "ximgproc/run_length_morphology.hpp" #include "ximgproc/edgepreserving_filter.hpp" #include "ximgproc/color_match.hpp" -#include "ximgproc.global_matting.hpp" +#include "ximgproc.globalmatting.hpp" /** @defgroup ximgproc Extended Image Processing From 622caaf1ad03e25ce8a0133b9a83c1b3d6733191 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 09:13:19 +0530 Subject: [PATCH 21/71] Update ximgproc.hpp --- modules/ximgproc/include/opencv2/ximgproc.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc.hpp b/modules/ximgproc/include/opencv2/ximgproc.hpp index 66409daf308..32330435b45 100644 --- a/modules/ximgproc/include/opencv2/ximgproc.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc.hpp @@ -59,7 +59,7 @@ #include "ximgproc/run_length_morphology.hpp" #include "ximgproc/edgepreserving_filter.hpp" #include "ximgproc/color_match.hpp" -#include "ximgproc.globalmatting.hpp" +#include "ximgproc/globalmatting.hpp" /** @defgroup ximgproc Extended Image Processing From 071c42434eb5457dca27e7c156ff32fe342328eb Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 09:20:06 +0530 Subject: [PATCH 22/71] Update CMakeLists.txt --- modules/ximgproc/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/CMakeLists.txt b/modules/ximgproc/CMakeLists.txt index f6f88bec66d..5875db25cd0 100644 --- a/modules/ximgproc/CMakeLists.txt +++ b/modules/ximgproc/CMakeLists.txt @@ -1,2 +1,2 @@ set(the_description "Extended image processing module. It includes edge-aware filters and etc.") -ocv_define_module(ximgproc opencv_core opencv_imgproc opencv_calib3d opencv_imgcodecs WRAP python java) +ocv_define_module(ximgproc opencv_core opencv_highgui opencv_imgproc opencv_calib3d opencv_imgcodecs WRAP python java) From 6ea2ca537f8839c1026cce09f6a444b650198248 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 23:34:19 +0530 Subject: [PATCH 23/71] Update globalmatting.hpp --- .../opencv2/ximgproc/globalmatting.hpp | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 0981399bb3d..51dc5284bc3 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -8,8 +8,24 @@ #include #include +// for sorting the boundary pixels according to intensity +struct IntensityComp +{ + IntensityComp(const cv::Mat_ &image) : image(image) + { + + } + bool operator()(const cv::Point &p0, const cv::Point &p1) const + { + const cv::Vec3b &c0 = image(p0.y, p0.x); + const cv::Vec3b &c1 = image(p1.y, p1.x); + + return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); + } + const cv::Mat_ ℑ +}; @@ -46,24 +62,7 @@ class CV_EXPORTS GlobalMatting float nearestDistance(const std::vector &boundary, const cv::Point &p); - // for sorting the boundary pixels according to intensity - struct IntensityComp - { - IntensityComp(const cv::Mat_ &image) : image(image) - { - - } - - bool operator()(const cv::Point &p0, const cv::Point &p1) const - { - const cv::Vec3b &c0 = image(p0.y, p0.x); - const cv::Vec3b &c1 = image(p1.y, p1.x); - - return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); - } - - const cv::Mat_ ℑ - }; + void expansionOfKnownRegions(const cv::Mat_ &image, cv::Mat_ &trimap, From ad6f044a8976a23a4c1b2c65c6d7c7a311fab3ac Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 23:51:10 +0530 Subject: [PATCH 24/71] Update globalmatting.hpp --- .../ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 51dc5284bc3..97e7116b68e 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -11,20 +11,20 @@ // for sorting the boundary pixels according to intensity struct IntensityComp { - IntensityComp(const cv::Mat_ &image) : image(image) + IntensityComp(const cv::Mat_ &img) : img(img) { } bool operator()(const cv::Point &p0, const cv::Point &p1) const { - const cv::Vec3b &c0 = image(p0.y, p0.x); - const cv::Vec3b &c1 = image(p1.y, p1.x); + const cv::Vec3b &c0 = img(p0.y, p0.x); + const cv::Vec3b &c1 = img(p1.y, p1.x); return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); } - const cv::Mat_ ℑ + const cv::Mat_ &img; }; From afc6138698a21cbf632e9a03b981883f9e09be4a Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Mon, 26 Aug 2019 23:55:41 +0530 Subject: [PATCH 25/71] Update globalmatting.hpp --- .../ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 97e7116b68e..38528cb62d3 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -11,20 +11,20 @@ // for sorting the boundary pixels according to intensity struct IntensityComp { - IntensityComp(const cv::Mat_ &img) : img(img) + IntensityComp(const cv::Mat_ &img_temp) : img_temp(img_temp) { } bool operator()(const cv::Point &p0, const cv::Point &p1) const { - const cv::Vec3b &c0 = img(p0.y, p0.x); - const cv::Vec3b &c1 = img(p1.y, p1.x); + const cv::Vec3b &c0 = img_temp(p0.y, p0.x); + const cv::Vec3b &c1 = img_temp(p1.y, p1.x); return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); } - const cv::Mat_ &img; + const cv::Mat_ &img_temp; }; From 6bcba8be45c840712098ddba11925b2b753ed07e Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 00:06:11 +0530 Subject: [PATCH 26/71] Update globalmatting.hpp --- modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 38528cb62d3..1e2bd73c96b 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -11,15 +11,15 @@ // for sorting the boundary pixels according to intensity struct IntensityComp { - IntensityComp(const cv::Mat_ &img_temp) : img_temp(img_temp) + IntensityComp(const cv::Mat_ &img_temp) : img(img_temp) { } bool operator()(const cv::Point &p0, const cv::Point &p1) const { - const cv::Vec3b &c0 = img_temp(p0.y, p0.x); - const cv::Vec3b &c1 = img_temp(p1.y, p1.x); + const cv::Vec3b &c0 = img(p0.y, p0.x); + const cv::Vec3b &c1 = img(p1.y, p1.x); return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); } From 403d8f546a2e1a0557fd3419016a99dbdf16aae6 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 00:13:18 +0530 Subject: [PATCH 27/71] Update globalmatting.hpp --- modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 1e2bd73c96b..a200119e810 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -24,7 +24,7 @@ struct IntensityComp return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); } - const cv::Mat_ &img_temp; + const cv::Mat_ &img; }; From 4ce6e54f7cceacf2cd2750d14afb400476dc8362 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 00:23:09 +0530 Subject: [PATCH 28/71] Update globalmatting.cpp --- modules/ximgproc/src/globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index fc2e0f864cd..da7de23befb 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -288,7 +288,7 @@ void GlobalMatting::calculateAlphaPatchMatch(const cv::Mat_ &image, int fi = s.fi + di; int bj = s.bj + dj; - if (fi < 0 || fi >= foregroundBoundary.size() || bj < 0 || bj >= backgroundBoundary.size()) + if (fi < 0 || (unsigned)fi >= foregroundBoundary.size() || bj < 0 || (unsigned)bj >= backgroundBoundary.size()) continue; const cv::Point &fp = foregroundBoundary[fi]; From 864c7ff9ce4ae3715f5ebefef1500c68a49b3da1 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 00:24:59 +0530 Subject: [PATCH 29/71] Update globalmatting.cpp --- modules/ximgproc/src/globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index da7de23befb..883e7b80f19 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -535,7 +535,7 @@ void GlobalMatting::getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: GlobalMatting::GlobalMatting() { - cout<<"The Global matting object has been instantiated"< Date: Tue, 27 Aug 2019 00:34:48 +0530 Subject: [PATCH 30/71] Update test_global_matting.cpp --- modules/ximgproc/test/test_global_matting.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/ximgproc/test/test_global_matting.cpp b/modules/ximgproc/test/test_global_matting.cpp index 318424eed4f..3d4a722878f 100644 --- a/modules/ximgproc/test/test_global_matting.cpp +++ b/modules/ximgproc/test/test_global_matting.cpp @@ -4,12 +4,12 @@ namespace opencv_test { namespace { -const std::string INPUT_DIR = "matting" -const std::string IMAGE_FILENAME = "input/doll.png" -const std::string TRIMAP_FILENAME = "trimap/doll.png" +const std::string INPUT_DIR = "matting"; +const std::string IMAGE_FILENAME = "input/doll.png"; +const std::string TRIMAP_FILENAME = "trimap/doll.png"; -class CV_GlobalMattingTest : public cvtest:BaseTest +class CV_GlobalMattingTest : public cvtest::BaseTest { public: CV_GlobalMattingTest(); From 130ed66be340b41d5b3513efa4e383afd5c4fa83 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 00:35:45 +0530 Subject: [PATCH 31/71] Update test_global_matting.cpp --- modules/ximgproc/test/test_global_matting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/test/test_global_matting.cpp b/modules/ximgproc/test/test_global_matting.cpp index 3d4a722878f..b50b69a49fd 100644 --- a/modules/ximgproc/test/test_global_matting.cpp +++ b/modules/ximgproc/test/test_global_matting.cpp @@ -35,7 +35,7 @@ void CV_GlobalMattingTest::runModel() if(img.cols!=trimap.cols || img.rows!=trimap.cols) { ts->printf(cvtest::TS::LOG,"Dimensions of trimap and the image are not the same"); - ts->set_failed_test_infor(cvtest::TS::FAIL_INVALID_TEST_PATH); + ts->set_failed_test_infor(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } From 8a49f43f21bf2af5dac4c297dce9087c4f1b1107 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 00:54:27 +0530 Subject: [PATCH 32/71] Update test_global_matting.cpp --- modules/ximgproc/test/test_global_matting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/test/test_global_matting.cpp b/modules/ximgproc/test/test_global_matting.cpp index b50b69a49fd..4d8c3746a75 100644 --- a/modules/ximgproc/test/test_global_matting.cpp +++ b/modules/ximgproc/test/test_global_matting.cpp @@ -35,7 +35,7 @@ void CV_GlobalMattingTest::runModel() if(img.cols!=trimap.cols || img.rows!=trimap.cols) { ts->printf(cvtest::TS::LOG,"Dimensions of trimap and the image are not the same"); - ts->set_failed_test_infor(cvtest::TS::FAIL_INVALID_TEST_DATA); + ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } From 6d707727eb74d8062d4d1c3a762d4efb19357be3 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 01:22:45 +0530 Subject: [PATCH 33/71] Update global_matting_sample.cpp --- modules/ximgproc/samples/global_matting_sample.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/ximgproc/samples/global_matting_sample.cpp b/modules/ximgproc/samples/global_matting_sample.cpp index 222a990f5f8..0dea8ee5e58 100644 --- a/modules/ximgproc/samples/global_matting_sample.cpp +++ b/modules/ximgproc/samples/global_matting_sample.cpp @@ -1,5 +1,6 @@ #include #include +#include using namespace std; using namespace cv; From c2336a4c6c7bd193eb75fb7f6a0f243be894affe Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 02:59:21 +0530 Subject: [PATCH 34/71] Update globalmatting.cpp --- modules/ximgproc/src/globalmatting.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index 883e7b80f19..da124c17ee8 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include "precomp.hpp" @@ -7,7 +7,7 @@ namespace cv { namespace ximgproc -{ +{ std::vector GlobalMatting::findBoundaryPixels(const cv::Mat_ &trimap, int a, int b) { std::vector result; @@ -530,18 +530,11 @@ void GlobalMatting::getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: alpha.at(y, x) = 255; } } - } GlobalMatting::GlobalMatting() { - } } } - - - - - From ccbf27fb94a967f49342d071df581f6f039de2f8 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 03:06:19 +0530 Subject: [PATCH 35/71] Update globalmatting.hpp --- .../opencv2/ximgproc/globalmatting.hpp | 97 +++++-------------- 1 file changed, 22 insertions(+), 75 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index a200119e810..0a6bba19ca3 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include // for sorting the boundary pixels according to intensity @@ -31,47 +31,46 @@ struct IntensityComp namespace cv { - + namespace ximgproc -{ +{ class CV_EXPORTS GlobalMatting { private: - + template inline T sqr(T a) { return a * a; } - - + + std::vector findBoundaryPixels(const cv::Mat_ &trimap, int a, int b); - + // Eq. 2 float calculateAlpha(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I); - + // Eq. 3 float colorCost(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I, float alpha); - + // Eq. 4 float distCost(const cv::Point &p0, const cv::Point &p1, float minDist); - - float colorDist(const cv::Vec3b &I0, const cv::Vec3b &I1); - + + float colorDist(const cv::Vec3b &I0, const cv::Vec3b &I1); float nearestDistance(const std::vector &boundary, const cv::Point &p); - + void expansionOfKnownRegions(const cv::Mat_ &image, cv::Mat_ &trimap, - int r, float c); - + int r, float c); + // erode foreground and background regions to increase the size of unknown region void erodeFB(cv::Mat_ &trimap, int r); - + struct Sample @@ -86,80 +85,28 @@ class CV_EXPORTS GlobalMatting const std::vector &foregroundBoundary, const std::vector &backgroundBoundary, std::vector > &samples); - + void expansionOfKnownRegionsHelper(const cv::Mat &_image, cv::Mat &_trimap, int r, float c); - + // erode foreground and background regions to increase the size of unknown region void erodeFB(cv::Mat &_trimap, int r); - - void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter); - - void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); - - + void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter); + + void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); public: GlobalMatting(); - + void globalMatting(cv::InputArray _image, cv::InputArray _trimap, cv::OutputArray _foreground, cv::OutputArray _alpha, cv::OutputArray _conf); void getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: Mat &alpha,int niter=9); - - - - - - + }; } } - #endif -/* -int main(int argc,char** argv) -{ - if(argc<3) - { - cout<<"Enter the path of image and trimap"<(y, x) == 0) - alpha.at(y, x) = 0; - else if (trimap.at(y, x) == 255) - alpha.at(y, x) = 255; - } - - cv::imwrite("GT04-alpha.png", alpha); - - return 0; -} -*/ From 315c4ae6c2a716ea3e8c4b05ba21a6843a2e59c7 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 03:09:43 +0530 Subject: [PATCH 36/71] Removing trailing whitespaces --- .../ximgproc/samples/global_matting_sample.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/modules/ximgproc/samples/global_matting_sample.cpp b/modules/ximgproc/samples/global_matting_sample.cpp index 0dea8ee5e58..035e79bca6f 100644 --- a/modules/ximgproc/samples/global_matting_sample.cpp +++ b/modules/ximgproc/samples/global_matting_sample.cpp @@ -10,38 +10,34 @@ int main(int argc,char** argv) if(argc<3) { cout<<"arg1: Directory of Input image"< Date: Tue, 27 Aug 2019 03:12:08 +0530 Subject: [PATCH 37/71] Removing whitespaces --- modules/ximgproc/test/test_global_matting.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/modules/ximgproc/test/test_global_matting.cpp b/modules/ximgproc/test/test_global_matting.cpp index 4d8c3746a75..e483ba58960 100644 --- a/modules/ximgproc/test/test_global_matting.cpp +++ b/modules/ximgproc/test/test_global_matting.cpp @@ -3,7 +3,7 @@ namespace opencv_test { namespace -{ +{ const std::string INPUT_DIR = "matting"; const std::string IMAGE_FILENAME = "input/doll.png"; const std::string TRIMAP_FILENAME = "trimap/doll.png"; @@ -13,7 +13,7 @@ class CV_GlobalMattingTest : public cvtest::BaseTest { public: CV_GlobalMattingTest(); - + protected: Ptr gm; void runModel(); @@ -23,7 +23,7 @@ void CV_GlobalMattingTest::runModel() { std::string img_path = std::string(ts->get_data_path()) + INPUT_DIR + "/" + IMAGE_FILENAME; std::string trimap_path = std::string(ts->get_data_path()) + INPUT_DIR + "/" + TRIMAP_FILENAME; - + Mat img = imread(img_path); Mat trimap = imread(trimap_path); if(img.empty() || trimap.empty()) @@ -38,11 +38,9 @@ void CV_GlobalMattingTest::runModel() ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } - Mat foreground,alpha; int niter = 9; this->gm->getMat(img,trimap,foreground,alpha,niter); - if(alpha.empty()) { ts->printf(cvtest::TS::LOG,"Could not find the alpha matte for the image\n"); @@ -70,4 +68,4 @@ TEST(CV_GlobalMattingTest,accuracy) } } -} +} From cbed56d297aa2ed01caa22bde2250c23801b3087 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 03:19:35 +0530 Subject: [PATCH 38/71] Rename global_matting_sample.cpp to globalmatting.cpp --- .../samples/{global_matting_sample.cpp => globalmatting.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/ximgproc/samples/{global_matting_sample.cpp => globalmatting.cpp} (100%) diff --git a/modules/ximgproc/samples/global_matting_sample.cpp b/modules/ximgproc/samples/globalmatting.cpp similarity index 100% rename from modules/ximgproc/samples/global_matting_sample.cpp rename to modules/ximgproc/samples/globalmatting.cpp From 63686e0524bae45d07efe9f97a47179513b35133 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 03:20:16 +0530 Subject: [PATCH 39/71] Rename test_global_matting.cpp to test_globalmatting.cpp --- .../test/{test_global_matting.cpp => test_globalmatting.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/ximgproc/test/{test_global_matting.cpp => test_globalmatting.cpp} (100%) diff --git a/modules/ximgproc/test/test_global_matting.cpp b/modules/ximgproc/test/test_globalmatting.cpp similarity index 100% rename from modules/ximgproc/test/test_global_matting.cpp rename to modules/ximgproc/test/test_globalmatting.cpp From c3411d758058fd33c2e7a1fa12ddc0da9db01f52 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 03:48:14 +0530 Subject: [PATCH 40/71] Update globalmatting.cpp --- modules/ximgproc/samples/globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/samples/globalmatting.cpp b/modules/ximgproc/samples/globalmatting.cpp index 035e79bca6f..e6063fa1662 100644 --- a/modules/ximgproc/samples/globalmatting.cpp +++ b/modules/ximgproc/samples/globalmatting.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include From 87446072c1d9c4135b3f6fbaf587673b3d2394e6 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 04:10:55 +0530 Subject: [PATCH 41/71] Update globalmatting.cpp --- modules/ximgproc/samples/globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/samples/globalmatting.cpp b/modules/ximgproc/samples/globalmatting.cpp index e6063fa1662..3c6ffbcba26 100644 --- a/modules/ximgproc/samples/globalmatting.cpp +++ b/modules/ximgproc/samples/globalmatting.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include using namespace std; using namespace cv; From c1cc64c137172768cf7d10c1efa9443eaadd730f Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 04:18:59 +0530 Subject: [PATCH 42/71] Update globalmatting.cpp --- modules/ximgproc/samples/globalmatting.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/ximgproc/samples/globalmatting.cpp b/modules/ximgproc/samples/globalmatting.cpp index 3c6ffbcba26..d570698fb08 100644 --- a/modules/ximgproc/samples/globalmatting.cpp +++ b/modules/ximgproc/samples/globalmatting.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using namespace std; using namespace cv; From c07720b6b99c3d8bb7f32511cd468019b604dfc7 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 04:28:36 +0530 Subject: [PATCH 43/71] Update globalmatting.cpp --- modules/ximgproc/samples/globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/samples/globalmatting.cpp b/modules/ximgproc/samples/globalmatting.cpp index d570698fb08..b41000cc80e 100644 --- a/modules/ximgproc/samples/globalmatting.cpp +++ b/modules/ximgproc/samples/globalmatting.cpp @@ -22,7 +22,7 @@ int main(int argc,char** argv) { niter = atoi(argv[3]); } - cv::Mat image = cv::imread(img_path, CV_LOAD_IMAGE_COLOR); + cv::Mat image = cv::imread(img_path, cv::IMREAD_COLOR); cv::Mat trimap = cv::imread(tri_path, CV_LOAD_IMAGE_GRAYSCALE); if(image.empty() || trimap.empty()) { From ffca714c39a5dda77da91c543bb4277b3b59c2b8 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 04:33:03 +0530 Subject: [PATCH 44/71] Update globalmatting.cpp --- modules/ximgproc/samples/globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/samples/globalmatting.cpp b/modules/ximgproc/samples/globalmatting.cpp index b41000cc80e..9414acae9f7 100644 --- a/modules/ximgproc/samples/globalmatting.cpp +++ b/modules/ximgproc/samples/globalmatting.cpp @@ -23,7 +23,7 @@ int main(int argc,char** argv) niter = atoi(argv[3]); } cv::Mat image = cv::imread(img_path, cv::IMREAD_COLOR); - cv::Mat trimap = cv::imread(tri_path, CV_LOAD_IMAGE_GRAYSCALE); + cv::Mat trimap = cv::imread(tri_path, cv::IMREAD_GRAYSCALE); if(image.empty() || trimap.empty()) { cout<<"Could not load the inputs"< Date: Tue, 27 Aug 2019 12:45:00 +0530 Subject: [PATCH 45/71] Update test_globalmatting.cpp --- modules/ximgproc/test/test_globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/test/test_globalmatting.cpp b/modules/ximgproc/test/test_globalmatting.cpp index e483ba58960..b63437b2291 100644 --- a/modules/ximgproc/test/test_globalmatting.cpp +++ b/modules/ximgproc/test/test_globalmatting.cpp @@ -4,7 +4,7 @@ namespace opencv_test { namespace { -const std::string INPUT_DIR = "matting"; +const std::string INPUT_DIR = "ximgproc"; const std::string IMAGE_FILENAME = "input/doll.png"; const std::string TRIMAP_FILENAME = "trimap/doll.png"; From 147e9ecda93eed1d76cba0b39dbbd0f46ca01d28 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 18:29:31 +0530 Subject: [PATCH 46/71] Update test_globalmatting.cpp --- modules/ximgproc/test/test_globalmatting.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/modules/ximgproc/test/test_globalmatting.cpp b/modules/ximgproc/test/test_globalmatting.cpp index b63437b2291..3b4c9ba3243 100644 --- a/modules/ximgproc/test/test_globalmatting.cpp +++ b/modules/ximgproc/test/test_globalmatting.cpp @@ -16,7 +16,9 @@ class CV_GlobalMattingTest : public cvtest::BaseTest protected: Ptr gm; + virtual void run(int); void runModel(); + }; void CV_GlobalMattingTest::runModel() @@ -55,12 +57,17 @@ void CV_GlobalMattingTest::runModel() return; } } + +void CV_GlobalMattingTest::run(int) +{ + runModel(); +} CV_GlobalMattingTest::CV_GlobalMattingTest() { gm = makePtr(); } - +CV_ TEST(CV_GlobalMattingTest,accuracy) { CV_GlobalMattingTest test; From e7df48b7ce69a5b441dff8e821c11ac0fb0bd503 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 18:42:54 +0530 Subject: [PATCH 47/71] Update test_globalmatting.cpp --- modules/ximgproc/test/test_globalmatting.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/ximgproc/test/test_globalmatting.cpp b/modules/ximgproc/test/test_globalmatting.cpp index 3b4c9ba3243..f12d4a50521 100644 --- a/modules/ximgproc/test/test_globalmatting.cpp +++ b/modules/ximgproc/test/test_globalmatting.cpp @@ -58,16 +58,17 @@ void CV_GlobalMattingTest::runModel() } } +CV_GlobalMattingTest::CV_GlobalMattingTest() +{ + gm = makePtr(); +} void CV_GlobalMattingTest::run(int) { runModel(); } -CV_GlobalMattingTest::CV_GlobalMattingTest() -{ - gm = makePtr(); -} -CV_ + + TEST(CV_GlobalMattingTest,accuracy) { CV_GlobalMattingTest test; From fd92eecfac4062917c0f7f0299e557b638aa202c Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 18:50:35 +0530 Subject: [PATCH 48/71] Update globalmatting.hpp --- .../include/opencv2/ximgproc/globalmatting.hpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 0a6bba19ca3..ed978a42db8 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -57,16 +57,15 @@ class CV_EXPORTS GlobalMatting // Eq. 4 float distCost(const cv::Point &p0, const cv::Point &p1, float minDist); - float colorDist(const cv::Vec3b &I0, const cv::Vec3b &I1); + float colorDist(const cv::Vec3b &I0, const cv::Vec3b &I1); float nearestDistance(const std::vector &boundary, const cv::Point &p); - + void expansionOfKnownRegions(const cv::Mat_ &image, cv::Mat_ &trimap, int r, float c); - // erode foreground and background regions to increase the size of unknown region void erodeFB(cv::Mat_ &trimap, int r); @@ -95,10 +94,8 @@ class CV_EXPORTS GlobalMatting void erodeFB(cv::Mat &_trimap, int r); void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter); - void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); public: - GlobalMatting(); void globalMatting(cv::InputArray _image, cv::InputArray _trimap, cv::OutputArray _foreground, cv::OutputArray _alpha, cv::OutputArray _conf); @@ -106,7 +103,7 @@ class CV_EXPORTS GlobalMatting void getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: Mat &alpha,int niter=9); }; - + +} } -} #endif From f2773a13f46bef39529a1d0006079bee1052dc5b Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 18:52:11 +0530 Subject: [PATCH 49/71] Update globalmatting.cpp --- modules/ximgproc/samples/globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/samples/globalmatting.cpp b/modules/ximgproc/samples/globalmatting.cpp index 9414acae9f7..8613be5b4fa 100644 --- a/modules/ximgproc/samples/globalmatting.cpp +++ b/modules/ximgproc/samples/globalmatting.cpp @@ -11,7 +11,7 @@ int main(int argc,char** argv) if(argc<3) { cout<<"arg1: Directory of Input image"< Date: Tue, 27 Aug 2019 18:53:14 +0530 Subject: [PATCH 50/71] Update test_globalmatting.cpp --- modules/ximgproc/test/test_globalmatting.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/ximgproc/test/test_globalmatting.cpp b/modules/ximgproc/test/test_globalmatting.cpp index f12d4a50521..36a151bdf57 100644 --- a/modules/ximgproc/test/test_globalmatting.cpp +++ b/modules/ximgproc/test/test_globalmatting.cpp @@ -18,7 +18,7 @@ class CV_GlobalMattingTest : public cvtest::BaseTest Ptr gm; virtual void run(int); void runModel(); - + }; void CV_GlobalMattingTest::runModel() @@ -57,15 +57,15 @@ void CV_GlobalMattingTest::runModel() return; } } - + CV_GlobalMattingTest::CV_GlobalMattingTest() { gm = makePtr(); -} +} void CV_GlobalMattingTest::run(int) { runModel(); -} +} @@ -74,6 +74,6 @@ TEST(CV_GlobalMattingTest,accuracy) CV_GlobalMattingTest test; test.safe_run(); } - + } } From cdd78a8a2d45723e8ae5ce348d90aa31263e896a Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 18:59:17 +0530 Subject: [PATCH 51/71] Removing whitespaces --- modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index ed978a42db8..b849224e909 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -93,15 +93,15 @@ class CV_EXPORTS GlobalMatting // erode foreground and background regions to increase the size of unknown region void erodeFB(cv::Mat &_trimap, int r); - void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter); - void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); + void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter); + void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); public: GlobalMatting(); void globalMatting(cv::InputArray _image, cv::InputArray _trimap, cv::OutputArray _foreground, cv::OutputArray _alpha, cv::OutputArray _conf); void getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: Mat &alpha,int niter=9); - + }; } From 3bd6b022629ccdaeafac48b03bcf12bf09a9df60 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 19:05:53 +0530 Subject: [PATCH 52/71] Removed whitespaces --- modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index b849224e909..87f96aa030f 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -94,7 +94,7 @@ class CV_EXPORTS GlobalMatting void erodeFB(cv::Mat &_trimap, int r); void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter); - void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); + void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); public: GlobalMatting(); From 18e313d8019fe9b9a3e0ff91376f8b2e48ce0ab9 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 19:32:49 +0530 Subject: [PATCH 53/71] Update test_globalmatting.cpp --- modules/ximgproc/test/test_globalmatting.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/ximgproc/test/test_globalmatting.cpp b/modules/ximgproc/test/test_globalmatting.cpp index 36a151bdf57..95fbb1e3210 100644 --- a/modules/ximgproc/test/test_globalmatting.cpp +++ b/modules/ximgproc/test/test_globalmatting.cpp @@ -1,4 +1,5 @@ #include "test_precomp.hpp" +using namespace cv; namespace opencv_test { @@ -26,8 +27,8 @@ void CV_GlobalMattingTest::runModel() std::string img_path = std::string(ts->get_data_path()) + INPUT_DIR + "/" + IMAGE_FILENAME; std::string trimap_path = std::string(ts->get_data_path()) + INPUT_DIR + "/" + TRIMAP_FILENAME; - Mat img = imread(img_path); - Mat trimap = imread(trimap_path); + Mat img = cv::imread(img_path,cv::IMREAD_COLOR); + Mat trimap = cv::imread(trimap_path,cv::IMREAD_GRAYSCALE); if(img.empty() || trimap.empty()) { ts->printf(cvtest::TS::LOG,"Test images not found!\n"); From 02df381175ce5be06d5425de1b7655a597bac09a Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 20:04:58 +0530 Subject: [PATCH 54/71] Update test_globalmatting.cpp --- modules/ximgproc/test/test_globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/test/test_globalmatting.cpp b/modules/ximgproc/test/test_globalmatting.cpp index 95fbb1e3210..31b39c0208c 100644 --- a/modules/ximgproc/test/test_globalmatting.cpp +++ b/modules/ximgproc/test/test_globalmatting.cpp @@ -5,7 +5,7 @@ namespace opencv_test { namespace { -const std::string INPUT_DIR = "ximgproc"; +const std::string INPUT_DIR = "cv/ximgproc"; const std::string IMAGE_FILENAME = "input/doll.png"; const std::string TRIMAP_FILENAME = "trimap/doll.png"; From 6eacc415ab77c1b196482f898246071cfb8f3f67 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 27 Aug 2019 20:23:47 +0530 Subject: [PATCH 55/71] Update test_globalmatting.cpp --- modules/ximgproc/test/test_globalmatting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/test/test_globalmatting.cpp b/modules/ximgproc/test/test_globalmatting.cpp index 31b39c0208c..a63333a9fb4 100644 --- a/modules/ximgproc/test/test_globalmatting.cpp +++ b/modules/ximgproc/test/test_globalmatting.cpp @@ -35,7 +35,7 @@ void CV_GlobalMattingTest::runModel() ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); return; } - if(img.cols!=trimap.cols || img.rows!=trimap.cols) + if(img.cols!=trimap.cols || img.rows!=trimap.rows) { ts->printf(cvtest::TS::LOG,"Dimensions of trimap and the image are not the same"); ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA); From 2850fe66ad2b766fa0b4238acc7d221aff306192 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Wed, 28 Aug 2019 01:07:11 +0530 Subject: [PATCH 56/71] Update globalmatting.cpp --- modules/ximgproc/src/globalmatting.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index da124c17ee8..ff1490c98d4 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -282,8 +282,8 @@ void GlobalMatting::calculateAlphaPatchMatch(const cv::Mat_ &image, if (r < 1) break; - int di = r * (rand() / (RAND_MAX + 1.f)); - int dj = r * (rand() / (RAND_MAX + 1.f)); + int di = int(r * (rand() / (RAND_MAX + 1.f))); + int dj = int(r * (rand() / (RAND_MAX + 1.f))); int fi = s.fi + di; int bj = s.bj + dj; @@ -419,7 +419,7 @@ void GlobalMatting::expansionOfKnownRegions(cv::InputArray _img, cv::InputOutput CV_Error(CV_StsBadArg, "image and trimap mush have same size"); for (int i = 0; i < niter; ++i) - expansionOfKnownRegionsHelper(img, trimap, i + 1, niter - i); + expansionOfKnownRegionsHelper(img, trimap, i + 1, float(niter - i)); erodeFB(trimap, 2); } @@ -470,8 +470,8 @@ void GlobalMatting::globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat break; case 128: { - alpha(y, x) = 255 * samples[y][x].alpha; - conf(y, x) = 255 * exp(-samples[y][x].cost / 6); + alpha(y, x) = uchar(255 * samples[y][x].alpha); + conf(y, x) = uchar(255 * exp(-samples[y][x].cost / 6)); cv::Point p = foregroundBoundary[samples[y][x].fi]; foreground(y, x) = image(p.y, p.x); break; From 405aa9b683538f361320ddcf209034ae937c7142 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 1 Sep 2019 01:08:20 +0530 Subject: [PATCH 57/71] Removing global structure from header file --- .../include/opencv2/ximgproc/globalmatting.hpp | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 87f96aa030f..6ddc7d0cda1 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -8,24 +8,6 @@ #include #include -// for sorting the boundary pixels according to intensity -struct IntensityComp -{ - IntensityComp(const cv::Mat_ &img_temp) : img(img_temp) - { - - } - - bool operator()(const cv::Point &p0, const cv::Point &p1) const - { - const cv::Vec3b &c0 = img(p0.y, p0.x); - const cv::Vec3b &c1 = img(p1.y, p1.x); - - return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); - } - - const cv::Mat_ &img; -}; From f24a6b6e6f03bf67133053c72277b3bbe655aa62 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 1 Sep 2019 01:12:03 +0530 Subject: [PATCH 58/71] Added the global structure into cpp file --- modules/ximgproc/src/globalmatting.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index ff1490c98d4..00eb60d935f 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -8,6 +8,27 @@ namespace cv { namespace ximgproc { + +struct IntensityComp +{ + IntensityComp(const cv::Mat_ &image) : image(image) + { + + } + + bool operator()(const cv::Point &p0, const cv::Point &p1) const + { + const cv::Vec3b &c0 = image(p0.y, p0.x); + const cv::Vec3b &c1 = image(p1.y, p1.x); + + return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); + } + + const cv::Mat_ ℑ +}; + + + std::vector GlobalMatting::findBoundaryPixels(const cv::Mat_ &trimap, int a, int b) { std::vector result; From 65818c67dee7000544b46472ce69fdaf5f1143c0 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 1 Sep 2019 01:19:01 +0530 Subject: [PATCH 59/71] Added license and removed extra header files --- modules/ximgproc/src/globalmatting.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index 00eb60d935f..ea7f16b2b20 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +// 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 "precomp.hpp" From 60fef49b7068906d918d776fa35052f0feb9b4c0 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 1 Sep 2019 01:21:55 +0530 Subject: [PATCH 60/71] Added License information --- modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 6ddc7d0cda1..4b26ed0f6db 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -1,3 +1,6 @@ +// 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 GLOBAL_MATTING_H #define GLOBAL_MATTING_H From ac5f5ba9a9b83a641e00b9178c7595544b849c27 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 1 Sep 2019 01:27:59 +0530 Subject: [PATCH 61/71] Removed whitespaces --- modules/ximgproc/src/globalmatting.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index ea7f16b2b20..7574fe7ce63 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -8,7 +8,7 @@ namespace cv { namespace ximgproc { - + struct IntensityComp { IntensityComp(const cv::Mat_ &image) : image(image) @@ -28,7 +28,7 @@ struct IntensityComp }; - + std::vector GlobalMatting::findBoundaryPixels(const cv::Mat_ &trimap, int a, int b) { std::vector result; From 0af5e5fa9afef4852881a5838e003d38935d9aab Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 1 Sep 2019 01:40:01 +0530 Subject: [PATCH 62/71] Update globalmatting.cpp --- modules/ximgproc/src/globalmatting.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index 7574fe7ce63..14fdced8196 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -11,24 +11,26 @@ namespace ximgproc struct IntensityComp { - IntensityComp(const cv::Mat_ &image) : image(image) - { + IntensityComp(const cv::Mat_ &img_temp) : img(img_temp) + { - } + } - bool operator()(const cv::Point &p0, const cv::Point &p1) const - { - const cv::Vec3b &c0 = image(p0.y, p0.x); - const cv::Vec3b &c1 = image(p1.y, p1.x); + bool operator()(const cv::Point &p0, const cv::Point &p1) const + { + const cv::Vec3b &c0 = img(p0.y, p0.x); + const cv::Vec3b &c1 = img(p1.y, p1.x); - return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); - } + return ((int)c0[0] + (int)c0[1] + (int)c0[2]) < ((int)c1[0] + (int)c1[1] + (int)c1[2]); + } - const cv::Mat_ ℑ + const cv::Mat_ &img; }; + + std::vector GlobalMatting::findBoundaryPixels(const cv::Mat_ &trimap, int a, int b) { std::vector result; From cda943d42b994819a4ed0bf599d62e101108e2b5 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 1 Sep 2019 15:05:31 +0530 Subject: [PATCH 63/71] Create globalmatting.md --- modules/ximgproc/tutorials/globalmatting.md | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 modules/ximgproc/tutorials/globalmatting.md diff --git a/modules/ximgproc/tutorials/globalmatting.md b/modules/ximgproc/tutorials/globalmatting.md new file mode 100644 index 00000000000..66d003e5e58 --- /dev/null +++ b/modules/ximgproc/tutorials/globalmatting.md @@ -0,0 +1,31 @@ +### A Global sampling method for Alpha Matting ### + +The Global sampling based method uses all known samples(from a trimap), Unlike other sampling based methods +which collect only nearby samples.
+It first estimates the foreground and background color and then computes the alpha matte. + +Input to the model is the Image and the trimap, and the output is the alpha matte. + +This [blog post](https://medium.com/vedacv/paper-summary-a-global-sampling-method-for-alpha-matting-490a4217eb2) gives a summary of the paper. + +### Results ### + +After evaluating this implementation on alphamatting.com, the results are almost as good as the original implementation. + +Following were the results: + +| Error type | Original implementation | This implementation | +| ----------- | ------------------------ | ------------------- | +| Sum of absolute differences | 31 | 31.3 | +| Mean square error | 28.3 | 29.5 | +| Gradient error | 25 | 26.3 | +| Connectivity error | 28 | 36.3 | + + +Some of the outputs with of this implementation are as follows : + +| Image | Trimap | Alpha matte(this implementation) | +| -------------- | -------------- | ------------------------ | +|![alt text](https://raw.githubusercontent.com/Nerdyvedi/GSOC-Opencv-matting/master/Global-Sampling-Method-for-Alpha-Matting/Input/doll.png "img1" ) |![alt text](https://raw.githubusercontent.com/Nerdyvedi/GSOC-Opencv-matting/master/Global-Sampling-Method-for-Alpha-Matting/Trimap/doll.png "trimap1" ) |![alt text](https://raw.githubusercontent.com/Nerdyvedi/GSOC-Opencv-matting/master/Global-Sampling-Method-for-Alpha-Matting/Results/doll.png "results1" ) | +|![alt text](https://raw.githubusercontent.com/Nerdyvedi/GSOC-Opencv-matting/master/Global-Sampling-Method-for-Alpha-Matting/Input/troll.png "img2" ) |![alt text](https://raw.githubusercontent.com/Nerdyvedi/GSOC-Opencv-matting/master/Global-Sampling-Method-for-Alpha-Matting/Trimap/troll.png "trimap2" ) |![alt text](https://raw.githubusercontent.com/Nerdyvedi/GSOC-Opencv-matting/master/Global-Sampling-Method-for-Alpha-Matting/Results/troll.png "results2" ) | +|![alt text](https://raw.githubusercontent.com/Nerdyvedi/GSOC-Opencv-matting/master/Global-Sampling-Method-for-Alpha-Matting/Input/donkey.png "img1" ) |![alt text](https://raw.githubusercontent.com/Nerdyvedi/GSOC-Opencv-matting/master/Global-Sampling-Method-for-Alpha-Matting/Trimap/donkey.png "trimap1" ) |![alt text](https://raw.githubusercontent.com/Nerdyvedi/GSOC-Opencv-matting/master/Global-Sampling-Method-for-Alpha-Matting/Results/donkey.png "results1" ) | From 07821618b82389b7ceb3bfbb94f677d6c96beb6e Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 1 Sep 2019 15:06:50 +0530 Subject: [PATCH 64/71] Update globalmatting.md --- modules/ximgproc/tutorials/globalmatting.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/tutorials/globalmatting.md b/modules/ximgproc/tutorials/globalmatting.md index 66d003e5e58..38f82d9ff20 100644 --- a/modules/ximgproc/tutorials/globalmatting.md +++ b/modules/ximgproc/tutorials/globalmatting.md @@ -15,7 +15,7 @@ After evaluating this implementation on alphamatting.com, the results are almost Following were the results: | Error type | Original implementation | This implementation | -| ----------- | ------------------------ | ------------------- | +| ----------- | ------------------------ | ------------------- | | Sum of absolute differences | 31 | 31.3 | | Mean square error | 28.3 | 29.5 | | Gradient error | 25 | 26.3 | From e5e6be1ce75450749baefe1574ee904f56d0f5f6 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Tue, 3 Sep 2019 23:36:13 +0530 Subject: [PATCH 65/71] Update globalmatting.md --- modules/ximgproc/tutorials/globalmatting.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ximgproc/tutorials/globalmatting.md b/modules/ximgproc/tutorials/globalmatting.md index 38f82d9ff20..a7f402ae14e 100644 --- a/modules/ximgproc/tutorials/globalmatting.md +++ b/modules/ximgproc/tutorials/globalmatting.md @@ -4,7 +4,7 @@ The Global sampling based method uses all known samples(from a trimap), Unlike o which collect only nearby samples.
It first estimates the foreground and background color and then computes the alpha matte. -Input to the model is the Image and the trimap, and the output is the alpha matte. +Input to the model is the Image and the trimap, and the output is the alpha matte of the image. This [blog post](https://medium.com/vedacv/paper-summary-a-global-sampling-method-for-alpha-matting-490a4217eb2) gives a summary of the paper. From afce16a0f1b66473d01039a1835bd5042c663b67 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 8 Sep 2019 10:46:23 +0530 Subject: [PATCH 66/71] Removed square function from the public header --- .../ximgproc/include/opencv2/ximgproc/globalmatting.hpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp index 4b26ed0f6db..5b4cd160888 100644 --- a/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp +++ b/modules/ximgproc/include/opencv2/ximgproc/globalmatting.hpp @@ -23,14 +23,6 @@ class CV_EXPORTS GlobalMatting { private: - template - inline T sqr(T a) - { - return a * a; - } - - - std::vector findBoundaryPixels(const cv::Mat_ &trimap, int a, int b); // Eq. 2 From 7e739dfbbb918b1f15c54beabe6428222b5c09ca Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 8 Sep 2019 10:47:16 +0530 Subject: [PATCH 67/71] Added the square function to the source file --- modules/ximgproc/src/globalmatting.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index 14fdced8196..c63eca93eba 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -8,6 +8,11 @@ namespace cv { namespace ximgproc { +template +inline T sqr(T a) +{ + return a * a; +} struct IntensityComp { From f238144e015ea4c43f786785ed9d51b446a5631f Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 8 Sep 2019 16:47:24 +0530 Subject: [PATCH 68/71] Added license information and removed BaseTest --- modules/ximgproc/test/test_globalmatting.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/ximgproc/test/test_globalmatting.cpp b/modules/ximgproc/test/test_globalmatting.cpp index a63333a9fb4..83642968d2b 100644 --- a/modules/ximgproc/test/test_globalmatting.cpp +++ b/modules/ximgproc/test/test_globalmatting.cpp @@ -1,3 +1,6 @@ +// 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 "test_precomp.hpp" using namespace cv; @@ -10,7 +13,7 @@ const std::string IMAGE_FILENAME = "input/doll.png"; const std::string TRIMAP_FILENAME = "trimap/doll.png"; -class CV_GlobalMattingTest : public cvtest::BaseTest +class CV_GlobalMattingTest { public: CV_GlobalMattingTest(); From 48abe60dc8821c2181ba09279324ed61734933da Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 8 Sep 2019 16:51:48 +0530 Subject: [PATCH 69/71] Using cvtest::findDatafile --- modules/ximgproc/test/test_globalmatting.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/ximgproc/test/test_globalmatting.cpp b/modules/ximgproc/test/test_globalmatting.cpp index 83642968d2b..0c7d9d2999d 100644 --- a/modules/ximgproc/test/test_globalmatting.cpp +++ b/modules/ximgproc/test/test_globalmatting.cpp @@ -27,8 +27,9 @@ class CV_GlobalMattingTest void CV_GlobalMattingTest::runModel() { - std::string img_path = std::string(ts->get_data_path()) + INPUT_DIR + "/" + IMAGE_FILENAME; - std::string trimap_path = std::string(ts->get_data_path()) + INPUT_DIR + "/" + TRIMAP_FILENAME; + std::string folder = std::string(cvtest::TS::ptr()->get_data_path()); + std::string img_path = folder + INPUT_DIR + "/" + IMAGE_FILENAME; + std::string trimap_path = folder + INPUT_DIR + "/" + TRIMAP_FILENAME; Mat img = cv::imread(img_path,cv::IMREAD_COLOR); Mat trimap = cv::imread(trimap_path,cv::IMREAD_GRAYSCALE); From 93f54a5e49929427f8aa20af2b5b5b1a87842e35 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 8 Sep 2019 17:00:37 +0530 Subject: [PATCH 70/71] Bringing y to outer loop and x in inner loop --- modules/ximgproc/src/globalmatting.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/ximgproc/src/globalmatting.cpp b/modules/ximgproc/src/globalmatting.cpp index c63eca93eba..9de1b9a8da4 100644 --- a/modules/ximgproc/src/globalmatting.cpp +++ b/modules/ximgproc/src/globalmatting.cpp @@ -351,8 +351,8 @@ void GlobalMatting::expansionOfKnownRegionsHelper(const cv::Mat &_image, int w = image.cols; int h = image.rows; - for (int x = 0; x < w; ++x) - for (int y = 0; y < h; ++y) + for (int y = 0; y < h; ++y) + for (int x = 0; x < w; ++x) { if (trimap(y, x) != 128) continue; @@ -383,8 +383,8 @@ void GlobalMatting::expansionOfKnownRegionsHelper(const cv::Mat &_image, } } - for (int x = 0; x < trimap.cols; ++x) - for (int y = 0; y < trimap.rows; ++y) + for (int y = 0; y < trimap.rows; ++y) + for (int x = 0; x < trimap.cols; ++x) { if (trimap(y, x) == 1) trimap(y, x) = 0; From 0a7ca9e2d718e2a3aa7eb64484d65442d38cf7a0 Mon Sep 17 00:00:00 2001 From: Vedanta Jha Date: Sun, 8 Sep 2019 17:08:20 +0530 Subject: [PATCH 71/71] Added license information to sample --- modules/ximgproc/samples/globalmatting.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/ximgproc/samples/globalmatting.cpp b/modules/ximgproc/samples/globalmatting.cpp index 8613be5b4fa..564633acbc0 100644 --- a/modules/ximgproc/samples/globalmatting.cpp +++ b/modules/ximgproc/samples/globalmatting.cpp @@ -1,3 +1,6 @@ +// 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 #include #include