error: OpenCV(4.8.0) :-1: error: (-5:Bad argument) in function ‘line‘

error: OpenCV(4.8.0) :-1: error: (-5:Bad argument) in function 'line' > Overload resolution failed: > - Layout of the output array img is incompatible with cv::Mat > - Expected Ptr<cv::UMat> for argument 'img'

在python中读取matlab保存的mat文件,然后进行一些处理出现上面报错。
此原因可能由于 matlab和python存储方式不同。

Pascal, C,C++,Python都是行优先存储的,而Fortran,MatLab是列优先存储的。

C order 以及 Fortran order

  • C order 指的是行优先的顺序(Row-major Order),即内存中同行的元素存在一起,
  • Fortran Order则指的是列优先的顺序(Column-major Order),即内存中同列的元素存在一起。

解决方法:在python中使用函数:
 

np.ascontiguousarray()

使得数组转化为C-order存储即可。

参考:
numpy中的np.ascontiguousarray()函数 - 知乎 (zhihu.com)

 若保存的mat文件为RGB图片,当使用mediapipe在本图片上进行提取手部关节点时候,可能会发现左右手识别反转,需要对图片进行水平翻转cv2.flip(),最好先水平翻转再转化为c coder格式排列,即将cv2.flip(),函数放在np.ascontiguousarray()函数前。若将cv2.flip()函数放在np.ascontiguousarray()函数之后会发现没有任何作用,左右手依然识别错误。

猜你喜欢

转载自blog.csdn.net/qq_41131123/article/details/134620690