您的位置:首页 > 理论基础

基于opencv的计算机视觉技术实现 第五章节第一个程序 改错

2017-02-13 18:25 741 查看
在5.1中,程序是要调用cvCircle,cvEllipse,cvLine等函数实现下面的笑脸:



但是源程序给出的结果如下:



代码如下:

#include <highgui.h>

#include<cv.h>

#include<iostream.h>

#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=150.;
end_angle_mouth=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);

}

分析一下,错误出现在这里:

start_angle_mouth=150.;
end_angle_mouth=347.;
angle_mouth=10.;


绘制出来的圆弧是反方向的,因此需要将各个值的大小变为负向。

即:

start_angle_mouth=-150.;
end_angle_mouth=-347.;
angle_mouth=-10.;


才是正确的结果。

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