Ask Your Question
2

How to split cv2.imread to 3 separate mats

asked 2012-11-04 13:18:29 -0500

OCR gravatar image

updated 2012-11-04 23:58:51 -0500

it is a newbie question, please, what's wrong with this code:

import cv2 
import cv2.cv as cv 

img = cv2.imread(fn)
cvImg = cv.fromarray(img)
#~ rgba = cv.CreateMat(cvImg.width, cvImg.height , cv.CV_8UC4)

sb = cv.CreateMat(cvImg.width, cvImg.height , cv.CV_8UC1)
sg = cv.CreateMat(cvImg.width, cvImg.height , cv.CV_8UC1)
sr = cv.CreateMat(cvImg.width, cvImg.height , cv.CV_8UC1)
sa = cv.CreateMat(cvImg.width, cvImg.height , cv.CV_8UC1)
cv.Split(cvImg,sb,sg,sr,sa)
"""
Traceback (most recent call last):
  File "squares2.py", line 188, in <module>
    cv.Split(cvImg,sb,sg,sr,sa)
cv2.error: dvec[j].size() == src.size()
"""
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2012-11-04 14:19:12 -0500

updated 2012-11-04 14:20:23 -0500

First read in the image:

import cv2
img = cv2.imread("/path/to/your/image.jpg")

Then using cv2.split is as easy as doing:

(channel_b, channel_g, channel_r) = cv2.split(img)

Which is equivalent to saying:

(channel_b, channel_g, channel_r) = (img[:,:,0], img[:,:,1], img[:,:,2])
edit flag offensive delete link more
2

answered 2012-11-05 06:36:49 -0500

Daniil Osokin gravatar image

To read RGBA image as 4-channel image, you should additionally pass a flag to imread:

cv2.imread("path", -1);
as it described in imread manual.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2012-11-04 13:18:29 -0500

Seen: 7,466 times

Last updated: Nov 05 '12