-
Notifications
You must be signed in to change notification settings - Fork 9
/
cutPatchesFromROI.m
131 lines (120 loc) · 5.45 KB
/
cutPatchesFromROI.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function [patches, measureSegChannel] = cutPatchesFromROI(roiShapes, channels, channelsToFetch, pixels, thisROI, zctStack, thisT)
global session;
measureSegChannel = 1;
for thisFetchChannel = channelsToFetch
%for thisROI = 1:numROI
% Copyright (C) 2013-2014 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.
numZ = roiShapes{thisROI}.numShapes;
timePoints = getROITimePoints(roiShapes{thisROI});
thisPatch = 1;
X = floor(roiShapes{thisROI}.shape1.getX.getValue+1); %svg entry in xml file indexes from (0, 0) instead of (1, 1), so +1
Y = floor(roiShapes{thisROI}.shape1.getY.getValue+1);
width = int32(roiShapes{thisROI}.shape1.getWidth.getValue);
height = int32(roiShapes{thisROI}.shape1.getHeight.getValue);
%Check to see if there's a tranform (translate) on the shape and
%modify the XY coords
transformCheck = roiShapes{thisROI}.shape1.getTransform;
if isempty(transformCheck)
transform = 'none';
else
transform = char(roiShapes{thisROI}.shape1.getTransform.getValue.getBytes');
end
if ~strcmp(transform, 'none') || ~isempty(transform)
if strncmp(transform, 'trans', 5)
closeBracket = findstr(transform, ')');
openBracket = findstr(transform, '(');
transformData = transform(openBracket+1:closeBracket-1);
spaceChar = findstr(transformData, ' ');
if isempty(spaceChar)
firstTranslate = str2double(transformData(1:end));
secondTranslate = 0;
else
firstTranslate = str2double(transformData(1:spaceChar-1));
secondTranslate = str2double(transformData(spaceChar+1:end));
end
X = X + firstTranslate;
Y = Y + secondTranslate;
end
if strncmp(transform, 'matrix', 6)
closeBracket = findstr(transform, ')');
openBracket = findstr(transform, '(');
transformData = transform(openBracket+1:closeBracket-1);
spaceChars = findstr(transformData, ' ');
firstPart = transformData(1:spaceChars(1)-1);
secondPart = transformData(spaceChars(1)+1:spaceChars(2)-1);
thirdPart = transformData(spaceChars(2)+1:spaceChars(3)-1);
fourthPart = transformData(spaceChars(3)+1:spaceChars(4)-1);
fifthPart = transformData(spaceChars(4)+1:spaceChars(5)-1);
sixthPart = transformData(spaceChars(5)+1:end);
transX = str2double(fifthPart);
transY = str2double(sixthPart);
X = X + transX;
Y = Y + transY;
end
end
maxX = pixels.getSizeX.getValue;
maxY = pixels.getSizeY.getValue;
pixelsId = pixels.getId.getValue;
imageId = pixels.getImage.getId.getValue;
for thisZ = 1:numZ
%for thisT = 1
thisPlane = zctStack(:,:,roiShapes{thisROI}.(['shape' num2str(thisZ)]).getTheZ.getValue+1,thisFetchChannel,thisT);
%thisPlane = getPlane(session, imageId, roiShapes{thisROI}.(['shape' num2str(thisZ)]).getTheZ.getValue, thisFetchChannel-1, roiShapes{thisROI}.(['shape' num2str(thisZ)]).getTheT.getValue);
patch(:,:, thisPatch) = zeros(height,width);
%Check for the ROI going outwith the image. If it does
%the patch will be contructed pixel by pixel. Otherwise
%it can be done with a quick matrix operation.
ROIEndX = X+width;
ROIEndY = Y+height;
if ROIEndX > maxX || ROIEndY > maxY || X < 0 || Y < 0
cropPatch = 1;
else
cropPatch = 0;
end
if cropPatch == 1
if X < 1
X = 1;
end
if Y < 1
Y = 1;
end
for col = 1:width
posX = col+X-1;
if posX > maxX %If the ROI was drawn to extend off the image, set the crop to the edge of the image only.
posX = maxX;
end
for row = 1:height
posY = row+Y-1;
if posY > maxY
posY = maxY;
end
patch(row, col, thisPatch) = thisPlane(posY, posX);
end
end
else
posX = floor(X+width-1);
posY = floor(Y+height-1);
patch(:,:,thisPatch) = thisPlane(Y:posY,X:posX);
end
thisPatch = thisPatch + 1;
patches{thisFetchChannel} = patch;
%end %for thisT = 1
end
patch = [];
end
end