.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/renderblender/ex3_1_blendercalibration.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_renderblender_ex3_1_blendercalibration.py: Blender example: Rendering calibration images --------------------------------------------- This example takes you through how to render calibration images for a given DIC setup. .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: Python import numpy as np from scipy.spatial.transform import Rotation from pathlib import Path import pyvale .. GENERATED FROM PYTHON SOURCE LINES 20-25 Firstly, a save path must be set. In order to do this a base path must be set. Then all the generated files will be saved to a subfolder within this specified base directory (e.g. blenderimages). If no base directory is specified, it will be set as your home directory. .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python base_dir = Path.cwd() .. GENERATED FROM PYTHON SOURCE LINES 29-34 Creating the scene ^^^^^^^^^^^^^^^^^^ In order to create a DIC setup in Blender, first a scene must be created. A scene is initialised using the `BlenderScene` class. All the subsequent objects and actions necessary are then methods of this class. .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python scene = pyvale.BlenderScene() .. GENERATED FROM PYTHON SOURCE LINES 37-41 The next thing to add to the scene is the calibration target. This is done by specifing the size of calibration target to add to the scene by passing in an array of (width, height, depth). The calibration target being simulated here is 12 x 9 with 10 mm spacing. .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: Python target = scene.add_cal_target(target_size=np.array([150, 100, 10])) .. GENERATED FROM PYTHON SOURCE LINES 45-60 The cameras can then be initialised. A stereo camera system is defined by a `CameraStereo` object, which contains the intrinsic parameters of both cameras as well as the extrinsic parameters between them. There are two ways to initialise a `CameraStereo` object. One way is to specify the camera parameters separately for each camera, create a `CameraStereo` object, and then add the stereo system using the `add_stereo_system` method. The other method is to use a convenience function, as shown below. This requires you to first initialise one camera. Then you can choose between either a face-on or symmetric stereo system. Then, either of the `symmetric_stereo_cameras` or `faceon_stereo_cameras` functions can be used to initialise a `CameraStereo` object. The only input required to these functions are the camera parameters for the first camera, and the desired stereo angle between the two. The cameras can then be added to the Blender scene using the `add_stereo_system` method. .. GENERATED FROM PYTHON SOURCE LINES 60-79 .. code-block:: Python cam_data_0 = pyvale.CameraData(pixels_num=np.array([1540, 1040]), pixels_size=np.array([0.00345, 0.00345]), pos_world=np.array([0, 0, 400]), rot_world=Rotation.from_euler("xyz", [0, 0, 0]), roi_cent_world=(0, 0, 0), focal_length=15.0) # Set this to "symmetric" to get a symmetric stereo system or set this to # "faceon" to get a face-on stereo system stereo_setup = "faceon" if stereo_setup == "symmetric": stereo_system = pyvale.CameraTools.symmetric_stereo_cameras( cam_data_0=cam_data_0, stereo_angle=15.0) if stereo_setup == "faceon": stereo_system = pyvale.CameraTools.faceon_stereo_cameras( cam_data_0=cam_data_0, stereo_angle=15.0) scene.add_stereo_system(stereo_system) .. GENERATED FROM PYTHON SOURCE LINES 80-91 Since this scene contains a stereo DIC system, a calibration file will be required to run the images through a DIC engine. A calibration file can be generated directly from the `CameraStereo` object. The calibration file will be saved in `YAML` format. However, if you wish to use MatchID to process the images, `save_calibration_mid` can be used instead to save the calibration in a format readable by MatchID. The calibration file will be saved to a sub-directory of the base directory called "calibration". This calibration file with "perfect" parameters can be used as a comparitive benchmark to the calibration gained from running the calibration files through a DIC engine. .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: Python stereo_system.save_calibration(base_dir) .. GENERATED FROM PYTHON SOURCE LINES 94-97 A light can the be added to the scene. Blender offers different light types: Point, Sun, Spot and Area. The light can also be moved and rotated like the camera. .. GENERATED FROM PYTHON SOURCE LINES 97-107 .. code-block:: Python light_data = pyvale.BlenderLightData(type=pyvale.BlenderLightType.POINT, pos_world=(0, 0, 200), rot_world=Rotation.from_euler("xyz", [0, 0, 0]), energy=1) light = scene.add_light(light_data) light.location = (0, 0, 210) light.rotation_euler = (0, 0, 0) # NOTE: The default is an XYZ Euler angle .. GENERATED FROM PYTHON SOURCE LINES 108-114 The calibration target pattern can then be added to the calibration target object. This is added in the same way that a speckle pattern is added to a sample. However, it is important to set the `cal` flag to True, as this means that the calibration target pattern will not be scaled in the same way as a speckle pattern. .. GENERATED FROM PYTHON SOURCE LINES 114-124 .. code-block:: Python material_data = pyvale.BlenderMaterialData() speckle_path = Path.cwd() / "src/pyvale/data/cal_target.tiff" mm_px_resolution = pyvale.CameraTools.calculate_mm_px_resolution(cam_data_0) scene.add_speckle(part=target, speckle_path=speckle_path, mat_data=material_data, mm_px_resolution=mm_px_resolution, cal=True) .. GENERATED FROM PYTHON SOURCE LINES 125-130 Rendering a set of images ^^^^^^^^^^^^^^^^^^^^^^^^^ Once all the objects have been added to the scene, a set of images can be rendered.Firstly, all the rendering parameters must be set, including parameters such as the number of threads to use. .. GENERATED FROM PYTHON SOURCE LINES 130-135 .. code-block:: Python render_data = pyvale.RenderData(cam_data=(stereo_system.cam_data_0, stereo_system.cam_data_1), base_dir=base_dir) .. GENERATED FROM PYTHON SOURCE LINES 136-143 The parameters for the calibration target's movement can then be set. This is done by setting the minimum and maximum angle and plunge limits, as well as the step value that they should be increased by. The x and y limit of the calibration target's movement (from the origin) can also be set if you wish to perform a calibration for a constrained optical setup. If these limits are not passed in they will be initialised from the FOV to cover the whole FOV of the cameras. .. GENERATED FROM PYTHON SOURCE LINES 143-149 .. code-block:: Python calibration_data = pyvale.CalibrationData(angle_lims=(-10, 10), angle_step=5, plunge_lims=(-5, 5), plunge_step=5) .. GENERATED FROM PYTHON SOURCE LINES 150-153 It is then possible to check the number of calibration images that will be rendered before rendering them. The only input that is needed is the `calibration_data` specified above. .. GENERATED FROM PYTHON SOURCE LINES 153-157 .. code-block:: Python number_calibration_images = pyvale.BlenderTools.number_calibration_images(calibration_data) print("Number of calibration images to be rendered:", number_calibration_images) .. GENERATED FROM PYTHON SOURCE LINES 158-162 The calibration images can then be rendered. This function will move the calibration target according to movement limits set above, and will also move the target rigidly across the FOV of the camera, in order to characterise the entire FOV of the cameras. .. GENERATED FROM PYTHON SOURCE LINES 162-166 .. code-block:: Python pyvale.BlenderTools.render_calibration_images(render_data, calibration_data, target) .. GENERATED FROM PYTHON SOURCE LINES 167-168 The rendered images will be saved to this filepath: .. GENERATED FROM PYTHON SOURCE LINES 168-171 .. code-block:: Python print("Save directory of the images:", (render_data.base_dir / "calimages")) .. GENERATED FROM PYTHON SOURCE LINES 172-174 There is also the option to save the scene as a Blender project file. This file can be opened with the Blender GUI to view the scene. .. GENERATED FROM PYTHON SOURCE LINES 174-176 .. code-block:: Python pyvale.BlenderTools.save_blender_file(base_dir) .. _sphx_glr_download_examples_renderblender_ex3_1_blendercalibration.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ex3_1_blendercalibration.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ex3_1_blendercalibration.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ex3_1_blendercalibration.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_