/* Splits color or grayscale image into multiple connected components
of nearly the same color/brightness using modification of Burt algorithm.
comp with contain a pointer to sequence (CvSeq)
of connected components (CvConnectedComp) */
/*
CVAPI(void) cvPyrSegmentation( IplImage* src, IplImage* dst,
CvMemStorage* storage, CvSeq** comp,
int level, double threshold1,
double threshold2 );
src
输入图像.
dst
输出图像.
storage
Storage: 存储连通部件的序列结果
comp
分割部件的输出序列指针 components.
level
建立金字塔的最大层数
threshold1
建立连接的错误阈值
threshold2
分割簇的错误阈值
函数 cvPyrSegmentation 实现了金字塔方法的图像分割。金字塔建立到 level 指定的最大层数。如果 p(c(a),c(b))<threshold1,则在层 i 的象素点 a 和它的相邻层的父亲象素 b 之间的连接被建立起来,
定义好连接部件后,它们被加入到某些簇中。如果p(c(A),c(B))<threshold2,则任何两个分割 A 和 B 属于同一簇。
如果输入图像只有一个通道,那么
p(c1,c2)=|c1-c2|.
如果输入图像有单个通道(红、绿、兰),那幺
p(c1,c2)=0,3·(c1r-c2r)+0,59·(c1g-c2g)+0,11·(c1b-c2b) .
每一个簇可以有多个连接部件。图像 src 和 dst 应该是 8-比特、单通道 或 3-通道图像,且大小一样
*/
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
void doPyrSegmentation( IplImage * src ,IplImage * dst
){
assert(src->width%2 == 0 && src->height%2 == 0);
CvMemStorage * stoage = cvCreateMemStorage(0) ;
CvSeq* comp=NULL;
int level = 3 ; //进行n层采样
double threshold1 = 150 ;
double threshold2 = 30 ;
cvPyrSegmentation(src,dst, stoage,&comp,level, threshold1,threshold2) ;
};
int main(int argc,char ** argv)
{
IplImage* src = NULL;
IplImage* dst = NULL;
src = cvLoadImage("C:\\Users\\Administrator\\Desktop\\1002.png");
dst=cvCreateImage(cvGetSize(src), src->depth,src->nChannels);
doPyrSegmentation(src,dst);
cvNamedWindow("src") ;
cvNamedWindow("dst") ;
cvShowImage("src",src);
cvShowImage("dst",dst);
cvWaitKey(0) ;
cvDestroyAllWindows();
return 0;
}