/
CreateImage.m
executable file
·100 lines (86 loc) · 3.89 KB
/
CreateImage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
% Copyright (C) 2011-2013 University of Dundee & Open Microscopy Environment.
% All rights reserved.
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
% Crate a new synthetic image and upload it to the server
try
% Create a connection
[client, session] = loadOmero();
fprintf(1, 'Created connection to %s\n', char(client.getProperty('omero.host')));
fprintf(1, 'Created session for user %s using group %s\n',...
char(session.getAdminService().getEventContext().userName),...
char(session.getAdminService().getEventContext().groupName));
% Information to edit
datasetId = str2double(client.getProperty('dataset.id'));
% Read the dimensions
sizeX = 200;
sizeY = 100;
sizeZ = 1; % The number of z-sections.
sizeT = 5; % The number of timepoints.
sizeC = 2; % The number of channels.
type = 'uint16';
% Retrieve pixel type
pixelsService = session.getPixelsService();
pixelTypes = toMatlabList(pixelsService.getAllEnumerations('omero.model.PixelsType'));
pixelTypeValues = arrayfun(@(x) char(x.getValue().getValue()),...
pixelTypes, 'Unif', false);
pixelType = pixelTypes(strcmp(pixelTypeValues, type));
% Create a new image
disp('Uploading new image onto the server');
description = sprintf('Dimensions: %g x %g x %g x %g x %g',...
sizeX, sizeY, sizeZ, sizeC, sizeT);
name = 'New image';
idNew = pixelsService.createImage(sizeX, sizeY, sizeZ, sizeT,...
toJavaList(0:sizeC-1, 'java.lang.Integer'), pixelType, name, description);
%load the image.
disp('Checking the created image');
imageNew = getImages(session, idNew.getValue());
assert(~isempty(imageNew), 'OMERO:CreateImage', 'Image Id not valid');
% 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 = imageNew.getPrimaryPixels();
store = session.createRawPixelsStore();
store.setPixelsId(pixels.getId().getValue(), false);
% Create template for upload
range = cast(1: intmax(type) / sizeX : intmax(type), type);
template = repmat(range', 1, sizeY);
byteArray = toByteArray(template, pixels);
% Upload template for every plane in the image
for z = 1 : sizeZ,
for c = 1:sizeC
for t = 1: sizeT,
index = sub2ind([sizeZ sizeC sizeT], z, c, t);
store.setPlane(byteArray, z - 1, c - 1, t - 1);
end
end
end
store.save(); %save the data
store.close(); %close
catch err
client.closeSession();
throw(err);
end
% Close the session
client.closeSession();