简体   繁体   中英

Extracting individual frames from an image

I am currently new to python and I want to extract the R, G and B frame separately from an image.

For instance, the variable which stores my image is img

What I want to know;

how do I make Rimg = img (:,:,1) Gimg = img (:,:,2) Bimg = img (:,:,3) Ofcource, these are MATlab pseudo codes and Rimg, Gimg and Bimg are just variables.

Numpy style :

Bimg = img[:,:,0]
Gimg = img[:,:,1]
Rimg = img[:,:,2]

OpenCV style :

B,G,R = cv2.split(img)

The other answers are correct, but this is a common enough operation that a one-liner can be idiomatic:

# Let `im` be a numpy array
r,g,b = im.transpose((2,0,1))

The transpose operator changes the axes around so that the first axis become the channel axis. Then you can use standard Python multiple assignment to assign to r , g , and b .

Assuming you load your data as a NumPy ndarray using any one of many image libraries in Python ( OpenCV , Mahotas , scikits.image , PIL ), then basically the exact same syntax as Matlab will work:

Rimg = img[:,:,0] 
Gimg = img[:,:,1] 
Bimg = img[:,:,2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM