Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting Image using OpenCV in python

Tags:

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

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

like image 350
Sagar Avatar asked Oct 04 '13 12:10

Sagar


People also ask

How do you split an image in OpenCV in Python?

Splitting Channelssplit() is used to split coloured/multi-channel image into separate single-channel images. The cv2. split() is an expensive operation in terms of performance(time). The order of the output vector of arrays depends on the order of channels of the input image.

How does cv2 split work?

The cv2. split() function splits the source multichannel image into several single-channel images. The cv2. merge() function merges several single-channel images into a multichannel image.


2 Answers

That is as simple as loading an image using cv2.imread and then use 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

like image 53
jabaldonedo Avatar answered Oct 27 '22 18:10

jabaldonedo


As mentioned in the documentation tutorial, the cv2.split() is a costly operation in terms of performance(time) if you don't want to operate on all the channels but only one/two, so the numpy indexing is preferred:

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

Edit: @Mitch McMabers, Thanks for pointing this out. Please use this method for max efficiency if you want to work on just one/two channels separately. If you want to operate on all three channels, access the channels using cv2.split() as mentioned in @jabaldeno's answer.

like image 40
Mohit Motwani Avatar answered Oct 27 '22 19:10

Mohit Motwani