如何在图像上显示多样且变化的图形或文字

现在大四,却正走在研究僧的道路上,有点可悲,其中因果就像置换一样,别人用3年努力学习,现在就轻松无比,只等待着毕业、工作;而我用3年来游戏,换的现在不得不为自己考虑未来,得失只在一念之间,是时候该努力一把了。

学习opencv已有2个星期,只能说是初窥门径,同时算是了解了为什么要学线性代数了。。。

先说简单的绘图吧

(1)线段

cvLine()是绘图函数中最简单的

void cvLine(

CvArr * array,//一般为一个图像类型的指针IplImage *;

CvPoint pt1,//线段起点坐标

CvPoint pt2,//线段终点坐标

CvScalar color,//线段颜色

int thickness=1,//线的粗细(像素)

int connectivity =8//设为反走样模式,默认值为“8连通”,这种是较为平滑不会走样的线性

);

(2)矩形

cvRectangle()用于画矩形。

void cvRectangle(

CvArr * array,

CvPoint pt1,

CvPoint pt2,//矩形对顶角的两点坐标

CvScalar color,

int thickness=1

);

(3)圆和椭圆

绘制圆

void cvCircle(

CvArr * array,

CvPoint center, //圆心坐标

int radius,//圆的半径

CvScalar color,

int thickness=1,

int connectivity =8,

);

绘制椭圆

void cvEllipse(

CvArr * img,

CvPoint center,

CvSize axes,  //仅包含宽度和高度的简单结构,height和width参数分别代表椭圆的长短半轴长

double angle,//指偏离主轴的角度,从X轴算起,逆时针方向为正

double start_angle,//弧线开始的角度

double end_angle,//弧线结束的角度

CvScalar color,

int thickness=1,

int line_type=8);

使用外接矩形是描述椭圆绘制的另一种方法

void  cvEllipseBox(
CvArr * img,

CvBox2D box,

CvScalar color,

int thickness=1,

int line_type=8,

int shift=0);

这里用到OpenCv的另一个结构CvBox2D:

typedef  struct

{CvPoint2D32f center;

CvSize2D32f size;

float  angle;//倾斜角度

}CvBox2D;

CvPoint2D32f 是CvPoint的浮点形式。

(4)多边形

void cvPolyLine(

CvArr * img,

CvPoint ** pts,

int * npts,

int contours,

int is_closed,

CvScalar color,

int thickness=1,

int line_type=8);

(5)文字和文体

OpenCv有一个主要的函数,叫cvPutText()。这个函数可以在图像上输出一些文本。参数text所指向的文本将打印到图像上。

void cvPutText(

CvArr * img,

const char * text,

CvPoint origin,

const CvFont * font,

CvScalar color);

获取CvFont * 指针的方式就是调用函数cvInitFont()。该函数采用一组参数配置一些用于屏幕输出的基本个特定字体。

为了建立一个可以传值给cvPutText()的CvFont,首先必须声明一个CvFont变量,然后把它传递给cvInitFont()。

void cvInitFont(

CvFont *font,

int font_face,//可选字体

double hscale,// 字体宽度,

double vscale,//字体高度

double shear =0,//字体斜度

int thickness=1,

int line_type=8

实现在图像上显示多样且变化的图形或文字

#include <cv.h>
#include<highgui.h>
#include<stdlib.h>
#include<stdio.h>
#define NUMBER 100
#define DELAY 5
char * windowname="drawing Demo";
CvScalar random_color(CvRNG * rng)
{
int color=cvRandInt(rng);
return CV_RGB(color&255,(color>>8)&255,(color>>16)&255);
}
int main()
{
int line_type=CV_AA;
int i;
CvPoint pt1,pt2;
CvPoint ptt[6];
CvPoint *pt[2];
int arr[2];
CvSize sz;
double angle;
CvFont font;
CvRNG rng;
int width=1000;
int height=700;
int width3=width*3;
int height3=height*3;
IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 );
cvNamedWindow(windowname,1);
cvZero(image);
cvShowImage(windowname,image);
rng=cvRNG((unsigned)-1);
pt[0]=&(ptt[0]);
pt[1]=&(ptt[3]);
arr[0]=3;
arr[1]=3;
for(i=0;i<NUMBER;i++)
{
pt1.x=cvRandInt(&rng)%width3-width;
pt1.y =cvRandInt(&rng)%height3-height;
                pt2.x =cvRandInt(&rng)%width3-width;
pt2.y =cvRandInt(&rng)%height3-height;
   cvLine( image, pt1, pt2, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
cvShowImage(windowname,image);
cvWaitKey(DELAY);
}
cvWaitKey(0);
cvZero(image);
for(i=0;i<NUMBER;i++)
{
pt1.x=cvRandInt(&rng)%width3-width;
pt1.y =cvRandInt(&rng)%height3-height;
                pt2.x =cvRandInt(&rng)%width3-width;
pt2.y =cvRandInt(&rng)%height3-height;
   cvRectangle( image, pt1, pt2, random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
cvShowImage(windowname,image);
cvWaitKey(DELAY);
}
cvWaitKey(0);
cvZero(image);
for(i=0;i<NUMBER;i++)
{
pt1.x=cvRandInt(&rng) % width3 - width;
pt1.y =cvRandInt(&rng)%height3-height;
sz.width =cvRandInt(&rng)%200;
sz.height =cvRandInt(&rng)%200;
angle=(cvRandInt(&rng)%1000)*0.180;
cvEllipse( image, pt1, sz, angle, angle - 100, angle + 200,random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
cvShowImage(windowname,image);
cvWaitKey(DELAY);
}
cvWaitKey(0);
cvZero(image);
for(i=0;i<NUMBER;i++)
{
pt[0][0].x =cvRandInt(&rng) % width3 - width;
pt[0][0].y =cvRandInt(&rng)%height3-height;
pt[0][1].x =cvRandInt(&rng) % width3 - width;
pt[0][1].y =cvRandInt(&rng)%height3-height;
pt[0][2].x =cvRandInt(&rng) % width3 - width;
pt[0][2].y =cvRandInt(&rng)%height3-height;
pt[1][0].x =cvRandInt(&rng) % width3 - width;
pt[1][0].y =cvRandInt(&rng)%height3-height;
pt[1][1].x =cvRandInt(&rng) % width3 - width;
pt[1][1].y =cvRandInt(&rng)%height3-height;
pt[1][2].x =cvRandInt(&rng) % width3 - width;
pt[1][2].y =cvRandInt(&rng)%height3-height;
cvPolyLine( image, pt, arr, 2, 1, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
cvShowImage(windowname,image);
cvWaitKey(DELAY);


}
cvWaitKey(0);
cvZero(image);
for(i=0;i<NUMBER;i++)
{
pt[0][0].x =cvRandInt(&rng) % width3 - width;
pt[0][0].y =cvRandInt(&rng)%height3-height;
pt[0][1].x =cvRandInt(&rng) % width3 - width;
pt[0][1].y =cvRandInt(&rng)%height3-height;
pt[0][2].x =cvRandInt(&rng) % width3 - width;
pt[0][2].y =cvRandInt(&rng)%height3-height;
pt[1][0].x =cvRandInt(&rng) % width3 - width;
pt[1][0].y =cvRandInt(&rng)%height3-height;
pt[1][1].x =cvRandInt(&rng) % width3 - width;
pt[1][1].y =cvRandInt(&rng)%height3-height;
pt[1][2].x =cvRandInt(&rng) % width3 - width;
pt[1][2].y =cvRandInt(&rng)%height3-height;
cvFillPoly( image, pt, arr, 2, random_color(&rng), line_type, 0 );
cvShowImage(windowname,image);
cvWaitKey(DELAY);


}
cvWaitKey(0);
cvZero(image);
for(i=0;i<NUMBER;i++)
{
pt1.x=cvRandInt(&rng) % width3 - width;
pt1.y =cvRandInt(&rng)%height3-height;
cvCircle(image,pt1,cvRandInt(&rng)%300,random_color(&rng),cvRandInt(&rng)%10,line_type,0);
cvShowImage(windowname,image);
cvWaitKey(DELAY);
}
cvWaitKey(0);
cvZero(image);
for(i=0;i<NUMBER;i++)
{
pt1.x=cvRandInt(&rng) % width3 - width;
pt1.y =cvRandInt(&rng)%height3-height;
cvInitFont(&font,cvRandInt(&rng) % 8,(cvRandInt(&rng)%100)*0.05+0.1,(cvRandInt(&rng)%100)*0.05+0.1,(cvRandInt(&rng)%5)*0.1,cvRandInt(&rng)%10,line_type);
cvPutText(image,"CSDN_gaohuiguang",pt1,&font,random_color(&rng));
cvShowImage(windowname,image);
cvWaitKey(DELAY);
}
cvWaitKey(0);
cvReleaseImage(&image);
cvDestroyWindow(windowname);
return 0;
}

截图








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值