上篇中介绍了图像金字塔的简单操作,这篇使用图像金字塔的知识来做一个高端点的操作,图像分割。
金字塔图像分割
OpenCV中使用图像金字塔进行图像分割操作,使用的函数为
cvPyrSegmentation( IplImage* src, IplImage* dst, CvMemStorage* storage, CvSeq** comp, int level, double threshold1, double threshold2 );
其中:src: 输入图像;dst: 输出图像;Storage:存储连通部件的序列结果;comp:分割部件的输出序列指针; level:建立金字塔的最大层数 ;threshold1:建立连接的错误阈值;threshold2:分割簇的错误阈值 。需要注意的是图像的尺寸以及金字塔的层数,图像的宽度和高度必须能被2整除,能够被2整除的次数决定了金字塔的最大层数。
下面是切割效果演示图:
这是只有一层金字塔时的分割图:
这是有三层金字塔时的分割图:
可以看出,金字塔层数越高,图像的分割效果越明显。
OpenCV源代码
// ImageSegmentation_ImagePyramid.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "highgui.h"
#include "cv.h"
//使用 cvPyrSegmentation 分割函数需要包含此头文件
#include <opencv2/legacy/legacy.hpp>
using namespace std;
using namespace cv;
bool DoImageSegmentation(IplImage *src