PDA

View Full Version : Flip WebCam input Horizontally


goro
05-20-2012, 11:03 PM
Hi,
I am new to vizard working on Augmented Reality. I am trying to flip my Web Camera input horizontally. I am successfully flipping it but my system performance is going down. Here is my code:

import viz
import vizact
viz.go()

video = viz.add('VideoCamera.dle')
ar = viz.add('ARToolKit.dle')

cam = video.addWebcam(type=viz.TEX_2D)
width, height, type = cam.getSize()
flipCam = viz.addBlankTexture([width,height])
width_3 = width*3

camera = ar.addCamera(flipCam)
dataCam = cam.getImageData()
dataFlipCam = flipCam.getImageData()

def mainLoop():
for y in range(0,height-1):
for x in range(0,width-1):
dataFlipCam[(3*x+y*width_3)] = dataCam[(3*(width-1-x)+y*width_3)]
dataFlipCam[(3*x+1+y*width_3)] = dataCam[(3*(width-1-x)+1+y*width_3)]
dataFlipCam[(3*x+2+y*width_3)] = dataCam[(3*(width-1-x)+2+y*width_3)]

flipCam.hint(viz.TEXTURE_MODIFIED_HINT)

vizact.ontimer(0,mainLoop)

Please suggest me alternate.

Thanks!

goro
05-22-2012, 01:28 AM
I have found the optimised way to flip the Image, Video and Web cam Input. Flipping is now not an issue but the issue is while flipping video & web cam images the colour changes.

This Image will illustrate what happens when I flip video:
http://i438.photobucket.com/albums/qq105/gaurav_kudalkar/Mona.jpg

Code for the above mentioned flipping video image

import viz
import vizact
import Image
viz.go()

def PIL_TO_VIZARD(texture):
image = Image.fromstring('RGB',[width,height],cam.getImageData())
"""Copy the PIL image to the Vizard texture"""
im = image.transpose(Image.FLIP_LEFT_RIGHT)
texture.setImageData(im.convert('RGB').tostring(), im.size)

#Create a blank Vizard texture
tex = viz.addBlankTexture([1,1])

cam = viz.addVideo('mona.mpg')
cam.play()
cam.loop()

width,height,type = cam.getSize()


#---------------------------- Texture Comparison -----------------------------#
quad1 = viz.addTexQuad()
quad1.setPosition([-.75, 2, 3])

quad1.texture(cam)

quad2 = viz.addTexQuad()
quad2.setPosition([.75, 2, 3])
quad2.texture(tex)

#-----------------------------------------------------------------------------#

vizact.ontimer(0,PIL_TO_VIZARD,tex)

Thanks!

farshizzo
05-22-2012, 08:30 AM
Do you actually need the image data to be flipped, or do you just want to display the texture flipped? If you just want to display the texture flipped horizontally, then simply apply a negative scale on the texture quad:quat.setScale([-1,1,1])

goro
05-22-2012, 10:38 AM
Thanks farshizzo for the reply. I actually need the Image data to be flipped. I am working on Augmented Reality Project & I want to create a Mirror. If I flipped the quad containing the Image then the marker wont get flipped, so the co-ordination between image & logo gets lost and mismatch happens. To overcome this issue I need help. I have solved the colour issue in flipping video image by modifying the code as shown below & got the output as shown in below image:

http://i438.photobucket.com/albums/qq105/gaurav_kudalkar/Mona_1.jpg

import viz
import vizact
import Image
viz.go()

def PIL_TO_VIZARD(texture):
image = Image.fromstring('RGB',[width,height],cam.getImageData())
"""Copy the PIL image to the Vizard texture"""
im = image.transpose(Image.FLIP_LEFT_RIGHT)
RGB2BGR = (
0.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0 )
texture.setImageData(im.convert('RGB',RGB2BGR).tos tring(),im.size)

#Create a blank Vizard texture
tex = viz.addBlankTexture([1,1])

cam = viz.addVideo('mona.mpg')
cam.play()
cam.loop()

width,height,type = cam.getSize()


#---------------------------- Texture Comparison -----------------------------#
quad1 = viz.addTexQuad()
quad1.setPosition([-.75, 2, 3])

quad1.texture(cam)

quad2 = viz.addTexQuad()
quad2.setPosition([.75, 2, 3])
quad2.texture(tex)

#-----------------------------------------------------------------------------#

vizact.ontimer(0,PIL_TO_VIZARD,tex)

goro
05-22-2012, 10:59 AM
I working on ARToolKit & Successfully flipped the Webcam Image. But now the issue is Marker is not getting detected in flipped texture.

Here is the code:

import viz
import vizact
import Image
viz.go()

#------------------------ Flip Horizontal ------------------------------#
def FlipHorizontal(cam_tex,texture):
image = Image.fromstring('RGB',[width,height],cam_tex.getImageData())
"""Copy the PIL image to the Vizard texture"""
im = image.transpose(Image.FLIP_LEFT_RIGHT)
RGB2BGR = (
0.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0 )
texture.setImageData(im.convert('RGB',RGB2BGR).tos tring(),im.size)

#Create a blank Vizard texture
tex = viz.addBlankTexture([1,1],format=viz.TEX_BGR)

#Load Video Camera
video = viz.add('VideoCamera.dle')
# Load Web Camera & set cam as Video Texture
cam = video.addWebcam()

#Load ARToolKit
ar = viz.add('ARToolKit.dle')

width,height,type = cam.getSize()

# Flip Video Texture Horizontally
vizact.ontimer(0,FlipHorizontal,cam,tex)

# Set ARToolKit Camera with cam as texture
camera = ar.addCamera(tex) #---- This Dosn't work
#camera = ar.addCamera(cam) #---- This works
camera.setDebug(True)

# Add AR Marker
marker = camera.addMarker('ar/patt.hiro',width = 1000.00) #patt.hiro or patt.hiro_fh

#Add logo model
logo = viz.add('logo.ive')

#Link logo model to marker sensor
link = viz.link(marker,logo)

farshizzo
05-29-2012, 09:20 AM
You need to flip the image before ARToolkit processes the camera image. Try scheduling the FlipHorizontal function with a lower update priority using the following code, instead of using vizact.ontimer:vizact.onupate(viz.PRIORITY_PLUGINS ,FlipHorizontal,cam,tex)

goro
05-31-2012, 09:50 PM
Nope.. Its not working... :(
I have even tried to set priority values from -1000 to 1000.

farshizzo
06-04-2012, 09:40 AM
Is the incoming raw video data flipped or are you flipping it for another reason? If you are artificially flipping the data and the marker you are tracking is not horizontally symmetrical, then ARToolkit will fail to track the marker.

goro
06-04-2012, 12:13 PM
My webcam is giving real image & not the flipped image. But my application requirement is my customer should see him on screen as he is seeing him in mirror & marker should also respond in the same way. So can you tell me any alternate method so I can flip my webcam input out and out before applying it to artoolkit?

goro
06-05-2012, 06:07 AM
This simple code flips the texture but it doesn't solve the purpose..
import viz
viz.go()

ar = viz.add('artoolkit.dle')
camera = ar.addWebCamera()

camera.background.setEuler([180,0,0])
camera.background.setPosition([640,0,0])

marker = camera.addMarker('ar/patt.worldviz',width = 1000)

logo = viz.add('logo.ive')

viz.link(marker,logo)

Marker positions miss match with the image if we go with this code.

farshizzo
06-05-2012, 03:52 PM
You will need to "flip" the position/rotation values of the markers as well. You can use the link.swapPos and link.swapEuler commands to accomplish this. Here is some sample code:import viz
viz.go()

#Add ARToolkit extension
ar = viz.add('artoolkit.dle')

#Create camera using first available webcam
camera = ar.addWebCamera()

#Flip background quad
camera.background.setEuler([180,0,0])
camera.background.setPosition([camera.texture.getSize()[0],0,0])

#Create a matrix marker with the specified id
marker = camera.addMatrixMarker(0,width=1000)

#Add logo model
logo = viz.add('logo.ive')

#Link logo model to marker sensor
link = viz.link(marker,logo)

#Swap X position and yaw,roll of euler rotation
link.swapPos([-1,2,3],target=viz.LINK_POS_OP)
link.swapEuler([-1,2,-3],target=viz.LINK_ORI_OP)

goro
06-06-2012, 12:15 AM
Thanks friend! Its working. :)