📌  相关文章
📜  cv2.error: OpenCV(4.3.0) io opencv modules imgproc src drawing.cpp:2374: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'fillPoly' (1)

📅  最后修改于: 2023-12-03 15:14:24.457000             🧑  作者: Mango

OpenCV Drawing Error: fillPoly Assertion Failed

Introduction

As a programmer using OpenCV, you may encounter an error message that appears similar to the following:

cv2.error: OpenCV(4.3.0) io opencv modules imgproc src drawing.cpp:2374: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'fillPoly'

This error message can be intimidating and make it difficult to understand what went wrong. In this article, we will explore what this error message means, why it occurs, and how to fix it.

Understanding the Error Message

The error message is composed of several parts:

  • cv2.error: This indicates that the error was raised by the OpenCV Python module.
  • OpenCV(4.3.0): This indicates the version of OpenCV you are using.
  • io opencv modules imgproc src drawing.cpp:2374: This provides some context on where in the OpenCV code the error occurred.
  • error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'fillPoly': This provides a specific error message, indicating that the assertion failed and providing the function in which the error occurred.
Why the Error Occurs

This error occurs when the fillPoly function is called with an invalid set of points. Specifically, the p.checkVector(2, CV_32S) >= 0 assertion checks that the points parameter of the function contains at least one vector of 2 integers, and that each integer is of type CV_32S (32-bit signed integer). If this assertion fails, the error message is raised.

How to Fix the Error

To fix the error, you need to ensure that the points parameter of the fillPoly function contains the correct set of points. The points parameter should be a numpy array of shape (n, 1, 2), where n is the number of points in the polygon. Each point should be a numpy.ndarray of shape (1, 2), containing the x and y coordinates respectively.

Here is an example of how to pass the correct set of points to the fillPoly function:

import numpy as np
import cv2

# define the points of the polygon
vertices = np.array([[[100,100], [300,100], [300,300], [100,300]]])

# create an empty image
img = np.zeros((400, 400), np.uint8)

# draw the polygon on the image
cv2.fillPoly(img, vertices, (255,255,255))

In this example, we define the polygon vertices as a numpy array of shape (1, 4, 2), where 1 indicates there is only one polygon, 4 indicates there are four points in the polygon, and 2 indicates there are two coordinates (x and y) for each point. We then create an empty image and call the fillPoly function with the vertices and the desired color ((255, 255, 255) in this case).

In summary, the fillPoly assertion error occurs when the input points parameter is not a valid set of points for a polygon. The solution is to ensure that the points are in the correct numpy array format, with each point represented as a (1, 2) numpy array, and encapsulated in a (n, 1, 2) numpy array.