python opencv里的图片格式

常见错误

TypeError: mat data type=18 is not supported

这是因为矩阵格式不正确。
opencv在python里矩阵是保存为numpy中的ndarray格式,需要通过numpy.array(矩阵转化)。
其次,opencv所能显示的类型也有要求,必须是int型。有的写法像素值在[0,1]内,需要转换:

>>> import numpy as np
>>> a = [[1,2,3],[4,5,6]]
>>> a *= 2
>>> a
[[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]
>>> b = np.array(a)
>>> b
array([[1, 2, 3],
       [4, 5, 6],
       [1, 2, 3],
       [4, 5, 6]])
>>> b*2
array([[ 2,  4,  6],
       [ 8, 10, 12],
       [ 2,  4,  6],
       [ 8, 10, 12]])
# [0,1]->[0,255]
a = np.array(a)
a *= 255
a = a.astype(int)
# 还有可能是astype(float32,uint8等类型)

猜你喜欢

转载自blog.csdn.net/san_junipero/article/details/79956887
今日推荐