繪製 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()

在這裏插入圖片描述

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