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....
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With