rainer rainer - 6 months ago 52
Python Question

python openCV debayer

I want to debayer a 16bit RAW still image with openCV, but have problems with the cvtColor function.
Color to Gray gives a correct result with this:

import cv2
import numpy as np
infile = '/media/rainer/IMG_2806.JPG'

img = cv2.imread(infile,1)
bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
resized = cv2.resize(bw, (0,0), fx=0.3, fy=0.3)

So how would the debayer look like in python 2.7?
this is not working:

infile = '/media/rainer/test.raw'

img = cv2.imread(infile,0)
debayer = cv2.cvtColor(img, cv2.CV_BayerBG2BGR)
resized = cv2.resize(debayer, (0,0), fx=0.3, fy=0.3)

THX a lot for help....


The problem is that OpenCV doesn't know the data type and size of the raw image that you are trying to load. You have to specify that through Numpy, since OpenCV arrays are Numpy arrays in Python. Try this: import numpy as np imsize = imrows*imcols with open(infile, "rb") as rawimage: img = np.fromfile(rawimage, np.dtype('u1'), imsize).reshape((imrows, imcols)) colour = cv2.cvtColor(img, cv2.COLOR_BAYER_BG2BGR)

Use np.dtype('u2') for 16 bpp images. Also note that you need cv2.COLOR_BAYER_BG2BGR instead of cv2.CV_BayerBG2BGR.