Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python openCV debayer

Tags:

python

opencv

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)
cv2.imshow('image',resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

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)
cv2.imshow('image',resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

THX a lot for help....

like image 222
rainer Avatar asked Mar 10 '14 18:03

rainer


2 Answers

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.

like image 77
Sameer Avatar answered Oct 18 '22 06:10

Sameer


Try this:

import os
import cv2

imagePath = '/path/to/image'
imageRaw = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE | cv2.IMREAD_ANYDEPTH)

rgb = cv2.cvtColor(imageRaw, cv2.COLOR_BAYER_BG2BGR)
cv2.imwrite('rgb.png', rgb)
like image 45
RawMean Avatar answered Oct 18 '22 07:10

RawMean