from opencv to cairo in python

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

from opencv to cairo in python

hv_francesco
i need to convert an opencv cvMat object (a frame grabbed from a
webcam), to a cairo imagesurface for a video library i'm writing for
shoebot (http://tinkerhouse.net/shoebot).
I'm using a numpy array as a buffer passed to
cairo.ImageSurface.create_for_data function
the chain of passages includes passing from cvMat to a PIL image, then
to numpy array (after swapping red and blue channels) and finally to
cairo imagesurface.
Can someone suggest a smarter/faster way to do it?
Is there a more direct way of transforming a cvMat to numpy array
without passing through PIL?
I think opencv and cairo both use BGR, so it's a bit nonsense to swap
b and r passing to PIL and then back to original scheme for cairo.
I'm trying to optimize my code for speed, but as you have probably
already guessed i'm a sort of newbie, any useful tip?
thanks in advance
francesco

Reply | Threaded
Open this post in threaded view
|

Re: from opencv to cairo in python

hv_francesco
this is a test code i'm using in order to pass from opencv to cairo,
actually it seems to basically work, when i use the cairo surface,
image is shown and color channels seem to be right, but values are
altered and totally out of range. what am i doing wrong?
any help would be greatly appreciated.

import opencv, numpy, cairo
from opencv import highgui as hg
im =
hg.cvLoadImage('/home/francesco/build/shoebot/assets/hva_studio_3.jpg')
im2 = opencv.cvCreateImage(opencv.cvGetSize(im),8, 4)
opencv.cvCvtColor(im,im2,opencv.CV_BGR2BGRA)
buffer =
numpy.fromstring(im2.imageData,dtype=numpy.uint32).astype(numpy.float32)
#buffer.shape=(im2.width, im2.height)
imagesurface=cairo.ImageSurface.create_for_data(buffer,cairo.FORMAT_ARGB32,im2.width,im2.height)