绘制 polygons and polylines:OpenCV版本

2023-05-16

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

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

绘制 polygons and polylines:OpenCV版本 的相关文章

  • 我无法在 docker 中安装 opencv-contrib-python

    我尝试安装opencv contrib python但我无法让它在 docker 上工作 它说找不到满足 opencv contrib python 要求的版本 I tried pip install opencv contrib pyth
  • 向 ca cv::Mat 添加文本比 cv::putText() 更好的方法吗?

    我想在 a 上添加一些文字cv Mat but cv putText 对我来说不够灵活 结盟 我需要将可变长度的标签放在运行时已知的几个像素位置 但由于cv putText 将输出的原点始终放在左侧 如果位置离左侧太远 我的文本就会消失在图
  • 将 Magick::Image 转换为 cv::Mat

    我正在尝试将通过 Magick 从 GIF 加载的图像转换为cv Mat 我已经从cv Mat to Magick Image但似乎无法找到如何从 Magick 中的图像中提取数据以便将其加载到 Mat 中 最好的方法是什么 供参考 反过来
  • 增加图像亮度而不溢出

    我在尝试增加图像亮度时遇到问题 这是原始图像 我想要得到的图像是这样的 现在使用以下代码增加亮度 image cv2 imread home wni vbshare tmp a4 index2 png 0 if sum image 0 le
  • Ubuntu OpenCV 无法编译

    我正在尝试使用以下命令编译 OpenCV 3 2 1 cmake DCMAKE BUILD TYPE Release DBUILD SHARED LIBS OFF DCMAKE INSTALL PREFIX usr local DOPENC
  • 如何在win32上安装OpenCV 2.0

    我需要在 Win32 上安装 OpenCV 我目前没有安装它 我下载了 OpenCV 2 0 0a win32 exe 并运行它 我现在到底该怎么办 没有 lib之类的东西 我找到了一些使用 cmake 构建版本的说明 http openc
  • 将嘈杂的硬币重塑为圆形

    我正在使用 JavaCV OpenCV 包装器 进行硬币检测 但是当硬币连接时我遇到了一些问题 如果我尝试侵蚀它们以分离这些硬币 它们就会失去圆形形状 如果我尝试计算每个硬币内部的像素 可能会出现问题 因此某些硬币可能会被误算为更大的硬币
  • 如何使用 OpenCV 找到红色区域? [复制]

    这个问题在这里已经有答案了 我正在尝试编写一个检测红色的程序 然而有时它比平常更暗 所以我不能只使用一个值 检测不同深浅的红色的最佳范围是多少 我目前使用的范围是 128 0 0 255 60 60 但有时它甚至检测不到我放在它前面的红色物
  • 如何提取图像中的表格

    我想从图像中提取表格 这个 python 模块https pypi org project ExtractTable https pypi org project ExtractTable 与他们的网站https www extractta
  • 带有 OpenCV 的增强现实 SDK [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何解决 Python 'Pyzbar' 库的导入错误?

    我刚刚开始熟悉 Pyzbar 库 但是当使用decode方法我得到一个错误 这是代码 import cv2 import numpy as np import pyzbar code image cv2 imread C Users Ace
  • 如何使用 OpenCV 检测图像帧中的对象?

    我正在使用 Raspberry Pi 开发一个漫游器 它将清扫房间并捡起掉落在地上的物体 为了检测物体 我使用了在流动站操作开始时拍摄的参考图像 以及每 10 秒单击一次的图像 新图像 为了确定图像帧是否发生变化 我在参考图像和新图像之间进
  • brew 链接 jpeg 问题

    我正在尝试安装opencv在 Mac OSX Lion 上 brew install opencv 我收到以下错误 以及其他一些类似的错误 Error The linking step did not complete successful
  • 查找彼此接近的对象边界

    我正在研究一个计算机视觉问题 其中问题的第一步是找到物体彼此靠近的位置 例如 在下图中 我感兴趣的是找到灰色标记的区域 Input Output 我目前的方法是首先反转图像 然后通过侵蚀进行形态梯度跟随 然后删除一些不感兴趣的轮廓 脚本如下
  • opencv中矩阵的超快中值(与matlab一样快)

    我正在 openCV 中编写一些代码 想要找到一个非常大的矩阵数组 单通道灰度 浮点数 的中值 我尝试了几种方法 例如对数组进行排序 使用 std sort 和选择中间条目 但与 matlab 中的中值函数相比 它非常慢 准确地说 在 ma
  • 相机标定(OpenCV 2.3)-如何使用畸变参数?

    我有一组带有一些附加标记的刚体图像 我在这些标记之一中定义了一个原点坐标系 我想获得该坐标系与在相机原点定义的坐标系之间的旋转和平移 我尝试了一段时间 POSIT 以下this http goo gl cUYYt 但从未获得可接受的结果 直
  • 如何在opencv python中为图像添加边框

    如果我有如下图所示的图像 如何在图像周围添加边框 以便最终图像的整体高度和宽度增加 但原始图像的高度和宽度保持在中间 下面的代码添加了一个大小恒定的边框10像素到原始图像的所有四个边 对于颜色 我假设您想要使用背景的平均灰度值 这是我根据图
  • 提高 pytesseract 从图像中正确识别文本的能力

    我正在尝试使用读取验证码pytesseract模块 大多数时候它都能提供准确的文本 但并非总是如此 这是读取图像 操作图像以及从图像中提取文本的代码 import cv2 import numpy as np import pytesser
  • Python:opencv warpPerspective 既不接受 2 个也不接受 3 个参数

    我发现单应矩阵如下特征匹配 单应性教程 https docs opencv org 3 4 1 d1 de0 tutorial py feature homography html using M mask cv2 findHomograp
  • 如何设置K-means openCV c++的初始中心

    我正在尝试使用 OpenCv 和 Kmeans 对图像进行分割 我刚刚实现的代码如下 include opencv2 objdetect objdetect hpp include opencv2 highgui highgui hpp i

随机推荐