OpenCv学习笔记(六):使用opencv画线、矩形、椭圆、多边形线、多边形体等

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

在MFC中我们经常会画线、矩形、椭圆、多边形等几何结构,opencv为我们提供了已经封装好的函数来实现该功能,而且简单方便。下面提供一下例子,是Opencv安装以后自带的小程序,学习完现在的程序你可以学会以下知识:(1)生成随机数,使用RNG类(2)画线(3)画矩形(4)画椭圆(5)画圆(6)输出文字、文本(7)如果不熟悉Opencv的还能学会point结构体的使用,Scalar结构使用。具体可以看下面的小例子。在vs2010下运行一下就会明白了!

#include "stdafx.h"

#include "opencv2/core/core.hpp"

#include "opencv2/highgui/highgui.hpp"

#include

using namespace cv;

void help()

{

printf("\nThis program demonstrates OpenCV drawing and text output functions.\n"

"Usage:\n"

" ./drawing\n");

}

static Scalar randomColor(RNG& rng)

{

int icolor = (unsigned)rng;

return Scalar(icolor&255, (icolor>>8)&255, (icolor>>16)&255);

}

int main()

{

help();

char wndname[] = "Drawing Demo";

const int NUMBER = 100;

const int DELAY = 5;

int lineType = CV_AA; // change it to 8 to see non-antialiased graphics

int i, width = 1000, height = 700;

int x1 = -width/2, x2 = width*3/2, y1 = -height/2, y2 = height*3/2;

RNG rng(0xFFFFFFFF);

Mat image = Mat::zeros(height, width, CV_8UC3);

imshow(wndname, image);

waitKey(DELAY);

for (i = 0; i < NUMBER; i++)

{

Point pt1, pt2;

pt1.x = rng.uniform(x1, x2);

pt1.y = rng.uniform(y1, y2);

pt2.x = rng.uniform(x1, x2);

pt2.y = rng.uniform(y1, y2);

line( image, pt1, pt2, randomColor(rng), rng.uniform(1,10), lineType );

imshow(wndname, image);

if(waitKey(DELAY) >= 0)

return 0;

}

for (i = 0; i < NUMBER; i++)

{

Point pt1, pt2;

pt1.x = rng.uniform(x1, x2);

pt1.y = rng.uniform(y1, y2);

pt2.x = rng.uniform(x1, x2);

pt2.y = rng.uniform(y1, y2);

int thickness = rng.uniform(-3, 10);

rectangle( image, pt1, pt2, randomColor(rng), MAX(thickness, -1), lineType ); imshow(wndname, image);

if(waitKey(DELAY) >= 0)

return 0;

}

for (i = 0; i < NUMBER; i++)

{

Point center;

center.x = rng.uniform(x1, x2);

center.y = rng.uniform(y1, y2);

Size axes;

axes.width = rng.uniform(0, 200);

axes.height = rng.uniform(0, 200);

double angle = rng.uniform(0, 180);

ellipse( image, center, axes, angle, angle - 100, angle + 200,

randomColor(rng), rng.uniform(-1,9), lineType );

imshow(wndname, image);

if(waitKey(DELAY) >= 0)

return 0;

}

for (i = 0; i< NUMBER; i++)

{

Point pt[2][3];

pt[0][0].x = rng.uniform(x1, x2);

pt[0][0].y = rng.uniform(y1, y2);

pt[0][1].x = rng.uniform(x1, x2);

pt[0][1].y = rng.uniform(y1, y2);

pt[0][2].x = rng.uniform(x1, x2);

pt[0][2].y = rng.uniform(y1, y2);

pt[1][0].x = rng.uniform(x1, x2);

pt[1][0].y = rng.uniform(y1, y2);

pt[1][1].x = rng.uniform(x1, x2);

pt[1][1].y = rng.uniform(y1, y2);

pt[1][2].x = rng.uniform(x1, x2);

pt[1][2].y = rng.uniform(y1, y2);

const Point* ppt[2] = {pt[0], pt[1]};

int npt[] = {3, 3};

polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1,10), lineType); imshow(wndname, image);

if(waitKey(DELAY) >= 0)

return 0;

}

for (i = 0; i< NUMBER; i++)

{

Point pt[2][3];

pt[0][0].x = rng.uniform(x1, x2);

pt[0][0].y = rng.uniform(y1, y2);

pt[0][1].x = rng.uniform(x1, x2);

pt[0][1].y = rng.uniform(y1, y2);

相关文档
最新文档