学习OpenCV2——MeanShift之图形分割

1. 原理

    用meanshift做图像平滑和分割,其实是一回事。其本质是经过迭代,将收敛点的像素值代替原来的像素值,从而去除了局部相似的纹理,同时保留了边缘等差异较大的特征。


        OpenCV中自带有基于meanshift的分割方法pyrMeanShiftFiltering()。由函数名pyrMeanShiftFiltering可知,这里是将meanshift算法和图像金字塔相结合用来分割的。

<span style="font-size:18px;">void PyrMeanShiftFiltering( const CvArr* srcarr,          //输入图像
				    CvArr* dstarr,        //输出图像
				   double  sp,            //颜色域半径
				    double sr,            //空间域半径
				       int max_level,     //金字塔最大层数                    
			    CvTermCriteria termcrit )     //迭代终止条件</span>

    要求输入和输出图像都是CV_8UC3类型,而且两者尺寸一样。实际上并不需要去先定义dstarr,因为程序里会将srcarr的格式赋值给dstarr。

    termcrit有三种情况,迭代次数、迭代精度和两者同时满足。默认为迭代次数为5同时迭代精度为1。termcrit是个结构体,其结构如下

<span style="font-size:18px;">typedef struct CvTermCriteria
{
    int    type;        /*CV_TERMCRIT_ITER或CV_TERMCRIT_EPS 或二者都是*/
    int    max_iter;   /* 最大迭代次数 */
    double epsilon;    /* 结果的精确性 */
}
CvTermCriteria;</span>
     使用pyrMeanShiftFiltering()进行图像分割非常简单,只需要定义sp0,sr,max_level和termrit,然后调用pyrMeanShiftFiltering()就行了。

    在实际操作时,为了使分割的结果显示得更明显,经常用floodFill( )将不同连通域涂上不同的颜色。具体情况参看下 面的实例。


2. 程序实例

    来看看OpenCV自带的一个用meanshift进行分割的例子

    原程序见   “  .\OpenCV249\sources\samples\cpp\meanshift_segmentation.cpp”

<span style="font-size:18px;">#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

static void help(char** argv)
{
    cout << "\nDemonstrate mean-shift based color segmentation in spatial pyramid.\n"
    << "Call:\n   " << argv[0] << " image\n"
    << "This program allows you to set the spatial and color radius\n"
    << "of the mean shift window as well as the number of pyramid reduction levels explored\n"
    << endl;
}

//This colors the segmentations
static void fl
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值