python cv.rectangle 缺_解决Python-OpenCV中cv2.rectangle报错

在PyTorch中测试DataLoader读取后的图像,对图像画框cv2.rectangle时报错:

TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)

网上搜索良久无果,维度和数值也都检查无误,后在Stack Overflow上发现解决方案:

img = (img.numpy().transpose((1, 2, 0))*255).astype(np.uint8)

img = img.copy()

这里使用copy后,报错问题解决,但是Stack Overflow上也没有人知道原因所在,

I faced the same problem with numpy 1.11.2 and opencv 3.3.0. Not sure why, but this did the job for me. Before using cv2.rectangle, add the line below:

python image = image.copy() # Change

先mark上,后面寻找详细原因,不过根据报错类型来看,直觉上是因为这个版本的numpy和opencv在做各种转换的时候遗留下了MAT内部数据结构的一些坑,导致了在操作rectangle时候刚好用到了MAT中的某个属性,不兼容后报错,后面有时间可以再看看cv的python版源码找找原因。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值