Page 1 of 2

Write Image to Omero Server

PostPosted: Thu May 17, 2018 6:48 pm
by Hang
Hi there,

I'm following the example here to write my code to write an image back to Omero:
https://github.com/openmicroscopy/openmicroscopy/blob/v5.4.6/examples/Training/matlab/CreateImage.m

1. I didn't see in here how an Matlab image is converted to an Omero image object or byte array. Please let me know how to get that done.

2. While reading the docs, seems like this line here only create the metadata.
Code: Select all
idNew = pixelsService.createImage(sizeX, sizeY, sizeZ, sizeT,...
        toJavaList(0:sizeC-1, 'java.lang.Integer'), pixelType, name, description);


Then, why is later in the code:
Code: Select all
imageNew = getImages(session, idNew.getValue());
...
...
pixels = imageNew.getPrimaryPixels();


The above lines are used to get the pixels? Won't this getting empty or null?

3.
Code: Select all
range =  cast(1: intmax(type) / sizeX  : intmax(type), type);

This line here seems to only work for unit16 for some reason...

Re: Write Image to Omero Server

PostPosted: Fri May 18, 2018 9:57 am
by sbesson
Hi Hang

Hang wrote:1. I didn't see in here how an Matlab image is converted to an Omero image object or byte array. Please let me know how to get that done.


I think the main confusion here arise from the image terminology:

  • a MATLAB image is an arrays of numeric data
  • an OMERO image is an object containing some metadata as well as other objects including Channel objects and a Pixels object describing the pixel data which is the equivalent of the MATLAB matrix. Read/write operations against the raw pixels data described by a Pixels Object needs to happen typically via the methods of the stateful RawPixelsStore API.

2. While reading the docs, seems like this line here only create the metadata.
Code: Select all
idNew = pixelsService.createImage(sizeX, sizeY, sizeZ, sizeT,...
        toJavaList(0:sizeC-1, 'java.lang.Integer'), pixelType, name, description);


Then, why is later in the code:
Code: Select all
imageNew = getImages(session, idNew.getValue());
...
...
pixels = imageNew.getPrimaryPixels();


The above lines are used to get the pixels? Won't this getting empty or null?


The code above will retrieve a Pixels object initialized by the pixel service createImage method although you are correct that at this stage, this object will exist but contains no real binary data.

3.
Code: Select all
range =  cast(1: intmax(type) / sizeX  : intmax(type), type);

This line here seems to only work for unit16 for some reason...

Which type of pixel data are your working with? I would expect the code above to work with any integer type (uint8 or uint16) which is what the training example is demonstrating.

For floating-point data types, this code path will indeed require to be adapted. I assume one using realmax instead of intmax should be one of the changes. Depending on the size, for this type of data the binary data might need to be written in tiles using setTile rather than in whole planes using setPlane as in the current example.


Best,
Sebastien

Re: Write Image to Omero Server

PostPosted: Wed May 23, 2018 1:08 am
by Hang
Hi,

Here is my code trying to write data back to Omero server:

Code: Select all
% Connect to Omero server and create a session
omero_client_1 = loadOmero('ceb-cad2');
p = parseOmeroProperties(omero_client_1);
session = omero_client_1.createSession('omero','1234');
datasetId = 3;
omero_client_1.enableKeepAlive(60);

images = getImages(session, 2);
image = images(1);
pixels = image.getPrimaryPixels;
sizeX = pixels.getSizeX.getValue();
sizeY = pixels.getSizeY.getValue();
sizeZ = pixels.getSizeZ.getValue();
sizeT = pixels.getSizeT.getValue();
sizeC = pixels.getSizeC.getValue();
plane1 = getPlane(session, image, 0,0,0);
plane2 = getPlane(session, image, 0,1,0);
plane3 = getPlane(session, image, 0,2,0);

image_new = cat(3, plane1, plane2, plane3);
% get image metadata
image_new_size = size(image_new);
sizeX_new = image_new_size(2);
sizeY_new = image_new_size(1);
sizeZ_new = 1;
sizeT_new = 1;
sizeC_new = image_new_size(3);
classOfIm = class(image_new);
%classOfIm = 'uint16';

% Retrieve pixel type
pixelsService = session.getPixelsService();
pixelTypes = toMatlabList(session.getTypesService().allEnumerations('omero.model.PixelsType'));
pixelTypeValues = arrayfun(@(x) char(x.getValue().getValue()),...
    pixelTypes, 'Unif', false);
pixelType = pixelTypes(strcmp(pixelTypeValues, classOfIm));

% Create a new image
disp('Uploading new image onto the server');
description = sprintf('Dimensions: %g x %g x %g x %g x %g',...
    sizeX_new, sizeY_new, sizeZ_new, sizeC_new, sizeT_new);
name = 'New image';
idNew = pixelsService.createImage(sizeX_new, sizeY_new, sizeZ_new, sizeT_new,...
    toJavaList(0:sizeC-1, 'java.lang.Integer'), pixelType, name, description);

disp('Checking the created image');
    imageNew = getImages(session, idNew.getValue());
    assert(~isempty(imageNew), 'OMERO:CreateImage', 'Image Id not valid');

% Set channel properties
disp('Adding metadata to the channels')
channels = loadChannels(session, imageNew);
for i = 1: numel(channels)
    channelName = ['Channel ' num2str(i)'];
    emissionWave = omero.model.LengthI;
    emissionWave.setValue(550);
    emissionWave.setUnit(omero.model.enums.UnitsLength.NANOMETER);
    channels(i).getLogicalChannel().setName(rstring(channelName));
    channels(i).getLogicalChannel().setEmissionWave(emissionWave);
end
session.getUpdateService().saveArray(toJavaList(channels));

% load the dataset
fprintf(1, 'Reading dataset: %g\n', datasetId);
dataset = getDatasets(session, datasetId, false);
assert(~isempty(dataset), 'OMERO:CreateImage', 'Dataset Id not valid');

% Link the new image to the dataset
fprintf(1, 'Linking image %g to dataset %g\n', idNew.getValue(), datasetId);
link = omero.model.DatasetImageLinkI;
link.setChild(omero.model.ImageI(idNew, false));
link.setParent(omero.model.DatasetI(dataset.getId().getValue(), false));
session.getUpdateService().saveAndReturnObject(link);

% Copy the data.
fprintf(1, 'Copying data to image %g\n', idNew.getValue());
pixels_test = imageNew.getPrimaryPixels();
sizeX_test = pixels_test.getSizeX.getValue();
sizeY_test = pixels_test.getSizeY.getValue();
sizeZ_test = pixels_test.getSizeZ.getValue();
sizeT_test = pixels_test.getSizeT.getValue();
sizeC_test = pixels_test.getSizeC.getValue();
store = session.createRawPixelsStore();
store.setPixelsId(pixels_test.getId().getValue(), false);

% to Byte Array
byteArray = toByteArray(plane1', pixels_test);

% Upload template for every plane in the image
for z = 1 : sizeZ_new
    for c = 1:sizeC_new
        for t = 1: sizeT_new
            index = sub2ind([sizeZ_new sizeC_new sizeT_new], z, c, t);
            store.setPlane(byteArray, z - 1, c - 1, t - 1);
        end
    end
end
store.save(); %save the data
store.close(); %close

figure;imshow(image_new);

disp('Done');
omero_client_1.closeSession();


Then, I'm getting error:

Code: Select all
Java exception occurred:
omero.InternalException
    serverStackTrace = "ome.conditions.InternalException:  Wrapped Exception:
    (java.lang.UnsupportedOperationException):
                        Non-tile based writing unsupported.
                           at
                                ome.io.bioformats.BfPyramidPixelBuffer.setPlane(BfPyramidPixelBuffer.java:1117)
                           at ome.services.RawPixelsBean.setPlane(RawPixelsBean.java:608)
                           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                           at
                                sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                           at
                                sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                                                           at
                                java.lang.reflect.Method.invoke(Method.java:498)
                           at
                                org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
                                                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
                                                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
                                                           at
                                ome.security.basic.EventHandler.invoke(EventHandler.java:153)
                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
                                                           at
                                ome.tools.hibernate.SessionHandler.doStateful(SessionHandler.java:216)
                           at ome.tools.hibernate.SessionHandler.invoke(SessionHandler.java:200)
                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
                                                           at
                                org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
                                                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
                                                           at
                                ome.tools.hibernate.ProxyCleanupFilter$Interceptor.invoke(ProxyCleanupFilter.java:249)
                                                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
                                                           at
                                ome.services.util.ServiceHandler.invoke(ServiceHandler.java:121)
                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
                                                           at
                                org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
                                                           at com.sun.proxy.$Proxy104.setPlane(Unknown
                                Source)
                           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                           at
                                sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                           at
                                sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                                                           at
                                java.lang.reflect.Method.invoke(Method.java:498)
                           at
                                org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
                                                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
                                                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
                                                           at
                                ome.security.basic.BasicSecurityWiring.invoke(BasicSecurityWiring.java:93)
                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
                                                           at
                                ome.services.blitz.fire.AopContextInitializer.invoke(AopContextInitializer.java:43)
                                                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
                                                           at
                                org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
                                                           at com.sun.proxy.$Proxy104.setPlane(Unknown
                                Source)
                           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                           at
                                sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                           at
                                sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                                                           at
                                java.lang.reflect.Method.invoke(Method.java:498)
                           at ome.services.blitz.util.IceMethodInvoker.invoke(IceMethodInvoker.java:172)
                           at ome.services.throttling.Callback.run(Callback.java:56)
                           at
                                ome.services.throttling.InThreadThrottlingStrategy.callInvokerOnRawArgs(InThreadThrottlingStrategy.java:56)
                                                           at
                                ome.services.blitz.impl.AbstractAmdServant.callInvokerOnRawArgs(AbstractAmdServant.java:140)
                                                           at
                                ome.services.blitz.impl.RawPixelsStoreI.setPlane_async(RawPixelsStoreI.java:221)
                                                           at
                                sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                           at
                                sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                           at
                                sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                                                           at
                                java.lang.reflect.Method.invoke(Method.java:498)
                           at
                                org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
                                                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
                                                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
                                                           at
                                omero.cmd.CallContext.invoke(CallContext.java:78)
                           at
                                org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
                                                           at
                                org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
                                                           at
                                com.sun.proxy.$Proxy105.setPlane_async(Unknown Source)
                           at omero.api._RawPixelsStoreTie.setPlane_async(_RawPixelsStoreTie.java:276)
                           at omero.api._RawPixelsStoreDisp.___setPlane(_RawPixelsStoreDisp.java:1308)
                           at omero.api._RawPixelsStoreDisp.__dispatch(_RawPixelsStoreDisp.java:1761)
                           at IceInternal.Incoming.invoke(Incoming.java:221)
                           at Ice.ConnectionI.invokeAll(ConnectionI.java:2536)
                           at Ice.ConnectionI.dispatch(ConnectionI.java:1145)
                           at Ice.ConnectionI.message(ConnectionI.java:1056)
                           at IceInternal.ThreadPool.run(ThreadPool.java:395)
                           at IceInternal.ThreadPool.access$300(ThreadPool.java:12)
                           at IceInternal.ThreadPool$EventHandlerThread.run(ThreadPool.java:832)
                           at java.lang.Thread.run(Thread.java:748)
                        "
    serverExceptionClass = "ome.conditions.InternalException"
    message = " Wrapped Exception: (java.lang.UnsupportedOperationException):
               Non-tile based writing unsupported."
   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   at java.lang.Class.newInstance(Class.java:442)
   at IceInternal.BasicStream.createUserException(BasicStream.java:2779)
   at IceInternal.BasicStream.access$300(BasicStream.java:14)
   at IceInternal.BasicStream$EncapsDecoder10.throwException(BasicStream.java:3298)
   at IceInternal.BasicStream.throwException(BasicStream.java:2291)
   at IceInternal.OutgoingAsync.throwUserException(OutgoingAsync.java:399)
   at omero.api.RawPixelsStorePrxHelper.end_setPlane(RawPixelsStorePrxHelper.java:6949)
   at omero.api.RawPixelsStorePrxHelper.setPlane(RawPixelsStorePrxHelper.java:6807)
   at omero.api.RawPixelsStorePrxHelper.setPlane(RawPixelsStorePrxHelper.java:6794)


So, seems like I do need to use setTile().. In that case, do I need to write a tile generator to divide image into tiles? How do I convert plane into tiles?

Hang

Re: Write Image to Omero Server

PostPosted: Wed May 23, 2018 9:17 am
by manics
We don't have any Java or Matlab examples, but we do have a Python script that might help you get started: https://github.com/ome/scripts/blob/v5. ... OIs.py#L47

If you need more help just ask, though most of the team are busy preparing for the annual OME users' meeting next week so we might be slower than normal in responding.

Re: Write Image to Omero Server

PostPosted: Wed May 23, 2018 9:49 pm
by Hang
Thanks for the support at such a busy time.

Here is something regarding to one of the Matlab code "toByteArray" from OMERO.matlab:

Code: Select all
% Check input matrix pixel type
type = char(pixels.ismatrix.getValue().getValue());


From the above code I'm getting error:
Code: Select all
>> pixels.ismatrix.getValue()
Dot indexing is not supported for variables of this type.


seems like 'ismatrix" returns a logical value, so "getValue()" should be called here...

Hang

Re: Write Image to Omero Server

PostPosted: Thu May 24, 2018 9:23 am
by sbesson
Hi Hang,

thanks for reporting this error. I cannot find the line of code you are referring to.
Code: Select all
sbesson@ls30630:tmp $ curl -O http://downloads.openmicroscopy.org/omero/5.4.6/artifacts/OMERO.matlab-5.4.6-ice36-b87.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 21.5M  100 21.5M    0     0  6922k      0  0:00:03  0:00:03 --:--:-- 6920k
sbesson@ls30630:tmp $ unzip -q OMERO.matlab-5.4.6-ice36-b87.zip
sbesson@ls30630:tmp $ find OMERO.matlab-5.4.6-ice36-b87/ -iname *.m -exec grep ismatrix {} \;
ip.addRequired('matrix', @(x) isnumeric(x) && ismatrix(x));


Looking at the source code, the type retrieval should read as follows:

Code: Select all
% Check input matrix pixel type
type = char(pixels.getPixelsType().getValue().getValue());


Is it possible you are working from a locally modified version of the toByteArray function?

Best,
Sebastien

Re: Write Image to Omero Server

PostPosted: Thu May 24, 2018 10:08 pm
by Hang
Oops, I must have edited it by accident..Thanks!

Re: Write Image to Omero Server

PostPosted: Fri May 25, 2018 2:08 pm
by Hang
Hi,

I was able to upload an image to the server following the example, however the image is display as a green image as shown in the attached image. Am I missing some channel information?

Code:

Code: Select all
% Connect to Omero server and create a session
omero_client_1 = loadOmero('ceb-cad2');
p = parseOmeroProperties(omero_client_1);
session = omero_client_1.createSession('omero','1234');
datasetId = 3;
omero_client_1.enableKeepAlive(60);

id = cast(id, 'uint8');
images = getImages(session, 32);
image = images(1);
pixels = image.getPrimaryPixels;
sizeX = pixels.getSizeX.getValue();
sizeY = pixels.getSizeY.getValue();
sizeZ = pixels.getSizeZ.getValue();
sizeT = pixels.getSizeT.getValue();
sizeC = pixels.getSizeC.getValue();
plane1 = getPlane(session, image, 0,0,0);
plane2 = getPlane(session, image, 0,1,0);
plane3 = getPlane(session, image, 0,2,0);

% process image with segmetation
image_new = cat(3, plane1, plane2, plane3);

im = imresize(image_new, 1/6);
border = 1;
sigma = [5 6 9]; %original values: 5 6 9
min_area_size = 150; %original value:150
[mask, marker] = find_malaria_markers(im, sigma, min_area_size, border);

% get image metadata
image_new_size = size(mask);
sizeX_new = image_new_size(2);
sizeY_new = image_new_size(1);
sizeZ_new = 1;
sizeT_new = 1;
%sizeC_new = image_new_size(3);
sizeC_new = 1;
classOfIm = class(image_new);

% Retrieve pixel type
pixelsService = session.getPixelsService();
pixelTypes = toMatlabList(session.getTypesService().allEnumerations('omero.model.PixelsType'));
pixelTypeValues = arrayfun(@(x) char(x.getValue().getValue()),...
    pixelTypes, 'Unif', false);
pixelType = pixelTypes(strcmp(pixelTypeValues, classOfIm));

% Create a new image
disp('Uploading new image onto the server');
description = sprintf('Dimensions: %g x %g x %g x %g x %g',...
    sizeX_new, sizeY_new, sizeZ_new, sizeC_new, sizeT_new);
name = 'New image';
idNew = pixelsService.createImage(sizeX_new, sizeY_new, sizeZ_new, sizeT_new,...
    toJavaList(0:sizeC-1, 'java.lang.Integer'), pixelType, name, description);

disp('Checking the created image');
    imageNew = getImages(session, idNew.getValue());
    assert(~isempty(imageNew), 'OMERO:CreateImage', 'Image Id not valid');

% Set channel properties
disp('Adding metadata to the channels')
channels = loadChannels(session, imageNew);
RGB = ["Red", "Green", "Blue"];
for i = 1: numel(channels)
    channelName = ['Channel ' num2str(i)'];
    emissionWave = omero.model.LengthI;
    emissionWave.setValue(550);
    emissionWave.setUnit(omero.model.enums.UnitsLength.NANOMETER);
    channels(i).getLogicalChannel().setName(rstring(RGB(i)));
    channels(i).getLogicalChannel().setEmissionWave(emissionWave);
end
session.getUpdateService().saveArray(toJavaList(channels));

% load the dataset
fprintf(1, 'Reading dataset: %g\n', datasetId);
dataset = getDatasets(session, datasetId, false);
assert(~isempty(dataset), 'OMERO:CreateImage', 'Dataset Id not valid');

% Link the new image to the dataset
fprintf(1, 'Linking image %g to dataset %g\n', idNew.getValue(), datasetId);
link = omero.model.DatasetImageLinkI;
link.setChild(omero.model.ImageI(idNew, false));
link.setParent(omero.model.DatasetI(dataset.getId().getValue(), false));
session.getUpdateService().saveAndReturnObject(link);

% Copy the data.
fprintf(1, 'Copying data to image %g\n', idNew.getValue());
pixels_test = imageNew.getPrimaryPixels();
sizeX_test = pixels_test.getSizeX.getValue();
sizeY_test = pixels_test.getSizeY.getValue();
sizeZ_test = pixels_test.getSizeZ.getValue();
sizeT_test = pixels_test.getSizeT.getValue();
sizeC_test = pixels_test.getSizeC.getValue();
store = session.createRawPixelsStore();
store.setPixelsId(pixels_test.getId().getValue(), false);

% to Byte Array
% byteArray1 = toByteArray(plane1', pixels);
% byteArray2 = toByteArray(plane2', pixels);
% byteArray3 = toByteArray(plane3', pixels);
%
% store.setPlane(byteArray1, 0, 0, 0);
% store.setPlane(byteArray2, 0, 1, 0);
% store.setPlane(byteArray3, 0, 2, 0);

mask_new = im2uint8(mask);
byteArray1 = toByteArray(mask_new', pixels_test);
store.setPlane(byteArray1, 0, 0, 0);

% Upload template for every plane in the image
% for z = 1 : sizeZ_new
%     for c = 1:sizeC_new
%         for t = 1: sizeT_new
%             % index = sub2ind([sizeZ_new sizeC_new sizeT_new], z, c, t);
%             store.setPlane(byteArray, z - 1, c - 1, t - 1);
%         end
%     end
% end
store.save(); %save the data
store.close(); %close

%figure;imshow(image_new);

disp('Done');
omero_client_1.closeSession();

Re: Write Image to Omero Server

PostPosted: Fri May 25, 2018 5:35 pm
by jmoore

Re: Write Image to Omero Server

PostPosted: Mon Jun 04, 2018 3:36 pm
by Hang
Hi,

Thanks for the reply and the color issue was solved.

Another question is that the image I saved to the server is not downloadable as other images in the dataset. When I right click the image, the download button is grayed out. So, how do I set the download property for an image.

Also, an general question: is there a documentation that covers all the Omero APIs? For example, in this case, I will be able to look up how to set the download property for an image. I really appreciate the speedy response from you guys, however, a documentation would help me to move along faster.

Thanks,

Hang