【问题标题】:OpenCV Fillpoly Assertion failedOpenCV Fillpoly 断言失败
【发布时间】:2020-12-31 09:46:52
【问题描述】:

我想填充面具的某个区域。我的代码看起来像这样 =

if __name__ == "__main__":

     while 2>1:
        with mss.mss() as sct:
            monitor = {"top": 360, "left": 810, "width": 650, "height": 475}
            output = "sct-{top}x{left}_{width}x{height}.png".format(**monitor)
            # Grab the data
            sct_img = np.array(sct.grab(monitor))
            shap = sct_img.shape
            mask = np.zeros_like(sct_img)
            pts = np.array([[1, 3], [4, 8], [1, 9]], np.int32)
            cv2.fillPoly(mask, pts, 255)

            cv2.imshow("OPENCV/NUMPY normal", mask)

            if cv2.waitKey(25) & 0xFF == ord("q"):
                cv2.destroyAllWindows()
                break

我得到错误:

cv2.fillPoly(mask, pts, 255) cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-k8sx3e60\opencv\modules\imgproc\src\drawing.cpp:2395: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'cv::fillPoly'

【问题讨论】:

  • 尝试 np.uint8 代替 np.int32

标签: python opencv pycharm


【解决方案1】:

根据official 示例,您必须将pts 作为listtype 传递:

cv.fillPoly(img, [ppt], (255, 255, 255), line_type) # ppt is an ndarray

因此,如果将pts 传递为[pts],应该可以解决问题。

cv2.fillPoly(mask, [pts], 255)

【讨论】:

  • 我必须写 (255,255,255) 而不是 255 才能得到白色。我把 pts 留作 pts
  • 是的,当你设置255 表示[255, 0, 0] 表示只显示蓝色。
猜你喜欢
  • 2014-02-10
  • 2012-11-26
  • 2014-11-24
  • 2014-05-28
  • 2015-09-12
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
相关资源
最近更新 更多