OpenCV python: cv2.split () vs slicing when receiving a channel in a BGR image

I want to get only the first channel (blue) in the BGR image, and then save it to disk. When I use cv2.split (), everything is fine

>>> 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 

But when using the simulate cut operation, I get an error

 >>> 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 

Elements of arrays b and c are equal, sizes and classes are also simulative. Why couldn’t I use simple slicing to get one of the channels?

+4
source share
1 answer

It turns out that if you did not cut directly, but copied the contents instead

 ... >>> c = zeros((a.shape[0],a.shape[1]), dtype=a.dtype) >>> c[:,:] = a[:,:,0] >>> cv2.imwrite('out.jpg', c) True 
+1
source

Source: https://habr.com/ru/post/1384149/


All Articles