转自 zouxy09

GrabCut 原理参考这里,以下为 GrabCut 源码:

——看别人写的好的代码也很享受,干净利落,有些处理的细节也学习一下。

  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41.  
  42. #include "precomp.hpp"
  43. #include "gcgraph.hpp"
  44. #include <limits>
  45.  
  46. using namespace cv;
  47.  
  48. /*
  49. This is implementation of image segmentation algorithm GrabCut described in
  50. "GrabCut — Interactive Foreground Extraction using Iterated Graph Cuts".
  51. Carsten Rother, Vladimir Kolmogorov, Andrew Blake.
  52. */
  53.  
  54. /*
  55. GMM - Gaussian Mixture Model
  56. */
  57. class GMM
  58. {
  59. public:
  60. static const int componentsCount = 5;
  61.  
  62. GMM( Mat& _model );
  63. double operator()( const Vec3d color ) const;
  64. double operator()( int ci, const Vec3d color ) const;
  65. int whichComponent( const Vec3d color ) const;
  66.  
  67. void initLearning();
  68. void addSample( int ci, const Vec3d color );
  69. void endLearning();
  70.  
  71. private:
  72. void calcInverseCovAndDeterm( int ci );
  73. Mat model;
  74. double* coefs;
  75. double* mean;
  76. double* cov;
  77.  
  78. double inverseCovs[componentsCount][3][3]; //协方差的逆矩阵
  79. double covDeterms[componentsCount]; //协方差的行列式
  80.  
  81. double sums[componentsCount][3];
  82. double prods[componentsCount][3][3];
  83. int sampleCounts[componentsCount];
  84. int totalSampleCount;
  85. };
  86.  
  87. //背景和前景各有一个对应的GMM(混合高斯模型)
  88. GMM::GMM( Mat& _model )
  89. {
  90. //一个像素的(唯一对应)高斯模型的参数个数或者说一个高斯模型的参数个数
  91. //一个像素RGB三个通道值,故3个均值,3*3个协方差,共用一个权值
  92. const int modelSize = 3/*mean*/ + 9/*covariance*/ + 1/*component weight*/;
  93. if( _model.empty() )
  94. {
  95. //一个GMM共有componentsCount个高斯模型,一个高斯模型有modelSize个模型参数
  96. _model.create( 1, modelSize*componentsCount, CV_64FC1 );
  97. _model.setTo(Scalar(0));
  98. }
  99. else if( (_model.type() != CV_64FC1) || (_model.rows != 1) || (_model.cols != modelSize*componentsCount) )
  100. CV_Error( CV_StsBadArg, "_model must have CV_64FC1 type, rows == 1 and cols == 13*componentsCount" );
  101.  
  102. model = _model;
  103.  
  104. //注意这些模型参数的存储方式:先排完componentsCount个coefs,再3*componentsCount个mean。
  105. //再3*3*componentsCount个cov。
  106. coefs = model.ptr<double>(0); //GMM的每个像素的高斯模型的权值变量起始存储指针
  107. mean = coefs + componentsCount; //均值变量起始存储指针
  108. cov = mean + 3*componentsCount; //协方差变量起始存储指针
  109.  
  110. for( int ci = 0; ci < componentsCount; ci++ )
  111. if( coefs[ci] > 0 )
  112. //计算GMM中第ci个高斯模型的协方差的逆Inverse和行列式Determinant
  113. //为了后面计算每个像素属于该高斯模型的概率(也就是数据能量项)
  114. calcInverseCovAndDeterm( ci );
  115. }
  116.  
  117. //计算一个像素(由color=(B,G,R)三维double型向量来表示)属于这个GMM混合高斯模型的概率。
  118. //也就是把这个像素像素属于componentsCount个高斯模型的概率与对应的权值相乘再相加,
  119. //具体见论文的公式(10)。结果从res返回。
  120. //这个相当于计算Gibbs能量的第一个能量项(取负后)。
  121. double GMM::operator()( const Vec3d color ) const
  122. {
  123. double res = 0;
  124. for( int ci = 0; ci < componentsCount; ci++ )
  125. res += coefs[ci] * (*this)(ci, color );
  126. return res;
  127. }
  128.  
  129. //计算一个像素(由color=(B,G,R)三维double型向量来表示)属于第ci个高斯模型的概率。
  130. //具体过程,即高阶的高斯密度模型计算式,具体见论文的公式(10)。结果从res返回
  131. double GMM::operator()( int ci, const Vec3d color ) const
  132. {
  133. double res = 0;
  134. if( coefs[ci] > 0 )
  135. {
  136. CV_Assert( covDeterms[ci] > std::numeric_limits<double>::epsilon() );
  137. Vec3d diff = color;
  138. double* m = mean + 3*ci;
  139. diff[0] -= m[0]; diff[1] -= m[1]; diff[2] -= m[2];
  140. double mult = diff[0]*(diff[0]*inverseCovs[ci][0][0] + diff[1]*inverseCovs[ci][1][0] + diff[2]*inverseCovs[ci][2][0])
  141. + diff[1]*(diff[0]*inverseCovs[ci][0][1] + diff[1]*inverseCovs[ci][1][1] + diff[2]*inverseCovs[ci][2][1])
  142. + diff[2]*(diff[0]*inverseCovs[ci][0][2] + diff[1]*inverseCovs[ci][1][2] + diff[2]*inverseCovs[ci][2][2]);
  143. res = 1.0f/sqrt(covDeterms[ci]) * exp(-0.5f*mult);
  144. }
  145. return res;
  146. }
  147.  
  148. //返回这个像素最有可能属于GMM中的哪个高斯模型(概率最大的那个)
  149. int GMM::whichComponent( const Vec3d color ) const
  150. {
  151. int k = 0;
  152. double max = 0;
  153.  
  154. for( int ci = 0; ci < componentsCount; ci++ )
  155. {
  156. double p = (*this)( ci, color );
  157. if( p > max )
  158. {
  159. k = ci; //找到概率最大的那个,或者说计算结果最大的那个
  160. max = p;
  161. }
  162. }
  163. return k;
  164. }
  165.  
  166. //GMM参数学习前的初始化,主要是对要求和的变量置零
  167. void GMM::initLearning()
  168. {
  169. for( int ci = 0; ci < componentsCount; ci++)
  170. {
  171. sums[ci][0] = sums[ci][1] = sums[ci][2] = 0;
  172. prods[ci][0][0] = prods[ci][0][1] = prods[ci][0][2] = 0;
  173. prods[ci][1][0] = prods[ci][1][1] = prods[ci][1][2] = 0;
  174. prods[ci][2][0] = prods[ci][2][1] = prods[ci][2][2] = 0;
  175. sampleCounts[ci] = 0;
  176. }
  177. totalSampleCount = 0;
  178. }
  179.  
  180. //增加样本,即为前景或者背景GMM的第ci个高斯模型的像素集(这个像素集是来用估
  181. //计计算这个高斯模型的参数的)增加样本像素。计算加入color这个像素后,像素集
  182. //中所有像素的RGB三个通道的和sums(用来计算均值),还有它的prods(用来计算协方差),
  183. //并且记录这个像素集的像素个数和总的像素个数(用来计算这个高斯模型的权值)。
  184. void GMM::addSample( int ci, const Vec3d color )
  185. {
  186. sums[ci][0] += color[0]; sums[ci][1] += color[1]; sums[ci][2] += color[2];
  187. prods[ci][0][0] += color[0]*color[0]; prods[ci][0][1] += color[0]*color[1]; prods[ci][0][2] += color[0]*color[2];
  188. prods[ci][1][0] += color[1]*color[0]; prods[ci][1][1] += color[1]*color[1]; prods[ci][1][2] += color[1]*color[2];
  189. prods[ci][2][0] += color[2]*color[0]; prods[ci][2][1] += color[2]*color[1]; prods[ci][2][2] += color[2]*color[2];
  190. sampleCounts[ci]++;
  191. totalSampleCount++;
  192. }
  193.  
  194. //从图像数据中学习GMM的参数:每一个高斯分量的权值、均值和协方差矩阵;
  195. //这里相当于论文中“Iterative minimisation”的step 2
  196. void GMM::endLearning()
  197. {
  198. const double variance = 0.01;
  199. for( int ci = 0; ci < componentsCount; ci++ )
  200. {
  201. int n = sampleCounts[ci]; //第ci个高斯模型的样本像素个数
  202. if( n == 0 )
  203. coefs[ci] = 0;
  204. else
  205. {
  206. //计算第ci个高斯模型的权值系数
  207. coefs[ci] = (double)n/totalSampleCount;
  208.  
  209. //计算第ci个高斯模型的均值
  210. double* m = mean + 3*ci;
  211. m[0] = sums[ci][0]/n; m[1] = sums[ci][1]/n; m[2] = sums[ci][2]/n;
  212.  
  213. //计算第ci个高斯模型的协方差
  214. double* c = cov + 9*ci;
  215. c[0] = prods[ci][0][0]/n - m[0]*m[0]; c[1] = prods[ci][0][1]/n - m[0]*m[1]; c[2] = prods[ci][0][2]/n - m[0]*m[2];
  216. c[3] = prods[ci][1][0]/n - m[1]*m[0]; c[4] = prods[ci][1][1]/n - m[1]*m[1]; c[5] = prods[ci][1][2]/n - m[1]*m[2];
  217. c[6] = prods[ci][2][0]/n - m[2]*m[0]; c[7] = prods[ci][2][1]/n - m[2]*m[1]; c[8] = prods[ci][2][2]/n - m[2]*m[2];
  218.  
  219. //计算第ci个高斯模型的协方差的行列式
  220. double dtrm = c[0]*(c[4]*c[8]-c[5]*c[7]) - c[1]*(c[3]*c[8]-c[5]*c[6]) + c[2]*(c[3]*c[7]-c[4]*c[6]);
  221. if( dtrm <= std::numeric_limits<double>::epsilon() )
  222. {
  223. //相当于如果行列式小于等于0,(对角线元素)增加白噪声,避免其变
  224. //为退化(降秩)协方差矩阵(不存在逆矩阵,但后面的计算需要计算逆矩阵)。
  225. // Adds the white noise to avoid singular covariance matrix.
  226. c[0] += variance;
  227. c[4] += variance;
  228. c[8] += variance;
  229. }
  230.  
  231. //计算第ci个高斯模型的协方差的逆Inverse和行列式Determinant
  232. calcInverseCovAndDeterm(ci);
  233. }
  234. }
  235. }
  236.  
  237. //计算协方差的逆Inverse和行列式Determinant
  238. void GMM::calcInverseCovAndDeterm( int ci )
  239. {
  240. if( coefs[ci] > 0 )
  241. {
  242. //取第ci个高斯模型的协方差的起始指针
  243. double *c = cov + 9*ci;
  244. double dtrm =
  245. covDeterms[ci] = c[0]*(c[4]*c[8]-c[5]*c[7]) - c[1]*(c[3]*c[8]-c[5]*c[6])
  246. + c[2]*(c[3]*c[7]-c[4]*c[6]);
  247.  
  248. //在C++中,每一种内置的数据类型都拥有不同的属性, 使用<limits>库可以获
  249. //得这些基本数据类型的数值属性。因为浮点算法的截断,所以使得,当a=2,
  250. //b=3时 10*a/b == 20/b不成立。那怎么办呢?
  251. //这个小正数(epsilon)常量就来了,小正数通常为可用给定数据类型的
  252. //大于1的最小值与1之差来表示。若dtrm结果不大于小正数,那么它几乎为零。
  253. //所以下式保证dtrm>0,即行列式的计算正确(协方差对称正定,故行列式大于0)。
  254. CV_Assert( dtrm > std::numeric_limits<double>::epsilon() );
  255. //三阶方阵的求逆
  256. inverseCovs[ci][0][0] = (c[4]*c[8] - c[5]*c[7]) / dtrm;
  257. inverseCovs[ci][1][0] = -(c[3]*c[8] - c[5]*c[6]) / dtrm;
  258. inverseCovs[ci][2][0] = (c[3]*c[7] - c[4]*c[6]) / dtrm;
  259. inverseCovs[ci][0][1] = -(c[1]*c[8] - c[2]*c[7]) / dtrm;
  260. inverseCovs[ci][1][1] = (c[0]*c[8] - c[2]*c[6]) / dtrm;
  261. inverseCovs[ci][2][1] = -(c[0]*c[7] - c[1]*c[6]) / dtrm;
  262. inverseCovs[ci][0][2] = (c[1]*c[5] - c[2]*c[4]) / dtrm;
  263. inverseCovs[ci][1][2] = -(c[0]*c[5] - c[2]*c[3]) / dtrm;
  264. inverseCovs[ci][2][2] = (c[0]*c[4] - c[1]*c[3]) / dtrm;
  265. }
  266. }
  267.  
  268. //计算beta,也就是Gibbs能量项中的第二项(平滑项)中的指数项的beta,用来调整
  269. //高或者低对比度时,两个邻域像素的差别的影响的,例如在低对比度时,两个邻域
  270. //像素的差别可能就会比较小,这时候需要乘以一个较大的beta来放大这个差别,
  271. //在高对比度时,则需要缩小本身就比较大的差别。
  272. //所以我们需要分析整幅图像的对比度来确定参数beta,具体的见论文公式(5)。
  273. /*
  274. Calculate beta - parameter of GrabCut algorithm.
  275. beta = 1/(2*avg(sqr(||color[i] - color[j]||)))
  276. */
  277. static double calcBeta( const Mat& img )
  278. {
  279. double beta = 0;
  280. for( int y = 0; y < img.rows; y++ )
  281. {
  282. for( int x = 0; x < img.cols; x++ )
  283. {
  284. //计算四个方向邻域两像素的差别,也就是欧式距离或者说二阶范数
  285. //(当所有像素都算完后,就相当于计算八邻域的像素差了)
  286. Vec3d color = img.at<Vec3b>(y,x);
  287. if( x>0 ) // left >0的判断是为了避免在图像边界的时候还计算,导致越界
  288. {
  289. Vec3d diff = color - (Vec3d)img.at<Vec3b>(y,x-1);
  290. beta += diff.dot(diff); //矩阵的点乘,也就是各个元素平方的和
  291. }
  292. if( y>0 && x>0 ) // upleft
  293. {
  294. Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x-1);
  295. beta += diff.dot(diff);
  296. }
  297. if( y>0 ) // up
  298. {
  299. Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x);
  300. beta += diff.dot(diff);
  301. }
  302. if( y>0 && x<img.cols-1) // upright
  303. {
  304. Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x+1);
  305. beta += diff.dot(diff);
  306. }
  307. }
  308. }
  309. if( beta <= std::numeric_limits<double>::epsilon() )
  310. beta = 0;
  311. else
  312. beta = 1.f / (2 * beta/(4*img.cols*img.rows - 3*img.cols - 3*img.rows + 2) ); //论文公式(5)
  313.  
  314. return beta;
  315. }
  316.  
  317. //计算图每个非端点顶点(也就是每个像素作为图的一个顶点,不包括源点s和汇点t)与邻域顶点
  318. //的边的权值。由于是无向图,我们计算的是八邻域,那么对于一个顶点,我们计算四个方向就行,
  319. //在其他的顶点计算的时候,会把剩余那四个方向的权值计算出来。这样整个图算完后,每个顶点
  320. //与八邻域的顶点的边的权值就都计算出来了。
  321. //这个相当于计算Gibbs能量的第二个能量项(平滑项),具体见论文中公式(4)
  322. /*
  323. Calculate weights of noterminal vertices of graph.
  324. beta and gamma - parameters of GrabCut algorithm.
  325. */
  326. static void calcNWeights( const Mat& img, Mat& leftW, Mat& upleftW, Mat& upW,
  327. Mat& uprightW, double beta, double gamma )
  328. {
  329. //gammaDivSqrt2相当于公式(4)中的gamma * dis(i,j)^(-1),那么可以知道,
  330. //当i和j是垂直或者水平关系时,dis(i,j)=1,当是对角关系时,dis(i,j)=sqrt(2.0f)。
  331. //具体计算时,看下面就明白了
  332. const double gammaDivSqrt2 = gamma / std::sqrt(2.0f);
  333. //每个方向的边的权值通过一个和图大小相等的Mat来保存
  334. leftW.create( img.rows, img.cols, CV_64FC1 );
  335. upleftW.create( img.rows, img.cols, CV_64FC1 );
  336. upW.create( img.rows, img.cols, CV_64FC1 );
  337. uprightW.create( img.rows, img.cols, CV_64FC1 );
  338. for( int y = 0; y < img.rows; y++ )
  339. {
  340. for( int x = 0; x < img.cols; x++ )
  341. {
  342. Vec3d color = img.at<Vec3b>(y,x);
  343. if( x-1>=0 ) // left //避免图的边界
  344. {
  345. Vec3d diff = color - (Vec3d)img.at<Vec3b>(y,x-1);
  346. leftW.at<double>(y,x) = gamma * exp(-beta*diff.dot(diff));
  347. }
  348. else
  349. leftW.at<double>(y,x) = 0;
  350. if( x-1>=0 && y-1>=0 ) // upleft
  351. {
  352. Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x-1);
  353. upleftW.at<double>(y,x) = gammaDivSqrt2 * exp(-beta*diff.dot(diff));
  354. }
  355. else
  356. upleftW.at<double>(y,x) = 0;
  357. if( y-1>=0 ) // up
  358. {
  359. Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x);
  360. upW.at<double>(y,x) = gamma * exp(-beta*diff.dot(diff));
  361. }
  362. else
  363. upW.at<double>(y,x) = 0;
  364. if( x+1<img.cols && y-1>=0 ) // upright
  365. {
  366. Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x+1);
  367. uprightW.at<double>(y,x) = gammaDivSqrt2 * exp(-beta*diff.dot(diff));
  368. }
  369. else
  370. uprightW.at<double>(y,x) = 0;
  371. }
  372. }
  373. }
  374.  
  375. //检查mask的正确性。mask为通过用户交互或者程序设定的,它是和图像大小一样的单通道灰度图,
  376. //每个像素只能取GC_BGD or GC_FGD or GC_PR_BGD or GC_PR_FGD 四种枚举值,分别表示该像素
  377. //(用户或者程序指定)属于背景、前景、可能为背景或者可能为前景像素。具体的参考:
  378. //ICCV2001“Interactive Graph Cuts for Optimal Boundary & Region Segmentation of Objects in N-D Images”
  379. //Yuri Y. Boykov Marie-Pierre Jolly
  380. /*
  381. Check size, type and element values of mask matrix.
  382. */
  383. static void checkMask( const Mat& img, const Mat& mask )
  384. {
  385. if( mask.empty() )
  386. CV_Error( CV_StsBadArg, "mask is empty" );
  387. if( mask.type() != CV_8UC1 )
  388. CV_Error( CV_StsBadArg, "mask must have CV_8UC1 type" );
  389. if( mask.cols != img.cols || mask.rows != img.rows )
  390. CV_Error( CV_StsBadArg, "mask must have as many rows and cols as img" );
  391. for( int y = 0; y < mask.rows; y++ )
  392. {
  393. for( int x = 0; x < mask.cols; x++ )
  394. {
  395. uchar val = mask.at<uchar>(y,x);
  396. if( val!=GC_BGD && val!=GC_FGD && val!=GC_PR_BGD && val!=GC_PR_FGD )
  397. CV_Error( CV_StsBadArg, "mask element value must be equel"
  398. "GC_BGD or GC_FGD or GC_PR_BGD or GC_PR_FGD" );
  399. }
  400. }
  401. }
  402.  
  403. //通过用户框选目标rect来创建mask,rect外的全部作为背景,设置为GC_BGD,
  404. //rect内的设置为 GC_PR_FGD(可能为前景)
  405. /*
  406. Initialize mask using rectangular.
  407. */
  408. static void initMaskWithRect( Mat& mask, Size imgSize, Rect rect )
  409. {
  410. mask.create( imgSize, CV_8UC1 );
  411. mask.setTo( GC_BGD );
  412.  
  413. rect.x = max(0, rect.x);
  414. rect.y = max(0, rect.y);
  415. rect.width = min(rect.width, imgSize.width-rect.x);
  416. rect.height = min(rect.height, imgSize.height-rect.y);
  417.  
  418. (mask(rect)).setTo( Scalar(GC_PR_FGD) );
  419. }
  420.  
  421. //通过k-means算法来初始化背景GMM和前景GMM模型
  422. /*
  423. Initialize GMM background and foreground models using kmeans algorithm.
  424. */
  425. static void initGMMs( const Mat& img, const Mat& mask, GMM& bgdGMM, GMM& fgdGMM )
  426. {
  427. const int kMeansItCount = 10; //迭代次数
  428. const int kMeansType = KMEANS_PP_CENTERS; //Use kmeans++ center initialization by Arthur and Vassilvitskii
  429.  
  430. Mat bgdLabels, fgdLabels; //记录背景和前景的像素样本集中每个像素对应GMM的哪个高斯模型,论文中的kn
  431. vector<Vec3f> bgdSamples, fgdSamples; //背景和前景的像素样本集
  432. Point p;
  433. for( p.y = 0; p.y < img.rows; p.y++ )
  434. {
  435. for( p.x = 0; p.x < img.cols; p.x++ )
  436. {
  437. //mask中标记为GC_BGD和GC_PR_BGD的像素都作为背景的样本像素
  438. if( mask.at<uchar>(p) == GC_BGD || mask.at<uchar>(p) == GC_PR_BGD )
  439. bgdSamples.push_back( (Vec3f)img.at<Vec3b>(p) );
  440. else // GC_FGD | GC_PR_FGD
  441. fgdSamples.push_back( (Vec3f)img.at<Vec3b>(p) );
  442. }
  443. }
  444. CV_Assert( !bgdSamples.empty() && !fgdSamples.empty() );
  445.  
  446. //kmeans中参数_bgdSamples为:每行一个样本
  447. //kmeans的输出为bgdLabels,里面保存的是输入样本集中每一个样本对应的类标签(样本聚为componentsCount类后)
  448. Mat _bgdSamples( (int)bgdSamples.size(), 3, CV_32FC1, &bgdSamples[0][0] );
  449. kmeans( _bgdSamples, GMM::componentsCount, bgdLabels,
  450. TermCriteria( CV_TERMCRIT_ITER, kMeansItCount, 0.0), 0, kMeansType );
  451. Mat _fgdSamples( (int)fgdSamples.size(), 3, CV_32FC1, &fgdSamples[0][0] );
  452. kmeans( _fgdSamples, GMM::componentsCount, fgdLabels,
  453. TermCriteria( CV_TERMCRIT_ITER, kMeansItCount, 0.0), 0, kMeansType );
  454.  
  455. //经过上面的步骤后,每个像素所属的高斯模型就确定的了,那么就可以估计GMM中每个高斯模型的参数了。
  456. bgdGMM.initLearning();
  457. for( int i = 0; i < (int)bgdSamples.size(); i++ )
  458. bgdGMM.addSample( bgdLabels.at<int>(i,0), bgdSamples[i] );
  459. bgdGMM.endLearning();
  460.  
  461. fgdGMM.initLearning();
  462. for( int i = 0; i < (int)fgdSamples.size(); i++ )
  463. fgdGMM.addSample( fgdLabels.at<int>(i,0), fgdSamples[i] );
  464. fgdGMM.endLearning();
  465. }
  466.  
  467. //论文中:迭代最小化算法step 1:为每个像素分配GMM中所属的高斯模型,kn保存在Mat compIdxs中
  468. /*
  469. Assign GMMs components for each pixel.
  470. */
  471. static void assignGMMsComponents( const Mat& img, const Mat& mask, const GMM& bgdGMM,
  472. const GMM& fgdGMM, Mat& compIdxs )
  473. {
  474. Point p;
  475. for( p.y = 0; p.y < img.rows; p.y++ )
  476. {
  477. for( p.x = 0; p.x < img.cols; p.x++ )
  478. {
  479. Vec3d color = img.at<Vec3b>(p);
  480. //通过mask来判断该像素属于背景像素还是前景像素,再判断它属于前景或者背景GMM中的哪个高斯分量
  481. compIdxs.at<int>(p) = mask.at<uchar>(p) == GC_BGD || mask.at<uchar>(p) == GC_PR_BGD ?
  482. bgdGMM.whichComponent(color) : fgdGMM.whichComponent(color);
  483. }
  484. }
  485. }
  486.  
  487. //论文中:迭代最小化算法step 2:从每个高斯模型的像素样本集中学习每个高斯模型的参数
  488. /*
  489. Learn GMMs parameters.
  490. */
  491. static void learnGMMs( const Mat& img, const Mat& mask, const Mat& compIdxs, GMM& bgdGMM, GMM& fgdGMM )
  492. {
  493. bgdGMM.initLearning();
  494. fgdGMM.initLearning();
  495. Point p;
  496. for( int ci = 0; ci < GMM::componentsCount; ci++ )
  497. {
  498. for( p.y = 0; p.y < img.rows; p.y++ )
  499. {
  500. for( p.x = 0; p.x < img.cols; p.x++ )
  501. {
  502. if( compIdxs.at<int>(p) == ci )
  503. {
  504. if( mask.at<uchar>(p) == GC_BGD || mask.at<uchar>(p) == GC_PR_BGD )
  505. bgdGMM.addSample( ci, img.at<Vec3b>(p) );
  506. else
  507. fgdGMM.addSample( ci, img.at<Vec3b>(p) );
  508. }
  509. }
  510. }
  511. }
  512. bgdGMM.endLearning();
  513. fgdGMM.endLearning();
  514. }
  515.  
  516. //通过计算得到的能量项构建图,图的顶点为像素点,图的边由两部分构成,
  517. //一类边是:每个顶点与Sink汇点t(代表背景)和源点Source(代表前景)连接的边,
  518. //这类边的权值通过Gibbs能量项的第一项能量项来表示。
  519. //另一类边是:每个顶点与其邻域顶点连接的边,这类边的权值通过Gibbs能量项的第二项能量项来表示。
  520. /*
  521. Construct GCGraph
  522. */
  523. static void constructGCGraph( const Mat& img, const Mat& mask, const GMM& bgdGMM, const GMM& fgdGMM, double lambda,
  524. const Mat& leftW, const Mat& upleftW, const Mat& upW, const Mat& uprightW,
  525. GCGraph<double>& graph )
  526. {
  527. int vtxCount = img.cols*img.rows; //顶点数,每一个像素是一个顶点
  528. int edgeCount = 2*(4*vtxCount - 3*(img.cols + img.rows) + 2); //边数,需要考虑图边界的边的缺失
  529. //通过顶点数和边数创建图。这些类型声明和函数定义请参考gcgraph.hpp
  530. graph.create(vtxCount, edgeCount);
  531. Point p;
  532. for( p.y = 0; p.y < img.rows; p.y++ )
  533. {
  534. for( p.x = 0; p.x < img.cols; p.x++)
  535. {
  536. // add node
  537. int vtxIdx = graph.addVtx(); //返回这个顶点在图中的索引
  538. Vec3b color = img.at<Vec3b>(p);
  539.  
  540. // set t-weights
  541. //计算每个顶点与Sink汇点t(代表背景)和源点Source(代表前景)连接的权值。
  542. //也即计算Gibbs能量(每一个像素点作为背景像素或者前景像素)的第一个能量项
  543. double fromSource, toSink;
  544. if( mask.at<uchar>(p) == GC_PR_BGD || mask.at<uchar>(p) == GC_PR_FGD )
  545. {
  546. //对每一个像素计算其作为背景像素或者前景像素的第一个能量项,作为分别与t和s点的连接权值
  547. fromSource = -log( bgdGMM(color) );
  548. toSink = -log( fgdGMM(color) );
  549. }
  550. else if( mask.at<uchar>(p) == GC_BGD )
  551. {
  552. //对于确定为背景的像素点,它与Source点(前景)的连接为0,与Sink点的连接为lambda
  553. fromSource = 0;
  554. toSink = lambda;
  555. }
  556. else // GC_FGD
  557. {
  558. fromSource = lambda;
  559. toSink = 0;
  560. }
  561. //设置该顶点vtxIdx分别与Source点和Sink点的连接权值
  562. graph.addTermWeights( vtxIdx, fromSource, toSink );
  563.  
  564. // set n-weights n-links
  565. //计算两个邻域顶点之间连接的权值。
  566. //也即计算Gibbs能量的第二个能量项(平滑项)
  567. if( p.x>0 )
  568. {
  569. double w = leftW.at<double>(p);
  570. graph.addEdges( vtxIdx, vtxIdx-1, w, w );
  571. }
  572. if( p.x>0 && p.y>0 )
  573. {
  574. double w = upleftW.at<double>(p);
  575. graph.addEdges( vtxIdx, vtxIdx-img.cols-1, w, w );
  576. }
  577. if( p.y>0 )
  578. {
  579. double w = upW.at<double>(p);
  580. graph.addEdges( vtxIdx, vtxIdx-img.cols, w, w );
  581. }
  582. if( p.x<img.cols-1 && p.y>0 )
  583. {
  584. double w = uprightW.at<double>(p);
  585. graph.addEdges( vtxIdx, vtxIdx-img.cols+1, w, w );
  586. }
  587. }
  588. }
  589. }
  590.  
  591. //论文中:迭代最小化算法step 3:分割估计:最小割或者最大流算法
  592. /*
  593. Estimate segmentation using MaxFlow algorithm
  594. */
  595. static void estimateSegmentation( GCGraph<double>& graph, Mat& mask )
  596. {
  597. //通过最大流算法确定图的最小割,也即完成图像的分割
  598. graph.maxFlow();
  599. Point p;
  600. for( p.y = 0; p.y < mask.rows; p.y++ )
  601. {
  602. for( p.x = 0; p.x < mask.cols; p.x++ )
  603. {
  604. //通过图分割的结果来更新mask,即最后的图像分割结果。注意的是,永远都
  605. //不会更新用户指定为背景或者前景的像素
  606. if( mask.at<uchar>(p) == GC_PR_BGD || mask.at<uchar>(p) == GC_PR_FGD )
  607. {
  608. if( graph.inSourceSegment( p.y*mask.cols+p.x /*vertex index*/ ) )
  609. mask.at<uchar>(p) = GC_PR_FGD;
  610. else
  611. mask.at<uchar>(p) = GC_PR_BGD;
  612. }
  613. }
  614. }
  615. }
  616.  
  617. //最后的成果:提供给外界使用的伟大的API:grabCut
  618. /*
  619. ****参数说明:
  620. img——待分割的源图像,必须是8位3通道(CV_8UC3)图像,在处理的过程中不会被修改;
  621. mask——掩码图像,如果使用掩码进行初始化,那么mask保存初始化掩码信息;在执行分割
  622. 的时候,也可以将用户交互所设定的前景与背景保存到mask中,然后再传入grabCut函
  623. 数;在处理结束之后,mask中会保存结果。mask只能取以下四种值:
  624. GCD_BGD(=0),背景;
  625. GCD_FGD(=1),前景;
  626. GCD_PR_BGD(=2),可能的背景;
  627. GCD_PR_FGD(=3),可能的前景。
  628. 如果没有手工标记GCD_BGD或者GCD_FGD,那么结果只会有GCD_PR_BGD或GCD_PR_FGD;
  629. rect——用于限定需要进行分割的图像范围,只有该矩形窗口内的图像部分才被处理;
  630. bgdModel——背景模型,如果为null,函数内部会自动创建一个bgdModel;bgdModel必须是
  631. 单通道浮点型(CV_32FC1)图像,且行数只能为1,列数只能为13x5;
  632. fgdModel——前景模型,如果为null,函数内部会自动创建一个fgdModel;fgdModel必须是
  633. 单通道浮点型(CV_32FC1)图像,且行数只能为1,列数只能为13x5;
  634. iterCount——迭代次数,必须大于0;
  635. mode——用于指示grabCut函数进行什么操作,可选的值有:
  636. GC_INIT_WITH_RECT(=0),用矩形窗初始化GrabCut;
  637. GC_INIT_WITH_MASK(=1),用掩码图像初始化GrabCut;
  638. GC_EVAL(=2),执行分割。
  639. */
  640. void cv::grabCut( InputArray _img, InputOutputArray _mask, Rect rect,
  641. InputOutputArray _bgdModel, InputOutputArray _fgdModel,
  642. int iterCount, int mode )
  643. {
  644. Mat img = _img.getMat();
  645. Mat& mask = _mask.getMatRef();
  646. Mat& bgdModel = _bgdModel.getMatRef();
  647. Mat& fgdModel = _fgdModel.getMatRef();
  648.  
  649. if( img.empty() )
  650. CV_Error( CV_StsBadArg, "image is empty" );
  651. if( img.type() != CV_8UC3 )
  652. CV_Error( CV_StsBadArg, "image mush have CV_8UC3 type" );
  653.  
  654. GMM bgdGMM( bgdModel ), fgdGMM( fgdModel );
  655. Mat compIdxs( img.size(), CV_32SC1 );
  656.  
  657. if( mode == GC_INIT_WITH_RECT || mode == GC_INIT_WITH_MASK )
  658. {
  659. if( mode == GC_INIT_WITH_RECT )
  660. initMaskWithRect( mask, img.size(), rect );
  661. else // flag == GC_INIT_WITH_MASK
  662. checkMask( img, mask );
  663. initGMMs( img, mask, bgdGMM, fgdGMM );
  664. }
  665.  
  666. if( iterCount <= 0)
  667. return;
  668.  
  669. if( mode == GC_EVAL )
  670. checkMask( img, mask );
  671.  
  672. const double gamma = 50;
  673. const double lambda = 9*gamma;
  674. const double beta = calcBeta( img );
  675.  
  676. Mat leftW, upleftW, upW, uprightW;
  677. calcNWeights( img, leftW, upleftW, upW, uprightW, beta, gamma );
  678.  
  679. for( int i = 0; i < iterCount; i++ )
  680. {
  681. GCGraph<double> graph;
  682. assignGMMsComponents( img, mask, bgdGMM, fgdGMM, compIdxs );
  683. learnGMMs( img, mask, compIdxs, bgdGMM, fgdGMM );
  684. constructGCGraph(img, mask, bgdGMM, fgdGMM, lambda, leftW, upleftW, upW, uprightW, graph );
  685. estimateSegmentation( graph, mask );
  686. }
  687. }

其中 gcgraph.hpp 转自 http://blog.csdn.net/wstcegg/article/details/39495535,先存着还没有看。

  1. #ifndef _CV_GCGRAPH_H_
  2. #define _CV_GCGRAPH_H_
  3.  
  4. template <class TWeight> class GCGraph
  5. {
  6. public:
  7. GCGraph();
  8. GCGraph( unsigned int vtxCount, unsigned int edgeCount );
  9. ~GCGraph();
  10. void create( unsigned int vtxCount, unsigned int edgeCount );
  11. int addVtx();
  12. void addEdges( int i, int j, TWeight w, TWeight revw );
  13. void addTermWeights( int i, TWeight sourceW, TWeight sinkW );
  14. TWeight maxFlow();
  15. bool inSourceSegment( int i );
  16. private:
  17. class Vtx //结点类型
  18. {
  19. public:
  20. Vtx *next; //在maxflow算法中用于构建先进-先出队列
  21. int parent; //父节点发出的弧
  22. int first; //首个相邻弧
  23. int ts; //time-stamp时间戳
  24. int dist; //distance,到树根的距离
  25. TWeight weight; //t-value, 即到终端结点的权值
  26. uchar t; //取值只能是0-1,s->t方向为0, t->s方向为1
  27. };
  28. class Edge //边类型
  29. {
  30. public:
  31. int dst; //弧指向的顶点
  32. int next; //同一个原点的下一条弧
  33. TWeight weight; //n-value, 弧的权值
  34. };
  35.  
  36. std::vector<Vtx> vtcs; //结点集合
  37. std::vector<Edge> edges; //弧集合
  38. TWeight flow; //图的流量
  39. };
  40.  
  41. template <class TWeight>
  42. GCGraph<TWeight>::GCGraph()
  43. {
  44. flow = 0; //流量初始化为0
  45. }
  46. GCGraph<TWeight>::GCGraph( unsigned int vtxCount, unsigned int edgeCount )
  47. {
  48. //构造函数并没有实质性的内容,而是把工作交给了create函数
  49. //大家一起想想是为啥~
  50. create( vtxCount, edgeCount );
  51. }
  52. template <class TWeight>
  53. GCGraph<TWeight>::~GCGraph()
  54. {
  55. }
  56. template <class TWeight>
  57. void GCGraph<TWeight>::create( unsigned int vtxCount, unsigned int edgeCount )
  58. {
  59. //原来creeate也没啥东西啊
  60. //为了提高内存分配效率,给两个集合预留足够空间
  61. vtcs.reserve( vtxCount );
  62. edges.reserve( edgeCount + 2 );
  63. flow = 0;
  64. }
  65.  
  66. /*
  67. 函数功能:
  68. 添加一个空结点,所有成员初始化为空
  69. 参数说明:

  70. 返回值:
  71. 当前结点在集合中的编号
  72. */
  73. template <class TWeight>
  74. int GCGraph<TWeight>::addVtx()
  75. {
  76. //添加结点
  77. Vtx v;
  78.  
  79. //将结点申请到的内存空间全部清0(第二个参数0)
  80. //目的:由于结点中存在成员变量为指针,指针设置为null保证安全
  81. memset( &v, 0, sizeof(Vtx));
  82. vtcs.push_back(v);
  83.  
  84. return (int)vtcs.size() - 1; //返回结点集合尺寸-1
  85. }
  86.  
  87. /*
  88. 函数功能:
  89. 添加一条结点i和结点j之间的弧n-link弧(普通结点之间的弧)
  90. 参数说明:
  91. int---i: 弧头结点编号
  92. int---j: 弧尾结点编号
  93. Tweight---w: 正向弧权值
  94. Tweight---reww: 逆向弧权值
  95. 返回值:
  96.  
  97. */
  98. template <class TWeight>
  99. void GCGraph<TWeight>::addEdges( int i, int j, TWeight w, TWeight revw )
  100. {
  101. // 检查结点编号有效性
  102. CV_Assert( i>=0 && i<(int)vtcs.size() );
  103. CV_Assert( j>=0 && j<(int)vtcs.size() );
  104.  
  105. CV_Assert( w>=0 && revw>=0 ); // 检查弧权值有效性
  106. CV_Assert( i != j ); // 不存在指向自己的结点
  107.  
  108. if( !edges.size() )
  109. edges.resize( 2 );
  110.  
  111. // 正向弧:fromI, 反向弧 toI
  112. Edge fromI, toI;
  113.  
  114. //===================================================//
  115. //以下为插入正向弧的操作
  116. fromI.dst = j; // 正向弧指向结点j
  117. fromI.weight = w; // 正向弧赋予权值w
  118.  
  119. //注意,为便于解释,下方一行代码与上面一行代码交换了顺序,没有任何实际影响
  120. //每个结点所发出的全部n-link弧(4个方向)都会被连接为一个链表,
  121. //采用头插法插入所有的弧
  122. fromI.next = vtcs[i].first; //将正向弧指向结点i的链表首部
  123. vtcs[i].first = (int)edges.size(); //修改结点i的第一个弧为当前正向弧
  124.  
  125. edges.push_back( fromI ); //正向弧加入弧集合
  126.  
  127. //===================================================//
  128. //以下为插入反向弧的操作,与上相同,不作解释
  129. toI.dst = i;
  130. toI.weight = revw;
  131. toI.next = vtcs[j].first;
  132. vtcs[j].first = (int)edges.size();
  133. edges.push_back( toI );
  134. }
  135.  
  136. /*
  137. 函数功能:
  138. 为结点i的添加一条t-link弧(到终端结点的弧)
  139. 参数说明:
  140. int---i: 结点编号
  141. Tweight---sourceW: 正向弧权值
  142. Tweight---sinkW: 逆向弧权值
  143. */
  144. template <class TWeight>
  145. void GCGraph<TWeight>::addTermWeights( int i, TWeight sourceW, TWeight sinkW )
  146. {
  147. CV_Assert( i>=0 && i<(int)vtcs.size() ); //结点编号有效性
  148.  
  149. TWeight dw = vtcs[i].weight;
  150. if( dw > 0 )
  151. sourceW += dw;
  152. else
  153. sinkW -= dw;
  154. flow += (sourceW < sinkW) ? sourceW : sinkW;
  155. vtcs[i].weight = sourceW - sinkW;
  156. }
  157.  
  158. template <class TWeight>
  159. {
  160. //本函数中仅有的可能出现的负值,下面如果存在判别某值是否小于0,
  161. //意味着判断当前结点是否为终端结点,或者孤立点
  162. const int TERMINAL = -1, ORPHAN = -2;
  163.  
  164. //先进先出队列,保存当前活动结点,stub为哨兵结点
  165. Vtx stub, *nilNode = &stub, *first = nilNode, *last = nilNode;
  166.  
  167. int curr_ts = 0; //当前时间戳
  168. stub.next = nilNode; //初始化活动结点队列,首结点指向自己
  169.  
  170. Vtx *vtxPtr = &vtcs[0]; //结点指针
  171. Edge *edgePtr = &edges[0]; //弧指针
  172.  
  173. std::vector<Vtx*> orphans; //孤立点集合
  174.  
  175. // 遍历所有的结点,初始化活动结点(active node)队列
  176. for( int i = 0; i < (int)vtcs.size(); i++ )
  177. {
  178. Vtx* v = vtxPtr + i;
  179. v->ts = 0;
  180. if( v->weight != 0 ) //当前结点t-vaule(即流量)不为0
  181. {
  182. last = last->next = v; //入队,插入到队尾
  183. v->dist = 1; //路径长度记1
  184. v->parent = TERMINAL; //标注其双亲为终端结点
  185.  
  186. //t为uchar类型, 例如: t=(1>0), t=1
  187. // t=(1<0), t=0
  188. //实际效果纪录当前结点流向, 正向取0,逆向取1
  189. v->t = v->weight < 0;
  190. }
  191. else
  192. v->parent = 0; //保持不变,孤儿结点
  193. }
  194.  
  195. first = first->next; //首结点作为哨兵使用,本身无实际意义,移动到下一节点,即第一个有效结点
  196. last->next = nilNode; //哨兵放置到队尾了。。。,检测到哨兵说明一层查找结束
  197. nilNode->next = 0; //哨兵后面啥都没有,清空
  198.  
  199. //很长的循环,每次都按照以下三个步骤运行:
  200. //搜索路径->拆分为森林->树的重构
  201. for(;;)
  202. {
  203. Vtx* v, *u; // v表示当前元素,u为其相邻元素
  204. int e0 = -1, ei = 0, ej = 0;
  205. TWeight minWeight, weight; // 路径最小割(流量), weight当前流量
  206. uchar vt; // 流向标识符,正向为0,反向为1
  207.  
  208. //===================================================//
  209. // 第一阶段: S 和 T 树的生长,找到一条s->t的路径
  210.  
  211. while( first != nilNode ) // 存在活动结点
  212. {
  213. v = first; // 取第一个元素存入v,作为当前结点
  214. if( v->parent ) // v非孤儿点
  215. {
  216. vt = v->t; // 纪录v的流向
  217. // 广度优先搜索,以此搜索当前结点所有相邻结点, 方法为:遍历所有相邻边,调出边的终点就是相邻结点
  218. for( ei = v->first; ei != 0; ei = edgePtr[ei].next )
  219. {
  220. // 此路不通,跳过,通常两个相邻点都会有关联的,不过如果关联值weight过小,为防止下溢出,置0!
  221. // 每对结点都拥有两个反向的边,ei^vt表明检测的边是与v结点同向的
  222. if( edgePtr[ei^vt].weight == 0 )
  223. continue;
  224. u = vtxPtr+edgePtr[ei].dst; // 取出邻接点u
  225. if( !u->parent ) // 无父节点,即为孤儿点,v接受u作为其子节点
  226. {
  227. u->t = vt; // 设置结点u与v的流向相同
  228. u->parent = ei ^ 1; // ei的末尾取反。。。
  229. u->ts = v->ts; // 更新时间戳,由于u的路径长度通过v计算得到,因此有效性相同
  230. u->dist = v->dist + 1; // u深度等于v加1
  231. if( !u->next ) // u不在队列中,入队,插入位置为队尾,
  232. {
  233. u->next = nilNode; // 修改下一元素指针指向哨兵
  234. last = last->next = u; // 插入队尾
  235. }
  236. continue; //continue_for() [遍历邻接点]
  237. }
  238.  
  239. if( u->t != vt ) // u和v的流向不同,u可以到达另一终点,则找到一条路径
  240. {
  241. e0 = ei ^ vt; //
  242. break; //break_for() [遍历邻接点]
  243. }
  244.  
  245. // u已经存在父节点,但是如果u的路径长度大于v+1,说明u走弯路了,修改u的路径,使其成为v的子结点
  246. // 进入条件:u的路径长度大于v的长度+1,且u的时间戳较早
  247. if( u->dist > v->dist+1 && u->ts <= v->ts )
  248. {
  249. // 从新设置u的父节点为v(编号ei),记录为当前的弧
  250. u->parent = ei ^ 1;
  251. u->ts = v->ts; // 更新u的时间戳与v相同
  252. u->dist = v->dist + 1; // u为v的子结点,路径长度加1
  253. }
  254. }
  255. if( e0 > 0 )
  256. break; //break_while() [查找s->t路径]
  257. }
  258. // 将刚处理完的结点从活动队列移除
  259. first = first->next;
  260. v->next = 0;
  261. }
  262.  
  263. if( e0 <= 0 ) //全部搜索结束,e0=0说明所有结点都已经处理完毕,e0<0说明已经搜索到了终端结点了。。
  264. break; //break_for(;;)[max_flow]
  265.  
  266. //===================================================//
  267. // 第二阶段: 流量统计与树的拆分
  268.  
  269. //===第一节===//
  270. //查找路径中的最小权值
  271. minWeight = edgePtr[e0].weight;
  272. assert( minWeight > 0 );
  273.  
  274. // 遍历整条路径分两个方向进行,从当前结点开始,向前回溯s树,向后回溯t树
  275. // 2次遍历, k=1: 回溯s树, k=0: 回溯t树
  276. for( int k = 1; k >= 0; k-- )
  277. {
  278. //回溯的方法为:取当前结点的父节点,判断是否为终端结点
  279. for( v = vtxPtr+edgePtr[e0^k].dst;/*此处无退出条件*/; v = vtxPtr+edgePtr[ei].dst )
  280. {
  281. if( (ei = v->parent) < 0 ) //回溯,ei纪录当前点的父边,回溯至终端结点,退出
  282. break;
  283. weight = edgePtr[ei^k].weight; //取当前弧的权值
  284. minWeight = MIN(minWeight, weight); //纪录当前路径最小流
  285. assert( minWeight > 0 );
  286. }
  287. weight = fabs(v->weight);
  288. minWeight = MIN(minWeight, weight); //取s树和t树的最小流
  289. assert( minWeight > 0 );
  290. }
  291.  
  292. //===第二节===//
  293. // 修改当前路径中的所有的weight权值
  294. /* 注意到任何时候s和t树的结点都只有一条弧使其连接到树中,
  295. 当这条弧权值减少为0则此结点从树中断开,
  296. 若其无子结点,则成为孤立点,
  297. 若其拥有子结点,则独立为森林,但是ei的子结点还不知道他们被孤立了!
  298. */
  299. edgePtr[e0].weight -= minWeight; //正向路径权值减少
  300. edgePtr[e0^1].weight += minWeight; //反向路径权值增加
  301. flow += minWeight; //修改当前流量
  302.  
  303. // k=1: source tree, k=0: destination tree
  304. for( int k = 1; k >= 0; k-- )
  305. {
  306. for( v = vtxPtr+edgePtr[e0^k].dst;/*此处无退出条件*/; v = vtxPtr+edgePtr[ei].dst )
  307. {
  308. if( (ei = v->parent) < 0 ) //某一方向搜索结束,退出
  309. break;
  310. edgePtr[ei^(k^1)].weight += minWeight; //n-value逆向增加
  311. //n-value正向减少,如果权值减少至0,则将ei标注为孤儿
  312. if( (edgePtr[ei^k].weight -= minWeight) == 0 )
  313. {
  314. orphans.push_back(v); //加入孤儿集合
  315. v->parent = ORPHAN; //修改父节点标记
  316. }
  317. }
  318.  
  319. v->weight = v->weight + minWeight*(1-k*2); //t-value修改(减少或增加)
  320. //如果权值减少至0,则将ei标注为孤儿
  321. if( v->weight == 0 )
  322. {
  323. orphans.push_back(v);
  324. v->parent = ORPHAN;
  325. }
  326. }
  327.  
  328. //===================================================//
  329. // 第三阶段: 树的重构
  330.  
  331. // 为孤儿找到新的父节点,恢复树结构
  332. curr_ts++;
  333. while( !orphans.empty() ) //存在孤儿
  334. {
  335. Vtx* v2 = orphans.back(); //取一个孤儿,记为v2
  336. orphans.pop_back(); //删除栈顶元素,两步操作等价于出栈
  337.  
  338. int d, minDist = INT_MAX;
  339. e0 = 0;
  340. vt = v2->t;
  341.  
  342. // 遍历当前结点的相邻点,ei为当前弧的编号
  343. for( ei = v2->first; ei != 0; ei = edgePtr[ei].next )
  344. {
  345. if( edgePtr[ei^(vt^1)].weight == 0 ) // 找到权值为0的边,无效,继续找
  346. continue;
  347. u = vtxPtr+edgePtr[ei].dst; // 邻接点记为u
  348. if( u->t != vt || u->parent == 0 ) // 不同方向的边,或者找到的点也是孤立点,继续找
  349. continue;
  350. // 计算当前点路径长度
  351. for( d = 0;; )
  352. {
  353. if( u->ts == curr_ts ) // 找到时间戳符合的结点,即此结点路径长度有效
  354. {
  355. d += u->dist; // 最终路径长度等于到u结点的距离加u结点的路径长度
  356. break;
  357. }
  358. ej = u->parent; // 继续寻找u的父节点
  359. d++; // 距有效点距离加1
  360. if( ej < 0 ) // TERMINAL = -1, ORPHAN = -2
  361. {
  362. if( ej == ORPHAN ) // 找到的父节点是孤立点
  363. d = INT_MAX-1; // 纪录d无穷大,即无法到达终端结点
  364. else // 找到的是终端结点
  365. {
  366. u->ts = curr_ts; // 更改时间戳为当前时刻,本次大循环中其路径长度是有效的!
  367. u->dist = 1; // 路径长度为1
  368. }
  369. break;
  370. }
  371. u = vtxPtr+edgePtr[ej].dst; // u指向父节点,继续回溯
  372. }
  373.  
  374. // 更新子结点的路径长度
  375. if( ++d < INT_MAX ) // 当前结点找到了父节点
  376. {
  377. if( d < minDist ) //
  378. {
  379. minDist = d; // 更新minDist
  380. e0 = ei; // e0记录找到的v2父弧
  381. }
  382. //
  383. for( u = vtxPtr+edgePtr[ei].dst; u->ts != curr_ts; u = vtxPtr+edgePtr[u->parent].dst )
  384. {
  385. u->ts = curr_ts;
  386. u->dist = --d;
  387. }
  388. }
  389. }//end_for
  390.  
  391. if( (v2->parent = e0) > 0 )
  392. {
  393. v2->ts = curr_ts; //v2时间戳为当前时刻,本次大循环中其路径长度是有效的!
  394. v2->dist = minDist;
  395. continue;
  396. }
  397.  
  398. // 未找到父节点,将此结点和其所有子节点置为孤儿
  399. v2->ts = 0; // v2为当前结点,时间戳置0
  400. for( ei = v2->first; ei != 0; ei = edgePtr[ei].next )
  401. {
  402. u = vtxPtr+edgePtr[ei].dst; // 邻接点
  403. ej = u->parent; // 邻接点的父节点
  404. if( u->t != vt || !ej ) // 邻接点无父节点或与本结点不同向?
  405. continue;
  406. if( edgePtr[ei^(vt^1)].weight && !u->next ) //u和v反向,则加入活动队列 (vt^1表示对vt取反)
  407. {
  408. u->next = nilNode;
  409. last = last->next = u;
  410. }
  411. if( ej > 0 && vtxPtr+edgePtr[ej].dst == v2 ) //当前节点确实是v2的子节点
  412. {
  413. orphans.push_back(u); //加入孤立点队列
  414. u->parent = ORPHAN; //标记其无父节点
  415. }
  416. }
  417. }//end_while
  418. }//end_for(;;)
  419.  
  420. return flow; //返回最大流量
  421. }
  422.  
  423. template <class TWeight>
  424. bool GCGraph<TWeight>::inSourceSegment( int i )
  425. {
  426. CV_Assert( i>=0 && i<(int)vtcs.size() );
  427. return vtcs[i].t == 0;
  428. }
  429.  
  430. #endif

[图像分割] OpenCV 的 GrabCut 函数使用和源码解读的更多相关文章

  1. 图像分割之(四)OpenCV的GrabCut函数使用和源码解读

    图像分割之(四)OpenCV的GrabCut函数使用和源码解读         分类:            图像处理            计算机视觉             2013-01-23 ...

  2. 《Netty5.0架构剖析和源码解读》【PDF】下载

    <Netty5.0架构剖析和源码解读>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062545 内容简介 Netty 是个异步的 ...

  3. Mybatis(四):MyBatis核心组件介绍原理解析和源码解读

    Mybatis核心成员 Configuration        MyBatis所有的配置信息都保存在Configuration对象之中,配置文件中的大部分配置都会存储到该类中 SqlSession ...

  4. Selenium系列(十) - 针对Select下拉框的操作和源码解读

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...

  5. Selenium系列(21) - Cookie操作和源码解读

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...

  6. 生成器执行函数co 源码解读

    本文所选内容均来自[co模块源码] /** * slice() reference. */ var slice = Array.prototype.slice; module.exports = co ...

  7. tomcat架构分析和源码解读

    最近在看<深入分析java web技术内幕>,书中讲解了一部分tomcat的相关知识,我也去查看了一些源码,看了大神们写的代码,我才知道自己就像在做加减乘除一样,这是不行的.还有好多包和类 ...

  8. python 微信跳一跳和源码解读

    刚好周末,想研究一下前阵子很火的微信跳一跳 下面进入正文. 本文适用对象为WIN10系统,安卓用户.目的在于让丝毫没有接触过Python的小伙伴都能成功运行,如果你恰好是这样的对象,那么跟着我开始操作 ...

  9. RocketMQ存储系统概要设计和源码解读

    普遍消息存储技术的选型 分布式KV存储 NewSQL存储:TiDB 文件系统:RocketMQ,kafka,RabbitMQ RocketMQ:所有的message存储在一个log里,不区分topic ...

随机推荐

  1. SuperSocket自定义server、session、command、分隔符,WinForm服务端

    文件下载地址https://files.cnblogs.com/files/xixixing/WindowsFormsApp.zip key和body以及body中参数间,默认通过空格分隔.修改构造函 ...

  2. (八)树控件(Tree Control),标签控件(tab control)

    树控件 基于对话框创建工程 // 01_TreeCtrlDlg.cpp : 实现文件 // #include "stdafx.h" #include "01_TreeCt ...

  3. 南京IT公司

    公司 (排名不分前后,有好的公司可以@我,及时更新) 1.中兴软创 http://www.ztesoft.com/cn/index.html 2.华为 http://www.huawei.com/cn ...

  4. Python图形用户界面-Tkinter

    Tkinter是什么 python 特定的GUI界面,是一个图像的窗口,tkinter是python 自带的,可以编辑的GUI界面,我们可以用GUI 实现很多一个直观的功能,如何想开发一个计算器,如果 ...

  5. linux系统nginx下反向代理解析二级目录泛目录教程

    解析规则1:     location /目录名 {           proxy_pass http://ip/目录名;           } 解析规则2:  location /目录名{    ...

  6. Manacher 算法学习小记

    概要 一个字符串有多少个回文的字串?最多有 \(O(n^2)\) 级别个.但 Manacher 算法却可以用 \(O(n)\) 的时间复杂度解决这个问题.同时 Manacher 算法实现非常简单. 一 ...

  7. BZOJ1070: [SCOI2007]修车(最小费用最大流,思维)

    Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同 的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序, ...

  8. LVS集群之DR模式

    今天来讲LVS-DR模式集群实现负载均衡的搭建方法 环境 主机名 IP   系统 角色 dir DIP:192.168.199.9 VIP:192.168.199.8 rhel7.4 集群服务器 no ...

  9. mac使用frida

    mac使用frida 安装 https://github.com/frida/frida/releases 根据手机的cpu的版本,选择相应的文件,一般通过手机信息可以看到 我这里是frida-ser ...

  10. python之json读写

    #将字典转json并写入文件 import json i=3 j=5 a={'a':i,'b':j} js=json.dumps(a) print(js) with open("/Users ...