5

我只想获取 BGR 图像中的第一个通道(蓝色),然后将其保存到磁盘。当我使用 cv2.split() 一切正常

>>> import cv2
>>> a = cv2.imread("/home/s18/theVIDEO/1_resized.jpg")
>>> b = cv2.split(a)[0]
>>> type(b)
<type 'numpy.ndarray'>                                                                                              
>>> b                                                                                              
array([[223, 222, 224, ...,  88,  80,  71],
[222, 221, 225, ...,  84,  78,  67],
[220, 221, 225, ...,  77,  71,  62],
..., 
[163, 178, 182, ..., 107, 107, 106],
[148, 170, 186, ..., 104, 104, 103],
[156, 181, 201, ..., 102, 101, 100]], dtype=uint8)
>>> b.shape
(600, 800)
>>> cv2.imwrite("/home/s18/theVIDEO/1_resized2.jpg", b)
True

但是在使用模拟切片操作时出现错误

>>> c = a[:,:,0]
>>> c
>>> type(c)
<type 'numpy.ndarray'>                                                                                              
array([[223, 222, 224, ...,  88,  80,  71],
[222, 221, 225, ...,  84,  78,  67],
[220, 221, 225, ...,  77,  71,  62],
..., 
[163, 178, 182, ..., 107, 107, 106],
[148, 170, 186, ..., 104, 104, 103],
[156, 181, 201, ..., 102, 101, 100]], dtype=uint8)
>>> c.shape
(600, 800)
>>> cv2.imwrite("/home/s18/theVIDEO/1_resized3.jpg", c)
False

数组 b 和 c 中的元素是相等的,维度和类也是相似的。为什么我不能使用简单的切片来获取其中一个频道?

4

1 回答 1

2

如果你不直接切片而是复制内容,结果是可以的

...
>>> c = zeros((a.shape[0],a.shape[1]), dtype=a.dtype)
>>> c[:,:] = a[:,:,0]
>>> cv2.imwrite('out.jpg', c)
True
于 2011-12-01T17:20:43.843 回答