opencv例程之彩色图像的分割

彩色图像的分割所用库函数如下:

//连通域数据结构定义

typedef struct CvConnectedComp
{
    double area;    /* area of the connected component  */
    CvScalar value; /* average color of the connected component */
    CvRect rect;    /* ROI of the component  */
    CvSeq* contour; /* optional component boundary
                      (the contour might have child contours corresponding to the holes)*/
}
CvConnectedComp;

 

//用指定颜色填充一个连接域

void cvFloodFill( CvArr* image, CvPoint seed_point, CvScalar new_val, CvScalar lo_diff=cvScalarAll(0), CvScalar up_diff=cvScalarAll(0), CvConnectedComp* comp=NULL, int flags=4, CvArr* mask=NULL );

函数参考:http://www.opencv.org.cn/index.php/Cv%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86

 

精简的演示代码如下:

#include<cv.h>
#include<highgui.h>
#include<stdio.h>
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"cvaux.lib")

const char* windowname = "彩色图像分割demo";//图像窗口标题
IplImage* image = NULL;//储存图像的指针
IplImage* mask = NULL;//储存掩码图像的指针
int lo_diff = 20, up_diff = 20;//上下的偏差

void OnMouse(int event, int x, int y, int flags, void* param)
{
	switch(event)
	{

		case CV_EVENT_LBUTTONDOWN:
		CvPoint center;
		center.x = x;
		center.y = y;
		CvConnectedComp comp; //连通组件对象
		int flags = 4 + (255 << 8) +CV_FLOODFILL_FIXED_RANGE;//填充标志 更新掩码图像
		cvFloodFill(image,center,CV_RGB(255,0,0),cvScalarAll(lo_diff),cvScalarAll(up_diff),&comp,flags,mask);
		printf("连通的像素数:%g\t种子点坐标:x=%d\ty=%d\n",comp.area,lo_diff,up_diff);
		break;
	}
}


int main()
{	
	image=cvLoadImage("fruits.jpg",1);
	mask = cvCreateImage(cvSize(image->width+2,image->height+2),IPL_DEPTH_8U,1);
	cvNamedWindow(windowname,1);//创建窗口
	cvNamedWindow("mask",1);//创建Mask窗口
	cvSetMouseCallback(windowname,OnMouse);//设置回调函数
	//创建滑块条
	cvCreateTrackbar("lo_diff",windowname,&lo_diff,255,NULL);
	cvCreateTrackbar("up_diff",windowname,&up_diff,255,NULL);
	while(true)
	{
		cvShowImage(windowname,image);
		cvShowImage("mask",mask);
		if(cvWaitKey(10)>=0) break;
	}
	cvDestroyWindow(windowname);
	cvReleaseImage(&image);
	cvReleaseImage(&mask);
	return 0;
}
 


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值