|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | +#ifndef __OPENCV_XIMGPROC_GLOBAL_MATTING_HPP__ |
| 5 | +#define __OPENCV_XIMGPROC_GLOBAL_MATTING_HPP__ |
| 6 | + |
| 7 | +#include <opencv2/imgproc.hpp> |
| 8 | +#include <opencv2/ximgproc/edge_filter.hpp> |
| 9 | + |
| 10 | + |
| 11 | +namespace cv |
| 12 | +{ |
| 13 | + |
| 14 | +namespace ximgproc |
| 15 | +{ |
| 16 | +class CV_EXPORTS GlobalMatting |
| 17 | +{ |
| 18 | + private: |
| 19 | + |
| 20 | + std::vector<cv::Point> findBoundaryPixels(const cv::Mat_<uchar> &trimap, int a, int b); |
| 21 | + |
| 22 | + // Eq. 2 |
| 23 | + float calculateAlpha(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I); |
| 24 | + |
| 25 | + // Eq. 3 |
| 26 | + float colorCost(const cv::Vec3b &F, const cv::Vec3b &B, const cv::Vec3b &I, float alpha); |
| 27 | + |
| 28 | + // Eq. 4 |
| 29 | + float distCost(const cv::Point &p0, const cv::Point &p1, float minDist); |
| 30 | + |
| 31 | + float colorDist(const cv::Vec3b &I0, const cv::Vec3b &I1); |
| 32 | + float nearestDistance(const std::vector<cv::Point> &boundary, const cv::Point &p); |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + void expansionOfKnownRegions(const cv::Mat_<cv::Vec3b> &image, |
| 38 | + cv::Mat_<uchar> &trimap, |
| 39 | + int r, float c); |
| 40 | + |
| 41 | + // erode foreground and background regions to increase the size of unknown region |
| 42 | + void erodeFB(cv::Mat_<uchar> &trimap, int r); |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + struct Sample |
| 47 | + { |
| 48 | + int fi, bj; |
| 49 | + float df, db; |
| 50 | + float cost, alpha; |
| 51 | + }; |
| 52 | + |
| 53 | + void calculateAlphaPatchMatch(const cv::Mat_<cv::Vec3b> &image, |
| 54 | + const cv::Mat_<uchar> &trimap, |
| 55 | + const std::vector<cv::Point> &foregroundBoundary, |
| 56 | + const std::vector<cv::Point> &backgroundBoundary, |
| 57 | + std::vector<std::vector<Sample> > &samples); |
| 58 | + |
| 59 | + void expansionOfKnownRegionsHelper(const cv::Mat &_image, |
| 60 | + cv::Mat &_trimap, |
| 61 | + int r, float c); |
| 62 | + |
| 63 | + |
| 64 | + // erode foreground and background regions to increase the size of unknown region |
| 65 | + void erodeFB(cv::Mat &_trimap, int r); |
| 66 | + |
| 67 | + void expansionOfKnownRegions(cv::InputArray _img, cv::InputOutputArray _trimap, int niter); |
| 68 | + void globalMattingHelper(cv::Mat _image, cv::Mat _trimap, cv::Mat &_foreground, cv::Mat &_alpha, cv::Mat &_conf); |
| 69 | + public: |
| 70 | + GlobalMatting(); |
| 71 | + |
| 72 | + void globalMatting(cv::InputArray _image, cv::InputArray _trimap, cv::OutputArray _foreground, cv::OutputArray _alpha, cv::OutputArray _conf); |
| 73 | + |
| 74 | + void getMat(cv::Mat image,cv::Mat trimap,cv::Mat &foreground,cv:: Mat &alpha,int niter=9); |
| 75 | + |
| 76 | +}; |
| 77 | + |
| 78 | +} |
| 79 | +} |
| 80 | +#endif |
0 commit comments