GraphCut & GrabCut

图像分割一直是图像处理中一项棘手的问题。图像分割算法从大的方面讲可以分为两类:
     1   全自动图像分割:一般采用聚类算法来最大化前景与背景的差。
     2   用户互动式图像分割:用户提供前景和背景的种子,然后对前景背景建立概率分布模型。
GraphCutGrabCut就是属于第二类图像分割算法。
没有用GraphCut的源码。opencv有GrabCut的一个例程。我稍作了一点操作上的修改而已。这个算法的效率不是很高,迭代了45次左右。刚开始没有添加种子点,后面需要。会丢失一些细节的(当然)。


源码:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#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;
}

//GC_BGD    = 0,  //!< an obvious background pixels
//GC_FGD    = 1,  //!< an obvious foreground (object) pixel
//GC_PR_BGD = 2,  //!< a possible background pixel
//GC_PR_FGD = 3   //!< a possible foreground pixel
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);
Mat resSave;

//CTRL键
const int BGD_KEY = CV_EVENT_FLAG_CTRLKEY;
//SHIFT键
const int FGD_KEY = CV_EVENT_FLAG_SHIFTKEY;

//产生一个mask comMask中为1或3的会在binMask中保留
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;
}

class GCApplication
{
public:
    //NOT_SET代表还没有开始绘图
    //IN_PROCESS代表正在绘图
    //SET代表绘图完成
    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();
    void saveimg();
    int getIterCount() const { return iterCount; }
    void clearRec();

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;
};
void GCApplication::clearRec(){
    rectangle( resSave, Point( rect.x, rect.y ), Point(rect.x + rect.width, rect.y + rect.height ), Scalar(0, 0, 0), 2);
}
void GCApplication::saveimg(){
    imwrite("newme.jpeg", resSave);
}
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;
    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();
}

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 );
    }

    vector<Point>::const_iterator it;
    //背景是蓝色的 不确定的背景是浅蓝
    //前景是红色的 不确定的前景是粉红
    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 );

    //每一次imshow之前,res都更新了,变成了原图(如果没有mask)
    //这样就有了矩形框的更新,而且,前一次的矩形框会消失
    //因为矩形框是直接在原图上画的!!!!!
    //终于找到了这个方法。
    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 );
    res.copyTo(resSave);
    return;
}

void GCApplication::setRectInMask()
{
    assert( !mask.empty() );
    //先全0
    mask.setTo( GC_BGD );
    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) );
}

void GCApplication::setLblsInMask( int flags, Point p, bool isPr )
{
    vector<Point> *bpxls, *fpxls;
    uchar bvalue, fvalue;
    if( !isPr )
    {
        //取地址 bpxls和bgdPxls同时改变
        //在画完矩形之前 这个Vector都是空的
        //画完之后开始给景打标签,Vector不为空
        bpxls = &bgdPxls;
        fpxls = &fgdPxls;
        bvalue = GC_BGD;
        fvalue = GC_FGD;
    }
    else
    {
        bpxls = &prBgdPxls;
        fpxls = &prFgdPxls;
        bvalue = GC_PR_BGD;
        fvalue = GC_PR_FGD;
    }
    //记住这种用法 代表flags==BGD_KEY CTRL
    if( flags & BGD_KEY )
    {
        //保存
        bpxls->push_back(p);
        //绘圆
        circle( mask, p, radius, bvalue, thickness );
    }
    //SHIFT
    if( flags & FGD_KEY )
    {
        fpxls->push_back(p);
        circle( mask, p, radius, fvalue, thickness );
    }
}

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 )
            {
                //没有按下CTRL SHIFT; 绘制矩形
                rectState = IN_PROCESS;
                rect = Rect( x, y, 1, 1 );
            }
            //矩形画完!了 标前后景
            if ( (isb || isf) && rectState == SET )
                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;
    }
}

int GCApplication::nextIter()
{
    if( isInitialized ) {
        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 );
        //mask会保存图片?
        //mask.copyTo(Newme);

        isInitialized = true;
    }
    iterCount++;
    //每次迭代以前清空之前的种子像素,会有新的mask
    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 )
{
//    if( argc!=2 )
//    {
//        help();
//        return 1;
//    }
//    string filename = argv[1];
    string filename = "../me.jpeg";
    if( filename.empty() )
    {
        cout << "\ncouldn't read in " << argv[1] << endl;
        return 1;
    }
    Mat image = imread( filename, 1 );
    resize(image, image, Size(int(image.cols/1.3), int(image.rows/1.3)), NULL, NULL, 1);
    if( image.empty() )
    {
        cout << "\n couldn't read image filename " << filename << endl;
        return 1;
    }

    help();

    const string winName = "image";
    //如果不是autosize,坐标点会出现问题诶...
    namedWindow( winName, WINDOW_AUTOSIZE );
    setMouseCallback( winName, on_mouse, 0 );

    gcapp.setImageAndWinName( image, winName );
    gcapp.showImage();

    for(;;)
    {
        int c = waitKey(0);
        switch( (char) c )
        {
            //16进制27:Esc
            case '\x1b': {
                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;
            case 's': {
                //把矩形去掉
                gcapp.clearRec();
                cout<<"saving"<<endl;
                gcapp.saveimg();
            }
                break;

        }

    }

    exit_main:
    destroyWindow( winName );
    return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Graphcut是一种图像分割算法,它基于图论理论和最小割最大流算法。在Python中,我们可以使用一些库来实现Graphcut算法,例如OpenCV和scikit-image。 在OpenCV中,我们可以使用grabCut函数来实现图像分割。在使用grabCut函数之前,我们需要准备一个掩膜图像,用于指示算法应该将图像的哪一部分视为背景和前景。掩膜图像通常是一个与原始图像尺寸相同的二进制图像,其中前景区域用白色表示,背景区域用黑色表示。 以下是一个使用OpenCV的grabCut函数的简单例子: ``` python import cv2 import numpy as np # 读取图像 img = cv2.imread('image.jpg') # 创建掩膜图像 mask = np.zeros(img.shape[:2], np.uint8) # 设置前景和背景模型 bgdModel = np.zeros((1,65),np.float64) fgdModel = np.zeros((1,65),np.float64) # 定义矩形边界(前景区域) rect = (50, 50, 300, 500) # 运行grabCut算法 cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) # 通过掩膜图像提取前景区域 mask2 = np.where((mask==2)|(mask==0), 0, 1).astype('uint8') img = img * mask2[:, :, np.newaxis] # 显示结果 cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 另一个库scikit-image也提供了Graphcut的实现。在scikit-image中,我们可以使用函数`graph.cut_normalized`来实现图像分割。该函数需要构建一个图形数据结构,其中每个像素作为图的一个节点,通过定义节点之间的边来表示像素之间的相似性。然后运行图割算法,将图像分割为前景和背景。 以下是一个使用scikit-image的Graphcut算法的简单例子: ``` python import numpy as np from skimage.feature import graph from skimage.segmentation import cut_normalized from skimage.io import imread # 读取图像 img = imread('image.jpg') # 转换图像为灰度图 gray_img = np.mean(img, axis=2).astype('uint8') # 构建图形数据结构 g = graph.rag_mean_color(gray_img, img) # 运行图割算法 labels = cut_normalized(gray_img, g) # 对每个像素进行着色 out = img.copy() for i in np.unique(labels): out[labels == i] = np.mean(img[labels == i], axis=0) # 显示结果 plt.imshow(out) plt.axis('off') plt.show() ``` 上述例子中,我们首先将图像转换为灰度图,然后构建一个连通图,其中每个节点表示一个像素,边的权重表示像素之间的相似性。接下来,我们运行图割算法,并根据分割结果对每个像素进行着色,最后显示结果。 以上是使用Python实现Graphcut算法的简单示例。根据具体的应用场景和需求,我们可以进一步调整参数和优化算法来获得更好的分割效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值