opencv——superpixel算法——SLIC,SEEDS,LSC

转载自https://blog.csdn.net/zhangyonggang886/article/details/52854219

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内容如下:

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

运行结果如下:


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

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

 1 #include <opencv2/core.hpp>
 2 
 3 namespace cv
 4 {
 5 namespace ximgproc
 6 {
 7 
 8 class CV_EXPORTS_W SuperpixelSEEDS : public Algorithm
 9 {
10 public:
11 
12 CV_WRAP virtual int getNumberOfSuperpixels() = 0;
13 
14 CV_WRAP virtual void iterate(InputArray img, int num_iterations=4) = 0;
15 
16 CV_WRAP virtual void getLabels(OutputArray labels_out) = 0;
17 
18 CV_WRAP virtual void getLabelContourMask(OutputArray image, bool thick_line = false) = 0;
19 
20 virtual ~SuperpixelSEEDS() {}
21 };
22 
23 CV_EXPORTS_W Ptr<SuperpixelSEEDS> createSuperpixelSEEDS(
24 int image_width, int image_height, int image_channels,
25 int num_superpixels, int num_levels, int prior = 2,
26 int histogram_bins=5, bool double_step = false);
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();//获取超像素的数量

运行结果如下:

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


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();//获取超像素的数量

运行结果如下:


三个算法完整代码如下:

#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] 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.

原文地址:https://www.cnblogs.com/lemon333333/p/10309607.html