Access live image from Matlab

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Access live image from Matlab

Vanessa King-2

Hi all,


I'm trying to obtain the current image on a Snap/Live display window from Matlab. As per https://micro-manager.org/wiki/Version_2.0_API_How_do_I#Access_image_data_and_metadata.3F, I have started with:

MMsetup_javaclasspath('C:\Program Files\Micro-Manager-2.0beta')

addpath(genpath( 'C:\Program Files\Micro-Manager-2.0beta\'));

 

import mmcorej.*

import org.micromanager.data.Datastore;

import org.micromanager.display.DisplayManager;

import org.micromanager.display.DisplayWindow;

import java.util.List;

 

%Get the datastore of current open window

Datastore store = mm.displays().getCurrentWindow().getDatastore();


However this throws the error: 


No constructor 'org.micromanager.data.Datastore' with matching signature found.
Error in trialMicroGUI (line 21)
Datastore store = mm.displays().getCurrentWindow().getDatastore();

I'm using the most recent nightly build of version 2.0beta. My javaclasspath includes all .jar files in the plugins\Micro-Manager\ folder. Any suggestions as to where I'm going wrong here would be appreciated. Thank you,

Vanessa



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: Access live image from Matlab

Kyle Douglass-2
Hi Vanessa,

On Wed, Jun 6, 2018 at 10:31 PM, Vanessa King <[hidden email]> wrote:


 

%Get the datastore of current open window

Datastore store = mm.displays().getCurrentWindow().getDatastore();



I'm surprised that this line even runs at all in MATLAB because it's actually Java :) You may need to create a new instance of your Datastore using MATLAB's javaObject() method. For example, here is a snippet from our lab's own MATLAB code of how I create a Datastore from a DataManager in Micro-Manager 2.0beta:

DataManager = javaObject('org.micromanager.data.internal.DefaultDataManager');
Datastore = DataManager.loadData(dataDirectory, OPEN_AS_VIRTUAL);

Alternatively, if this doesn't work, try the following (I have not tested it). Note that StartMMStudio.m is located inside the Micro-Manager installation directory and should be your primary interface between MATLAB and Micro-Manager:

gui = StartMMStudio('C:\Program Files\Micro-Manager-2.0beta\');
mmc = gui.getCore;
acq = gui.getAcquisitionEngine;

datastore = gui.displays().getCurrentWindow().getDatastore();

Cheers,
Kyle

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: Access live image from Matlab

Vanessa King
Thanks for the help Kyle, I'm not sure how I didn't realize that that was Java! I ended using a solution based on your alternative suggestion. 

However, I am now running into issues while trying to use the save the datastore. Following the example from the version 2.0 How Do I... page, I am using store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);
But this throws the error: 

Undefined function or variable 'SaveMode'.

Error in Focus>pushbutton4_Callback (line 302)
store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);

I'm assuming this isn't an error involved with importing the Datastore class since I can create and store things inside a Datastore-type variable. It also seems to me that this wouldn't be an issue caused by syntax errors, but please correct me if I'm wrong here. Any advice would be much appreciated.

Thanks,
Vanessa

On Fri, Jun 15, 2018 at 2:45 AM, Kyle Douglass <[hidden email]> wrote:
Hi Vanessa,

On Wed, Jun 6, 2018 at 10:31 PM, Vanessa King <[hidden email]> wrote:


 

%Get the datastore of current open window

Datastore store = mm.displays().getCurrentWindow().getDatastore();



I'm surprised that this line even runs at all in MATLAB because it's actually Java :) You may need to create a new instance of your Datastore using MATLAB's javaObject() method. For example, here is a snippet from our lab's own MATLAB code of how I create a Datastore from a DataManager in Micro-Manager 2.0beta:

DataManager = javaObject('org.micromanager.data.internal.DefaultDataManager');
Datastore = DataManager.loadData(dataDirectory, OPEN_AS_VIRTUAL);

Alternatively, if this doesn't work, try the following (I have not tested it). Note that StartMMStudio.m is located inside the Micro-Manager installation directory and should be your primary interface between MATLAB and Micro-Manager:

gui = StartMMStudio('C:\Program Files\Micro-Manager-2.0beta\');
mmc = gui.getCore;
acq = gui.getAcquisitionEngine;

datastore = gui.displays().getCurrentWindow().getDatastore();

Cheers,
Kyle

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: Access live image from Matlab

Kyle Douglass-2
Hi Vanessa,

On Mon, Jun 18, 2018 at 8:52 PM, Vanessa King <[hidden email]> wrote:
Thanks for the help Kyle,

You're welcome!
 

However, I am now running into issues while trying to use the save the datastore. Following the example from the version 2.0 How Do I... page, I am using store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);
But this throws the error: 

Undefined function or variable 'SaveMode'.

Error in Focus>pushbutton4_Callback (line 302)
store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);


According to the Javadocs (https://valelab4.ucsf.edu/~MM/doc-2.0.0-beta2/mmstudio/org/micromanager/data/Datastore.html), SaveMode is a static Java enum nested inside the Datastore class, so you shouldn't need to initialize it using MATLAB's javaObject(...) method. This means that the way you're using it in MATLAB appears correct.

However according to this StackOverflow question (https://stackoverflow.com/questions/1223795/using-java-enums-or-public-static-fields-in-matlab) it looks like you may need to access the enum constant named MULTIPAGE_TIFF using the full package name. Try changing Datastore.SaveMode.MULTIPAGE_TIFF to org.micromanager.data.Datastore.SaveMode.MULTIPAGE_TIFF. Or, try adding the import org.micromanager.data.Datastore; statement back to your script if you removed it from the last iteration.

Write back if you make any progress.
Cheers,
Kyle


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: Access live image from Matlab

Vanessa King
I've tried your suggestion of using the full package name, but this continued to give the same error. I also tried importing org.micromanager.data.Datastore.SaveMode prior to the save function, but this failed as well. Matlab did not recognize it as a valid import statement, saying that it was not a fully qualified class name. Just in case, I also tried using the javaObject(...) method, but this gave an error saying that org.micromanager.data.Datastore.SaveMode.MULTIPAGE_TIFF is not on the Java class path. 

So it appears that this is an import issue. I double checked my static Java class path, and it definitely contains all .jar files in the plugins folder and the ij.jar from outside the plugins folder. My script currently has no issue importing and using all of: 

import mmcorej.*

import org.micromanager.data.Datastore;

import org.micromanager.data.Image;

import java.util.List;

import org.micromanager.SnapLiveManager;


I don't see why the class path to SaveMode cannot be found by Matlab but the Datastore class path can be. Any more ideas as to what could be causing this error or other options I could try would be greatly appreciated!

Thanks,
Vanessa

On Tue, Jun 19, 2018 at 6:37 AM, Kyle Douglass <[hidden email]> wrote:
Hi Vanessa,

On Mon, Jun 18, 2018 at 8:52 PM, Vanessa King <[hidden email]> wrote:
Thanks for the help Kyle,

You're welcome!
 

However, I am now running into issues while trying to use the save the datastore. Following the example from the version 2.0 How Do I... page, I am using store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);
But this throws the error: 

Undefined function or variable 'SaveMode'.

Error in Focus>pushbutton4_Callback (line 302)
store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);


According to the Javadocs (https://valelab4.ucsf.edu/~MM/doc-2.0.0-beta2/mmstudio/org/micromanager/data/Datastore.html), SaveMode is a static Java enum nested inside the Datastore class, so you shouldn't need to initialize it using MATLAB's javaObject(...) method. This means that the way you're using it in MATLAB appears correct.

However according to this StackOverflow question (https://stackoverflow.com/questions/1223795/using-java-enums-or-public-static-fields-in-matlab) it looks like you may need to access the enum constant named MULTIPAGE_TIFF using the full package name. Try changing Datastore.SaveMode.MULTIPAGE_TIFF to org.micromanager.data.Datastore.SaveMode.MULTIPAGE_TIFF. Or, try adding the import org.micromanager.data.Datastore; statement back to your script if you removed it from the last iteration.

Write back if you make any progress.
Cheers,
Kyle


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: Access live image from Matlab

Kyle Douglass-2
Hi Vanessa,

I did a bit more digging. It seems that accessing Java nested classes and enums (SaveMode is a nested enum) from within MATLAB requires a bit of trickery.

According to this MATLAB Answers thread (https://www.mathworks.com/matlabcentral/answers/15711-how-do-i-access-a-java-inner-class-from-matlab), you should be able to get the value encoded by MULTIPAGE_TIFF using MATLAB's javaMethod(...). Try the following:

MULTIPAGE_TIFF = javaMethod('valueOf', 'org.micromanager.data.Datastore$SaveMode', 'MULTIPAGE_TIFF')
(If that doesn't work, try changing the dollar sign to a period.)

I think most of your difficulties are coming from how MATLAB accesses Java objects, but hopefully we can find a workaround.

Best,
Kyle

On Tue, Jun 19, 2018, 18:48 Vanessa King <[hidden email]> wrote:
I've tried your suggestion of using the full package name, but this continued to give the same error. I also tried importing org.micromanager.data.Datastore.SaveMode prior to the save function, but this failed as well. Matlab did not recognize it as a valid import statement, saying that it was not a fully qualified class name. Just in case, I also tried using the javaObject(...) method, but this gave an error saying that org.micromanager.data.Datastore.SaveMode.MULTIPAGE_TIFF is not on the Java class path. 

So it appears that this is an import issue. I double checked my static Java class path, and it definitely contains all .jar files in the plugins folder and the ij.jar from outside the plugins folder. My script currently has no issue importing and using all of: 

import mmcorej.*

import org.micromanager.data.Datastore;

import org.micromanager.data.Image;

import java.util.List;

import org.micromanager.SnapLiveManager;


I don't see why the class path to SaveMode cannot be found by Matlab but the Datastore class path can be. Any more ideas as to what could be causing this error or other options I could try would be greatly appreciated!

Thanks,
Vanessa

On Tue, Jun 19, 2018 at 6:37 AM, Kyle Douglass <[hidden email]> wrote:
Hi Vanessa,

On Mon, Jun 18, 2018 at 8:52 PM, Vanessa King <[hidden email]> wrote:
Thanks for the help Kyle,

You're welcome!
 

However, I am now running into issues while trying to use the save the datastore. Following the example from the version 2.0 How Do I... page, I am using store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);
But this throws the error: 

Undefined function or variable 'SaveMode'.

Error in Focus>pushbutton4_Callback (line 302)
store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);


According to the Javadocs (https://valelab4.ucsf.edu/~MM/doc-2.0.0-beta2/mmstudio/org/micromanager/data/Datastore.html), SaveMode is a static Java enum nested inside the Datastore class, so you shouldn't need to initialize it using MATLAB's javaObject(...) method. This means that the way you're using it in MATLAB appears correct.

However according to this StackOverflow question (https://stackoverflow.com/questions/1223795/using-java-enums-or-public-static-fields-in-matlab) it looks like you may need to access the enum constant named MULTIPAGE_TIFF using the full package name. Try changing Datastore.SaveMode.MULTIPAGE_TIFF to org.micromanager.data.Datastore.SaveMode.MULTIPAGE_TIFF. Or, try adding the import org.micromanager.data.Datastore; statement back to your script if you removed it from the last iteration.

Write back if you make any progress.
Cheers,
Kyle


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: Access live image from Matlab

Vanessa King
Hi Kyle,

Your suggestion here worked, I can now save images just fine using the line you provided. Thank you for the help!

Vanessa

On Tue, Jun 19, 2018 at 1:52 PM, Kyle Douglass <[hidden email]> wrote:
Hi Vanessa,

I did a bit more digging. It seems that accessing Java nested classes and enums (SaveMode is a nested enum) from within MATLAB requires a bit of trickery.

According to this MATLAB Answers thread (https://www.mathworks.com/matlabcentral/answers/15711-how-do-i-access-a-java-inner-class-from-matlab), you should be able to get the value encoded by MULTIPAGE_TIFF using MATLAB's javaMethod(...). Try the following:

MULTIPAGE_TIFF = javaMethod('valueOf', 'org.micromanager.data.Datastore$SaveMode', 'MULTIPAGE_TIFF')
(If that doesn't work, try changing the dollar sign to a period.)

I think most of your difficulties are coming from how MATLAB accesses Java objects, but hopefully we can find a workaround.

Best,
Kyle


On Tue, Jun 19, 2018, 18:48 Vanessa King <[hidden email]> wrote:
I've tried your suggestion of using the full package name, but this continued to give the same error. I also tried importing org.micromanager.data.Datastore.SaveMode prior to the save function, but this failed as well. Matlab did not recognize it as a valid import statement, saying that it was not a fully qualified class name. Just in case, I also tried using the javaObject(...) method, but this gave an error saying that org.micromanager.data.Datastore.SaveMode.MULTIPAGE_TIFF is not on the Java class path. 

So it appears that this is an import issue. I double checked my static Java class path, and it definitely contains all .jar files in the plugins folder and the ij.jar from outside the plugins folder. My script currently has no issue importing and using all of: 

import mmcorej.*

import org.micromanager.data.Datastore;

import org.micromanager.data.Image;

import java.util.List;

import org.micromanager.SnapLiveManager;


I don't see why the class path to SaveMode cannot be found by Matlab but the Datastore class path can be. Any more ideas as to what could be causing this error or other options I could try would be greatly appreciated!

Thanks,
Vanessa

On Tue, Jun 19, 2018 at 6:37 AM, Kyle Douglass <[hidden email]> wrote:
Hi Vanessa,

On Mon, Jun 18, 2018 at 8:52 PM, Vanessa King <[hidden email]> wrote:
Thanks for the help Kyle,

You're welcome!
 

However, I am now running into issues while trying to use the save the datastore. Following the example from the version 2.0 How Do I... page, I am using store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);
But this throws the error: 

Undefined function or variable 'SaveMode'.

Error in Focus>pushbutton4_Callback (line 302)
store.save(Datastore.SaveMode.MULTIPAGE_TIFF, savePath);


According to the Javadocs (https://valelab4.ucsf.edu/~MM/doc-2.0.0-beta2/mmstudio/org/micromanager/data/Datastore.html), SaveMode is a static Java enum nested inside the Datastore class, so you shouldn't need to initialize it using MATLAB's javaObject(...) method. This means that the way you're using it in MATLAB appears correct.

However according to this StackOverflow question (https://stackoverflow.com/questions/1223795/using-java-enums-or-public-static-fields-in-matlab) it looks like you may need to access the enum constant named MULTIPAGE_TIFF using the full package name. Try changing Datastore.SaveMode.MULTIPAGE_TIFF to org.micromanager.data.Datastore.SaveMode.MULTIPAGE_TIFF. Or, try adding the import org.micromanager.data.Datastore; statement back to your script if you removed it from the last iteration.

Write back if you make any progress.
Cheers,
Kyle


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: Access live image from Matlab

Kyle Douglass-2
Hi Vanessa,

On Tue, Jun 19, 2018 at 10:21 PM, Vanessa King <[hidden email]> wrote:

Your suggestion here worked, I can now save images just fine using the line you provided. Thank you for the help!


You're welcome. I'm glad to hear that it works.

Cheers,
Kyle


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: Access live image from Matlab

xandr
Hi Vanessa,

I'm also trying to work with live image from matlab. Do you mind posting
your matlab code?

Best,
Alex



--
Sent from: http://micro-manager.3463995.n2.nabble.com/


_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: Access live image from Matlab

Vanessa King
Hi Alex,
Here is my matlab code that asks MicroManager to take a picture and save it to my computer. I have this functionality imbedded in a gui, hence the use of gui.live() and gui.data(). 

%Take the picture, and put in a datastore type variable
image = gui.live().snap(true).get(0);
store = gui.data().createRAMDatastore();
store.putImage(image)
%Save the image
mode = javaMethod('valueOf', 'org.micromanager.data.Datastore$SaveMode', 'SINGLEPLANE_TIFF_SERIES');
savePath = "your folder name"
store.save(mode,savePath);
Hope this helps!
Cheers,
Vanessa

On Thu, Nov 8, 2018 at 3:33 PM xandr <[hidden email]> wrote:
Hi Vanessa,

I'm also trying to work with live image from matlab. Do you mind posting
your matlab code?

Best,
Alex



--
Sent from: http://micro-manager.3463995.n2.nabble.com/


_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general


_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general