cvPyrSegmentation 高斯图像金字塔做分割

/*
 * cvPyrSegmentation用高斯图像金字塔做分割, 此函数还不支持直接画出contour,只能画出外接矩形
 * 实验结果中分割的效果不佳
 * Rocky Chen
 * 2012/3/18
 */
#include "stdio.h"
#include "stdafx.h"
#include "highgui.h"
#include <cv.h>
#include <cxcore.h>
#include "iostream"

using namespace std;
using namespace cv;

int main(int argc, char** argv)  
{  
 const char* win_original="original photo";  
 const char* win_PyrSegmentation = "PyrSegmentation"; 
 const char* photo_file="E:\\iVew\\AB\\001.jpg"; //640*480 一定要检查图像是否能被2整除n次!!!!!

 IplImage* img = cvLoadImage(photo_file);
 assert(img != NULL); //有指针或内存分配要检查是否为空.表达式写完整,不能用assert(!img)代替
 IplImage* img_dst = cvCreateImage(cvGetSize(img), img->depth,3);
 assert(img_dst != NULL);  
 CvMemStorage* storage = cvCreateMemStorage(0); //分配存储区域
 assert( storage !=NULL); 
 CvSeq* comp = NULL; //cvPyrSegmentation 需要用指向此指针的指针.
 
 cvNamedWindow(win_original);
 cvNamedWindow(win_PyrSegmentation);
 
 cvPyrSegmentation(img, img_dst, storage, &comp, 4, 150, 30);  // 图像的分辨率必须能被2整除n=4次
 for (int i = comp->total -3; i < comp->total; i++)

CvConnectedComp* cc = (CvConnectedComp*) cvGetSeqElem(comp,i); //取序列
//画出block的外接矩形, cvPyrSegmentation还不支持直接画出contour, 分割的效果不佳
cvRectangle(img, 
cvPoint(cc->rect.x,cc->rect.y), 
cvPoint(cc->rect.x + cc->rect.width, cc->rect.y + cc->rect.height),
CV_RGB(255,0,0));
}

cvShowImage(win_original,img);
cvShowImage(win_PyrSegmentation, img_dst);
cvWaitKey();

cvReleaseMemStorage(&storage);
cvReleaseImage(&img);
cvReleaseImage(&img_dst);
cvDestroyAllWindows();

 return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值