[英]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. 我目前是python的新手,我想从图像中分别提取R,G和B帧。
For instance, the variable which stores my image is img 例如,存储我的图像的变量是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. 我如何使
Rimg = img (:,:,1)
Gimg = img (:,:,2)
Bimg = img (:,:,3)
当然,这些是MATlab伪代码,Rimg,Gimg和Bimg只是变量。
Numpy style : 脾气暴躁的风格:
Bimg = img[:,:,0]
Gimg = img[:,:,1]
Rimg = img[:,:,2]
OpenCV style : OpenCV样式:
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. transpose
运算符围绕更改轴,以使第一个轴成为通道轴。 Then you can use standard Python multiple assignment to assign to r
, g
, and b
. 然后,您可以使用标准的Python多重分配来分配给
r
, g
和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: 假设您使用Python中的许多图像库( OpenCV , Mahotas , scikits.image , PIL )中的任何一个将数据作为NumPy
ndarray
加载,那么基本上可以使用与Matlab完全相同的语法:
Rimg = img[:,:,0]
Gimg = img[:,:,1]
Bimg = img[:,:,2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.