在opencv中翻转图像后无法在图像上绘制框。
>>im = cv2.imread('demo.jpg')
>>im = im[:,::-1]
>>drawed_im = cv2.rectangle(im, (10,10), (50,50), (255, 0, 0), thickness=2)
>>TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)
最佳答案
这是一个奇怪的问题,我无法确定其原因。我之前在其他绘图函数中看到过它,我使用的解决方案是将图像重新转换为 numpy 数组。
import cv2
import numpy as np
im = cv2.imread('demo.jpg')
im = np.array(im[:,::-1]) # cast as array
drawed_im = cv2.rectangle(im, (10,10), (50,50), (255, 0, 0), thickness=2)
关于numpy - 调用 cv2.rectangle 时无法绘制框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49571138/