opencv(5)---绘图函数

来源:互联网 发布:微信 for windows 编辑:程序博客网 时间:2024/06/05 02:09

常用的绘图函数
绘图函数

一 line

函数定义

/** @brief Draws a line segment connecting two points.The function line draws the line segment between pt1 and pt2 points in the image. The line isclipped by the image boundaries. For non-antialiased lines with integer coordinates, the 8-connectedor 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding endings. Antialiasedlines are drawn using Gaussian filtering.@param img Image.@param pt1 First point of the line segment.@param pt2 Second point of the line segment.@param color Line color.@param thickness Line thickness.@param lineType Type of the line, see cv::LineTypes.@param shift Number of fractional bits in the point coordinates. */CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,                     int thickness = 1, int lineType = LINE_8, int shift = 0);

二 circle

函数定义

/** @brief Draws a circle.The function circle draws a simple or filled circle with a given center and radius.@param img Image where the circle is drawn.@param center Center of the circle.@param radius Radius of the circle.@param color Circle color.@param thickness Thickness of the circle outline, if positive. Negative thickness means that afilled circle is to be drawn.@param lineType Type of the circle boundary. See the line description.@param shift Number of fractional bits in the coordinates of the center and in the radius value. */CV_EXPORTS_W void circle(InputOutputArray img, Point center, int radius,                       const Scalar& color, int thickness = 1,                       int lineType = LINE_8, int shift = 0);

三 rectangle

1. 函数定义

方式一

/** @brief Draws a simple, thick, or filled up-right rectangle.The function rectangle draws a rectangle outline or a filled rectangle whose two opposite cornersare pt1 and pt2.@param img Image.@param pt1 Vertex of the rectangle.@param pt2 Vertex of the rectangle opposite to pt1 .@param color Rectangle color or brightness (grayscale image).@param thickness Thickness of lines that make up the rectangle. Negative values, like CV_FILLED ,mean that the function has to draw a filled rectangle.@param lineType Type of the line. See the line description.@param shift Number of fractional bits in the point coordinates. */CV_EXPORTS_W void rectangle(InputOutputArray img, Point pt1, Point pt2,                          const Scalar& color, int thickness = 1,                          int lineType = LINE_8, int shift = 0);

方式二

/** @overloaduse `rec` parameter as alternative specification of the drawn rectangle: `r.tl() andr.br()-Point(1,1)` are opposite corners*/CV_EXPORTS void rectangle(CV_IN_OUT Mat& img, Rect rec,                          const Scalar& color, int thickness = 1,                          int lineType = LINE_8, int shift = 0);

其中,Rect类型为左上坐标,长,宽

四 ellipse

函数定义

函数以及参数说明

/** @brief Draws a simple or thick elliptic arc or fills an ellipse sector.The functions ellipse with less parameters draw an ellipse outline, a filled ellipse, an ellipticarc, or a filled ellipse sector. A piecewise-linear curve is used to approximate the elliptic arcboundary. If you need more control of the ellipse rendering, you can retrieve the curve usingellipse2Poly and then render it with polylines or fill it with fillPoly . If you use the firstvariant of the function and want to draw the whole ellipse, not an arc, pass startAngle=0 andendAngle=360 . The figure below explains the meaning of the parameters.@param img Image.@param center Center of the ellipse.@param axes Half of the size of the ellipse main axes.@param angle Ellipse rotation angle in degrees.@param startAngle Starting angle of the elliptic arc in degrees.@param endAngle Ending angle of the elliptic arc in degrees.@param color Ellipse color.@param thickness Thickness of the ellipse arc outline, if positive. Otherwise, this indicates thata filled ellipse sector is to be drawn.@param lineType Type of the ellipse boundary. See the line description.@param shift Number of fractional bits in the coordinates of the center and values of axes. */

构造方式一

CV_EXPORTS_W void ellipse(InputOutputArray img, Point center, Size axes,                        double angle, double startAngle, double endAngle,                        const Scalar& color, int thickness = 1,                        int lineType = LINE_8, int shift = 0);

构造方式二

/** @overload@param img Image.@param box Alternative ellipse representation via RotatedRect. This means that the function drawsan ellipse inscribed in the rotated rectangle.@param color Ellipse color.@param thickness Thickness of the ellipse arc outline, if positive. Otherwise, this indicates thata filled ellipse sector is to be drawn.@param lineType Type of the ellipse boundary. See the line description.*/CV_EXPORTS_W void ellipse(InputOutputArray img, const RotatedRect& box, const Scalar& color,                        int thickness = 1, int lineType = LINE_8);

五 绘制多边形

1.有填充的多边形
fillPloy

/** @brief Fills the area bounded by one or more polygons.The function fillPoly fills an area bounded by several polygonal contours. The function can fillcomplex areas, for example, areas with holes, contours with self-intersections (some of theirparts), and so forth.@param img Image.@param pts Array of polygons where each polygon is represented as an array of points.@param color Polygon color.@param lineType Type of the polygon boundaries. See the line description.@param shift Number of fractional bits in the vertex coordinates.@param offset Optional offset of all points of the contours. */CV_EXPORTS_W void fillPoly(InputOutputArray img, InputArrayOfArrays pts,                           const Scalar& color, int lineType = LINE_8, int shift = 0,                           Point offset = Point() );

2.无填充的多边形
polylines

/** @overload */CV_EXPORTS void polylines(Mat& img, const Point* const* pts, const int* npts,                          int ncontours, bool isClosed, const Scalar& color,                          int thickness = 1, int lineType = LINE_8, int shift = 0 );

六 putText

函数定义

/** @brief Draws a text string.The function putText renders the specified text string in the image. Symbols that cannot be renderedusing the specified font are replaced by question marks. See getTextSize for a text rendering codeexample.@param img Image.@param text Text string to be drawn.@param org Bottom-left corner of the text string in the image.@param fontFace Font type, see cv::HersheyFonts.@param fontScale Font scale factor that is multiplied by the font-specific base size.@param color Text color.@param thickness Thickness of the lines used to draw a text.@param lineType Line type. See the line for details.@param bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise,it is at the top-left corner. */CV_EXPORTS_W void putText( InputOutputArray img, const String& text, Point org,                         int fontFace, double fontScale, Scalar color,                         int thickness = 1, int lineType = LINE_8,                         bool bottomLeftOrigin = false );

七 代码

mainwindow.cpp

#include "mainwindow.h"#include <opencv2/opencv.hpp>#include <iostream>using namespace cv;using namespace std;MainWindow::MainWindow(QWidget *parent)    : QMainWindow(parent){    Mat Img(300,300,CV_8UC3,Scalar(0,0,0));    //直线    //   line(Img,Point(10,10),Point(200,200),Scalar(0,255,255),2,8);    //圆    //circle(Img,Point(150,150),100,Scalar(255,255,0),-1,8);    //矩形方式一    // rectangle(Img,Point(10,10),Point(200,200),Scalar(255.255,0),4,5);    //矩形方式二    // rectangle(Img,Rect(10,10,190,190),Scalar(255,255,0),4,2,0);    //椭圆方式一    //ellipse(Img,Point(150,150),Size(100,50),45,0,360,Scalar(255,0,0),-1,8);    //椭圆方式2    //ellipse(Img,RotatedRect(Point(150,150),Size(100,50),45),Scalar(255,255,0),-1,8);    Point ppt[]={Point(120,50),Point(180,50),Point(210,100),Point(180,150),Point(120,150),Point(90,100)};    const Point *pts[]={ppt};    int npt[]={6};    //fillPoly函数的使用方式    //fillPoly(Img,pts,npt,1,Scalar(255,255,0),8);    //polyLines函数    //polylines(Img,pts,npt,1,true,Scalar(0,154,209),4,8);   //putText使用方式    putText(Img,"China YE YE ",Point(50,50),CV_FONT_HERSHEY_COMPLEX,2,Scalar(255,255,0),1,8);    imshow("img",Img);    waitKey(0);}
0 0