我收到此代码错误:
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: Layout of the output array
img
is incompatible withcv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)
最佳答案
创建dtype = np.int64
numpy矩阵时使用black
似乎是个问题。使用RGB
图像时,通常不需要64位整数值,可以安全地将8位char值用作:
black = np.zeros(shape = (512, 512, 3), dtype = np.uint8)
但是,OpenCV在给定的矩阵中支持32位整数值,但是对于创建RGB图像来说似乎是过大的,因为所有RGB域颜色仅需要8位即可表示像素颜色分量(0-255)。
关于python-3.x - TypeError : Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53915690/