Messing around with the camera input - made a small program which acts a bit like the “green screen” visual effect used in (older) TV/films.
Best to use with a stationary iPad - ideally on a stand. Tap the bottom right, but without standing in the field of view of the camera to capture the reference background then walk in front of the camera.a__
-- Magic Mirror
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
counter=0
cameraSource(CAMERA_FRONT)
refimg = image(CAMERA) --used as the background reference image
curimg=image(CAMERA) --used to store the current image from the camera
xres=32/4 --the size of the horizontal blocks - should divide into 1280 to leave an integer
yres=18/2 --the size of the vertical blocks - should divide into 720 to leave an integer
thresh=40. -- a threshold for detecting the difference between the background pixel and current pixel
dispimg=image(1280,720) -- used for the image to be displayed
bgimg=image(xres,yres) -- create a small image for use as the background blocks
setContext(bgimg)
background(0,0,200) -- currently set to blue but could be changed to anything
setContext()
--an alternative single background image. make the code substitution commemted on later too
--[[
backimg=image(1280,720)
setContext(backimg)
sprite("Cargo Bot:Codea Icon",WIDTH/2,HEIGHT/2,1280,720)
setContext()
]]--
end
-- This function gets called once every frame
function draw()
counter = counter + 1
text("Tap in bottom right to set the background reference image",WIDTH/2,HEIGHT/2)
if refimg~=nil then
--
if counter>0 then
counter=0
local curimg = image(CAMERA)
setContext(dispimg)
local xc=0
for i=1,1280,xres do
local yc=0
xc = xc + 1
for j=1,720,yres do
yc=yc+1
local r,g,b,a= curimg:get(i,j)
--check each reference point in turn - if the rgb values are comparable to the reference image then treat as background, otherwise treat as new and show that portion of the camera image
if math.abs(ref[xc][yc].r-tonumber(r))<thresh and math.abs(ref[xc][yc].g-tonumber(g))<thresh and math.abs(ref[xc][yc].b-tonumber(b))<thresh then
sprite(bgimg,i,j,xres,yres)
--substitute above with the following for a full single image as defined earlier
-- sprite(backimg:copy(i,j,xres,yres),i,j,xres,yres)
else
sprite(curimg:copy(i,j,xres,yres),i,j,xres,yres)
end
end
end
end
setContext()
sprite(dispimg,WIDTH/2,HEIGHT/2)
end
--camera button image
sprite("Cargo Bot:Crate Goal Yellow",WIDTH*0.95,HEIGHT*0.05,0.1*WIDTH,0.1*HEIGHT)
sprite("Cargo Bot:Record Solution Icon",WIDTH*0.95,HEIGHT*0.05,0.05*WIDTH,0.05*HEIGHT)
collectgarbage()
end
function touched(t)
if t.state==ENDED and t.x>0.9*WIDTH and t.y<0.1*HEIGHT then
--take new reference image
cameraSource(CAMERA_FRONT)
refimg = image(CAMERA)
--camera snapshot resolution from my instance is 1280,720
--generate a series of checkpoints and store the pixel color for comparison
ref={}
local xc=0
for i=1,1280,xres do
local yc=0
xc = xc + 1
ref[xc]={}
for j=1,720,yres do
local red,green,blue,alpha= refimg:get(i,j)
table.insert(ref[xc],{r=red,g=green,b=blue,a=alpha})
end
end
end
end