mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
635 lines
26 KiB
635 lines
26 KiB
14 years ago
|
Drawing Functions
|
||
|
=================
|
||
|
|
||
|
.. highlight:: cpp
|
||
|
|
||
|
Drawing functions work with matrices/images of arbitrary depth.
|
||
|
The boundaries of the shapes can be rendered with antialiasing (implemented only for 8-bit images for now).
|
||
14 years ago
|
All the functions include the parameter ``color`` that uses an RGB value (that may be constructed
|
||
12 years ago
|
with the ``Scalar`` constructor
|
||
14 years ago
|
) for color
|
||
14 years ago
|
images and brightness for grayscale images. For color images, the channel ordering
|
||
|
is normally *Blue, Green, Red*.
|
||
14 years ago
|
This is what :ocv:func:`imshow`, :ocv:func:`imread`, and :ocv:func:`imwrite` expect.
|
||
14 years ago
|
So, if you form a color using the
|
||
14 years ago
|
``Scalar`` constructor, it should look like:
|
||
14 years ago
|
|
||
|
.. math::
|
||
|
|
||
14 years ago
|
\texttt{Scalar} (blue \_ component, green \_ component, red \_ component[, alpha \_ component])
|
||
14 years ago
|
|
||
14 years ago
|
If you are using your own image rendering and I/O functions, you can use any channel ordering. The drawing functions process each channel independently and do not depend on the channel order or even on the used color space. The whole image can be converted from BGR to RGB or to a different color space using
|
||
14 years ago
|
:ocv:func:`cvtColor` .
|
||
14 years ago
|
|
||
14 years ago
|
If a drawn figure is partially or completely outside the image, the drawing functions clip it. Also, many drawing functions can handle pixel coordinates specified with sub-pixel accuracy. This means that the coordinates can be passed as fixed-point numbers encoded as integers. The number of fractional bits is specified by the ``shift`` parameter and the real point coordinates are calculated as
|
||
|
:math:`\texttt{Point}(x,y)\rightarrow\texttt{Point2f}(x*2^{-shift},y*2^{-shift})` . This feature is especially effective when rendering antialiased shapes.
|
||
14 years ago
|
|
||
14 years ago
|
.. note:: The functions do not support alpha-transparency when the target image is 4-channel. In this case, the ``color[3]`` is simply copied to the repainted pixels. Thus, if you want to paint semi-transparent shapes, you can paint them in a separate buffer and then blend it with the main image.
|
||
14 years ago
|
|
||
12 years ago
|
.. note::
|
||
12 years ago
|
|
||
12 years ago
|
* An example on using variate drawing functions like line, rectangle, ... can be found at opencv_source_code/samples/cpp/drawing.cpp
|
||
12 years ago
|
|
||
14 years ago
|
circle
|
||
14 years ago
|
----------
|
||
14 years ago
|
Draws a circle.
|
||
|
|
||
11 years ago
|
.. ocv:function:: void circle( InputOutputArray img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=LINE_8, int shift=0 )
|
||
14 years ago
|
|
||
12 years ago
|
.. ocv:pyfunction:: cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) -> img
|
||
14 years ago
|
|
||
13 years ago
|
.. ocv:cfunction:: void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
|
||
|
|
||
14 years ago
|
:param img: Image where the circle is drawn.
|
||
14 years ago
|
|
||
14 years ago
|
:param center: Center of the circle.
|
||
14 years ago
|
|
||
14 years ago
|
:param radius: Radius of the circle.
|
||
14 years ago
|
|
||
14 years ago
|
:param color: Circle color.
|
||
14 years ago
|
|
||
14 years ago
|
:param thickness: Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn.
|
||
14 years ago
|
|
||
14 years ago
|
:param lineType: Type of the circle boundary. See the :ocv:func:`line` description.
|
||
14 years ago
|
|
||
14 years ago
|
:param shift: Number of fractional bits in the coordinates of the center and in the radius value.
|
||
14 years ago
|
|
||
14 years ago
|
The function ``circle`` draws a simple or filled circle with a given center and radius.
|
||
14 years ago
|
|
||
14 years ago
|
clipLine
|
||
14 years ago
|
------------
|
||
14 years ago
|
Clips the line against the image rectangle.
|
||
|
|
||
14 years ago
|
.. ocv:function:: bool clipLine(Size imgSize, Point& pt1, Point& pt2)
|
||
14 years ago
|
|
||
14 years ago
|
.. ocv:function:: bool clipLine(Rect imgRect, Point& pt1, Point& pt2)
|
||
14 years ago
|
|
||
14 years ago
|
.. ocv:pyfunction:: cv2.clipLine(imgRect, pt1, pt2) -> retval, pt1, pt2
|
||
|
|
||
13 years ago
|
.. ocv:cfunction:: int cvClipLine( CvSize img_size, CvPoint* pt1, CvPoint* pt2 )
|
||
|
|
||
13 years ago
|
:param imgSize: Image size. The image rectangle is ``Rect(0, 0, imgSize.width, imgSize.height)`` .
|
||
14 years ago
|
|
||
14 years ago
|
:param imgRect: Image rectangle.
|
||
14 years ago
|
|
||
14 years ago
|
:param pt1: First line point.
|
||
14 years ago
|
|
||
14 years ago
|
:param pt2: Second line point.
|
||
14 years ago
|
|
||
14 years ago
|
The functions ``clipLine`` calculate a part of the line segment that is entirely within the specified rectangle.
|
||
|
They return ``false`` if the line segment is completely outside the rectangle. Otherwise, they return ``true`` .
|
||
14 years ago
|
|
||
14 years ago
|
ellipse
|
||
14 years ago
|
-----------
|
||
14 years ago
|
Draws a simple or thick elliptic arc or fills an ellipse sector.
|
||
|
|
||
11 years ago
|
.. ocv:function:: 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 )
|
||
14 years ago
|
|
||
11 years ago
|
.. ocv:function:: void ellipse( InputOutputArray img, const RotatedRect& box, const Scalar& color, int thickness=1, int lineType=LINE_8 )
|
||
14 years ago
|
|
||
12 years ago
|
.. ocv:pyfunction:: cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) -> img
|
||
|
|
||
|
.. ocv:pyfunction:: cv2.ellipse(img, box, color[, thickness[, lineType]]) -> img
|
||
14 years ago
|
|
||
13 years ago
|
.. ocv:cfunction:: void cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle, double start_angle, double end_angle, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
|
||
|
|
||
|
.. ocv:cfunction:: void cvEllipseBox( CvArr* img, CvBox2D box, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
|
||
|
|
||
14 years ago
|
:param img: Image.
|
||
14 years ago
|
|
||
14 years ago
|
:param center: Center of the ellipse.
|
||
14 years ago
|
|
||
11 years ago
|
:param axes: Half of the size of the ellipse main axes.
|
||
14 years ago
|
|
||
14 years ago
|
:param angle: Ellipse rotation angle in degrees.
|
||
14 years ago
|
|
||
14 years ago
|
:param startAngle: Starting angle of the elliptic arc in degrees.
|
||
14 years ago
|
|
||
14 years ago
|
:param endAngle: Ending angle of the elliptic arc in degrees.
|
||
14 years ago
|
|
||
14 years ago
|
:param box: Alternative ellipse representation via :ocv:class:`RotatedRect` or ``CvBox2D``. This means that the function draws an ellipse inscribed in the rotated rectangle.
|
||
14 years ago
|
|
||
14 years ago
|
:param color: Ellipse color.
|
||
14 years ago
|
|
||
14 years ago
|
:param thickness: Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn.
|
||
14 years ago
|
|
||
14 years ago
|
:param lineType: Type of the ellipse boundary. See the :ocv:func:`line` description.
|
||
14 years ago
|
|
||
14 years ago
|
:param shift: Number of fractional bits in the coordinates of the center and values of axes.
|
||
14 years ago
|
|
||
14 years ago
|
The functions ``ellipse`` with less parameters draw an ellipse outline, a filled ellipse, an elliptic arc, or a filled ellipse sector.
|
||
14 years ago
|
A piecewise-linear curve is used to approximate the elliptic arc boundary. If you need more control of the ellipse rendering, you can retrieve the curve using
|
||
14 years ago
|
:ocv:func:`ellipse2Poly` and then render it with
|
||
|
:ocv:func:`polylines` or fill it with
|
||
14 years ago
|
:ocv:func:`fillPoly` . If you use the first variant of the function and want to draw the whole ellipse, not an arc, pass ``startAngle=0`` and ``endAngle=360`` . The figure below explains the meaning of the parameters.
|
||
14 years ago
|
|
||
14 years ago
|
**Figure 1. Parameters of Elliptic Arc**
|
||
14 years ago
|
|
||
14 years ago
|
.. image:: pics/ellipse.png
|
||
14 years ago
|
|
||
14 years ago
|
ellipse2Poly
|
||
14 years ago
|
----------------
|
||
14 years ago
|
Approximates an elliptic arc with a polyline.
|
||
|
|
||
13 years ago
|
.. ocv:function:: void ellipse2Poly( Point center, Size axes, int angle, int arcStart, int arcEnd, int delta, vector<Point>& pts )
|
||
14 years ago
|
|
||
14 years ago
|
.. ocv:pyfunction:: cv2.ellipse2Poly(center, axes, angle, arcStart, arcEnd, delta) -> pts
|
||
14 years ago
|
|
||
14 years ago
|
:param center: Center of the arc.
|
||
14 years ago
|
|
||
11 years ago
|
:param axes: Half of the size of the ellipse main axes. See the :ocv:func:`ellipse` for details.
|
||
13 years ago
|
|
||
|
:param angle: Rotation angle of the ellipse in degrees. See the :ocv:func:`ellipse` for details.
|
||
|
|
||
13 years ago
|
:param arcStart: Starting angle of the elliptic arc in degrees.
|
||
14 years ago
|
|
||
13 years ago
|
:param arcEnd: Ending angle of the elliptic arc in degrees.
|
||
14 years ago
|
|
||
14 years ago
|
:param delta: Angle between the subsequent polyline vertices. It defines the approximation accuracy.
|
||
14 years ago
|
|
||
14 years ago
|
:param pts: Output vector of polyline vertices.
|
||
14 years ago
|
|
||
14 years ago
|
The function ``ellipse2Poly`` computes the vertices of a polyline that approximates the specified elliptic arc. It is used by
|
||
14 years ago
|
:ocv:func:`ellipse` .
|
||
14 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
fillConvexPoly
|
||
14 years ago
|
------------------
|
||
14 years ago
|
Fills a convex polygon.
|
||
|
|
||
12 years ago
|
.. ocv:function:: void fillConvexPoly( Mat& img, const Point* pts, int npts, const Scalar& color, int lineType=LINE_8, int shift=0 )
|
||
|
|
||
|
.. ocv:function:: void fillConvexPoly( InputOutputArray img, InputArray points, const Scalar& color, int lineType=LINE_8, int shift=0 )
|
||
14 years ago
|
|
||
12 years ago
|
.. ocv:pyfunction:: cv2.fillConvexPoly(img, points, color[, lineType[, shift]]) -> img
|
||
14 years ago
|
|
||
13 years ago
|
.. ocv:cfunction:: void cvFillConvexPoly( CvArr* img, const CvPoint* pts, int npts, CvScalar color, int line_type=8, int shift=0 )
|
||
|
|
||
14 years ago
|
:param img: Image.
|
||
14 years ago
|
|
||
14 years ago
|
:param pts: Polygon vertices.
|
||
14 years ago
|
|
||
14 years ago
|
:param npts: Number of polygon vertices.
|
||
14 years ago
|
|
||
14 years ago
|
:param color: Polygon color.
|
||
14 years ago
|
|
||
14 years ago
|
:param lineType: Type of the polygon boundaries. See the :ocv:func:`line` description.
|
||
14 years ago
|
|
||
14 years ago
|
:param shift: Number of fractional bits in the vertex coordinates.
|
||
14 years ago
|
|
||
|
The function ``fillConvexPoly`` draws a filled convex polygon.
|
||
14 years ago
|
This function is much faster than the function ``fillPoly`` . It can fill not only convex polygons but any monotonic polygon without self-intersections,
|
||
|
that is, a polygon whose contour intersects every horizontal line (scan line) twice at the most (though, its top-most and/or the bottom edge could be horizontal).
|
||
14 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
fillPoly
|
||
14 years ago
|
------------
|
||
14 years ago
|
Fills the area bounded by one or more polygons.
|
||
|
|
||
12 years ago
|
.. ocv:function:: void fillPoly( Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=LINE_8, int shift=0, Point offset=Point() )
|
||
|
|
||
|
.. ocv:function:: void fillPoly( InputOutputArray img, InputArrayOfArrays pts, const Scalar& color, int lineType=LINE_8, int shift=0, Point offset=Point() )
|
||
14 years ago
|
|
||
12 years ago
|
.. ocv:pyfunction:: cv2.fillPoly(img, pts, color[, lineType[, shift[, offset]]]) -> img
|
||
14 years ago
|
|
||
13 years ago
|
.. ocv:cfunction:: void cvFillPoly( CvArr* img, CvPoint** pts, const int* npts, int contours, CvScalar color, int line_type=8, int shift=0 )
|
||
|
|
||
14 years ago
|
:param img: Image.
|
||
14 years ago
|
|
||
14 years ago
|
:param pts: Array of polygons where each polygon is represented as an array of points.
|
||
14 years ago
|
|
||
14 years ago
|
:param npts: Array of polygon vertex counters.
|
||
14 years ago
|
|
||
14 years ago
|
:param ncontours: Number of contours that bind the filled region.
|
||
14 years ago
|
|
||
14 years ago
|
:param color: Polygon color.
|
||
14 years ago
|
|
||
14 years ago
|
:param lineType: Type of the polygon boundaries. See the :ocv:func:`line` description.
|
||
14 years ago
|
|
||
14 years ago
|
:param shift: Number of fractional bits in the vertex coordinates.
|
||
14 years ago
|
|
||
13 years ago
|
:param offset: Optional offset of all points of the contours.
|
||
|
|
||
14 years ago
|
The function ``fillPoly`` fills an area bounded by several polygonal contours. The function can fill complex areas, for example,
|
||
13 years ago
|
areas with holes, contours with self-intersections (some of their parts), and so forth.
|
||
14 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
getTextSize
|
||
14 years ago
|
---------------
|
||
14 years ago
|
Calculates the width and height of a text string.
|
||
|
|
||
12 years ago
|
.. ocv:function:: Size getTextSize(const String& text, int fontFace, double fontScale, int thickness, int* baseLine)
|
||
14 years ago
|
|
||
14 years ago
|
.. ocv:pyfunction:: cv2.getTextSize(text, fontFace, fontScale, thickness) -> retval, baseLine
|
||
|
|
||
13 years ago
|
.. ocv:cfunction:: void cvGetTextSize( const char* text_string, const CvFont* font, CvSize* text_size, int* baseline )
|
||
|
|
||
14 years ago
|
:param text: Input text string.
|
||
14 years ago
|
|
||
12 years ago
|
:param text_string: Input text string in C format.
|
||
|
|
||
13 years ago
|
:param fontFace: Font to use. See the :ocv:func:`putText` for details.
|
||
14 years ago
|
|
||
13 years ago
|
:param fontScale: Font scale. See the :ocv:func:`putText` for details.
|
||
14 years ago
|
|
||
13 years ago
|
:param thickness: Thickness of lines used to render the text. See :ocv:func:`putText` for details.
|
||
14 years ago
|
|
||
14 years ago
|
:param baseLine: Output parameter - y-coordinate of the baseline relative to the bottom-most text point.
|
||
14 years ago
|
|
||
12 years ago
|
:param baseline: Output parameter - y-coordinate of the baseline relative to the bottom-most text point.
|
||
|
|
||
|
:param font: Font description in terms of old C API.
|
||
|
|
||
|
:param text_size: Output parameter - The size of a box that contains the specified text.
|
||
|
|
||
14 years ago
|
The function ``getTextSize`` calculates and returns the size of a box that contains the specified text.
|
||
|
That is, the following code renders some text, the tight box surrounding it, and the baseline: ::
|
||
14 years ago
|
|
||
12 years ago
|
String text = "Funny text inside the box";
|
||
14 years ago
|
int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
|
||
|
double fontScale = 2;
|
||
|
int thickness = 3;
|
||
14 years ago
|
|
||
14 years ago
|
Mat img(600, 800, CV_8UC3, Scalar::all(0));
|
||
14 years ago
|
|
||
14 years ago
|
int baseline=0;
|
||
|
Size textSize = getTextSize(text, fontFace,
|
||
|
fontScale, thickness, &baseline);
|
||
|
baseline += thickness;
|
||
14 years ago
|
|
||
14 years ago
|
// center the text
|
||
|
Point textOrg((img.cols - textSize.width)/2,
|
||
|
(img.rows + textSize.height)/2);
|
||
14 years ago
|
|
||
14 years ago
|
// draw the box
|
||
|
rectangle(img, textOrg + Point(0, baseline),
|
||
|
textOrg + Point(textSize.width, -textSize.height),
|
||
|
Scalar(0,0,255));
|
||
|
// ... and the baseline first
|
||
|
line(img, textOrg + Point(0, thickness),
|
||
|
textOrg + Point(textSize.width, thickness),
|
||
|
Scalar(0, 0, 255));
|
||
14 years ago
|
|
||
14 years ago
|
// then put the text itself
|
||
|
putText(img, text, textOrg, fontFace, fontScale,
|
||
|
Scalar::all(255), thickness, 8);
|
||
|
|
||
14 years ago
|
|
||
|
InitFont
|
||
|
--------
|
||
|
Initializes font structure (OpenCV 1.x API).
|
||
|
|
||
13 years ago
|
.. ocv:cfunction:: void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 )
|
||
14 years ago
|
|
||
13 years ago
|
:param font: Pointer to the font structure initialized by the function
|
||
14 years ago
|
|
||
13 years ago
|
:param font_face: Font name identifier. Only a subset of Hershey fonts http://sources.isc.org/utils/misc/hershey-font.txt are supported now:
|
||
14 years ago
|
|
||
13 years ago
|
* **CV_FONT_HERSHEY_SIMPLEX** normal size sans-serif font
|
||
14 years ago
|
|
||
13 years ago
|
* **CV_FONT_HERSHEY_PLAIN** small size sans-serif font
|
||
14 years ago
|
|
||
13 years ago
|
* **CV_FONT_HERSHEY_DUPLEX** normal size sans-serif font (more complex than ``CV_FONT_HERSHEY_SIMPLEX`` )
|
||
14 years ago
|
|
||
13 years ago
|
* **CV_FONT_HERSHEY_COMPLEX** normal size serif font
|
||
14 years ago
|
|
||
13 years ago
|
* **CV_FONT_HERSHEY_TRIPLEX** normal size serif font (more complex than ``CV_FONT_HERSHEY_COMPLEX`` )
|
||
14 years ago
|
|
||
13 years ago
|
* **CV_FONT_HERSHEY_COMPLEX_SMALL** smaller version of ``CV_FONT_HERSHEY_COMPLEX``
|
||
14 years ago
|
|
||
13 years ago
|
* **CV_FONT_HERSHEY_SCRIPT_SIMPLEX** hand-writing style font
|
||
14 years ago
|
|
||
13 years ago
|
* **CV_FONT_HERSHEY_SCRIPT_COMPLEX** more complex variant of ``CV_FONT_HERSHEY_SCRIPT_SIMPLEX``
|
||
14 years ago
|
|
||
13 years ago
|
The parameter can be composited from one of the values above and an optional ``CV_FONT_ITALIC`` flag, which indicates italic or oblique font.
|
||
14 years ago
|
|
||
|
|
||
13 years ago
|
:param hscale: Horizontal scale. If equal to ``1.0f`` , the characters have the original width depending on the font type. If equal to ``0.5f`` , the characters are of half the original width.
|
||
14 years ago
|
|
||
|
|
||
13 years ago
|
:param vscale: Vertical scale. If equal to ``1.0f`` , the characters have the original height depending on the font type. If equal to ``0.5f`` , the characters are of half the original height.
|
||
14 years ago
|
|
||
|
|
||
13 years ago
|
:param shear: Approximate tangent of the character slope relative to the vertical line. A zero value means a non-italic font, ``1.0f`` means about a 45 degree slope, etc.
|
||
14 years ago
|
|
||
|
|
||
13 years ago
|
:param thickness: Thickness of the text strokes
|
||
14 years ago
|
|
||
|
|
||
13 years ago
|
:param line_type: Type of the strokes, see :ocv:func:`line` description
|
||
14 years ago
|
|
||
|
|
||
|
The function initializes the font structure that can be passed to text rendering functions.
|
||
|
|
||
|
.. seealso:: :ocv:cfunc:`PutText`
|
||
|
|
||
13 years ago
|
.. _Line:
|
||
14 years ago
|
|
||
14 years ago
|
line
|
||
14 years ago
|
--------
|
||
14 years ago
|
Draws a line segment connecting two points.
|
||
|
|
||
11 years ago
|
.. ocv:function:: void line( InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=LINE_8, int shift=0 )
|
||
14 years ago
|
|
||
12 years ago
|
.. ocv:pyfunction:: cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img
|
||
14 years ago
|
|
||
13 years ago
|
.. ocv:cfunction:: void cvLine( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
|
||
|
|
||
14 years ago
|
:param img: Image.
|
||
14 years ago
|
|
||
14 years ago
|
:param pt1: First point of the line segment.
|
||
14 years ago
|
|
||
14 years ago
|
:param pt2: Second point of the line segment.
|
||
14 years ago
|
|
||
14 years ago
|
:param color: Line color.
|
||
14 years ago
|
|
||
14 years ago
|
:param thickness: Line thickness.
|
||
14 years ago
|
|
||
14 years ago
|
:param lineType: Type of the line:
|
||
14 years ago
|
|
||
12 years ago
|
* **LINE_8** (or omitted) - 8-connected line.
|
||
14 years ago
|
|
||
12 years ago
|
* **LINE_4** - 4-connected line.
|
||
14 years ago
|
|
||
12 years ago
|
* **LINE_AA** - antialiased line.
|
||
14 years ago
|
|
||
14 years ago
|
:param shift: Number of fractional bits in the point coordinates.
|
||
14 years ago
|
|
||
14 years ago
|
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.
|
||
12 years ago
|
Antialiased lines are drawn using Gaussian filtering.
|
||
14 years ago
|
|
||
|
|
||
11 years ago
|
arrowedLine
|
||
|
----------------
|
||
|
Draws a arrow segment pointing from the first point to the second one.
|
||
|
|
||
|
.. ocv:function:: void arrowedLine(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0, double tipLength=0.1)
|
||
|
|
||
|
:param img: Image.
|
||
|
|
||
|
:param pt1: The point the arrow starts from.
|
||
|
|
||
|
:param pt2: The point the arrow points to.
|
||
|
|
||
|
:param color: Line color.
|
||
|
|
||
|
:param thickness: Line thickness.
|
||
|
|
||
|
:param lineType: Type of the line:
|
||
|
|
||
|
* **8** (or omitted) - 8-connected line.
|
||
|
|
||
|
* **4** - 4-connected line.
|
||
|
|
||
|
* **CV_AA** - antialiased line.
|
||
|
|
||
|
:param shift: Number of fractional bits in the point coordinates.
|
||
|
|
||
|
:param tipLength: The length of the arrow tip in relation to the arrow length
|
||
|
|
||
|
The function ``arrowedLine`` draws an arrow between ``pt1`` and ``pt2`` points in the image. See also :ocv:func:`line`.
|
||
|
|
||
|
|
||
14 years ago
|
LineIterator
|
||
|
------------
|
||
14 years ago
|
.. ocv:class:: LineIterator
|
||
14 years ago
|
|
||
14 years ago
|
Class for iterating pixels on a raster line. ::
|
||
14 years ago
|
|
||
|
class LineIterator
|
||
|
{
|
||
|
public:
|
||
|
// creates iterators for the line connecting pt1 and pt2
|
||
|
// the line will be clipped on the image boundaries
|
||
|
// the line is 8-connected or 4-connected
|
||
|
// If leftToRight=true, then the iteration is always done
|
||
|
// from the left-most point to the right most,
|
||
|
// not to depend on the ordering of pt1 and pt2 parameters
|
||
|
LineIterator(const Mat& img, Point pt1, Point pt2,
|
||
|
int connectivity=8, bool leftToRight=false);
|
||
|
// returns pointer to the current line pixel
|
||
|
uchar* operator *();
|
||
|
// move the iterator to the next pixel
|
||
|
LineIterator& operator ++();
|
||
|
LineIterator operator ++(int);
|
||
12 years ago
|
Point pos() const;
|
||
14 years ago
|
|
||
14 years ago
|
// internal state of the iterator
|
||
|
uchar* ptr;
|
||
|
int err, count;
|
||
|
int minusDelta, plusDelta;
|
||
|
int minusStep, plusStep;
|
||
|
};
|
||
|
|
||
14 years ago
|
The class ``LineIterator`` is used to get each pixel of a raster line. It can be treated as versatile implementation of the Bresenham algorithm where you can stop at each pixel and do some extra processing, for example, grab pixel values along the line or draw a line with an effect (for example, with XOR operation).
|
||
14 years ago
|
|
||
12 years ago
|
The number of pixels along the line is stored in ``LineIterator::count`` . The method ``LineIterator::pos`` returns the current position in the image ::
|
||
14 years ago
|
|
||
|
// grabs pixels along the line (pt1, pt2)
|
||
|
// from 8-bit 3-channel image to the buffer
|
||
|
LineIterator it(img, pt1, pt2, 8);
|
||
12 years ago
|
LineIterator it2 = it;
|
||
14 years ago
|
vector<Vec3b> buf(it.count);
|
||
14 years ago
|
|
||
14 years ago
|
for(int i = 0; i < it.count; i++, ++it)
|
||
|
buf[i] = *(const Vec3b)*it;
|
||
12 years ago
|
|
||
|
// alternative way of iterating through the line
|
||
12 years ago
|
for(int i = 0; i < it2.count; i++, ++it2)
|
||
|
{
|
||
|
Vec3b val = img.at<Vec3b>(it2.pos());
|
||
|
CV_Assert(buf[i] == val);
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
rectangle
|
||
14 years ago
|
-------------
|
||
14 years ago
|
Draws a simple, thick, or filled up-right rectangle.
|
||
|
|
||
11 years ago
|
.. ocv:function:: void rectangle( InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=LINE_8, int shift=0 )
|
||
14 years ago
|
|
||
12 years ago
|
.. ocv:function:: void rectangle( Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=LINE_8, int shift=0 )
|
||
14 years ago
|
|
||
12 years ago
|
.. ocv:pyfunction:: cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img
|
||
14 years ago
|
|
||
13 years ago
|
.. ocv:cfunction:: void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
|
||
|
|
||
14 years ago
|
:param img: Image.
|
||
14 years ago
|
|
||
14 years ago
|
:param pt1: Vertex of the rectangle.
|
||
14 years ago
|
|
||
13 years ago
|
:param pt2: Vertex of the rectangle opposite to ``pt1`` .
|
||
13 years ago
|
|
||
13 years ago
|
:param rec: Alternative specification of the drawn rectangle.
|
||
14 years ago
|
|
||
14 years ago
|
:param color: Rectangle color or brightness (grayscale image).
|
||
14 years ago
|
|
||
14 years ago
|
: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.
|
||
14 years ago
|
|
||
14 years ago
|
:param lineType: Type of the line. See the :ocv:func:`line` description.
|
||
14 years ago
|
|
||
14 years ago
|
:param shift: Number of fractional bits in the point coordinates.
|
||
14 years ago
|
|
||
14 years ago
|
The function ``rectangle`` draws a rectangle outline or a filled rectangle whose two opposite corners are ``pt1`` and ``pt2``, or ``r.tl()`` and ``r.br()-Point(1,1)``.
|
||
14 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
polylines
|
||
14 years ago
|
-------------
|
||
14 years ago
|
Draws several polygonal curves.
|
||
|
|
||
12 years ago
|
.. ocv:function:: 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 )
|
||
13 years ago
|
|
||
12 years ago
|
.. ocv:function:: void polylines( InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar& color, int thickness=1, int lineType=LINE_8, int shift=0 )
|
||
14 years ago
|
|
||
12 years ago
|
.. ocv:pyfunction:: cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) -> img
|
||
14 years ago
|
|
||
13 years ago
|
.. ocv:cfunction:: void cvPolyLine( CvArr* img, CvPoint** pts, const int* npts, int contours, int is_closed, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
|
||
14 years ago
|
|
||
14 years ago
|
:param img: Image.
|
||
14 years ago
|
|
||
14 years ago
|
:param pts: Array of polygonal curves.
|
||
14 years ago
|
|
||
14 years ago
|
:param npts: Array of polygon vertex counters.
|
||
14 years ago
|
|
||
14 years ago
|
:param ncontours: Number of curves.
|
||
14 years ago
|
|
||
14 years ago
|
:param isClosed: Flag indicating whether the drawn polylines are closed or not. If they are closed, the function draws a line from the last vertex of each curve to its first vertex.
|
||
14 years ago
|
|
||
14 years ago
|
:param color: Polyline color.
|
||
14 years ago
|
|
||
14 years ago
|
:param thickness: Thickness of the polyline edges.
|
||
14 years ago
|
|
||
14 years ago
|
:param lineType: Type of the line segments. See the :ocv:func:`line` description.
|
||
14 years ago
|
|
||
14 years ago
|
:param shift: Number of fractional bits in the vertex coordinates.
|
||
14 years ago
|
|
||
14 years ago
|
The function ``polylines`` draws one or more polygonal curves.
|
||
14 years ago
|
|
||
14 years ago
|
|
||
13 years ago
|
drawContours
|
||
|
----------------
|
||
|
Draws contours outlines or filled contours.
|
||
|
|
||
12 years ago
|
.. ocv:function:: void 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() )
|
||
13 years ago
|
|
||
12 years ago
|
.. ocv:pyfunction:: cv2.drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]]) -> image
|
||
13 years ago
|
|
||
|
.. ocv:cfunction:: void cvDrawContours( CvArr * img, CvSeq* contour, CvScalar external_color, CvScalar hole_color, int max_level, int thickness=1, int line_type=8, CvPoint offset=cvPoint(0,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=CV_FILLED`` ), the contour interiors are
|
||
|
drawn.
|
||
|
|
||
|
:param lineType: Line connectivity. See :ocv:func:`line` for details.
|
||
|
|
||
|
: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 :math:`\texttt{offset}=(dx,dy)` .
|
||
|
|
||
|
:param contour: Pointer to the first contour.
|
||
|
|
||
13 years ago
|
:param external_color: Color of external contours.
|
||
13 years ago
|
|
||
13 years ago
|
:param hole_color: Color of internal contours (holes).
|
||
13 years ago
|
|
||
|
The function draws contour outlines in the image if
|
||
|
:math:`\texttt{thickness} \ge 0` or fills the area bounded by the contours if
|
||
|
:math:`\texttt{thickness}<0` . The example below shows how to retrieve connected components from the binary image and label them: ::
|
||
|
|
||
12 years ago
|
#include "opencv2/imgproc.hpp"
|
||
|
#include "opencv2/highgui.hpp"
|
||
13 years ago
|
|
||
|
using namespace cv;
|
||
12 years ago
|
using namespace std;
|
||
13 years ago
|
|
||
|
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<Point> > contours;
|
||
|
vector<Vec4i> hierarchy;
|
||
|
|
||
|
findContours( src, contours, hierarchy,
|
||
12 years ago
|
RETR_CCOMP, CHAIN_APPROX_SIMPLE );
|
||
13 years ago
|
|
||
|
// 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 );
|
||
12 years ago
|
drawContours( dst, contours, idx, color, FILLED, 8, hierarchy );
|
||
13 years ago
|
}
|
||
|
|
||
|
namedWindow( "Components", 1 );
|
||
|
imshow( "Components", dst );
|
||
|
waitKey(0);
|
||
|
}
|
||
|
|
||
11 years ago
|
.. note::
|
||
12 years ago
|
|
||
11 years ago
|
* An example using the drawContour functionality can be found at opencv_source_code/samples/cpp/contours2.cpp
|
||
|
* An example using drawContours to clean up a background segmentation result at opencv_source_code/samples/cpp/segment_objects.cpp
|
||
12 years ago
|
|
||
11 years ago
|
* (Python) An example using the drawContour functionality can be found at opencv_source/samples/python2/contours.py
|
||
13 years ago
|
|
||
14 years ago
|
|
||
14 years ago
|
putText
|
||
14 years ago
|
-----------
|
||
14 years ago
|
Draws a text string.
|
||
|
|
||
11 years ago
|
.. ocv:function:: 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 )
|
||
14 years ago
|
|
||
13 years ago
|
.. ocv:pyfunction:: cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> None
|
||
14 years ago
|
|
||
14 years ago
|
.. ocv:cfunction:: void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color )
|
||
12 years ago
|
|
||
14 years ago
|
:param img: Image.
|
||
14 years ago
|
|
||
14 years ago
|
:param text: Text string to be drawn.
|
||
14 years ago
|
|
||
14 years ago
|
:param org: Bottom-left corner of the text string in the image.
|
||
14 years ago
|
|
||
14 years ago
|
:param font: ``CvFont`` structure initialized using :ocv:cfunc:`InitFont`.
|
||
|
|
||
14 years ago
|
:param fontFace: Font type. One of ``FONT_HERSHEY_SIMPLEX``, ``FONT_HERSHEY_PLAIN``, ``FONT_HERSHEY_DUPLEX``, ``FONT_HERSHEY_COMPLEX``, ``FONT_HERSHEY_TRIPLEX``, ``FONT_HERSHEY_COMPLEX_SMALL``, ``FONT_HERSHEY_SCRIPT_SIMPLEX``, or ``FONT_HERSHEY_SCRIPT_COMPLEX``,
|
||
11 years ago
|
where each of the font ID's can be combined with ``FONT_ITALIC`` to get the slanted letters.
|
||
14 years ago
|
|
||
14 years ago
|
:param fontScale: Font scale factor that is multiplied by the font-specific base size.
|
||
14 years ago
|
|
||
14 years ago
|
:param color: Text color.
|
||
14 years ago
|
|
||
14 years ago
|
:param thickness: Thickness of the lines used to draw a text.
|
||
14 years ago
|
|
||
14 years ago
|
:param lineType: Line type. See the ``line`` for details.
|
||
14 years ago
|
|
||
14 years ago
|
:param bottomLeftOrigin: When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
|
||
14 years ago
|
|
||
14 years ago
|
The function ``putText`` renders the specified text string in the image.
|
||
14 years ago
|
Symbols that cannot be rendered using the specified font are
|
||
14 years ago
|
replaced by question marks. See
|
||
14 years ago
|
:ocv:func:`getTextSize` for a text rendering code example.
|