Friday, June 19, 2009

3D Anaglyph Python Code

If you have two cameras plugged in and everything installed and supported then this code might work to provide a 3D Red/Cyan video stream.

The extensive comments and documentation are provided as part of outsourcing the development of this code.

from opencv import cv
from opencv import highgui
from time import sleep

def makeMagic(left, right, out):
chans=[]
for i in range(6):
chans.append(cv.cvCreateImage(cv.cvGetSize(left),8,1))
cv.cvSplit(left, chans[0], chans[1], chans[2], None);
cv.cvSplit(right, chans[3], chans[4], chans[5], None);
cv.cvMerge(chans[3],chans[4],chans[2], None, out);

#cv.cvMerge(None,chans[1],None, None, out);

cam=[]
def main():
cam.append(highgui.cvCreateCameraCapture(0))
cam.append(highgui.cvCreateCameraCapture(1))
highgui.cvNamedWindow ("carrots", highgui.CV_WINDOW_AUTOSIZE)
uno=highgui.cvQueryFrame(cam[0]);
dos=highgui.cvQueryFrame(cam[1]);
highgui.cvShowImage("carrots",uno);
highgui.cvWaitKey(0);
highgui.cvShowImage("carrots",dos);
highgui.cvWaitKey(0);
merge=cv.cvCreateImage(cv.cvGetSize(uno),8,3)
makeMagic(uno, dos, merge)
highgui.cvShowImage("carrots",merge);
highgui.cvWaitKey(0);
while True :
uno=highgui.cvQueryFrame(cam[0]);
dos=highgui.cvQueryFrame(cam[1]);
makeMagic(uno, dos, merge);
highgui.cvShowImage("carrots",merge);
if highgui.cvWaitKey(1)=="s":
cam.append(cam.pop(0))
print "tick"

if __name__=="__main__":
main()


I broke the wires on one of the cameras so there is no video today.

2 comments:

Charles said...

Got a question, on what hardware shall I run the python code?

I think to run the python code, a microchip and flash memory should be somewhere in this system.

I Heart Robotics said...

The code was setup to run on an Ubuntu desktop with OpenCV installed.

I saw it run once with the dual USB web cam setup, however I have not been able to get it to work again on another machine.

Part of the problem appears to be driver related as the cameras I'm using are really old and don't support video4linux 2. I'll update the code once I get it fixed or change cameras.

I'm moving the new setup to dual Quickcam 9000 pros.

You can probably run the image processing on an FPGA but the Linux based Gumstix seems easier to deal with.