python OpenCV输出列表
我想从图像的像素颜色中获得一个简单的python列表。输出列表应按一维顺序排列,如下所示:python OpenCV输出列表,python,list,opencv,converter,nested-lists,Python,List,Opencv,Converter,Nested Lists,我想从图像的像素颜色中获得一个简单的python列表。输出列表应按一维顺序排列,如下所示: output = [B1,G1,R1,B2,G2,R2,B3,G3,R3....] 要获取图像数据,请执行以下操作: import cv2 image = cv2.imread('a.png') image array([[[ 0, 0, 255], [ 0, 0, 255], [ 0, 0, 255]], [[ 0, 255,
output = [B1,G1,R1,B2,G2,R2,B3,G3,R3....]
要获取图像数据,请执行以下操作:
import cv2
image = cv2.imread('a.png')
image
array([[[ 0, 0, 255],
[ 0, 0, 255],
[ 0, 0, 255]],
[[ 0, 255, 0],
[ 0, 255, 0],
[ 0, 255, 0]],
[[255, 0, 0],
[255, 0, 0],
[255, 0, 0]]], dtype=uint8)
f = image.flatten()
f
[array([ 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 255, 0, 0,
255, 0, 0, 255, 0, 255, 0, 0, 255, 0, 0, 255, 0,
0],数据类型=uint8)]
有没有办法得到:
f = [0,0,255,0,0,255,0,0,255,0,255,0,0,255,0,0,255,0,255,0,0,255,0,0,255,0,0]
由于cv2中的opencv图像是numpy数组,请使用numpy:
import cv2
import numpy as np
# simulate a bgr image:
>>> a = np.array([[[1,2,3], [4,5,6]],[[1,2,3], [4,5,6]]])
>>> a
array([[[1, 2, 3],
[4, 5, 6]],
[[1, 2, 3],
[4, 5, 6]]])
>>> b = np.reshape(a,-1)
>>> b
array([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6])
.tolist()方法由Abid Rahman先生提出建议
>>> import numpy as np
>>> f = np.random.randint(0,255,(3,3,3))
>>> print(f)
[[[193 61 68]
[223 102 0]
[ 45 204 7]]
[[ 64 193 60]
[ 94 157 49]
[ 38 116 5]]
[[ 64 107 197]
[225 246 22]
[186 102 156]]]
>>> g = f.flatten()
>>> print g
[193 61 68 223 102 0 45 204 7 64 193 60 94 157 49 38 116 5
64 107 197 225 246 22 186 102 156]
>>> g.dtype
dtype('int32')
>>> type(g)
<type 'numpy.ndarray'>
>>> h = g.tolist()
>>> print h
[193, 61, 68, 223, 102, 0, 45, 204, 7, 64, 193, 60, 94, 157, 49, 38,
116, 5, 64, 107, 197, 225, 246, 22, 186, 102, 156]
>>> type(h)
<type 'list'>
>>将numpy作为np导入
>>>f=np.random.randint(0255,(3,3,3))
>>>印刷品(f)
[[[193 61 68]
[223 102 0]
[ 45 204 7]]
[[ 64 193 60]
[ 94 157 49]
[ 38 116 5]]
[[ 64 107 197]
[225 246 22]
[186 102 156]]]
>>>g=f.展平()
>>>打印g
[193 61 68 223 102 0 45 204 7 64 193 60 94 157 49 38 116 5
64 107 197 225 246 22 186 102 156]
>>>g.D类型
数据类型('int32')
>>>类型(g)
>>>h=g.tolist()
>>>打印h
[193, 61, 68, 223, 102, 0, 45, 204, 7, 64, 193, 60, 94, 157, 49, 38,
116, 5, 64, 107, 197, 225, 246, 22, 186, 102, 156]
>>>类型(h)
f=f[0],这行吗?可能是:list(f)或f=f.tolist()