demosaicing of image

389 views
Skip to first unread message

cody dooderson

unread,
Dec 26, 2013, 1:21:05 PM12/26/13
to pydc...@googlegroups.com
Hello; 
My question concerns using color a color camera. I am using a Point Grey Firefly MV color camera. The image is being delivered bayer encoded. I wrote a quick python decoder for it and this is what I got. 

 --> 

The problem is that it is slow and it makes the image smaller. Does anybody know how to access dc1394's demosaicing functions, and or use opencv or numpy to make this a bit faster?

Here is my code, (it's based mostly on one of the examples)

#
# This file is part of pydc1394.
# pydc1394 is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# pydc1394 is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with pydc1394.  If not, see
# <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009, 2010 by Holger Rapp <Holge...@gmx.net>
# and the pydc1394 contributors (see README File)


import sys
from time import sleep
from pydc1394 import DC1394Library, Camera
import Image
import matplotlib


l = DC1394Library()
cams = l.enumerate_cameras()
cam0_handle = cams[0]

print "Opening camera!"
cam0 = Camera(l, cam0_handle['guid'])


#those can be automated, the other are manual
try:
    cam0.brightness.mode = 'auto'
    cam0.exposure.mode = 'auto'
    cam0.white_balance.mode = 'auto'
except AttributeError: # thrown if the camera misses one of the features
    pass

for feat in cam0.features:
    print "feat: %s (cam0): %s" % (feat,cam0.__getattribute__(feat).val)

#choose color mode
print cam0.modes
cam0.mode = cam0.modes[0]

if len(sys.argv) > 1:
    cam0.start(interactive=False)
    sleep(0.5) #let hardware start !
    matrix = cam0.shot()
    cam0.stop()
else:
    cam0.start(interactive=True)
    sleep(0.5) #let hardware start !
    matrix = cam0.current_image
    cam0.stop()

print "Shape:", matrix.shape
i = Image.fromarray(matrix)
i.save("mosaic.bmp")

print('demosaicing from RGGB format')
im = Image.new('RGB',  (matrix.shape[1]/2, matrix.shape[0]/2))
px = im.load()
for y in range( 0, matrix.shape[0], 2):
    for x in range( 0, matrix.shape[1], 2):
        r = matrix[y][x]
        g = matrix[y][x+1]/2 + matrix[y+1][x]/2
        b = matrix[y+1][x+1]
        px[x/2 , y/2] = (r,g,b)

print('saving'  )        
im.save('rgb.png')

 





Gergely Imreh

unread,
Dec 31, 2013, 9:41:36 PM12/31/13
to pydc...@googlegroups.com
Hey Cody,

Would the demosaicing algorithm that does this for you, one of the functions on this page?
http://damien.douxchamps.net/ieee1394/libdc1394/api/conversions/
I guess the Bayer decoding ones?

I think all the functions are declared in "_dc1394core",  but not used in the rest of the library. You could probably use it directly from the _dc1394core maybe?

If you have some example of the actual BMP file you record, could you send a link to it? (not attach to the email, just share via Dropbox or similar)

Cheers,
   Greg


--
You received this message because you are subscribed to the Google Groups "pydc1394" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydc1394+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

cody dooderson

unread,
Jul 3, 2014, 3:45:19 PM7/3/14
to pydc...@googlegroups.com
Do you think you could point me in the right direction. I noticed that there are a lot of bayer encoding enums in the _dc1394core.py file. I'm just no really sure how to go about using those?
Message has been deleted

cody dooderson

unread,
Jul 4, 2014, 3:30:29 PM7/4/14
to pydc...@googlegroups.com
I think I found a more efficient way to de-mosaic using numpy. This is not the correct way, but it gives ok results and is fast. 
def demosaic_super_simple( m ):
    r = m[0::2, 0::2] #gets every other row and column
    g =  (( m[0::2, 1::2]/2 + m[1::2, 0::2]/2)).clip(0,255)
    b = m[1::2, 1::2]
    ms = np.dstack([r,g,b])#stacks 3 2d arrays into one 3d array
    return ms
Where m is the 2d matrix in RGGB Bayer encoding. 
Reply all
Reply to author
Forward
0 new messages