I am detecting the marker using OpenCV from an external python script. Now, using the python script I can obtain the coordinates of the center of my marker. These coordinates are obviously in image plane. In order to transform these coordinates from the image plane to global coordinates, I went through the model provided at Models/components/sensors/Camera pixels to 3D positions.ttm. Following the method suggested in this model, I have added a non threaded child script to my vision sensor. Here is the code for it -
Code: Select all
-- lua
function sysCall_init()
sim = require('sim')
gimbal_camera = sim.getObject('.')
resX=sim.getObjectInt32Param(gimbal_camera,sim.visionintparam_resolution_x)
resY=sim.getObjectInt32Param(gimbal_camera,sim.visionintparam_resolution_y)
xAngle=sim.getObjectFloatParam(gimbal_camera,sim.visionfloatparam_perspective_angle)
yAngle=xAngle
local ratio=resX/resY
if resX>resY then
yAngle=2*math.atan(math.tan(xAngle/2)/ratio)
else
xAngle=2*math.atan(math.tan(yAngle/2)/ratio)
end
end
function sysCall_actuation()
-- put your actuation code here
end
function sysCall_sensing(coord) -- coord is being supplied by my external python script.
if coord ~= nil then
local m=sim.getObjectMatrix(gimbal_camera)
local depthMap=sim.getVisionSensorDepth(gimbal_camera,1)
depthMap=sim.unpackFloatTable(depthMap)
-- Obtain depth value information from depth man
local index = coord[1] * resY + coord[2] + 1
local depthValue = depthMap[index]
-- Convert coordinates from image plane to camera frame of reference
local cameraCoord = {0, 0, depthValue}
cameraCoord[1] = depthValue * math.tan(xAngle * 0.5) * (0.5 - (coord[1] / (resX - 1))) / 0.5
cameraCoord[2] = depthValue * math.tan(yAngle * 0.5) * ((coord[2] / (resY - 1)) - 0.5) / 0.5
-- Transform 3D Camera Coordinates to Global Coordinates
local globalCoord = sim.multiplyVector(m, cameraCoord)
local actualCoord = sim.getObjectPosition(54, sim.handle_world) -- 54 is the handle of the landing surface.
print(globalCoord, 'transformed coordinates', actualCoord, 'actual coordinates')
end
end
function sysCall_cleanup()
-- do some clean-up here
end
-- See the user manual or the available code snippets for additional callback functions and details
Any kind of assistance or guidance will be quite helpful! Thank you in advance!