绘制 polygons and polylines:OpenCV版本

OpenCV documentation index - OpenCV 文档索引
https://www.docs.opencv.org/

master (4.x)
https://www.docs.opencv.org/master/

3.4 (3.4.x)
https://www.docs.opencv.org/3.4/

2.4 (2.4.x)
https://www.docs.opencv.org/2.4/

1. 3.4 (3.4.x) -> Modules -> Image Processing -> Drawing Functions -> polylines()

1.1 Function Documentation - polylines()

void cv::polylines (InputOutputArray img,
InputArrayOfArrays pts,
bool isClosed,
const Scalar & color,
int thickness = 1,
int lineType = LINE_8,
int shift = 0

Python

img = cv.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]	)
#include <opencv2/imgproc.hpp>

Draws several polygonal curves.
绘制几条多边形曲线。

Parameters
img - Image.
pts - Array of polygonal curves.
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.
color - Polyline color.
thickness - Thickness of the polyline edges.
lineType - Type of the line segments.
shift - Number of fractional bits in the vertex coordinates.

The function cv::polylines draws one or more polygonal curves.
函数 cv :polylines 绘制一条或多条多边形曲线。

To draw a polygon, first you need coordinates of vertices. Make those points into an array of shape ROWSx1x2 where ROWS are number of vertices and it should be of type int32. Here we draw a small polygon of with four vertices in yellow color.
画多边形,首先需要给出一组 (数组) 顶点的座标,形式是 ROWSx1x2,ROWS 表示顶点的数量,它的类型是 int32。这里我们用黄色画一个小的四边形。

If third argument is False, you will get a polylines joining all the points, not a closed shape.
如果第三个参数设为 False,只会得到顶点依次相连的图形 (首尾不连),而不会得到所有顶点封闭连接的图形。

cv2.polylines() can be used to draw multiple lines. Just create a list of all the lines you want to draw and pass it to the function. All lines will be drawn individually. It is more better and faster way to draw a group of lines than calling cv2.line() for each line.
cv2.polylines() 可以被用来同时画多条线,只需要同时创建多个线段,传入函数中,它将独立的画出所有线,这比重复为每条线调用 cv2.line() 更快更好。

pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
img = cv2.polylines(img,[pts],True,(0,255,255))

2. 2.4.13 (2.4.x) -> core. The Core Functionality -> Drawing Functions -> polylines()

https://docs.opencv.org/2.4/index.html

2.1 Function Documentation - polylines()

Draws several polygonal curves.

C++: 
void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

C++: 
void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

Python: 
cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) → None

C: 
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 )

Python:
cv.PolyLine(img, polys, is_closed, color, thickness=1, lineType=8, shift=0) → None
  • Parameters

img - Image.
pts - Array of polygonal curves.
npts - Array of polygon vertex counters.
ncontours - Number of curves.
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.
color - Polyline color.
thickness - Thickness of the polyline edges.
lineType - Type of the line segments.
shift - Number of fractional bits in the vertex coordinates.
The function polylines draws one or more polygonal curves.

3. polygons and polylines

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
polylines()
Draws several polygonal curves.
"""

# Import required packages
import numpy as np
import cv2

# Dictionary containing some colors.
colors = {'blue': (255, 0, 0), 'green': (0, 255, 0), 'red': (0, 0, 255), 'yellow': (0, 255, 255),
          'magenta': (255, 0, 255), 'cyan': (255, 255, 0), 'white': (255, 255, 255), 'black': (0, 0, 0),
          'gray': (125, 125, 125), 'rand': np.random.randint(0, high=256, size=(3,)).tolist(),
          'dark_gray': (50, 50, 50), 'light_gray': (220, 220, 220)}

image = np.zeros((512, 512, 3), np.uint8)
cv2.namedWindow("image_show")

while (True):
    # We are going to draw several polylines.
    """
    polylines(img, pts, isClosed, color, thickness=None, lineType=None, shift=None)
    polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) -> img
    .   @brief Draws several polygonal curves.
    .
    .   @param img - Image.
    .   @param pts - Array of polygonal curves.
    .   @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.
    .   @param color - Polyline color.
    .   @param thickness - Thickness of the polyline edges.
    .   @param lineType - Type of the line segments. See the line description.
    .   @param shift - Number of fractional bits in the vertex coordinates.
    .
    .   The function polylines draws one or more polygonal curves.
    """
    # These points define a triangle:
    pts = np.array([[250, 5], [220, 80], [280, 80]], np.int32)
    # Reshape to shape (number_vertex, 1, 2)
    pts = pts.reshape((-1, 1, 2))
    # Print the shapes: this line is not necessary, only for visualization:
    print("shape of pts '{}'".format(pts.shape))
    # Draw this polygon with True option:
    cv2.polylines(image, [pts], True, colors['green'], 3)

    # These points define a triangle:
    pts = np.array([[250, 105], [220, 180], [280, 180]], np.int32)
    # Reshape to shape (number_vertex, 1, 2)
    pts = pts.reshape((-1, 1, 2))
    # Print the shapes:
    print("shape of pts '{}'".format(pts.shape))
    # Draw this polygon with False option:
    cv2.polylines(image, [pts], False, colors['green'], 3)

    # These points define a pentagon:
    pts = np.array([[20, 90], [60, 60], [100, 90], [80, 130], [40, 130]], np.int32)
    # Reshape to shape (number_vertex, 1, 2)
    pts = pts.reshape((-1, 1, 2))
    # Print the shapes:
    print("shape of pts '{}'".format(pts.shape))
    # Draw this polygon with True option:
    cv2.polylines(image, [pts], True, colors['blue'], 3)

    # These points define a pentagon:
    pts = np.array([[20, 180], [60, 150], [100, 180], [80, 220], [40, 220]], np.int32)
    # Reshape to shape (number_vertex, 1, 2)
    pts = pts.reshape((-1, 1, 2))
    # Print the shapes:
    print("shape of pts '{}'".format(pts.shape))
    # Draw this polygon with False option:
    cv2.polylines(image, [pts], False, colors['blue'], 3)

    # These points define a rectangle:
    pts = np.array([[150, 100], [200, 100], [200, 150], [150, 150]], np.int32)
    # Reshape to shape (number_vertex, 1, 2)
    pts = pts.reshape((-1, 1, 2))
    # Print the shapes:
    print("shape of pts '{}'".format(pts.shape))
    # Draw this polygon with False option:
    cv2.polylines(image, [pts], True, colors['yellow'], 3)

    # These points define a rectangle:
    pts = np.array([[150, 200], [200, 200], [200, 250], [150, 250]], np.int32)
    # Reshape to shape (number_vertex, 1, 2)
    pts = pts.reshape((-1, 1, 2))
    # Print the shapes:
    print("shape of pts '{}'".format(pts.shape))
    # Draw this polygon with False option:
    cv2.polylines(image, [pts], False, colors['yellow'], 3)

    cv2.imshow("image_show", image)

    k = cv2.waitKey(200) & 0xFF
    if 27 == k:
        break

cv2.destroyAllWindows()

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章