在python中使用OpenCV分割图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19181485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:03:59  来源:igfitidea点击:

Splitting Image using OpenCV in python

pythonopencvpython-2.7

提问by Sagar

I have aN image and want to split it into three RGB channel images using CV2 in python.

我有一个图像,想在 python 中使用 CV2 将其拆分为三个 RGB 通道图像。

I also want the good documentation where i can find the all function of openCV as I am new to OpenCV completely.

我还想要很好的文档,我可以在其中找到 openCV 的所有功能,因为我完全是 OpenCV 的新手。

采纳答案by jabaldonedo

That is as simple as loading an image using cv2.imreadand then use cv2.split:

这就像使用cv2.imread然后使用加载图像一样简单cv2.split

>>> import cv2
>>> import numpy as np
>>> img = cv2.imread("foo.jpg")
>>> b,g,r = cv2.split(img)

OpenCV documentation is available from docs.opencv.org

OpenCV 文档可从docs.opencv.org 获得

回答by Mohit Motwani

As mentioned in the documentation tutorial, the cv2.split() is a costly operation in terms of performance(time) so the numpy indexing is preferred:

正如文档教程中所提到的,cv2.split() 在性能(时间)方面是一项代价高昂的操作,因此首选 numpy 索引:

import cv2
import numpy as np
img = cv2.imread("foo.jpg")
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]

Remember that opencv reads the images as BGR instead of RGB

请记住,opencv 将图像读取为 BGR 而不是 RGB