您的位置:首页 > 运维架构

OpenCv常用函数实例

2012-12-06 13:32 393 查看
/** 
* Real Time Eye Tracking and Blink Detection with OpenCV 
* 
* @author Nash <me@nashruddin.com> 
* @license GPL 
* @website http://nashruddin.com 
* 
* See the tutorial at 
* http://nashruddin.com/Real_Time_Eye_Tracking_and_Blink_Detection 
*/ 
#include <stdio.h>   
#include "cv.h"   
#include "highgui.h"   

#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cxcore.lib")

void GetColor();

int GetFace();  

void GetCup();
 
int main(int argc, char** argv)   
{   	
	//GetColor(img);
	//GetFace(img);  
	GetCup();
	cvWaitKey(0);
	system("pause");
	return 0;   
}   

void GetCup()
{
	cvNamedWindow("GetCup",CV_WINDOW_AUTOSIZE);
	IplImage *img1=cvLoadImage("img1.jpg");
	IplImage *img2=cvLoadImage("img2.jpg");

	IplImage *diff=cvCreateImage(cvSize(img1->width,img1->height),img1->depth,img1->nChannels);
	cvSub(img1,img2,diff);///背景差
	IplImage *gray=cvCreateImage(cvSize(img1->width,img1->height),8,1);
	cvCvtColor(diff,gray,CV_BGR2GRAY);//转化为8位灰度

	cvThreshold(gray,gray,32,255, CV_THRESH_BINARY);//二值化

	IplConvKernel*    kernel; 
	kernel = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_CROSS, NULL);   
	cvMorphologyEx(gray,gray,NULL,kernel,CV_MOP_OPEN,7);///开运算

	/////////////////////////////////////////查找最大面积轮廓
	CvMemStorage*    storage;
	CvSeq* comp=0;
	storage = cvCreateMemStorage(0);
	cvFindContours(gray,storage,&comp,sizeof(CvContour));
	double maxArea=0;
	CvRect maxRect;
	maxRect.x=0;
	maxRect.y=0;
	maxRect.width=0;
	maxRect.height=0;
	for(;comp != 0;)
	{
		double area=fabs(cvContourArea(comp));
		if(area>maxArea)

		{
			CvRect r=cvBoundingRect(comp,1);
			maxRect.x=r.x;
			maxRect.y=r.y;
			maxRect.width=r.width;
			maxRect.height=r.height;			
		}

		comp=comp->h_next;
	}
    ////画矩形
// 	CvPoint a,c;
// 	a.x=maxRect.x;  
// 	a.y=maxRect.y;
// 	c.x=maxRect.x+maxRect.width;
// 	c.y=maxRect.y+maxRect.height;
// 	cvRectangle(img2,a,c,CV_RGB(255,0,0),2,8);
	/////////////////////////////////////////////////面积最大区域 复制
    cvSetImageROI(img2,maxRect);
	IplImage *maxAreaImg;
	maxAreaImg=cvCreateImage(cvSize(maxRect.width,maxRect.height),8,3);
	cvCopy(img2,maxAreaImg);
	cvResetImageROI(img2);
	////////////////////////////////漫水法颜色填充
	CvPoint seedPoint;
	seedPoint.x=0;
	seedPoint.y=0;
	CvScalar newVal;
	newVal.val[0]=0;
	newVal.val[1]=0;
	newVal.val[2]=0;
	CvScalar loDiff = cvScalarAll(2);
	CvScalar upDiff = cvScalarAll(203);  

	cvNamedWindow("GetCup2",CV_WINDOW_AUTOSIZE);
	cvShowImage("GetCup2",img2);

	CvConnectedComp conComp;
	cvFloodFill(img2,seedPoint,newVal,loDiff,upDiff,&conComp);///漫水法填充,并返回区域轮廓

	CvPoint a,b;
	a.x=conComp.rect.x;
	a.y=conComp.rect.y;
	b.x=conComp.rect.x+conComp.rect.width;
	b.y=conComp.rect.y+conComp.rect.height;			
	cvRectangle(img2,a,b,CV_RGB(255,0,0),2,8);
	cvShowImage("GetCup",img2);
	///////////////////////////////////////
	

	
}

int GetFace()   
{  
	IplImage *img=cvLoadImage("face.jpg");

	cvNamedWindow("testF", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("testF2", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("testF3", CV_WINDOW_AUTOSIZE);

	IplImage *img2;
	img2=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
	img2=cvCloneImage(img);

	CvSeq* comp=0;
	CvMemStorage*    storage;
	storage = cvCreateMemStorage(0);  

	CvSeq* comp2=0;
	CvMemStorage*    storage2;
	storage2 = cvCreateMemStorage(0);  

	IplConvKernel*    kernel; 
	kernel = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_CROSS, NULL);   
	
	if (!storage)   
		printf("cannot allocate memory storage!");   
    IplImage* _diff;   
	IplImage* diff;
	diff=cvCreateImage(cvSize(img->width,img->height),img->depth,1);

	cvCvtColor(img,diff,CV_BGR2GRAY);//灰度图
    
    cvThreshold(diff,diff, 127, 255, CV_THRESH_BINARY);   //二值化
	cvShowImage("testF",diff);
	

	cvMorphologyEx(diff, diff, NULL, kernel, CV_MOP_OPEN, 7);//闭运算
	

	 _diff = cvCloneImage(diff); 
	int nface=cvFindContours(_diff, storage, &comp, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));   //查轮廓
	printf("%d\n",nface);
	for(int i=0;comp != 0;i=i+100)
	{
		CvRect r = cvBoundingRect(comp, 1); 
		double area=fabs(cvContourArea(comp,CV_WHOLE_SEQ));
		printf("%lf\n",area);
		printf("width=%d  height=%d\n",r.width,r.height);
		CvPoint a,c;
		a.x=r.x;  
		a.y=r.y;
		c.x=r.x+r.width;
		c.y=r.y+r.height;
		cvRectangle(img2,a,c,CV_RGB(255,0,0),2,8);

		cvShowImage("testF3",img2);
		//////////////////////////////

		cvSetImageROI(diff,r);
		cvFindContours(diff, storage2, &comp2, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));   //查轮廓

		for(;comp2 != 0;)
		{
			CvRect r2=cvBoundingRect(comp2,1);
			CvPoint a2,c2;
			a2.x=r2.x+r.x;  
			a2.y=r2.y+r.y;
			c2.x=r2.x+r2.width+r.x;
			c2.y=r2.y+r2.height+r.y;

			cvRectangle(img,a2,c2,CV_RGB(255,i,i/6),2,8);
			cvShowImage("testF2",img);
			comp2=comp2->h_next;
		}

		cvWaitKey(100);
		cvResetImageROI(diff);
		comp = comp->h_next; 
	}

	  
    return 0;   
}   

///提取颜色
void GetColor()
{

	IplImage *img=cvLoadImage("face.jpg");

	IplImage*   pImg_test = img;
	int r, g, b;
	
	int intS = 0;

	IplImage* newImg;
	newImg=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);

	cvNamedWindow("test",CV_WINDOW_AUTOSIZE);
	for(int x = 0; x < pImg_test->width; x++)
	{
		for(int y = 0; y < pImg_test->height; y++) 
		{
			r = CV_IMAGE_ELEM(pImg_test, uchar, y, x * 3 + 2);
			g = CV_IMAGE_ELEM(pImg_test, uchar, y, x * 3 + 1);
			b = CV_IMAGE_ELEM(pImg_test, uchar, y, x * 3);
			if (r >= 210) 
			{
				CvScalar val;
				val.val[0]=r;
				val.val[1]=g;
				val.val[2]=b;
				cvSet2D(newImg,y,x,val);
				
			}
		}
	}

	
cvShowImage("test",newImg);

printf("ok");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: