【问题标题】:TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)TypeError: 输出数组 img 的布局与 cv::Mat 不兼容 (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)
【发布时间】:2019-05-23 18:00:27
【问题描述】:

我收到此代码的错误:

import matplotlib.pyplot as plt
import cv2
import numpy as np

black = np.zeros(shape = (512, 512, 3), dtype = np.int64)
cv2.circle(black, center = (100, 100), radius = 50, color = (0, 255, 0), thickness = 10)

plt.imshow(black)

在结果中,黑色图像上应打印一个绿色圆圈。但我得到了

TypeError: 输出数组img 的布局不兼容

cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)

【问题讨论】:

    标签: python-3.x opencv error-handling compiler-errors jupyter-notebook


    【解决方案1】:

    在创建 black numpy 矩阵时使用 dtype = np.int64 似乎是问题所在。在使用RGB 图像时,您通常不需要 64 位整数值,您可以安全地使用 8 位字符值:

    black = np.zeros(shape = (512, 512, 3), dtype = np.uint8)
    

    然而 OpenCV 支持给定矩阵中的 32 位整数值,但创建 RGB 图像似乎有点过头了,因为所有 RGB 域颜色只需要 8 位来表示像素颜色分量(0-255 )。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 2012-12-12
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      相关资源
      最近更新 更多