💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
# 8. 绘图函数 绘图函数顾名思义就是在图像上进行绘画,我们经常需要在图像中进行一些标画来标注有效信息 ## 常见函数 ### 线标画函数 ```C++ void cv::line ( InputOutputArray img, Point pt1, Point pt2, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0 ) /** @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 is clipped by the image boundaries. For non-antialiased lines with integer coordinates, the 8-connected or 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding endings. Antialiased lines are drawn using Gaussian filtering. 功能线在图像中的pt1和pt2点之间绘制线段。线由图像边界剪裁。对于具有整数坐标的非抗锯齿线,使用8-连通或4-连通Bresenham算法。粗线是用圆形端点绘制的。使用高斯滤波绘制抗锯齿线。 @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 #LineTypes. @param shift Number of fractional bits in the point coordinates. */ ``` ### 矩形绘制 ```C++ void cv::rectangle ( InputOutputArray img, Point pt1, Point pt2, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0 ) /** @brief Draws a simple, thick, or filled up-right rectangle. 简介 绘制简单的、粗的或填充的右矩形。 函数cv::rectangle绘制一个矩形轮廓或一个填充矩形,其两个相对的角是pt1和pt2。 The function cv::rectangle draws a rectangle outline or a filled rectangle whose two opposite corners are 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 #FILLED, mean that the function has to draw a filled rectangle. @param lineType Type of the line. See #LineTypes @param shift Number of fractional bits in the point coordinates. */ void cv::rectangle ( InputOutputArray img, Rect rec, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0 ) /** @overload 重载 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. use rec parameter as alternative specification of the drawn rectangle: r.tl() and r.br()-Point(1,1) are opposite corners 使用rec参数作为绘制矩形的替代规范:r.tl()和r.br()-(1,1)是对角点 */ ``` > 矩形绘制函数只能绘制最简单的矩形,而不能绘制旋转矩形,旋转矩形的绘制方法如下: > ```C++ > #include <opencv2/opencv.hpp> > typedef Armor cv::RotatedRect > // Armor装甲板类是有旋转矩形类似性质的自定义类,便于讲解,只做最基础定义 > // SurfaceVertices()与boxPoints()基本等价 > > int main(){ > Armor armor; > cv::Mat output; > auto vertices = armor.SurfaceVertices(); //旋转矩形的四个点的向量 > auto num_vertices = vertices.size(); //四个点 > for (std::size_t i = 0; i < num_vertices; ++i) { //遍历连线 > cv::line(output, vertices[i], vertices[(i + 1) % num_vertices], cv::Scalar(0, 255, 0)); > } > cv::imshow("result", output); > cv::waitKey(0); > return 0; > } > ``` ### 圆形绘制 ```C++ void cv::circle ( InputOutputArray img, Point center, int radius, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0 ) /** @brief Draws a circle. 简介 画一个圆 The function cv::circle draws a simple or filled circle with a given center and radius. 函数cv::circle绘制具有给定圆心和半径的简单圆或填充圆。 @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 values, like #FILLED, mean that a filled circle is to be drawn. @param lineType Type of the circle boundary. See #LineTypes @param shift Number of fractional bits in the coordinates of the center and in the radius value. */ ``` ### 标志绘制 ```C++ void cv::drawMarker ( InputOutputArray img, Point position, const Scalar & color, int markerType = MARKER_CROSS, int markerSize = 20, int thickness = 1, int line_type = 8 ) /** @brief Draws a marker on a predefined position in an image. 简介 在图像中的预定义位置绘制标记。 The function cv::drawMarker draws a marker on a given position in the image. For the moment several marker types are supported, see #MarkerTypes for more information. 函数cv::drawMarker在图像中的给定位置绘制标记。目前支持多种标记类型,有关详细信息,请参见#标记类型。 @param img Image. @param position The point where the crosshair is positioned. @param color Line color. @param markerType The specific type of marker you want to use, see #MarkerTypes @param thickness Line thickness. @param line_type Type of the line, See #LineTypes @param markerSize The length of the marker axis [default = 20 pixels] */ ``` | MarkerTypes | Description | | --------------------- | ----------------------------------------------------------- | | MARKER_CROSS | A crosshair marker shape. | | MARKER_TILTED_CROSS | A 45 degree tilted crosshair marker shape. | | MARKER_STAR | A star marker shape, combination of cross and tilted cross. | | MARKER_DIAMOND | A diamond marker shape. | | MARKER_SQUARE | A square marker shape. | | MARKER_TRIANGLE_UP | An upwards pointing triangle marker shape. | | MARKER_TRIAN../image/ | :-: ![marker](https://raw.githubusercontent.com/C12H22O13/ai_note/master/image/marker.png) ### 轮廓绘制 ```C++ void cv::drawContours ( InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar & color, int thickness = 1, int lineType = LINE_8, InputArray hierarchy = noArray(), int maxLevel = INT_MAX, Point offset = Point() ) /** @brief Draws contours outlines or filled contours. 简介 绘制轮廓或填充轮廓。 The function draws contour outlines in the image if thickness >= 0 or fills the area bounded by the contours if thickness < 0 . The example below shows how to retrieve connected components from the binary image and label them: 如果厚度>=0,该函数在图像中绘制轮廓轮廓;如果厚度<0,则填充轮廓所限定的区域。下面的示例显示了如何从二进制图像检索连接的组件并为其添加标签: */ #include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" using namespace cv; using namespace std; int main( int argc, char** argv ) { Mat src; // the first command-line parameter must be a filename of the binary // (black-n-white) image if( argc != 2 || !(src=imread(argv[1], 0)).data) return -1; Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3); src = src > 1; namedWindow( "Source", 1 ); imshow( "Source", src ); vector<vector<Pointcontours>>; vector<Vec4i> hierarchy; findContours( src, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE ); // iterate through all the top-level contours, // draw each connected component with its own random color int idx = 0; for( ; idx >= 0; idx = hierarchy[idx][0] ) { Scalar color( rand()&255, rand()&255, rand()&255 ); drawContours( dst, contours, idx, color, FILLED, 8, hierarchy ); } namedWindow( "Components", 1 ); imshow( "Components", dst ); waitKey(0); } /* @param image Destination image. @param contours All the input contours. Each contour is stored as a point vector. @param contourIdx Parameter indicating a contour to draw. If it is negative, all the contours are drawn. @param color Color of the contours. @param thickness Thickness of lines the contours are drawn with. If it is negative (for example, thickness=#FILLED ), the contour interiors are drawn. @param lineType Line connectivity. See #LineTypes @param hierarchy Optional information about hierarchy. It is only needed if you want to draw only some of the contours (see maxLevel ). @param maxLevel Maximal level for drawn contours. If it is 0, only the specified contour is drawn. If it is 1, the function draws the contour(s) and all the nested contours. If it is 2, the function draws the contours, all the nested contours, all the nested-to-nested contours, and so on. This parameter is only taken into account when there is hierarchy available. @param offset Optional contour shift parameter. Shift all the drawn contours by the specified offset=(dx,dy) . @note When thickness=#FILLED, the function is designed to handle connected components with holes correctly even when no hierarchy date is provided. This is done by analyzing all the outlines together using even-odd rule. This may give incorrect results if you have a joint collection of separately retrieved contours. In order to solve this problem, you need to call #drawContours separately for each sub-group of contours, or iterate over the collection using contourIdx parameter. 当厚度=#填充时,该功能设计为即使未提供层次结构日期,也能正确处理带孔的连接组件。这是通过使用奇偶规则一起分析所有轮廓来完成的。如果有单独检索的等高线的联合集合,则可能会给出错误的结果。为了解决这个问题,您需要为每个等高线子组分别调用#drawContours,或者使用contourIdx参数迭代集合。 */ ``` ### 绘制文字 ```C++ void cv::putText ( InputOutputArray img, const String & text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1, int lineType = LINE_8, bool bottomLeftOrigin = false ) /** @brief Draws a text string. 简介 绘制文本字符串。 The function cv::putText renders the specified text string in the image. Symbols that cannot be rendered using the specified font are replaced by question marks. See #getTextSize for a text rendering code example. 函数cv::putText在图像中呈现指定的文本字符串。无法使用指定字体呈现的符号将替换为问号。有关文本呈现代码示例,请参见#getTextSize。 @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 #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 #LineTypes @param bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner. */ ``` 绘图函数还有椭圆绘图、箭头绘图、多边形绘图等,一般不使用故不在赘述