学习OpenCV范例(六)——基本绘图

这篇博客介绍了如何使用OpenCV进行基本的图形绘制,包括调用line函数画直线,ellipse函数画椭圆,circle函数画圆以及fillPoly函数画填充的多边形和rectangle函数画矩形。文章提供了代码实现、运行结果展示以及涉及到的OpenCV类和函数解析。
摘要由CSDN通过智能技术生成

基本绘图相对来说也是非常简单的,只是几个函数的调用而已,在这里OpenCV教程中已经讲得非常详细了,我这里只是贴出代码,运行结果,函数参数解析和函数功能。

1、绘图

  • 用OpenCV的函数 line 绘 直线
  • 用OpenCV的函数 ellipse 绘 椭圆
  • 用OpenCV的函数 rectangle 绘 矩形
  • 用OpenCV的函数 circle 绘 圆
  • 用OpenCV的函数 fillPoly 绘 填充的多边形

2、代码实现

#include "stdafx.h"


/**
 * @file Drawing_1.cpp
 * @brief Simple sample code
 */

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#define w 400

using namespace cv;

/// Function headers
void MyEllipse( Mat img, double angle );
void MyFilledCircle( Mat img, Point center );
void MyPolygon( Mat img );
void MyLine( Mat img, Point start, Point end );

/**
 * @function main
 * @brief Main function
 */
int main( void ){

	/// 窗口名字
	char atom_window[] = "Drawing 1: Atom";
	char rook_window[] = "Drawing 2: Rook";

	/// 创建空全黑像素的空图像
	Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
	Mat rook_image = Mat::zeros( w, w, CV_8UC3 );

	/// 1. 画一个简单的原子。

	/// 1.a. 创建椭圆
	MyEllipse( atom_image, 90 );
	MyEllipse( atom_image, 0 );
	MyEllipse( atom_image, 45 );
	MyEllipse( atom_image, -45 );

	/// 1.b. 创建圆
	MyFilledCircle( atom_image, Point( w/2.0, w/2.0) );

	/// 2. 画一个赌棍

	/// 2.a. 创建一个凸多边形
	MyPolygon( rook_image );

	/// 2.b. 创建矩形
	rectangle( rook_image,
		Point( 0, 7*w/8.0 ),
		Point( w, w),
		Scalar( 0, 255, 255 ),
		-1,
		8 );

	/// 2.c. 画几条直线
	MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
	MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
	MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
	MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );

  /// 3.显示图片,并且将窗口移动到指定的位置
  imshow( atom_window, atom_image );
  moveWindow( atom_window, 0, 200 );
  imshow( rook_window, rook_image );
  moveWindow( rook_window, w, 200 );

  waitKey( 0 );
  return(0);
}

/// Function Declaration

/**
 * @function MyEllipse
 * @brief Draw a fixed-size ellipse with different angles
 */
void MyEllipse( Mat img, double angle )
{
  int thickness = 2;
  int lineType = 8;

  ellipse( img,
       Point( w/2, w/2 ),
       Size( w/4, w/16 ),
       angle,
       0,
       360,
       Scalar( 255, 0, 0 ),
       thickness,
       lineType );
}

/**
 * @function MyFilledCircle
 * @brief Draw a fixed-size filled circle
 */
void MyFilledCircle( Mat img, Point center )
{
  int thickness = -1;
  int lineType = 8;

  circle( img,
      center,
      w/32,
      Scalar( 0, 0, 255 ),
      thickness,
      lineType );
}

/**
 * @function MyPolygon
 * @function Draw a simple concave polygon (rook)
 */
void MyPolygon( Mat img )
{
  int lineType = 8;

  /** 建立一些点*/
  Point rook_po
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值