BioGTK  6.7.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
MaskData.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7namespace BioGTK
8{
13 public class MaskData : IDisposable
14 {
15 public int[] mShape;
16 public List<float> mMask;
17 public List<float> mIoU;
18 public List<float> mStability;
19 public List<int> mBox;
20
21 public List<List<float>> mfinalMask;
22 public MaskData()
23 {
24 this.mShape = new int[4];
25 this.mMask = new List<float>();
26 this.mIoU = new List<float>();
27 this.mStability = new List<float>();
28 this.mBox = new List<int>();
29
30 this.mfinalMask = new List<List<float>>();
31 }
32
33 public void Filter(float pred_iou_thresh, float stability_score_thresh)
34 {
35 List<float> m = new List<float>();
36 List<float> i = new List<float>();
37 List<float> s = new List<float>();
38 int batch = 0;
39 for (int j = 0; j < this.mShape[1]; j++)
40 {
41 if (this.mIoU[j] > pred_iou_thresh && this.mStability[j]> stability_score_thresh)
42 {
43 this.mfinalMask.Add(this.mMask.GetRange(j * this.mShape[2] * this.mShape[3], this.mShape[2] * this.mShape[3]));
44 //m.AddRange(this.mMask.GetRange(j * this.mShape[2] * this.mShape[3], this.mShape[2] * this.mShape[3]));
45 i.Add(this.mIoU[j]);
46 s.Add(this.mStability[j]);
47 batch++;
48 }
49 }
50 this.mShape[1] = batch;
51 this.mStability.Clear();
52 this.mMask.Clear();
53 this.mIoU.Clear();
54 //this.mMask.AddRange(m);
55 this.mIoU.AddRange(i);
56 this.mStability.AddRange(s);
57 }
58
59 public float[] CalculateStabilityScore(float maskThreshold, float thresholdOffset)
60 {
61 int batchSize = this.mShape[1];
62 int width = this.mShape[3];
63 int height = this.mShape[2];
64
65 float[] intersections = new float[batchSize];
66 float[] unions = new float[batchSize];
67
68 for (int i = 0; i < batchSize; i++)
69 {
70 float intersectionSum = 0;
71 float unionSum = 0;
72
73 for (int j = 0; j < width; j++)
74 {
75 for (int k = 0; k < height; k++)
76 {
77 int index = i * width * height + k * width + j;
78 if (this.mMask[index] > maskThreshold + thresholdOffset)
79 {
80 intersectionSum++;
81 }
82 if (this.mMask[index] > maskThreshold - thresholdOffset)
83 {
84 unionSum++;
85 }
86 }
87 }
88
89 intersections[i] = intersectionSum;
90 unions[i] = unionSum;
91 }
92
93 float[] stabilityScores = new float[batchSize];
94 for (int i = 0; i < batchSize; i++)
95 {
96 stabilityScores[i] = intersections[i] / unions[i];
97 }
98
99 return stabilityScores;
100 }
101
102 public void Cat(MaskData md)
103 {
104 this.mShape[0] = md.mShape[0];
105 this.mShape[1] += md.mShape[1];
106 this.mShape[2] = md.mShape[2];
107 this.mShape[3] = md.mShape[3];
108 this.mBox.AddRange(md.mBox);
109 this.mMask.AddRange(md.mMask);
110 this.mStability.AddRange(md.mStability);
111 this.mIoU.AddRange(md.mIoU);
112 this.mfinalMask.AddRange(md.mfinalMask);
113 }
114
115 public int[] batched_mask_to_box()
116 {
117 int C = this.mShape[1];
118 int width = this.mShape[3];
119 int height = this.mShape[2];
120
121 int[] boxes = new int[C*4];
122
123 for (int c = 0; c < C; c++)
124 {
125 bool emptyMask = true;
126 int top = height;
127 int bottom = 0;
128 int left = width;
129 int right = 0;
130
131 for (int i = 0; i < width; i++)
132 {
133 for (int j = 0; j < height; j++)
134 {
135 //int index = c * width * height + j * width + i;
136 //if (this.mMask[index] > 0)
137 int index =j * width + i;
138 if (this.mfinalMask[c][index] > 0)
139 {
140 emptyMask = false;
141 top = Math.Min(top, j);
142 bottom = Math.Max(bottom, j);
143 left = Math.Min(left, i);
144 right = Math.Max(right, i);
145 }
146 }
147 }
148
149 if (emptyMask)
150 {
151 boxes[c*4]=0;
152 boxes[c * 4+1] = 0;
153 boxes[c * 4+2] = 0;
154 boxes[c * 4+3] = 0;
155 }
156 else
157 {
158 boxes[c * 4] = left;
159 boxes[c * 4 + 1] = top;
160 boxes[c * 4 + 2] = right;
161 boxes[c * 4 + 3] = bottom;
162 }
163 }
164
165 return boxes;
166 }
167
168 public void Dispose()
169 {
170 mMask.Clear();
171 mIoU.Clear();
172 mMask.Clear();
173 mStability.Clear();
174 }
175 }
176 /*
181 class MicroMaskData : IDisposable
182 {
183 public int[] mShape;
184 public List<float> mMask;
185 public List<float> mIoU;
186 public List<List<float>> mfinalMask;
187 public MicroMaskData()
188 {
189 this.mMask = new List<float>();
190 this.mIoU = new List<float>();
191 }
192
193 public void Filter(float pred_iou_thresh, float stability_score_thresh)
194 {
195 List<float> m = new List<float>();
196 List<float> i = new List<float>();
197 List<float> s = new List<float>();
198 int batch = 0;
199 for (int j = 0; j < this.mShape[1]; j++)
200 {
201 if (this.mIoU[j] > pred_iou_thresh)
202 {
203 this.mfinalMask.Add(this.mMask.GetRange(j * this.mShape[2] * this.mShape[3], this.mShape[2] * this.mShape[3]));
204 //m.AddRange(this.mMask.GetRange(j * this.mShape[2] * this.mShape[3], this.mShape[2] * this.mShape[3]));
205 i.Add(this.mIoU[j]);
206 batch++;
207 }
208 }
209 this.mShape[1] = batch;
210 this.mMask.Clear();
211 this.mIoU.Clear();
212 //this.mMask.AddRange(m);
213 this.mIoU.AddRange(i);
214 }
215
216
217 public float[] CalculateStabilityScore(float maskThreshold, float thresholdOffset)
218 {
219 int batchSize = this.mShape[1];
220 int width = this.mShape[3];
221 int height = this.mShape[2];
222
223 float[] intersections = new float[batchSize];
224 float[] unions = new float[batchSize];
225
226 for (int i = 0; i < batchSize; i++)
227 {
228 float intersectionSum = 0;
229 float unionSum = 0;
230
231 for (int j = 0; j < width; j++)
232 {
233 for (int k = 0; k < height; k++)
234 {
235 int index = i * width * height + k * width + j;
236 if (this.mMask[index] > maskThreshold + thresholdOffset)
237 {
238 intersectionSum++;
239 }
240 if (this.mMask[index] > maskThreshold - thresholdOffset)
241 {
242 unionSum++;
243 }
244 }
245 }
246
247 intersections[i] = intersectionSum;
248 unions[i] = unionSum;
249 }
250
251 float[] stabilityScores = new float[batchSize];
252 for (int i = 0; i < batchSize; i++)
253 {
254 stabilityScores[i] = intersections[i] / unions[i];
255 }
256
257 return stabilityScores;
258 }
259
260 public void Cat(MaskData md)
261 {
262 this.mShape[0] = md.mShape[0];
263 this.mShape[1] += md.mShape[1];
264 this.mShape[2] = md.mShape[2];
265 this.mShape[3] = md.mShape[3];
266 this.mMask.AddRange(md.mMask);
267 this.mIoU.AddRange(md.mIoU);
268 this.mfinalMask.AddRange(md.mfinalMask);
269 }
270
271 public int[] batched_mask_to_box()
272 {
273 int C = this.mShape[1];
274 int width = this.mShape[3];
275 int height = this.mShape[2];
276
277 int[] boxes = new int[C * 4];
278
279 for (int c = 0; c < C; c++)
280 {
281 bool emptyMask = true;
282 int top = height;
283 int bottom = 0;
284 int left = width;
285 int right = 0;
286
287 for (int i = 0; i < width; i++)
288 {
289 for (int j = 0; j < height; j++)
290 {
291 //int index = c * width * height + j * width + i;
292 //if (this.mMask[index] > 0)
293 int index = j * width + i;
294 if (this.mfinalMask[c][index] > 0)
295 {
296 emptyMask = false;
297 top = Math.Min(top, j);
298 bottom = Math.Max(bottom, j);
299 left = Math.Min(left, i);
300 right = Math.Max(right, i);
301 }
302 }
303 }
304
305 if (emptyMask)
306 {
307 boxes[c * 4] = 0;
308 boxes[c * 4 + 1] = 0;
309 boxes[c * 4 + 2] = 0;
310 boxes[c * 4 + 3] = 0;
311 }
312 else
313 {
314 boxes[c * 4] = left;
315 boxes[c * 4 + 1] = top;
316 boxes[c * 4 + 2] = right;
317 boxes[c * 4 + 3] = bottom;
318 }
319 }
320
321 return boxes;
322 }
323
324 public void Dispose()
325 {
326 mMask.Clear();
327 mIoU.Clear();
328 mMask.Clear();
329 }
330 }
331 */
332}
A structure for storing masks and their related data in batched format. Implements basic filtering an...
Definition MaskData.cs:14
Definition ML.cs:17