Main Content

read

Return image data captured with ideal camera

Since R2022b

    Description

    image = read(idealCamera) returns image data that is captured with the ideal camera specified by idealCamera in the simulation 3D environment.

    example

    Examples

    collapse all

    Create an ideal camera in the 3D environment using the sim3d.sensors.IdealCamera object. You can capture images of the 3D environment and display them in MATLAB®. Use the read function to extract image data from the 3D environment.

    Create a 3D environment and set up communication with the Unreal Engine® simulation environment using the output function OutputImpl and the update function UpdateImpl. The sim3d.World object can send and receive data about the 3D environment to and from the Unreal Engine at each simulation step using output and update functions, respectively. Before the Unreal Engine simulates, MATLAB calls the output function and sends data to the Unreal Engine. Then, the Unreal Engine executes at each time step and sends data to MATLAB in the update function. You can use the update function to read this data or change values after each simulation step.

    world = sim3d.World('Output',@outputImpl,'Update',@updateImpl);

    Create a box actor in the 3D environment using the sim3d.Actor object and add the box to the world.

    cube = sim3d.Actor( ...
            ActorName="Cube", ...
            Mobility=sim3d.utils.MobilityTypes.Movable);
    createShape(cube,"box");
    add(world,cube);

    Create an ideal camera object using sim3d.sensors.IdealCamera object and set the location of the ideal camera using the Translation property. Add the idealcamera to the world.

    idealCamera = sim3d.sensors.IdealCamera( ...
            ActorName="IdealCamera");
    idealCamera.Translation = [-3 0 0];
    add(world,idealCamera);

    Set the Simulation 3D Viewer window point of view and run the co-simulation.

    viewport = createViewport(world,Translation=[-3 0 0]);
    sampletime = 1/60;
    stoptime = 5;
    run(world,sampletime,stoptime);

    Figure contains an axes object. The axes object contains an object of type image.

    Output Function

    The output function sends data about the actor to the Unreal Engine environment at each simulation step. For this example, the function rotates the Cube abut its Z-axis by updating the Rotation property of the Cube at each simulation step.

    function outputImpl(world)
        world.Actors.Cube.Rotation(3) = world.Actors.Cube.Rotation(3) ...
        + 0.01; 
    end

    Update Function

    The update function reads data from the Unreal Engine environment at each simulation step. For this example, the update function uses the read function of the sim3d.sensors.IdealCamera object to get image data from the IdealCamera in the Unreal Engine and uses the image function to display it in MATLAB.

    function updateImpl(world)
        sceneImage = read(world.Actors.IdealCamera);
        image(sceneImage);
    end

    Input Arguments

    collapse all

    Virtual camera that captures the image, specified as a sim3d.sensors.IdealCamera object.

    Example: idealCamera = sim3d.sensors.IdealCamera

    Output Arguments

    collapse all

    Image data returned by camera, returned as an m-by-n-by-3 array of RGB triplet values. m is the vertical resolution of the image. n is the horizontal resolution of the image.

    Data Types: uint8

    Version History

    Introduced in R2022b