OPenCV绘图函数

                                                         来源于《基于OpenCV的计算机视觉技术实现》第五章

  OPenCV提供了绘制线段、矩形、圆形、椭圆(椭圆弧)、多边形等基本曲线与形状的函数。

具体的函数说明如表5一l所示。



下面的程序 实现了绘制一个笑脸 。其中绘制笑脸的圆形轮廓,眼睛,嘴巴分别调用了 cvCircle,cvEllipse,cvLine等函数。

//程序5-1 曲线与形状绘制 即绘制了一个笑脸
//
 
#include "stdafx.h"
#include <highgui.h>
#include<cv.h>
#include<iostream>
#define thickness  2						     //线条粗细
#define line_type  CV_AA				// CV_AA表示抗锯齿类型直线
void main()
{
	//CV_RGB( 255., 158., 97. )三个分量是按R,G,B顺序排列的
	CvScalar color=CV_RGB( 255., 158., 97. );			
	CvPoint pt1_Rect;					//确定外围矩形画框的两个点(对角线上两个点)
	CvPoint pt2_Rect;
	CvPoint center;					//图像中心既是笑脸的中心	
	int radius=0;						//笑脸的半径
	//---------------------眼睛有关的变量---------------------------------
	CvPoint center_l_eye;				//左眼的中心
	CvPoint center_r_eye;				//右眼的中心	
	CvSize  axes_eye;					//左右眼的大小
	double angle_l_eye=15;				//左眼的偏转角:正的表示逆时针转
	double angle_r_eye=-15;			//右眼的偏转角:负数表示顺时针转
	
	double start_angle_eye=0.;
	double end_angle_eye=360.;
	//---------------------嘴巴有关的变量-----------------------------------
	CvPoint pt1_l_mouth;				//笑嘴的嘴角的点
	CvPoint pt2_l_mouth;
	CvPoint pt1_r_mouth;
	CvPoint pt2_r_mouth;
	CvSize  axes_mouth;				    //嘴的大小			
	double angle_mouth=0.;				//嘴的偏转角
	double start_angle_mouth=0.;		//画嘴的圆弧的起始角
	double end_angle_mouth=360.;		//画嘴的圆弧的终止角

	//--------绘制笑脸的目的图像img的初始化-------
	IplImage* img = cvCreateImage( cvSize(600,600), IPL_DEPTH_8U, 3 );
	cvNamedWindow( "image", CV_WINDOW_AUTOSIZE );

	//----------------------------外围矩形画框---------------------------
	pt1_Rect.x=0;
	pt1_Rect.y=0;
	pt2_Rect.x=600;
	pt2_Rect.y=600;
	color=CV_RGB( 97., 158., 255. );
	//------------------- cvRectangle画矩形-------------
	cvRectangle(img, pt1_Rect, pt2_Rect, color, CV_FILLED, line_type, 0);
	
//----------------------------笑脸的轮廓---------------------------

	color= CV_RGB ( 255., 158., 97. );	//颜色为黄色
	center.x=300;
	center.y=300;
	radius=200;						//笑脸半径
	//------------------- cvCircle画圆形即为笑脸的轮廓-------------
	cvCircle(img,center, radius, color, CV_FILLED, line_type, 0);

//----------------------------画眼睛---------------------------
	color= CV_RGB ( 156., 25., 255. );	//眼睛颜色为紫色
	center_l_eye.x=240;
	center_l_eye.y=200;
	center_r_eye.x=360;
	center_r_eye.y=200;
	axes_eye.width=16;
	axes_eye.height=30;
	angle_l_eye=10;
	angle_r_eye=-5;
	start_angle_eye=0.;				//绘制整个椭圆时起始角为0°,终止角为360°
	end_angle_eye=360.;
	//----------左眼----------- cvEllipse画椭圆------------
cvEllipse(img, center_l_eye, axes_eye, angle_l_eye, start_angle_eye, end_angle_eye, color, CV_FILLED, line_type, 0);
	//----------右眼----------- cvEllipse画椭圆---------------
cvEllipse(img, center_r_eye, axes_eye, angle_r_eye, start_angle_eye, end_angle_eye, color, CV_FILLED, line_type, 0); 
//----------------------------画嘴巴---------------------------
	color= CV_RGB ( 255., 255., 0. );	//嘴巴颜色为黄色

	pt1_l_mouth.y=300;
	pt1_l_mouth.x=150;
	pt2_l_mouth.y=270;
	pt2_l_mouth.x=180;

	pt1_r_mouth.y=270;
	pt1_r_mouth.x=400;
	pt2_r_mouth.y=300;
	pt2_r_mouth.x=430;
	axes_mouth.width=130;
	axes_mouth.height=100;
	start_angle_mouth=24;//150
	end_angle_mouth=135;//347
	angle_mouth=10;
	//----左边的嘴角的线段----
	cvLine(img, pt1_l_mouth, pt2_l_mouth, color, 4, line_type, 0);
	//---右边的嘴角的线段---
	cvLine(img, pt1_r_mouth, pt2_r_mouth, color, 4, line_type, 0);
	//---嘴巴的圆弧----------
	cvEllipse(img, center, axes_mouth, angle_mouth, start_angle_mouth, end_angle_mouth, color, 4, line_type, 0);

	cvShowImage( "image", img );
	cvWaitKey(0);
	cvReleaseImage(&img);
}

 


效果


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值