opencv 金字塔图像分割

opencv中有封装好的cvPyrSegmentation函数,参数解释如下:

PyrSegmentation
用金字塔实现图像分割 

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-通道图像,且大小一样 



Threshold1,Threshold2的解读:

看到别人博客里有人在问这两个参数的区别,其实就如上面黄色底纹文字所说,

金字塔分割先通过p(c(a),c(b))<threshold1在像素a,b...中找连通域,也就是所谓的连接部件A,B...

第二步通过p(c(A),c(B))<threshold2判断两个联通与是否属于同一个簇,簇即使我们最后得到的分割结果


例程:

[cpp]  view plain copy
  1. void ON_SEGMENT(int a)  
  2. {     
  3.     cvPyrSegmentation(images0, images1, storage, &comp,   
  4.         level, threshold1+1, threshold2+1);  
  5.     cvShowImage("Segmentation", images1);  
  6. }  
  7.   
  8. void CCVMFCView::OnPyrSegmentation()  
  9. {  
  10.     images[0] = cvCloneImage(workImg);  
  11.     cvFlip(images[0]);  
  12.   
  13.     cvNamedWindow("Segmentation", 1);  
  14.   
  15.     storage = cvCreateMemStorage ( block_size );  
  16.   
  17.     images[0]->width &= -(1<<level);  
  18.     images[0]->height &= -(1<<level);  
  19.   
  20.     images0 = cvCloneImage( images[0] );  
  21.     images1 = cvCloneImage( images[0] );  
  22.     // 对彩色图像进行分割  
  23.     l = 1;  
  24.     threshold1 =255;  
  25.     threshold2 =30;  
  26.   
  27.   
  28.     sthreshold1 = cvCreateTrackbar("Threshold1""Segmentation",   
  29.         &threshold1, 255, ON_SEGMENT);  
  30.     sthreshold2 = cvCreateTrackbar("Threshold2""Segmentation",     
  31.         &threshold2, 255,ON_SEGMENT);  
  32.   
  33.     ON_SEGMENT(1);  
  34.   
  35.     cvWaitKey(0);  
  36.     cvDestroyWindow( "Segmentation" );   
  37.     cvFlip(images1);  
  38.   
  39.     images1->width = workImg->width;  
  40.     images1->height = workImg->height;  
  41.   
  42.     cvReleaseMemStorage(&storage );  
  43.     cvReleaseImage(&images[0]);  
  44.     cvReleaseImage(&images0);  
  45.   
  46.     m_dibFlag=imageReplace(images1,&workImg);   
  47.     m_ImageType=1;  
  48.     Invalidate();  
  49. }  

金字塔图像分割结果:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值