----- -----《“GrabCut” — Interactive Foreground Extraction using Iterated Graph Cuts》
以下是我對grabcut的簡單認識
grabcut是在graphcut基礎上演變來的,grabcut算法利用了圖像中的紋理(顔色)資訊和邊界(反差)資訊,隻要少量的使用者互動操作即可得到比較好的分割結果。
一、Graph cut的介紹
圖1
首先,介紹一種圖,如圖1所示,這是一個特殊的圖(graph),它的特殊之處在于除了中間的像素點之間(網格部分)構成圖之外,他還有兩個特殊頂點T(彙點)和S(源點),分别表示背景(Background)和前景(Object),以及各個像素和這兩個點之間的連線,這兩個特殊點是種子點,是使用者在進行圖像分割過程中标注出來的點(使用者互動過程中指明哪裡是背景,哪裡是前景),如圖2所示。
圖2
這種圖的頂點确定了,那它各個點之間的邊值又是怎麼确定的呢?
這裡我們就要引入最重要的公式了,利用這個公式不僅能解決上面的這個問題,還能幫我們進行圖像分割,神奇,圖像能量表示公式:
公式1
首先看公式(1)E(A)=lamda*R(A)+B(A),其中R(A)是區域項,表示像素A屬于背景或者前景的機率,在圖1中表示所有像素點與頂點T和頂點S的連線的邊的權值,那我們怎麼知道R(A) 為多少呢?這裡要用到這個公式2,它等于該像素點屬于前景或是背景的機率的負對數。
公式2
B(A)代表邊界項,它對應圖1中相鄰像素點之間的邊的權值,表示為兩個像素點接近程度,這裡用公式3來表示,兩個像素點的像素值大小越相近,B(A)的值就越大,兩個像素值大小越不相近,B(A)的值越小。
公式3
lamda表示R(A)和B(A)的比重,若lamda為0,代表隻考慮B(A),這裡的lamda也是通過計算得出來的,具體詳見第二部分。
這樣我們不難發現,若是R(A)越小,代表像素屬于前景或是背景的機率也就越大,B(A)越小,代表兩個像素點不相近,不屬于一類像素,應該分開,是以當E(A)能量越小,代表分割越準确。
那麼我們如何進行分割呢?這裡根據《Interactive Graph Cuts for Optimal Boundary & Region Segmentation of Objects in N-D Images》這篇論文,提出了最小割方法。最小割算法原理可以參考https://blog.csdn.net/chinacoy/article/details/45040897。通俗易懂。
二、grabcut 介紹
與graph cut相比,grabcut具有以下優點:
1、隻需一個長方形的目标框
2、增加少量的使用者互動,分割更完美
3、border matting技術使目标分割邊界更加自然
與graph cut 的不同之處在于:
1、Graph Cut 目标、背景是灰階直方圖,Grab Cut是RGB三通道的混合高斯模型(GMM)
2、Graph Cut 分割一次完成,Grab Cut是不斷進行分割估計和模型參數學習的疊代過程
3、Graph Cut 需要指定目标和背景種子點,Grab Cut 隻需框選目标,允許不完全标注
上算法流程圖,配合下面代碼更容易看懂一些:
初始化部分 模型疊代部分:
grabcut()函數内部代碼:
/*M///
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#include "gcgraph.hpp"
#include <limits>
using namespace cv;
/*
This is implementation of image segmentation algorithm GrabCut described in
"GrabCut — Interactive Foreground Extraction using Iterated Graph Cuts".
Carsten Rother, Vladimir Kolmogorov, Andrew Blake.
*/
/*
GMM - Gaussian Mixture Model
*/
class GMM
{
public:
static const int componentsCount = 5;
GMM( Mat& _model );
double operator()( const Vec3d color ) const;
double operator()( int ci, const Vec3d color ) const;
int whichComponent( const Vec3d color ) const;
void initLearning();
void addSample( int ci, const Vec3d color );
void endLearning();
private:
void calcInverseCovAndDeterm( int ci );
Mat model;
double* coefs;
double* mean;
double* cov;
double inverseCovs[componentsCount][3][3]; //協方差的逆矩陣
double covDeterms[componentsCount]; //協方差的行列式
double sums[componentsCount][3];
double prods[componentsCount][3][3];
int sampleCounts[componentsCount];
int totalSampleCount;
};
//背景和前景各有一個對應的GMM(混合高斯模型)
GMM::GMM( Mat& _model )
{
//一個像素的(唯一對應)高斯模型的參數個數或者說一個高斯模型的參數個數
//一個像素RGB三個通道值,故3個均值,3*3個協方差,共用一個權值
const int modelSize = 3/*mean*/ + 9/*covariance*/ + 1/*component weight*/;
if( _model.empty() )
{
//一個GMM共有componentsCount個高斯模型,一個高斯模型有modelSize個模型參數
_model.create( 1, modelSize*componentsCount, CV_64FC1 );
_model.setTo(Scalar(0));
}
else if( (_model.type() != CV_64FC1) || (_model.rows != 1) || (_model.cols != modelSize*componentsCount) )
CV_Error( CV_StsBadArg, "_model must have CV_64FC1 type, rows == 1 and cols == 13*componentsCount" );
model = _model;
//注意這些模型參數的存儲方式:先排完componentsCount個coefs,再3*componentsCount個mean。
//再3*3*componentsCount個cov。
coefs = model.ptr<double>(0); //GMM的每個像素的高斯模型的權值變量起始存儲指針
mean = coefs + componentsCount; //均值變量起始存儲指針
cov = mean + 3*componentsCount; //協方差變量起始存儲指針
for( int ci = 0; ci < componentsCount; ci++ )
if( coefs[ci] > 0 )
//計算GMM中第ci個高斯模型的協方差的逆Inverse和行列式Determinant
//為了後面計算每個像素屬于該高斯模型的機率(也就是資料能量項)
calcInverseCovAndDeterm( ci );
}
//計算一個像素(由color=(B,G,R)三維double型向量來表示)屬于這個GMM混合高斯模型的機率。
//也就是把這個像素像素屬于componentsCount個高斯模型的機率與對應的權值相乘再相加,
//具體見論文的公式(10)。結果從res傳回。
//這個相當于計算Gibbs能量的第一個能量項(取負後)。
double GMM::operator()( const Vec3d color ) const
{
double res = 0;
for( int ci = 0; ci < componentsCount; ci++ )
res += coefs[ci] * (*this)(ci, color );
return res;
}
//計算一個像素(由color=(B,G,R)三維double型向量來表示)屬于第ci個高斯模型的機率。
//具體過程,即高階的高斯密度模型計算式,具體見論文的公式(10)。結果從res傳回
double GMM::operator()( int ci, const Vec3d color ) const
{
double res = 0;
if( coefs[ci] > 0 )
{
CV_Assert( covDeterms[ci] > std::numeric_limits<double>::epsilon() );
Vec3d diff = color;
double* m = mean + 3*ci;
diff[0] -= m[0]; diff[1] -= m[1]; diff[2] -= m[2];
double mult = diff[0]*(diff[0]*inverseCovs[ci][0][0] + diff[1]*inverseCovs[ci][1][0] + diff[2]*inverseCovs[ci][2][0])
+ diff[1]*(diff[0]*inverseCovs[ci][0][1] + diff[1]*inverseCovs[ci][1][1] + diff[2]*inverseCovs[ci][2][1])
+ diff[2]*(diff[0]*inverseCovs[ci][0][2] + diff[1]*inverseCovs[ci][1][2] + diff[2]*inverseCovs[ci][2][2]);
res = 1.0f/sqrt(covDeterms[ci]) * exp(-0.5f*mult);
}
return res;
}
//傳回這個像素最有可能屬于GMM中的哪個高斯模型(機率最大的那個)
int GMM::whichComponent( const Vec3d color ) const
{
int k = 0;
double max = 0;
for( int ci = 0; ci < componentsCount; ci++ )
{
double p = (*this)( ci, color );
if( p > max )
{
k = ci; //找到機率最大的那個,或者說計算結果最大的那個
max = p;
}
}
return k;
}
//GMM參數學習前的初始化,主要是對要求和的變量置零
void GMM::initLearning()
{
for( int ci = 0; ci < componentsCount; ci++)
{
sums[ci][0] = sums[ci][1] = sums[ci][2] = 0;
prods[ci][0][0] = prods[ci][0][1] = prods[ci][0][2] = 0;
prods[ci][1][0] = prods[ci][1][1] = prods[ci][1][2] = 0;
prods[ci][2][0] = prods[ci][2][1] = prods[ci][2][2] = 0;
sampleCounts[ci] = 0;
}
totalSampleCount = 0;
}
//增加樣本,即為前景或者背景GMM的第ci個高斯模型的像素集(這個像素集是來用估
//計計算這個高斯模型的參數的)增加樣本像素。計算加入color這個像素後,像素集
//中所有像素的RGB三個通道的和sums(用來計算均值),還有它的prods(用來計算協方差),
//并且記錄這個像素集的像素個數和總的像素個數(用來計算這個高斯模型的權值)。
void GMM::addSample( int ci, const Vec3d color )
{
sums[ci][0] += color[0]; sums[ci][1] += color[1]; sums[ci][2] += color[2];
prods[ci][0][0] += color[0]*color[0]; prods[ci][0][1] += color[0]*color[1]; prods[ci][0][2] += color[0]*color[2];
prods[ci][1][0] += color[1]*color[0]; prods[ci][1][1] += color[1]*color[1]; prods[ci][1][2] += color[1]*color[2];
prods[ci][2][0] += color[2]*color[0]; prods[ci][2][1] += color[2]*color[1]; prods[ci][2][2] += color[2]*color[2];
sampleCounts[ci]++;
totalSampleCount++;
}
//從圖像資料中學習GMM的參數:每一個高斯分量的權值、均值和協方差矩陣;
//這裡相當于論文中“Iterative minimisation”的step 2
void GMM::endLearning()
{
const double variance = 0.01;
for( int ci = 0; ci < componentsCount; ci++ )
{
int n = sampleCounts[ci]; //第ci個高斯模型的樣本像素個數
if( n == 0 )
coefs[ci] = 0;
else
{
//計算第ci個高斯模型的權值系數
coefs[ci] = (double)n/totalSampleCount;
//計算第ci個高斯模型的均值
double* m = mean + 3*ci;
m[0] = sums[ci][0]/n; m[1] = sums[ci][1]/n; m[2] = sums[ci][2]/n;
//計算第ci個高斯模型的協方差
double* c = cov + 9*ci;
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];
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];
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];
//計算第ci個高斯模型的協方差的行列式
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]);
if( dtrm <= std::numeric_limits<double>::epsilon() )
{
//相當于如果行列式小于等于0,(對角線元素)增加白噪聲,避免其變
//為退化(降秩)協方差矩陣(不存在逆矩陣,但後面的計算需要計算逆矩陣)。
// Adds the white noise to avoid singular covariance matrix.
c[0] += variance;
c[4] += variance;
c[8] += variance;
}
//計算第ci個高斯模型的協方差的逆Inverse和行列式Determinant
calcInverseCovAndDeterm(ci);
}
}
}
//計算協方差的逆Inverse和行列式Determinant
void GMM::calcInverseCovAndDeterm( int ci )
{
if( coefs[ci] > 0 )
{
//取第ci個高斯模型的協方差的起始指針
double *c = cov + 9*ci;
double dtrm =
covDeterms[ci] = 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]);
//在C++中,每一種内置的資料類型都擁有不同的屬性, 使用<limits>庫可以獲
//得這些基本資料類型的數值屬性。因為浮點算法的截斷,是以使得,當a=2,
//b=3時 10*a/b == 20/b不成立。那怎麼辦呢?
//這個小正數(epsilon)常量就來了,小正數通常為可用給定資料類型的
//大于1的最小值與1之差來表示。若dtrm結果不大于小正數,那麼它幾乎為零。
//是以下式保證dtrm>0,即行列式的計算正确(協方差對稱正定,故行列式大于0)。
CV_Assert( dtrm > std::numeric_limits<double>::epsilon() );
//三階方陣的求逆
inverseCovs[ci][0][0] = (c[4]*c[8] - c[5]*c[7]) / dtrm;
inverseCovs[ci][1][0] = -(c[3]*c[8] - c[5]*c[6]) / dtrm;
inverseCovs[ci][2][0] = (c[3]*c[7] - c[4]*c[6]) / dtrm;
inverseCovs[ci][0][1] = -(c[1]*c[8] - c[2]*c[7]) / dtrm;
inverseCovs[ci][1][1] = (c[0]*c[8] - c[2]*c[6]) / dtrm;
inverseCovs[ci][2][1] = -(c[0]*c[7] - c[1]*c[6]) / dtrm;
inverseCovs[ci][0][2] = (c[1]*c[5] - c[2]*c[4]) / dtrm;
inverseCovs[ci][1][2] = -(c[0]*c[5] - c[2]*c[3]) / dtrm;
inverseCovs[ci][2][2] = (c[0]*c[4] - c[1]*c[3]) / dtrm;
}
}
//計算beta,也就是Gibbs能量項中的第二項(平滑項)中的指數項的beta,用來調整
//高或者低對比度時,兩個鄰域像素的差别的影響的,例如在低對比度時,兩個鄰域
//像素的差别可能就會比較小,這時候需要乘以一個較大的beta來放大這個差别,
//在高對比度時,則需要縮小本身就比較大的差别。
//是以我們需要分析整幅圖像的對比度來确定參數beta,具體的見論文公式(5)。
/*
Calculate beta - parameter of GrabCut algorithm.
beta = 1/(2*avg(sqr(||color[i] - color[j]||)))
*/
static double calcBeta( const Mat& img )
{
double beta = 0;
for( int y = 0; y < img.rows; y++ )
{
for( int x = 0; x < img.cols; x++ )
{
//計算四個方向鄰域兩像素的差别,也就是歐式距離或者說二階範數
//(當所有像素都算完後,就相當于計算八鄰域的像素差了)
Vec3d color = img.at<Vec3b>(y,x);
if( x>0 ) // left >0的判斷是為了避免在圖像邊界的時候還計算,導緻越界
{
Vec3d diff = color - (Vec3d)img.at<Vec3b>(y,x-1);
beta += diff.dot(diff); //矩陣的點乘,也就是各個元素平方的和
}
if( y>0 && x>0 ) // upleft
{
Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x-1);
beta += diff.dot(diff);
}
if( y>0 ) // up
{
Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x);
beta += diff.dot(diff);
}
if( y>0 && x<img.cols-1) // upright
{
Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x+1);
beta += diff.dot(diff);
}
}
}
if( beta <= std::numeric_limits<double>::epsilon() )
beta = 0;
else
beta = 1.f / (2 * beta/(4*img.cols*img.rows - 3*img.cols - 3*img.rows + 2) ); //論文公式(5)
return beta;
}
//計算圖每個非端點頂點(也就是每個像素作為圖的一個頂點,不包括源點s和彙點t)與鄰域頂點
//的邊的權值。由于是無向圖,我們計算的是八鄰域,那麼對于一個頂點,我們計算四個方向就行,
//在其他的頂點計算的時候,會把剩餘那四個方向的權值計算出來。這樣整個圖算完後,每個頂點
//與八鄰域的頂點的邊的權值就都計算出來了。
//這個相當于計算Gibbs能量的第二個能量項(平滑項),具體見論文中公式(4)
/*
Calculate weights of noterminal vertices of graph.
beta and gamma - parameters of GrabCut algorithm.
*/
static void calcNWeights( const Mat& img, Mat& leftW, Mat& upleftW, Mat& upW,
Mat& uprightW, double beta, double gamma )
{
//gammaDivSqrt2相當于公式(4)中的gamma * dis(i,j)^(-1),那麼可以知道,
//當i和j是垂直或者水準關系時,dis(i,j)=1,當是對角關系時,dis(i,j)=sqrt(2.0f)。
//具體計算時,看下面就明白了
const double gammaDivSqrt2 = gamma / std::sqrt(2.0f);
//每個方向的邊的權值通過一個和圖大小相等的Mat來儲存
leftW.create( img.rows, img.cols, CV_64FC1 );
upleftW.create( img.rows, img.cols, CV_64FC1 );
upW.create( img.rows, img.cols, CV_64FC1 );
uprightW.create( img.rows, img.cols, CV_64FC1 );
for( int y = 0; y < img.rows; y++ )
{
for( int x = 0; x < img.cols; x++ )
{
Vec3d color = img.at<Vec3b>(y,x);
if( x-1>=0 ) // left //避免圖的邊界
{
Vec3d diff = color - (Vec3d)img.at<Vec3b>(y,x-1);
leftW.at<double>(y,x) = gamma * exp(-beta*diff.dot(diff));
}
else
leftW.at<double>(y,x) = 0;
if( x-1>=0 && y-1>=0 ) // upleft
{
Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x-1);
upleftW.at<double>(y,x) = gammaDivSqrt2 * exp(-beta*diff.dot(diff));
}
else
upleftW.at<double>(y,x) = 0;
if( y-1>=0 ) // up
{
Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x);
upW.at<double>(y,x) = gamma * exp(-beta*diff.dot(diff));
}
else
upW.at<double>(y,x) = 0;
if( x+1<img.cols && y-1>=0 ) // upright
{
Vec3d diff = color - (Vec3d)img.at<Vec3b>(y-1,x+1);
uprightW.at<double>(y,x) = gammaDivSqrt2 * exp(-beta*diff.dot(diff));
}
else
uprightW.at<double>(y,x) = 0;
}
}
}
//檢查mask的正确性。mask為通過使用者互動或者程式設定的,它是和圖像大小一樣的單通道灰階圖,
//每個像素隻能取GC_BGD or GC_FGD or GC_PR_BGD or GC_PR_FGD 四種枚舉值,分别表示該像素
//(使用者或者程式指定)屬于背景、前景、可能為背景或者可能為前景像素。具體的參考:
//ICCV2001“Interactive Graph Cuts for Optimal Boundary & Region Segmentation of Objects in N-D Images”
//Yuri Y. Boykov Marie-Pierre Jolly
/*
Check size, type and element values of mask matrix.
*/
static void checkMask( const Mat& img, const Mat& mask )
{
if( mask.empty() )
CV_Error( CV_StsBadArg, "mask is empty" );
if( mask.type() != CV_8UC1 )
CV_Error( CV_StsBadArg, "mask must have CV_8UC1 type" );
if( mask.cols != img.cols || mask.rows != img.rows )
CV_Error( CV_StsBadArg, "mask must have as many rows and cols as img" );
for( int y = 0; y < mask.rows; y++ )
{
for( int x = 0; x < mask.cols; x++ )
{
uchar val = mask.at<uchar>(y,x);
if( val!=GC_BGD && val!=GC_FGD && val!=GC_PR_BGD && val!=GC_PR_FGD )
CV_Error( CV_StsBadArg, "mask element value must be equel"
"GC_BGD or GC_FGD or GC_PR_BGD or GC_PR_FGD" );
}
}
}
//通過使用者框選目标rect來建立mask,rect外的全部作為背景,設定為GC_BGD,
//rect内的設定為 GC_PR_FGD(可能為前景)
/*
Initialize mask using rectangular.
*/
static void initMaskWithRect( Mat& mask, Size imgSize, Rect rect )
{
mask.create( imgSize, CV_8UC1 );
mask.setTo( GC_BGD );
rect.x = max(0, rect.x);
rect.y = max(0, rect.y);
rect.width = min(rect.width, imgSize.width-rect.x);
rect.height = min(rect.height, imgSize.height-rect.y);
(mask(rect)).setTo( Scalar(GC_PR_FGD) );
}
//通過k-means算法來初始化背景GMM和前景GMM模型
/*
Initialize GMM background and foreground models using kmeans algorithm.
*/
static void initGMMs( const Mat& img, const Mat& mask, GMM& bgdGMM, GMM& fgdGMM )
{
const int kMeansItCount = 10; //疊代次數
const int kMeansType = KMEANS_PP_CENTERS; //Use kmeans++ center initialization by Arthur and Vassilvitskii
Mat bgdLabels, fgdLabels; //記錄背景和前景的像素樣本集中每個像素對應GMM的哪個高斯模型,論文中的kn
vector<Vec3f> bgdSamples, fgdSamples; //背景和前景的像素樣本集
Point p;
for( p.y = 0; p.y < img.rows; p.y++ )
{
for( p.x = 0; p.x < img.cols; p.x++ )
{
//mask中标記為GC_BGD和GC_PR_BGD的像素都作為背景的樣本像素
if( mask.at<uchar>(p) == GC_BGD || mask.at<uchar>(p) == GC_PR_BGD )
bgdSamples.push_back( (Vec3f)img.at<Vec3b>(p) );
else // GC_FGD | GC_PR_FGD
fgdSamples.push_back( (Vec3f)img.at<Vec3b>(p) );
}
}
CV_Assert( !bgdSamples.empty() && !fgdSamples.empty() );
//kmeans中參數_bgdSamples為:每行一個樣本
//kmeans的輸出為bgdLabels,裡面儲存的是輸入樣本集中每一個樣本對應的類标簽(樣本聚為componentsCount類後)
Mat _bgdSamples( (int)bgdSamples.size(), 3, CV_32FC1, &bgdSamples[0][0] );
kmeans( _bgdSamples, GMM::componentsCount, bgdLabels,
TermCriteria( CV_TERMCRIT_ITER, kMeansItCount, 0.0), 0, kMeansType );
Mat _fgdSamples( (int)fgdSamples.size(), 3, CV_32FC1, &fgdSamples[0][0] );
kmeans( _fgdSamples, GMM::componentsCount, fgdLabels,
TermCriteria( CV_TERMCRIT_ITER, kMeansItCount, 0.0), 0, kMeansType );
//經過上面的步驟後,每個像素所屬的高斯模型就确定的了,那麼就可以估計GMM中每個高斯模型的參數了。
bgdGMM.initLearning();
for( int i = 0; i < (int)bgdSamples.size(); i++ )
bgdGMM.addSample( bgdLabels.at<int>(i,0), bgdSamples[i] );
bgdGMM.endLearning();
fgdGMM.initLearning();
for( int i = 0; i < (int)fgdSamples.size(); i++ )
fgdGMM.addSample( fgdLabels.at<int>(i,0), fgdSamples[i] );
fgdGMM.endLearning();
}
//論文中:疊代最小化算法step 1:為每個像素配置設定GMM中所屬的高斯模型,kn儲存在Mat compIdxs中
/*
Assign GMMs components for each pixel.
*/
static void assignGMMsComponents( const Mat& img, const Mat& mask, const GMM& bgdGMM,
const GMM& fgdGMM, Mat& compIdxs )
{
Point p;
for( p.y = 0; p.y < img.rows; p.y++ )
{
for( p.x = 0; p.x < img.cols; p.x++ )
{
Vec3d color = img.at<Vec3b>(p);
//通過mask來判斷該像素屬于背景像素還是前景像素,再判斷它屬于前景或者背景GMM中的哪個高斯分量
compIdxs.at<int>(p) = mask.at<uchar>(p) == GC_BGD || mask.at<uchar>(p) == GC_PR_BGD ?
bgdGMM.whichComponent(color) : fgdGMM.whichComponent(color);
}
}
}
//論文中:疊代最小化算法step 2:從每個高斯模型的像素樣本集中學習每個高斯模型的參數
/*
Learn GMMs parameters.
*/
static void learnGMMs( const Mat& img, const Mat& mask, const Mat& compIdxs, GMM& bgdGMM, GMM& fgdGMM )
{
bgdGMM.initLearning();
fgdGMM.initLearning();
Point p;
for( int ci = 0; ci < GMM::componentsCount; ci++ )
{
for( p.y = 0; p.y < img.rows; p.y++ )
{
for( p.x = 0; p.x < img.cols; p.x++ )
{
if( compIdxs.at<int>(p) == ci )
{
if( mask.at<uchar>(p) == GC_BGD || mask.at<uchar>(p) == GC_PR_BGD )
bgdGMM.addSample( ci, img.at<Vec3b>(p) );
else
fgdGMM.addSample( ci, img.at<Vec3b>(p) );
}
}
}
}
bgdGMM.endLearning();
fgdGMM.endLearning();
}
//通過計算得到的能量項建構圖,圖的頂點為像素點,圖的邊由兩部分構成,
//一類邊是:每個頂點與Sink彙點t(代表背景)和源點Source(代表前景)連接配接的邊,
//這類邊的權值通過Gibbs能量項的第一項能量項來表示。
//另一類邊是:每個頂點與其鄰域頂點連接配接的邊,這類邊的權值通過Gibbs能量項的第二項能量項來表示。
/*
Construct GCGraph
*/
static void constructGCGraph( const Mat& img, const Mat& mask, const GMM& bgdGMM, const GMM& fgdGMM, double lambda,
const Mat& leftW, const Mat& upleftW, const Mat& upW, const Mat& uprightW,
GCGraph<double>& graph )
{
int vtxCount = img.cols*img.rows; //頂點數,每一個像素是一個頂點
int edgeCount = 2*(4*vtxCount - 3*(img.cols + img.rows) + 2); //邊數,需要考慮圖邊界的邊的缺失
//通過頂點數和邊數建立圖。這些類型聲明和函數定義請參考gcgraph.hpp
graph.create(vtxCount, edgeCount);
Point p;
for( p.y = 0; p.y < img.rows; p.y++ )
{
for( p.x = 0; p.x < img.cols; p.x++)
{
// add node
int vtxIdx = graph.addVtx(); //傳回這個頂點在圖中的索引
Vec3b color = img.at<Vec3b>(p);
// set t-weights
//計算每個頂點與Sink彙點t(代表背景)和源點Source(代表前景)連接配接的權值。
//也即計算Gibbs能量(每一個像素點作為背景像素或者前景像素)的第一個能量項
double fromSource, toSink;
if( mask.at<uchar>(p) == GC_PR_BGD || mask.at<uchar>(p) == GC_PR_FGD )
{
//對每一個像素計算其作為背景像素或者前景像素的第一個能量項,作為分别與t和s點的連接配接權值
fromSource = -log( bgdGMM(color) );
toSink = -log( fgdGMM(color) );
}
else if( mask.at<uchar>(p) == GC_BGD )
{
//對于确定為背景的像素點,它與Source點(前景)的連接配接為0,與Sink點的連接配接為lambda
fromSource = 0;
toSink = lambda;
}
else // GC_FGD
{
fromSource = lambda;
toSink = 0;
}
//設定該頂點vtxIdx分别與Source點和Sink點的連接配接權值
graph.addTermWeights( vtxIdx, fromSource, toSink );
// set n-weights n-links
//計算兩個鄰域頂點之間連接配接的權值。
//也即計算Gibbs能量的第二個能量項(平滑項)
if( p.x>0 )
{
double w = leftW.at<double>(p);
graph.addEdges( vtxIdx, vtxIdx-1, w, w );
}
if( p.x>0 && p.y>0 )
{
double w = upleftW.at<double>(p);
graph.addEdges( vtxIdx, vtxIdx-img.cols-1, w, w );
}
if( p.y>0 )
{
double w = upW.at<double>(p);
graph.addEdges( vtxIdx, vtxIdx-img.cols, w, w );
}
if( p.x<img.cols-1 && p.y>0 )
{
double w = uprightW.at<double>(p);
graph.addEdges( vtxIdx, vtxIdx-img.cols+1, w, w );
}
}
}
}
//論文中:疊代最小化算法step 3:分割估計:最小割或者最大流算法
/*
Estimate segmentation using MaxFlow algorithm
*/
static void estimateSegmentation( GCGraph<double>& graph, Mat& mask )
{
//通過最大流算法确定圖的最小割,也即完成圖像的分割
graph.maxFlow();
Point p;
for( p.y = 0; p.y < mask.rows; p.y++ )
{
for( p.x = 0; p.x < mask.cols; p.x++ )
{
//通過圖分割的結果來更新mask,即最後的圖像分割結果。注意的是,永遠都
//不會更新使用者指定為背景或者前景的像素
if( mask.at<uchar>(p) == GC_PR_BGD || mask.at<uchar>(p) == GC_PR_FGD )
{
if( graph.inSourceSegment( p.y*mask.cols+p.x /*vertex index*/ ) )
mask.at<uchar>(p) = GC_PR_FGD;
else
mask.at<uchar>(p) = GC_PR_BGD;
}
}
}
}
//最後的成果:提供給外界使用的偉大的API:grabCut
/*
****參數說明:
img——待分割的源圖像,必須是8位3通道(CV_8UC3)圖像,在處理的過程中不會被修改;
mask——掩碼圖像,如果使用掩碼進行初始化,那麼mask儲存初始化掩碼資訊;在執行分割
的時候,也可以将使用者互動所設定的前景與背景儲存到mask中,然後再傳入grabCut函
數;在處理結束之後,mask中會儲存結果。mask隻能取以下四種值:
GCD_BGD(=0),背景;
GCD_FGD(=1),前景;
GCD_PR_BGD(=2),可能的背景;
GCD_PR_FGD(=3),可能的前景。
如果沒有手工标記GCD_BGD或者GCD_FGD,那麼結果隻會有GCD_PR_BGD或GCD_PR_FGD;
rect——用于限定需要進行分割的圖像範圍,隻有該矩形視窗内的圖像部分才被處理;
bgdModel——背景模型,如果為null,函數内部會自動建立一個bgdModel;bgdModel必須是
單通道浮點型(CV_32FC1)圖像,且行數隻能為1,列數隻能為13x5;
fgdModel——前景模型,如果為null,函數内部會自動建立一個fgdModel;fgdModel必須是
單通道浮點型(CV_32FC1)圖像,且行數隻能為1,列數隻能為13x5;
iterCount——疊代次數,必須大于0;
mode——用于訓示grabCut函數進行什麼操作,可選的值有:
GC_INIT_WITH_RECT(=0),用矩形窗初始化GrabCut;
GC_INIT_WITH_MASK(=1),用掩碼圖像初始化GrabCut;
GC_EVAL(=2),執行分割。
*/
void cv::grabCut( InputArray _img, InputOutputArray _mask, Rect rect,
InputOutputArray _bgdModel, InputOutputArray _fgdModel,
int iterCount, int mode )
{
Mat img = _img.getMat();
Mat& mask = _mask.getMatRef();
Mat& bgdModel = _bgdModel.getMatRef();
Mat& fgdModel = _fgdModel.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" );
GMM bgdGMM( bgdModel ), fgdGMM( fgdModel );
Mat compIdxs( img.size(), CV_32SC1 );
if( mode == GC_INIT_WITH_RECT || mode == GC_INIT_WITH_MASK )
{
if( mode == GC_INIT_WITH_RECT )
initMaskWithRect( mask, img.size(), rect );
else // flag == GC_INIT_WITH_MASK
checkMask( img, mask );
initGMMs( img, mask, bgdGMM, fgdGMM );
}
if( iterCount <= 0)
return;
if( mode == GC_EVAL )
checkMask( img, mask );
const double gamma = 50;
const double lambda = 9*gamma;
const double beta = calcBeta( img );
Mat leftW, upleftW, upW, uprightW;
calcNWeights( img, leftW, upleftW, upW, uprightW, beta, gamma );
for( int i = 0; i < iterCount; i++ )
{
GCGraph<double> graph;
assignGMMsComponents( img, mask, bgdGMM, fgdGMM, compIdxs );
learnGMMs( img, mask, compIdxs, bgdGMM, fgdGMM );
constructGCGraph(img, mask, bgdGMM, fgdGMM, lambda, leftW, upleftW, upW, uprightW, graph );
estimateSegmentation( graph, mask );
}
}
添加個人了解部分:
一、類型轉化
InputArray、InputOutputArray、InputOutputArray、InputOutputArray轉化為Mat類型
二、檢查輸入圖像的合法性
是否為空、是否是CV_8UC3類型
三、建立前景、背景高斯混合模型(GMM)
确定每個高斯模型參數個數=均值(3)+協方差(3*3)+權值(1)
建立混合高斯模型(參數=模型個數*每個模型的參數)
定義模型參數存儲方式:權值、均值、協方差變量的起始指針
四、構造Mat 類型的單通道compIdxs(與img大小相同),用于存儲每個像素屬于哪個高斯模型
五、判斷grabcut函數進行的操作
GC_INIT_WITH_RECT:(初始矩形框)建立一個cv_8uc1類型的和圖檔大小一樣的mask,設定為背景;在這個mask上選擇和矩形一樣大小地方設定為前景
GC_INIT_WITH_MASK:(檢查mask合法性)mask是否為空、是否是cv_8uc1類型,行列是否與圖檔大小一緻、mask取值是否為4中枚舉值
六、初始化GMMS:
确定k-means疊代次數(10),類型(KMEANS_PP_CENTERS).
掃描像素點,根據mask将像素點加入前景、背景樣本集
執行k-means聚類,輸出每一個樣本(像素)對應哪個高斯模型
bdgGMM. initLearning():參數、求和變量置零
bdgGMM. addSample( int ci, const Vec3d color ):計算每個高斯模型color[i](i=1,2,3)的總和,協方差,第ci高斯模型樣本個數,混合高斯模型總樣本個數
bdgGMM. endLearning():計算第ci高斯模型的權值、均值、協方差、協方差的逆和行列式
七、計算每個邊的權重:
初始化gama=50,lamda=9*gama(無窮大),計算bata(歐氏距離)
利用公式gamma * exp(-beta*diff.dot(diff))計算像素四個方向的權重
權重儲存在mat理性的變量
八、fdgGMM同理
九、疊代過程
step1:為每個像素配置設定模型
1、通過mask判斷像素屬于前景還是背景
2、判斷屬于哪個高斯分量(高斯模型密度計算式)
3、将機率最大的ci存儲到對應的compidx中
step2:學習高斯模型參數
1、 利用 bdgGMM. initLearning(): bdgGMM. addSample( int ci, const Vec3d color ),bdgGMM. endLearning(),計算參數。
2、建構圖:每個點與源點和彙點的連接配接權值、鄰域頂點間的連接配接權值
step3:分割
1、最大流算法分割成兩部分
2、更新mask(不更新指定的前景和背景)
回到step1直到疊代結束。
應用grabcut進行圖檔分割:
#pragma once
#include"pch.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
//#include "BorderMatting.h"
#include <iostream>
using namespace std;
using namespace cv;
static void help()
{
cout << "\nThis program demonstrates GrabCut segmentation -- select an object in a region\n"
"and then grabcut will attempt to segment it out.\n"
"Call:\n"
"./grabcut <image_name>\n"
"\nSelect a rectangular area around the object you want to segment\n" <<
"\nHot keys: \n"
"\tESC - quit the program\n"
"\tr - restore the original image\n"
"\tn - next iteration\n"
"\n"
"\tleft mouse button - set rectangle\n"
"\n"
"\tCTRL+left mouse button - set GC_BGD pixels\n"
"\tSHIFT+left mouse button - set CG_FGD pixels\n"
"\n"
"\tCTRL+right mouse button - set GC_PR_BGD pixels\n"
"\tSHIFT+right mouse button - set CG_PR_FGD pixels\n" << endl;
}
const Scalar RED = Scalar(0, 0, 255);
const Scalar PINK = Scalar(230, 130, 255);
const Scalar BLUE = Scalar(255, 0, 0);
const Scalar LIGHTBLUE = Scalar(255, 255, 160);
const Scalar GREEN = Scalar(0, 255, 0);
const int BGD_KEY = CV_EVENT_FLAG_CTRLKEY; //Ctrl鍵
const int FGD_KEY = CV_EVENT_FLAG_SHIFTKEY; //Shift鍵
static void getBinMask(const Mat& comMask, Mat& binMask)
{
if (comMask.empty() || comMask.type() != CV_8UC1)
CV_Error(CV_StsBadArg, "comMask is empty or has incorrect type (not CV_8UC1)");
if (binMask.empty() || binMask.rows != comMask.rows || binMask.cols != comMask.cols)
binMask.create(comMask.size(), CV_8UC1);
binMask = comMask & 1; //得到mask的最低位,實際上是隻保留确定的或者有可能的前景點當做mask
}
class GCApplication
{
public:
enum { NOT_SET = 0, IN_PROCESS = 1, SET = 2 };
static const int radius = 2;
static const int thickness = -1;
void reset();
void setImageAndWinName(const Mat& _image, const string& _winName);
void showImage() const;
void mouseClick(int event, int x, int y, int flags, void* param);
int nextIter();
int getIterCount() const { return iterCount; }
private:
void setRectInMask();
void setLblsInMask(int flags, Point p, bool isPr);
const string* winName;
const Mat* image;
Mat mask;
Mat bgdModel, fgdModel;
uchar rectState, lblsState, prLblsState;
bool isInitialized;
Rect rect;
vector<Point> fgdPxls, bgdPxls, prFgdPxls, prBgdPxls;
int iterCount;
// BorderMatting
};
/*給類的變量指派*/
void GCApplication::reset()
{
if (!mask.empty())
mask.setTo(Scalar::all(GC_BGD));
bgdPxls.clear(); fgdPxls.clear();
prBgdPxls.clear(); prFgdPxls.clear();
isInitialized = false;
rectState = NOT_SET; //NOT_SET == 0
lblsState = NOT_SET;
prLblsState = NOT_SET;
iterCount = 0;
}
/*給類的成員變量指派而已*/
void GCApplication::setImageAndWinName(const Mat& _image, const string& _winName)
{
if (_image.empty() || _winName.empty())
return;
image = &_image;
winName = &_winName;
mask.create(image->size(), CV_8UC1);
reset();
}
/*顯示4個點,一個矩形和圖像内容,因為後面的步驟很多地方都要用到這個函數,是以單獨拿出來*/
void GCApplication::showImage() const
{
if (image->empty() || winName->empty())
return;
Mat res;
Mat binMask;
if (!isInitialized)
image->copyTo(res);
else
{
getBinMask(mask, binMask);
image->copyTo(res, binMask); //按照最低位是0還是1來複制,隻保留跟前景有關的圖像,比如說可能的前景,可能的背景
}
vector<Point>::const_iterator it;
/*下面4句代碼是将選中的4個點用不同的顔色顯示出來*/
for (it = bgdPxls.begin(); it != bgdPxls.end(); ++it) //疊代器可以看成是一個指針
circle(res, *it, radius, BLUE, thickness);
for (it = fgdPxls.begin(); it != fgdPxls.end(); ++it) //确定的前景用紅色表示
circle(res, *it, radius, RED, thickness);
for (it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it)
circle(res, *it, radius, LIGHTBLUE, thickness);
for (it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it)
circle(res, *it, radius, PINK, thickness);
/*畫矩形*/
if (rectState == IN_PROCESS || rectState == SET)
rectangle(res, Point(rect.x, rect.y), Point(rect.x + rect.width, rect.y + rect.height), GREEN, 2);
imshow(*winName, res);
}
/*該步驟完成後,mask圖像中rect内部是3,外面全是0*/
void GCApplication::setRectInMask()
{
assert(!mask.empty());
mask.setTo(GC_BGD); //GC_BGD == 0
rect.x = max(0, rect.x);
rect.y = max(0, rect.y);
rect.width = min(rect.width, image->cols - rect.x);
rect.height = min(rect.height, image->rows - rect.y);
(mask(rect)).setTo(Scalar(GC_PR_FGD)); //GC_PR_FGD == 3,矩形内部,為可能的前景點
}
void GCApplication::setLblsInMask(int flags, Point p, bool isPr)
{
vector<Point> *bpxls, *fpxls;
uchar bvalue, fvalue;
if (!isPr) //确定的點
{
bpxls = &bgdPxls;
fpxls = &fgdPxls;
bvalue = GC_BGD; //0
fvalue = GC_FGD; //1
}
else //機率點
{
bpxls = &prBgdPxls;
fpxls = &prFgdPxls;
bvalue = GC_PR_BGD; //2
fvalue = GC_PR_FGD; //3
}
if (flags & BGD_KEY)
{
bpxls->push_back(p);
circle(mask, p, radius, bvalue, thickness); //該點處為2
}
if (flags & FGD_KEY)
{
fpxls->push_back(p);
circle(mask, p, radius, fvalue, thickness); //該點處為3
}
}
/*滑鼠響應函數,參數flags為CV_EVENT_FLAG的組合*/
void GCApplication::mouseClick(int event, int x, int y, int flags, void*)
{
// TODO add bad args check
switch (event)
{
case CV_EVENT_LBUTTONDOWN: // set rect or GC_BGD(GC_FGD) labels左鍵按下
{
bool isb = (flags & BGD_KEY) != 0,
isf = (flags & FGD_KEY) != 0;
if (rectState == NOT_SET && !isb && !isf)//隻有左鍵按下時
{
rectState = IN_PROCESS; //表示正在畫矩形
rect = Rect(x, y, 1, 1);
}
if ((isb || isf) && rectState == SET) //按下了alt鍵或者shift鍵,且畫好了矩形,表示正在畫前景背景點
lblsState = IN_PROCESS;
}
break;
case CV_EVENT_RBUTTONDOWN: // set GC_PR_BGD(GC_PR_FGD) labels右鍵按下
{
bool isb = (flags & BGD_KEY) != 0,
isf = (flags & FGD_KEY) != 0;
if ((isb || isf) && rectState == SET) //正在畫可能的前景背景點
prLblsState = IN_PROCESS;
}
break;
case CV_EVENT_LBUTTONUP://左鍵松起
if (rectState == IN_PROCESS)
{
rect = Rect(Point(rect.x, rect.y), Point(x, y)); //矩形結束
rectState = SET;
setRectInMask();
assert(bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty());
showImage();
}
if (lblsState == IN_PROCESS) //已畫了前後景點
{
setLblsInMask(flags, Point(x, y), false); //畫出前景點
lblsState = SET;
showImage();
}
break;
case CV_EVENT_RBUTTONUP://右鍵松起
if (prLblsState == IN_PROCESS)
{
setLblsInMask(flags, Point(x, y), true); //畫出背景點
prLblsState = SET;
showImage();
}
break;
case CV_EVENT_MOUSEMOVE:
if (rectState == IN_PROCESS)
{
rect = Rect(Point(rect.x, rect.y), Point(x, y));
assert(bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty());
showImage(); //不斷的顯示圖檔
}
else if (lblsState == IN_PROCESS)
{
setLblsInMask(flags, Point(x, y), false);
showImage();
}
else if (prLblsState == IN_PROCESS)
{
setLblsInMask(flags, Point(x, y), true);
showImage();
}
break;
}
}
/*該函數進行grabcut算法,并且傳回算法運作疊代的次數*/
int GCApplication::nextIter()
{
if (isInitialized)
//使用grab算法進行一次疊代,參數2為mask,裡面存的mask位是:矩形内部除掉那些可能是背景或者已經确定是背景後的所有的點,且mask同時也為輸出
//儲存的是分割後的前景圖像
grabCut(*image, mask, rect, bgdModel, fgdModel, 1);
else
{
if (rectState != SET)
return iterCount;
if (lblsState == SET || prLblsState == SET)
grabCut(*image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_MASK);
else
grabCut(*image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_RECT);
isInitialized = true;
}
iterCount++;
bgdPxls.clear(); fgdPxls.clear();
prBgdPxls.clear(); prFgdPxls.clear();
return iterCount;
}
GCApplication gcapp;
static void on_mouse(int event, int x, int y, int flags, void* param)
{
gcapp.mouseClick(event, x, y, flags, param);
}
int main(int argc, char** argv)
{
string filename = "D:/picture/source/1.jpg";
Mat image = imread(filename, 1);
if (image.empty())
{
cout << "\n Durn, couldn't read image filename " << filename << endl;
return 1;
}
help();
const string winName = "image";
cvNamedWindow(winName.c_str(), CV_WINDOW_AUTOSIZE);
cvSetMouseCallback(winName.c_str(), on_mouse, 0);//window_name 回掉函數需要注冊到的視窗,即産生事件的視窗,on_mouse 在注冊視窗點選滑鼠時,執行的回掉函數,param 用于傳遞到回掉函數的參數
gcapp.setImageAndWinName(image, winName);
gcapp.showImage();
for (;;)
{
int c = cvWaitKey(0);
switch ((char)c)
{
case '\x1b'://esc退出
cout << "Exiting ..." << endl;
goto exit_main;
case 'r':
cout << endl;
gcapp.reset();
gcapp.showImage();
break;
case 'n':
int iterCount = gcapp.getIterCount();
cout << "<" << iterCount << "... ";
int newIterCount = gcapp.nextIter();
if (newIterCount > iterCount)
{
gcapp.showImage();
cout << iterCount << ">" << endl;
}
else
cout << "rect must be determined>" << endl;
break;
}
}
exit_main:
cvDestroyWindow(winName.c_str());
return 0;
}
分割結果顯示:
參考https://blog.csdn.net/zouxy09/article/details/8535087
https://blog.csdn.net/wh1319501722/article/details/80272167
https://blog.csdn.net/yangyangyang20092010/article/details/25967303