opencv3以上实现超像素

感谢博主的整理,收藏用,原文出自:https://blog.csdn.net/zhangyonggang886/article/details/52854219?locationNum=5&fps=1

opencv关于超像素生成,目前没有发现网上有代码,这里为了方便大家使用超像素,我整理了一下opencv生成超像素的方法,希望对大家有帮助。

这里主要介绍使用opencv生成superpixel,主要介绍的算法为SLIC,SEEDS,LSC。但是目前superpixel生成算法在OpenCV 3.1.0的Release版本中并不存在,因为他们是存放在opencv_contrib目录下面的未稳定功能模块,所以如果我们想要使用这个目录的功能,就需要自己重新进行OpenCV的编译。编译opencv网上有好多教程,大家可以查一查,按照教程一般都能够自己编译opencv。编译所需要的资源如下: 
opencv3.1下载地址:http://opencv.org/downloads.html 
opencv_contrib-master下载地址:https://github.com/opencv/opencv 
cmake-gui下载地址:https://cmake.org/download/ 
这里我就不具体介绍如何编译opencv了,编译以后的文件目录如下: 
这里写图片描述

install就是我们需要的目录,这里面的目录结构和我们下载opencv release版本差不多,见下图:

这里写图片描述
按照官网release版本做相应的配置就OK了。这里如要配置内容如下:这里写图片描述
这里写图片描述

具体的编译,配置就简单介绍到这里,下面是本文的主要内容。

superpixel相关的类在cv::ximgproc命名空间下,在opencv文档中。我们可以看到一下内容: 
这里写图片描述
opencv官方文档中SLIC内容如下:

#include <opencv2/core.hpp>
namespace cv
{
namespace ximgproc
{
class CV_EXPORTS_W SuperpixelSLIC : public Algorithm
{
public:
   // 这个函数用于获得超像素的数量
    CV_WRAP virtual int getNumberOfSuperpixels() const = 0;
    //迭代的次数
    CV_WRAP virtual void iterate( int num_iterations = 10 ) = 0;
    //获得图像超像素标签,是一个CV_32SC1的Mat,标签的值在这个范围内[0, getNumberOfSuperpixels()]
    CV_WRAP virtual void getLabels( OutputArray labels_out ) const = 0;
    //获取超像素的边界,用于展示superpixel分割情况
    CV_WRAP virtual void getLabelContourMask( OutputArray image, bool thick_line = true ) const = 0;
    //这里主要是合并一些小的superpixel,min_element_size 最小超像素,像素点的数量
    CV_WRAP virtual void enforceLabelConnectivity( int min_element_size = 25 ) = 0;
};
    //算法的种类
    enum SLIC { SLIC = 100, SLICO = 101 };
    //静态构造方法
    CV_EXPORTS_W Ptr<SuperpixelSLIC> createSuperpixelSLIC( InputArray image, int algorithm = SLICO, int region_size = 10, float ruler = 10.0f );
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

完整代码如下:

#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include <opencv2/ximgproc.hpp>
#include<ctime>
using namespace cv;
using namespace std;

int main()
{
    clock_t start;
    clock_t end;
    Mat frame,labels;
    VideoCapture capture("E://image/skating2.avi");//打开文件
    Mat mask;
    if (!capture.isOpened()) 
    {
        cout << "文件打开失败!" << endl;  
    }

    while (1) 
    {
        capture >> frame;//获取一帧图像
        start = clock();//开始计时

        Ptr<cv::ximgproc::SuperpixelSLIC> slic = cv::ximgproc::createSuperpixelSLIC(frame);//创建一个对象

        slic->iterate();//迭代次数,默认为10
        slic->enforceLabelConnectivity();
        slic->getLabelContourMask(mask);//获取超像素的边界
        slic->getLabels(labels);//获取labels
        int number = slic->getNumberOfSuperpixels();//获取超像素的数量

        frame.setTo(Scalar(255, 255, 255), mask);
        end = clock();//结束计时
        cout << "时间:" << end - start << endl;

        imshow("test", frame);

        int key = waitKey(1);
        if (key == 27)
            break;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

运行结果如下: 
这里写图片描述

这里面具体参数的设定,大家可以参考文档和算法作者的论文,论文已经在参考文献中列举出来了。

opencv官方文档中SEEDS内容如下:

#include <opencv2/core.hpp>

namespace cv
{
namespace ximgproc
{

class CV_EXPORTS_W SuperpixelSEEDS : public Algorithm
{
public:

    CV_WRAP virtual int getNumberOfSuperpixels() = 0;

    CV_WRAP virtual void iterate(InputArray img, int num_iterations=4) = 0;

    CV_WRAP virtual void getLabels(OutputArray labels_out) = 0;

    CV_WRAP virtual void getLabelContourMask(OutputArray image, bool thick_line = false) = 0;

    virtual ~SuperpixelSEEDS() {}
};

CV_EXPORTS_W Ptr<SuperpixelSEEDS> createSuperpixelSEEDS(
    int image_width, int image_height, int image_channels,
    int num_superpixels, int num_levels, int prior = 2,
    int histogram_bins=5, bool double_step = false);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

seeds算法实现过程差不多,代码片段如下:

    //这里可以放到循环外面
    Ptr<cv::ximgproc::SuperpixelSEEDS> seeds = cv::ximgproc::createSuperpixelSEEDS(frame.cols, frame.rows, frame.channels(), 1000, 15, 2, 5, true);

    seeds->iterate(frame);//迭代次数,默认为4
    seeds->getLabels(labels);//获取labels
    seeds->getLabelContourMask(mask);;//获取超像素的边界
    int number_seeds = seeds->getNumberOfSuperpixels();//获取超像素的数量
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

运行结果如下: 
这里写图片描述
opencv官方文档中LSC内容如下:

#include <opencv2/core.hpp>

namespace cv
{
namespace ximgproc
{

class CV_EXPORTS_W SuperpixelLSC : public Algorithm
{
public:

    CV_WRAP virtual int getNumberOfSuperpixels() const = 0;

    CV_WRAP virtual void iterate( int num_iterations = 10 ) = 0;

    CV_WRAP virtual void getLabels( OutputArray labels_out ) const = 0;

    CV_WRAP virtual void getLabelContourMask( OutputArray image, bool thick_line = true ) const = 0;

    CV_WRAP virtual void enforceLabelConnectivity( int min_element_size = 20 ) = 0;
};

    CV_EXPORTS_W Ptr<SuperpixelLSC> createSuperpixelLSC( InputArray image, int region_size = 10, float ratio = 0.075f );
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

LSC算法的代码片段如下:

    Ptr<cv::ximgproc::SuperpixelLSC> lsc = cv::ximgproc::createSuperpixelLSC(frame);

    lsc->iterate();//迭代次数,默认为4
    lsc->enforceLabelConnectivity();
    lsc->getLabels(labels);//获取labels
    lsc->getLabelContourMask(mask);;//获取超像素的边界
    int number_lsc = lsc->getNumberOfSuperpixels();//获取超像素的数量
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

运行结果如下: 
这里写图片描述

三个算法完整代码如下:

#include "stdafx.h"
#include<opencv2/opencv.hpp>
#include <opencv2/ximgproc.hpp>
#include<ctime>
using namespace cv;
using namespace std;

int main()
{
    clock_t start;
    clock_t end;
    Mat frame,labels;
    VideoCapture capture("E://image/skating2.avi");//打开文件
    Mat mask;
    if (!capture.isOpened()) 
    {
        cout << "文件打开失败!" << endl;  
    }

    while (1) 
    {
        capture >> frame;//获取一帧图像
        start = clock();//开始计时

        //Ptr<cv::ximgproc::SuperpixelSLIC> slic = cv::ximgproc::createSuperpixelSLIC(frame);//创建一个对象

        //slic->iterate();//迭代次数,默认为10
        //slic->enforceLabelConnectivity();
        //slic->getLabelContourMask(mask);//获取超像素的边界
        //slic->getLabels(labels);//获取labels
        //int number_slic = slic->getNumberOfSuperpixels();//获取超像素的数量

        //这里可以放到循环外面
        //Ptr<cv::ximgproc::SuperpixelSEEDS> seeds = cv::ximgproc::createSuperpixelSEEDS(frame.cols, frame.rows, frame.channels(), 1000, 15, 2, 5, true);

        //seeds->iterate(frame);//迭代次数,默认为4
        //seeds->getLabels(labels);//获取labels
        //seeds->getLabelContourMask(mask);;//获取超像素的边界
        //int number_seeds = seeds->getNumberOfSuperpixels();//获取超像素的数量

        Ptr<cv::ximgproc::SuperpixelLSC> lsc = cv::ximgproc::createSuperpixelLSC(frame);

        lsc->iterate();//迭代次数,默认为4
        lsc->enforceLabelConnectivity();
        lsc->getLabels(labels);//获取labels
        lsc->getLabelContourMask(mask);;//获取超像素的边界
        int number_lsc = lsc->getNumberOfSuperpixels();//获取超像素的数量

        frame.setTo(Scalar(255, 255, 255), mask);
        end = clock();//结束计时
        cout << "时间:" << end - start << endl;

        imshow("test", frame);

        int key = waitKey(1);
        if (key == 27)
            break;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

大体上介绍到这里,如果文中描述有什么问题,大家可以提出来。

参考文献: 
[1] Zhengqin Li and Jiansheng Chen. Superpixel segmentation using linear spectral clustering. June 2015. 
[2] Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua, and Sabine Susstrunk. Slic superpixels compared to state-of-the-art superpixel methods. IEEE Trans. Pattern Anal. Mach. Intell., 34(11):2274–2282, nov 2012. 
[3] Michael Van den Bergh, Xavier Boix, Gemma Roig, Benjamin de Capitani, and Luc Van Gool. Seeds: Superpixels extracted via energy-driven sampling. In Computer Vision–ECCV 2012, pages 13–26. Springer, 2012.

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值