The Core Functionality7(Basic Drawing)

Goals

In this tutorial you will learn how to:

  • Draw a line by using the OpenCV function line()(直线)
  • Draw an ellipse by using the OpenCV function ellipse()(椭圆)
  • Draw a rectangle by using the OpenCV function recctangel()(矩形)
  • Draw a circle by using the OpenCV function circle()(圆)
  • Draw a filled polygon by using the OpenCV function fillPoly()(填充多边形)

Point(点)

Point pt;
pt.x = 10;
pt.y = 8;

或者:Point pt=Point(10,8)

Scalar

  • 代表一个4元素的向量。标量类型广泛用于OpenCV传递像素值。
  • 在本教程中,我们将广泛使用它来表示BGR颜色值(3个参数)。如果不使用最后一个参数,则没有必要定义它。
  • 让我们来看一个例子,如果我们要求一个颜色参数,并且我们给:
    Scalar(a,b,c)
    我们将定义BGR颜色,例如:蓝色= a绿色= b红色= c

Code

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#define w 400
using namespace cv;
void MyEllipse( Mat img, double angle );
void MyFilledCircle( Mat img, Point center );
void MyPolygon( Mat img );
void MyLine( Mat img, Point start, Point end );
int main( void ){
  char atom_window[] = "Drawing 1: Atom"; //图像1
  char rook_window[] = "Drawing 2: Rook"; //图像2
  Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
  Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
  
  MyEllipse( atom_image, 90 );
  MyEllipse( atom_image, 0 );
  MyEllipse( atom_image, 45 );
  MyEllipse( atom_image, -45 );
  
  MyFilledCircle( atom_image, Point( w/2, w/2) );
  
  MyPolygon( rook_image );
  
  //图像,相对顶点1和2,颜色,填充,线型
  rectangle( rook_image,
         Point( 0, 7*w/8 ),
         Point( w, w),
         Scalar( 0, 255, 255 ),
         FILLED,
         LINE_8 );
  
  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 ) );
  
  imshow( atom_window, atom_image );
  moveWindow( atom_window, 0, 200 );
  imshow( rook_window, rook_image );
  moveWindow( rook_window, w, 200 );
  waitKey( 0 );
  return(0);
}
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 );
}
void MyFilledCircle( Mat img, Point center )
{
  //图像,中心,半径,颜色,FILLLED(-1),LINE_8(8连通线型)
  circle( img,
      center,
      w/32,
      Scalar( 0, 0, 255 ),
      FILLED,
      LINE_8 );
}
void MyPolygon( Mat img )
{
  int lineType = LINE_8;
  Point rook_points[1][20];
  rook_points[0][0]  = Point(    w/4,   7*w/8 );
  rook_points[0][1]  = Point(  3*w/4,   7*w/8 );
  rook_points[0][2]  = Point(  3*w/4,  13*w/16 );
  rook_points[0][3]  = Point( 11*w/16, 13*w/16 );
  rook_points[0][4]  = Point( 19*w/32,  3*w/8 );
  rook_points[0][5]  = Point(  3*w/4,   3*w/8 );
  rook_points[0][6]  = Point(  3*w/4,     w/8 );
  rook_points[0][7]  = Point( 26*w/40,    w/8 );
  rook_points[0][8]  = Point( 26*w/40,    w/4 );
  rook_points[0][9]  = Point( 22*w/40,    w/4 );
  rook_points[0][10] = Point( 22*w/40,    w/8 );
  rook_points[0][11] = Point( 18*w/40,    w/8 );
  rook_points[0][12] = Point( 18*w/40,    w/4 );
  rook_points[0][13] = Point( 14*w/40,    w/4 );
  rook_points[0][14] = Point( 14*w/40,    w/8 );
  rook_points[0][15] = Point(    w/4,     w/8 );
  rook_points[0][16] = Point(    w/4,   3*w/8 );
  rook_points[0][17] = Point( 13*w/32,  3*w/8 );
  rook_points[0][18] = Point(  5*w/16, 13*w/16 );
  rook_points[0][19] = Point(    w/4,  13*w/16 );
  const Point* ppt[1] = { rook_points[0] }; //说明数组的每个元素均为指针,而这个指针为顶点集合的首元素开始地址
  int npt[] = { 20 }; //指针
  //图像,顶点集(指针的指针),边(指针的指针),数量,颜色,线型
  fillPoly( img,
        ppt,
        npt,
        1,
        Scalar( 255, 255, 255 ),
        lineType );
}
void MyLine( Mat img, Point start, Point end )
{
  int thickness = 2;
  int lineType = LINE_8;
  //图像,开始,结束,颜色,线宽,线型
  line( img,
    start,
    end,
    Scalar( 0, 0, 0 ),
    thickness,
    lineType );

}

CMakeLists.txt文件如下:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "-std=c++11")
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

install(TARGETS DisplayImage RUNTIME DESTINATION bin)

运行结果如下:



猜你喜欢

转载自blog.csdn.net/qq_27806947/article/details/80182236