Drawing a line into a vision sensor image

Typically: "How do I... ", "How can I... " questions
Post Reply
tkh
Posts: 1
Joined: 24 Mar 2020, 20:01

Drawing a line into a vision sensor image

Post by tkh » 25 Mar 2020, 13:03

I am using a blob detection for detecting objects and I would like to mark the centre of the object with a cross by drawing 2 short lines in the image of the vision sensor. The follwing code runs, but the cross is not drawn. Any help?

function sysCall_init()
sensor=sim.getObjectAssociatedWithScript(sim.handle_self)
imgHandle=simIM.readFromVisionSensor(sensor)
center={64,64}

end
function sysCall_actuation()
res, pack1, pack2 = sim.readVisionSensor(sensor)
center={64,64}
simIM.line(imgHandle, {center[1]-40, center[2]}, {center[1]+40, center[2]}, {100,100,100})
simIM.line(imgHandle, {center[1], center[2]-40}, {center[1], center[2]+40}, {100,100,100})
simIM.writeToVisionSensor(imgHandle,sensor)
.....

fferri
Posts: 556
Joined: 09 Sep 2013, 19:28

Re: Drawing a line into a vision sensor image

Post by fferri » 25 Mar 2020, 13:59

Following code works. The vision sensor is in "explicit handling" mode.

Code: Select all

function sysCall_init()
    sensor=sim.getObjectAssociatedWithScript(sim.handle_self)
    center={x=64,y=64}; color={100,100,100}
end
function sysCall_actuation()
    sim.handleVisionSensor(sensor)
    imgHandle=simIM.readFromVisionSensor(sensor)
    simIM.line(imgHandle, {center.x-40, center.y}, {center.x+40, center.y}, color)
    simIM.line(imgHandle, {center.x, center.y-40}, {center.x, center.y+40}, color)
    simIM.writeToVisionSensor(imgHandle, sensor)
    simIM.destroy(imgHandle)
end

coppelia
Site Admin
Posts: 7706
Joined: 14 Dec 2012, 00:25

Re: Drawing a line into a vision sensor image

Post by coppelia » 26 Mar 2020, 10:14

Or the same via the vision callback function (the below script should be attached to the vision sensor):

Code: Select all

function sysCall_init()
    color={100,100,100}
end

function sysCall_vision(inData)
    if simIM then
        local imgHandle=simIM.readFromVisionSensor(inData.handle)
        local center={x=inData.resolution[1]/2,y=inData.resolution[2]/2}
        simIM.line(imgHandle, {center.x-40, center.y}, {center.x+40, center.y}, color)
        simIM.line(imgHandle, {center.x, center.y-40}, {center.x, center.y+40}, color)
        simIM.writeToVisionSensor(imgHandle, inData.handle)
        simIM.destroy(imgHandle)
    end
    outData={}
    outData.trigger=false -- whether the sensor should trigger
    outData.packedPackets={}
    return outData
end
Cheers

Post Reply