|
| 1 | +--- |
| 2 | +title: "Example 1: Creating a New ML Module for Adding a Value to Each Voxel" |
| 3 | +date: 2026-03-15T08:56:33+02:00 |
| 4 | +status: "OK" |
| 5 | +draft: false |
| 6 | +weight: 602 |
| 7 | +tags: ["Advanced", "Tutorial", "Image Processing", "C++"] |
| 8 | +menu: |
| 9 | + main: |
| 10 | + identifier: "cpp1" |
| 11 | + title: "Example 1: Creating a New ML Module for Adding a Value to Each Voxel" |
| 12 | + weight: 602 |
| 13 | + parent: "cpp" |
| 14 | +--- |
| 15 | + |
| 16 | +# Example 1: Creating a New ML Module for Adding a Value to Each Voxel |
| 17 | + |
| 18 | +## Precondition |
| 19 | +Make sure to have [cmake](https://cmake.org/download) installed. This example has been created using CMake Legacy Release (3.31.11). |
| 20 | + |
| 21 | +## Introduction |
| 22 | +In this example, we develop our own C++ ML module, which adds a constant value to each voxel of the given input image. |
| 23 | + |
| 24 | +## Steps to Do |
| 25 | +### Create a New ML Module |
| 26 | +Before creating the module, make sure to have your own user package available. See [Package creation](tutorials/basicmechanisms/macromodules/package/) for details about Packages. |
| 27 | + |
| 28 | +Use the *Project Wizard* via the menu entry {{< menuitem "File" "Run Project Wizard ..." >}} to create a new ML module. Select *ML Module* and click *Run Wizard*. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +Enter properties of your new module and give your module the name `SimpleAdd`. Make sure to select your user package and name your project *SimpleAdd*. |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + Click *Next*. The next screen of the Wizard allows you to define the inputs and outputs of your module. Select *Module Type* as *New style ML Module*, make sure to have one input and one output and leave the rest of the settings unchanged. |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +Click *Next*. On the next screen, we can define some additional properties of our module. Select *Add activateAttachments()*, unselect *Add configuration hints* and select *Add MDL window with fields*. |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +Click *Next*. The Module Field Interface allows you to define additional fields for the module. More fields can be added later but this is the easiest way to add fields. Click *New* to create a new field, then enter the following: |
| 45 | +* **Field Name:** constantValue |
| 46 | +* **Field Type:** Double |
| 47 | +* **Field Comment:** This constant value is added to each voxel. |
| 48 | +* **Field Value:** 0. |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +Click *Create*. You see a screen showing the results of the module creation process. In the case the Wizard finished succesfully, you can close the window. Additionally, an explorer window opens showing the created folder containing your sources and the *CMakeLists.txt*. |
| 53 | + |
| 54 | +The foundation of the module has been created with the Wizard. From here on, the programming starts. |
| 55 | + |
| 56 | +After module creation, the module database needs to be reloaded. |
| 57 | + |
| 58 | +### Preparing the Project |
| 59 | +The Project Wizard creates a *CMakeLists.txt* file that describes the typical projects settings and used source files. This file can be translated manually with the CMake tool into a project file for your preferred C++ development tool. But most Integrated Development Environments (IDEs) nowadays can open CMake files directly. |
| 60 | + |
| 61 | +Just make sure that the MLAB_ROOT environment variable is set on your system and points to the packages directory of your MeVisLab installation, because this is used to resolve the reference to the 'MeVisLab' project. |
| 62 | + |
| 63 | +Open a command line and change to your current module directory (the directory containing your *CMakeLists.txt* file). Enter **cmake . -G "Visual Studio 17"**. After execution, a lot of files are generated by CMake. |
| 64 | + |
| 65 | +For further documentation about our use of CMake, see: [CMake for MeVisLab - Documentation](https://mevislabdownloads.mevis.de/docs/current/MeVisLab/Resources/Documentation/Publish/SDK/CMakeManual/#mainBook). |
| 66 | + |
| 67 | +### Programming the Functions of the ML Module |
| 68 | +Open the file *ALL_BUILD.vcxproj* in your preferred C++ development environment. Select the file *mlSimpleAdd.cpp*. |
| 69 | + |
| 70 | +{{<alert class="info" caption="Note">}} |
| 71 | +In the following code examples, the comment lines already available in the created *.cpp* file are added for better overview. |
| 72 | +{{</alert>}} |
| 73 | + |
| 74 | +#### Implementing *calculateOutputImageProperties* |
| 75 | +As we add a constant value to each voxel, we need to adjust the value range of the output image, which results in: |
| 76 | + |
| 77 | +{{< highlight filename="" >}} |
| 78 | +```c++ |
| 79 | +outMin = inMin + constValue |
| 80 | +outMax = inMax + constValue |
| 81 | +``` |
| 82 | +{{</highlight>}} |
| 83 | + |
| 84 | +Change the file *mlSimpleAdd.cpp* as shown below. |
| 85 | + |
| 86 | +{{< highlight filename="mlSimpleAdd.cpp" >}} |
| 87 | +```c++ |
| 88 | +void SimpleAdd::calculateOutputImageProperties(int /*outputIndex*/, PagedImage* outputImage) |
| 89 | +{ |
| 90 | + // Set up data types and read-only flags of output image and input subimages. |
| 91 | + SimpleAddOutputImageHandler::setupKnownProperties(outputImage); |
| 92 | + |
| 93 | + // Change properties of output image outputImage here whose |
| 94 | + // defaults are inherited from the input image 0 (if there is one). |
| 95 | + // Get the constant add value. |
| 96 | + const MLdouble constantValue = _constantValueFld->getDoubleValue(); |
| 97 | + |
| 98 | + // Get the input image's minimum and maximum values. |
| 99 | + const MLdouble inMinValue = getInputImage(0)->getMinVoxelValue(); |
| 100 | + const MLdouble inMaxValue = getInputImage(0)->getMaxVoxelValue(); |
| 101 | + |
| 102 | + // Set the output image's minimum and maximum values. |
| 103 | + outputImage->setMinVoxelValue(inMinValue + constantValue); |
| 104 | + outputImage->setMaxVoxelValue(inMaxValue + constantValue); |
| 105 | + |
| 106 | + // Verify whether the input/output data types are supported by our handler. |
| 107 | + // This will invalidate the output image if the type combination is not supported by the handler. |
| 108 | + SimpleAddOutputImageHandler::verifyProperties(outputImage); |
| 109 | +} |
| 110 | +``` |
| 111 | +{{</highlight>}} |
| 112 | +
|
| 113 | +{{<alert class="info" caption="Note">}} |
| 114 | +*outputIndex* is the index number of the output connector. It is commented out in this example, because we only defined one output. In the case of more than one outputs, uncomment this parameter. |
| 115 | +{{</alert>}} |
| 116 | +
|
| 117 | +#### Implementing *typedCalculateOutputSubImage* |
| 118 | +Next, we are going to finally change the voxel values of the image. Open the file *mlSimpleAddOutputImageHandler.cpp*. A loop over all voxels of the output page is generated automatically and we want to add the constant value to each voxel. Add the following line at the start of the method to obtain the current constant value in the correct data type: |
| 119 | +
|
| 120 | +{{< highlight filename="mlSimpleAddOutputImageHandler.cpp" >}} |
| 121 | +```c++ |
| 122 | + // Compute subimage of output image outIndex from input subimages. |
| 123 | + const OUTTYPE constantValue = static_cast<OUTTYPE>(_parameters.constantValue); |
| 124 | +``` |
| 125 | +{{</highlight>}} |
| 126 | + |
| 127 | +Then, change the inner line of the loop so that the constant value is added to the value of the input voxel: |
| 128 | + |
| 129 | +{{< highlight filename="mlSimpleAddOutputImageHandler.cpp" >}} |
| 130 | +```c++ |
| 131 | + // Process all row voxels. |
| 132 | + for (; p.x <= rowEnd; ++p.x, ++outVoxel, ++inVoxel0) |
| 133 | + { |
| 134 | + *outVoxel = *inVoxel0 + constantValue; |
| 135 | + } |
| 136 | +``` |
| 137 | +{{</highlight>}} |
| 138 | +
|
| 139 | +Compile the project in the development environment. Make sure to select a *Release* build. |
| 140 | +
|
| 141 | +### Use Your Module in MeVisLab |
| 142 | +Your compiled **.dll* is available in your project directory under *Sources/lib*. In order to use it in MeVisLab, it needs to be copied to the *lib* folder of your user package. |
| 143 | +
|
| 144 | +This only works in a post-build step. |
| 145 | +
|
| 146 | +If the environment variable *MLAB_AUTOMATIC_POSTBUILD_COPY* is set, the newly compiled DLLs and *.lib* files are copied to the correct location when MeVisLab restarts. Otherwise, they must be copied manually. |
| 147 | +
|
| 148 | +For testing purposes, you can use a `LocalImage` module and two `View2D` modules. Connect the `SimpleAdd` module to the second `View2D` and change the <field>Constant Value</field> field. |
| 149 | +
|
| 150 | + |
| 151 | +
|
| 152 | +The output image of the module `SimpleAdd` is automatically recalculated on changing the field <field>Constant Value</field>. This is already implemented in the generated code of the file below: |
| 153 | +
|
| 154 | +{{< highlight filename="mlSimpleAdd.cpp" >}} |
| 155 | +```c++ |
| 156 | + void SimpleAdd::handleNotification(Field* field) |
| 157 | + { |
| 158 | + // Handle changes of module parameters and input image fields here. |
| 159 | + bool touchOutputs = false; |
| 160 | + if (isInputImageField(field)) |
| 161 | + { |
| 162 | + touchOutputs = true; |
| 163 | + } |
| 164 | + else if (field == _constantValueFld) |
| 165 | + { |
| 166 | + touchOutputs = true; |
| 167 | + } |
| 168 | +
|
| 169 | + if (touchOutputs) |
| 170 | + { |
| 171 | + // Touch all output image fields to notify connected modules. |
| 172 | + touchOutputImageFields(); |
| 173 | + } |
| 174 | + } |
| 175 | +``` |
| 176 | +{{</highlight>}} |
| 177 | + |
| 178 | +## Summary |
| 179 | +* MeVisLab allows to develop your own C++ modules. |
| 180 | +* The Project Wizard already generates all necessary *.cpp* and *.h* files and a loop through all voxels of the input image. |
| 181 | +* Changes of user-defined fields automatically lead to a recalculation of the input image. |
0 commit comments