Solved

Image Simulation in Matlab - Black Output Image

  • 29 June 2023
  • 3 replies
  • 134 views

Hi all,

 

I am looking to build an API in MATLAB to perform image simulations in Zemax. I am having trouble with the basic case.

My MATLAB Standalone Application:

function [] = BeginApplication(TheApplication, filename)

import ZOSAPI.*;

TheSystem = TheApplication.PrimarySystem;

TheSystem.LoadFile(filename, false);

% Create analysis

im_analysis = TheSystem.Analyses.New_ImageSimulation();

im_settings = im_analysis.GetSettings();

im_settings.OutputFile = 'testfinal.BMP';

im_analysis.ApplyAndWaitForCompletion();

end

 

If I run this on the “Double Gauss 5 degree field.zmx” sample file, I get an image generated that simply [0,0,0]  pixels.

I can run the simulation on the same .zmx file in OpticsStudio without issue.

 

Any ideas what I am doing wrong?

 

 

icon

Best answer by BeauDiddley 13 July 2023, 15:36

View original

3 replies

Userlevel 2
Badge

I recreated your example in Python and it outputs a more meaningful file:

    System = ZOSAPI.ZOSAPI_Connection().CreateNewApplication().PrimarySystem
System.LoadFile(file, False)

im_analysis = System.Analyses.New_ImageSimulation()
interface = ZOSAPI.Analysis.Settings.ExtendedScene.IAS_ImageSimulation
im_settings = interface(im_analysis.GetSettings())
im_settings.OutputFile = 'testfinal.BMP'
im_analysis.ApplyAndWaitForCompletion()

Note that the “interface = ...“ part is an adaption to Python which should not be necessary for Matlab (to my knowledge).

 

Userlevel 5
Badge +2

Hi @BeauDiddley ,

Thanks for your question here on the Community Forums!

I have tested your MATLAB code on my side, and when the Zemax sample file was loaded correctly from the API, then I got a reasonable output as below:

I only got an empty output like you when the sample file was not loaded correctly from the API:

Please note that you need to define the full path together with the extension of the file, and not just the file name, in order for the LoadFile function to work correctly. 

You may use the following code to load the Double Gauss file:
samplesFolder = TheApplication.SamplesDir;
DGfile = System.String.Concat(samplesFolder, '\Sequential\Objectives\Double Gauss 5 degree field.zos');
TheSystem.LoadFile(DGfile, false)
Also, the LoadFile function returns a bool variable which indicates whether the loading process was successful or not. Please double-check that to make sure the file loading was completed successfully.

Best,
Csilla

That worked great, Csilla! Thank you.

 

Hi @BeauDiddley ,

Thanks for your question here on the Community Forums!

I have tested your MATLAB code on my side, and when the Zemax sample file was loaded correctly from the API, then I got a reasonable output as below:

I only got an empty output like you when the sample file was not loaded correctly from the API:

Please note that you need to define the full path together with the extension of the file, and not just the file name, in order for the LoadFile function to work correctly. 

You may use the following code to load the Double Gauss file:
samplesFolder = TheApplication.SamplesDir;
DGfile = System.String.Concat(samplesFolder, '\Sequential\Objectives\Double Gauss 5 degree field.zos');
TheSystem.LoadFile(DGfile, false)
Also, the LoadFile function returns a bool variable which indicates whether the loading process was successful or not. Please double-check that to make sure the file loading was completed successfully.

Best,
Csilla

 

Reply