opencv——superpixel算法——SLIC,SEEDS,LSC
来源:互联网 发布:微博微数据能看访客 编辑:程序博客网 时间:2024/06/05 16:57
整理资料不容易,版权所,转载请标明出处,谢谢!!
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 );}}
完整代码如下:
#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内容如下:
#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);}}
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.
- opencv——superpixel算法——SLIC,SEEDS,LSC
- SLIC算法 超像素 superpixel
- SLIC超像素(superpixel)算法
- SLIC超像素(superpixel)算法
- SLIC超像素(superpixel)算法
- SLIC超像素(superpixel)算法
- 算法学习——LSC最大公有子序列 java实现
- SLIC superpixel实现分析
- SLIC与目前最优超像素算法的比较 SLIC Superpixels Compared to State-of-the-art Superpixel Methods
- LSC 的优化算法
- SLIC Superpixels Compared toState-of-the-Art Superpixel Methods
- SLIC Superpixels Compared to State-of-the-Art Superpixel Methods
- Seeds算法(SEEDS: Superpixels Extracted via Energy-Driven Sampling)
- LSC
- 分水 算法——opencv
- OpenCV学习——Bresenham算法
- 学习OpenCV——学习CamShift算法
- 学习OpenCV——EMD算法
- LeetCode 155. Min Stack
- 跨浏览器 创建xmlhttprequest 对象 以及和服务器交互的全过程
- 关于 inet_ntoa函数
- Web 网页通信之长连接、长轮询
- linux 用户和用户组的管理
- opencv——superpixel算法——SLIC,SEEDS,LSC
- Unity3D 射线法获取被点击对象的名称
- Linux常用命令
- 顺序表上进行增,删,查,改操作,C++实现
- Gazebo機器人仿真學習探索筆記(七)连接ROS
- Android xxxActivity does not exsits
- int类型整数的表示范围
- 代码中的输入输出(重定向、文件流C/C++)
- 如何查看jdk的版本是32位还是64位?