forked from qupath/qupath
-
Notifications
You must be signed in to change notification settings - Fork 13
/
DetectCytokeratinCV.java
395 lines (311 loc) · 14.9 KB
/
DetectCytokeratinCV.java
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*-
* #%L
* This file is part of QuPath.
* %%
* Copyright (C) 2014 - 2016 The Queen's University of Belfast, Northern Ireland
* Contact: IP Management (ipmanagement@qub.ac.uk)
* %%
* 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 3 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, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package qupath.opencv;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.bytedeco.opencv.global.opencv_core.*;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.javacpp.indexer.Indexer;
import org.bytedeco.javacpp.indexer.IntIndexer;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.MatVector;
import org.bytedeco.opencv.opencv_core.Scalar;
import org.bytedeco.opencv.opencv_core.Size;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qupath.lib.color.ColorDeconvolutionStains;
import qupath.lib.color.ColorTransformer;
import qupath.lib.color.ColorTransformer.ColorTransformMethod;
import qupath.lib.common.GeneralTools;
import qupath.lib.images.ImageData;
import qupath.lib.objects.PathObject;
import qupath.lib.objects.PathObjects;
import qupath.lib.objects.classes.PathClassFactory;
import qupath.lib.objects.classes.PathClassFactory.PathClasses;
import qupath.lib.plugins.AbstractDetectionPlugin;
import qupath.lib.plugins.DetectionPluginTools;
import qupath.lib.plugins.ObjectDetector;
import qupath.lib.plugins.parameters.ParameterList;
import qupath.lib.regions.RegionRequest;
import qupath.lib.roi.PathROIToolsAwt;
import qupath.lib.roi.RectangleROI;
import qupath.lib.roi.ShapeSimplifierAwt;
import qupath.lib.roi.interfaces.PathShape;
import qupath.lib.roi.interfaces.ROI;
import qupath.opencv.processing.OpenCVTools;
/**
* Simple command to detect tumor regions stained with cytokeratin.
*
* @author Pete Bankhead
*
*/
public class DetectCytokeratinCV extends AbstractDetectionPlugin<BufferedImage> {
private final static Logger logger = LoggerFactory.getLogger(DetectCytokeratinCV.class);
transient private CytokeratinDetector detector;
static class CytokeratinDetector implements ObjectDetector<BufferedImage> {
// TODO: REQUEST DOWNSAMPLE IN PLUGINS
private List< PathObject> pathObjects = new ArrayList<>();
transient private RegionRequest lastRequest = null;
transient private BufferedImage img = null;
private String lastResultsDescription = null;
@Override
public Collection<PathObject> runDetection(final ImageData<BufferedImage> imageData, ParameterList params, ROI pathROI) throws IOException {
// Reset any detected objects
pathObjects.clear();
// Parse parameters
double downsample = Math.max(1, params.getIntParameterValue("downsampleFactor"));
// .addIntParameter("downsampleFactor", "Downsample factor", 2, "", 1, 8, "Amount to downsample image prior to detection - higher values lead to smaller images (and faster but less accurate processing)")
// .addDoubleParameter("gaussianSigmaMicrons", "Gaussian sigma", 5, GeneralTools.micrometerSymbol(), "Gaussian filter size - higher values give a smoother (less-detailed) result")
// .addDoubleParameter("thresholdTissue", "Tissue threshold", 0.1, "OD units", "Threshold to use for tissue detection (used to create stroma annotation)")
// .addDoubleParameter("thresholdDAB", "DAB threshold", 0.1, "OD units", "Threshold to use for cytokeratin detection (used to create tumour annotation)")
// .addIntParameter("separationDistanceMicrons", "Separation distance", 1, GeneralTools.micrometerSymbol(), "Approximate space to create between tumour & stroma classes when they occur side-by-side");
double thresholdTissue = params.getDoubleParameterValue("thresholdTissue");
double thresholdDAB = params.getDoubleParameterValue("thresholdDAB");
double gaussianSigmaMicrons = params.getDoubleParameterValue("gaussianSigmaMicrons");
double separationDistanceMicrons = params.getDoubleParameterValue("separationDistanceMicrons");
// Derive more useful values
double pixelSize = imageData.getServer().getAveragedPixelSizeMicrons() * downsample;
double gaussianSigma = gaussianSigmaMicrons / pixelSize;
int separationDiameter = 0;
if (separationDistanceMicrons > 0) {
separationDiameter = (int)(separationDistanceMicrons / pixelSize * 2 + .5);
// Ensure we have an odd value or zero (will be used for filter size if non-zero)
if (separationDiameter > 0 && separationDiameter % 2 == 0)
separationDiameter++;
}
// Read the image, if necessary
RegionRequest request = RegionRequest.createInstance(imageData.getServerPath(), downsample, pathROI);
if (img == null || !request.equals(lastRequest)) {
img = imageData.getServer().readBufferedImage(request);
lastRequest = request;
}
int w = img.getWidth();
int h = img.getHeight();
// Extract the color deconvolved channels
// TODO: Support alternative stain vectors
if (!imageData.isBrightfield()) {
logger.error("Only brightfield images are supported!");
return Collections.emptyList();
}
ColorDeconvolutionStains stains = imageData.getColorDeconvolutionStains();
// Since we relaxed the strict rule this needs to be H-DAB, at least print a warning if it is not
if (!stains.isH_DAB()) {
logger.warn("{} was originally designed for H-DAB staining - here, {} will be used in place of hematoxylin and {} in place of DAB",
this.getClass().getSimpleName(), stains.getStain(1).getName(), stains.getStain(2).getName());
}
int[] rgb = img.getRGB(0, 0, w, h, null, 0, w);
float[] pxHematoxylin = ColorTransformer.getTransformedPixels(rgb, ColorTransformMethod.Stain_1, null, stains);
float[] pxDAB = ColorTransformer.getTransformedPixels(rgb, ColorTransformMethod.Stain_2, null, stains);
// float[] pxHematoxylin = ColorDeconvolution.colorDeconvolveRGBArray(rgb, stains, 0, null);
// float[] pxDAB = ColorDeconvolution.colorDeconvolveRGBArray(rgb, stains, 1, null);
// Create OpenCV Mats
Mat matOD = new Mat(h, w, CV_32FC1);
Mat matDAB = new Mat(h, w, CV_32FC1);
OpenCVTools.putPixelsFloat(matOD, pxHematoxylin);
OpenCVTools.putPixelsFloat(matDAB, pxDAB);
// Add the DAB to the haematoxylin values
add(matOD, matDAB, matOD);
// If the third channel isn't a residual channel, add it too
if (!stains.getStain(3).isResidual()) {
float[] pxThird = ColorTransformer.getTransformedPixels(rgb, ColorTransformMethod.Stain_3, null, stains);
// float[] pxThird = ColorDeconvolution.colorDeconvolveRGBArray(rgb, stains, 2, null);
Mat matThird = new Mat(h, w, CV_32FC1);
OpenCVTools.putPixelsFloat(matThird, pxThird);
add(matOD, matThird, matOD);
}
// Apply Gaussian filter
Size gaussSize = new Size();
opencv_imgproc.GaussianBlur(matOD, matOD, gaussSize, gaussianSigma);
opencv_imgproc.GaussianBlur(matDAB, matDAB, gaussSize, gaussianSigma);
// Threshold
Mat matBinaryTissue = new Mat();
if (thresholdTissue > 0)
compare(matOD, new Mat(1, 1, CV_32FC1, Scalar.all(thresholdTissue)), matBinaryTissue, CMP_GT);
Mat matBinaryDAB = new Mat();
if (thresholdDAB > 0)
compare(matDAB, new Mat(1, 1, CV_32FC1, Scalar.all(thresholdDAB)), matBinaryDAB, CMP_GT);
// Ensure everything in the DAB image is removed from the tissue image
if (!matBinaryTissue.empty() && !matBinaryDAB.empty())
subtract(matBinaryTissue, matBinaryDAB, matBinaryTissue);
// Cleanup as required
if (separationDiameter > 0 && !matBinaryTissue.empty() && !matBinaryDAB.empty()) {
Mat strel = opencv_imgproc.getStructuringElement(opencv_imgproc.MORPH_ELLIPSE, new Size(separationDiameter, separationDiameter));
opencv_imgproc.erode(matBinaryTissue, matBinaryTissue, strel);
opencv_imgproc.erode(matBinaryDAB, matBinaryDAB, strel);
}
Area areaTissue = getArea(matBinaryTissue);
Area areaDAB = getArea(matBinaryDAB);
AffineTransform transform = AffineTransform.getTranslateInstance(request.getX(), request.getY());
transform.scale(downsample, downsample);
Area areaROI = null;
if (pathROI != null && !(pathROI instanceof RectangleROI)) {
areaROI = PathROIToolsAwt.getArea(pathROI);
}
double simplifyAmount = downsample * 1.5; // May want to revise this...
if (areaTissue != null) {
areaTissue = areaTissue.createTransformedArea(transform);
if (areaROI != null)
areaTissue.intersect(areaROI);
if (!areaTissue.isEmpty()) {
PathShape roiTissue = PathROIToolsAwt.getShapeROI(areaTissue, -1, request.getZ(), request.getT());
roiTissue = ShapeSimplifierAwt.simplifyShape(roiTissue, simplifyAmount);
pathObjects.add(PathObjects.createAnnotationObject(roiTissue, PathClassFactory.getDefaultPathClass(PathClasses.STROMA)));
}
}
if (areaDAB != null) {
areaDAB = areaDAB.createTransformedArea(transform);
if (areaROI != null)
areaDAB.intersect(areaROI);
if (!areaDAB.isEmpty()) {
PathShape roiDAB = PathROIToolsAwt.getShapeROI(areaDAB, -1, request.getZ(), request.getT());
roiDAB = ShapeSimplifierAwt.simplifyShape(roiDAB, simplifyAmount);
pathObjects.add(PathObjects.createAnnotationObject(roiDAB, PathClassFactory.getDefaultPathClass(PathClasses.TUMOR)));
}
}
matOD.release();
matDAB.release();
matBinaryDAB.release();
matBinaryTissue.release();
lastResultsDescription = String.format("Detected %s", pathObjects.toString());
return pathObjects;
}
@Override
public String getLastResultsDescription() {
return lastResultsDescription;
}
}
public static Area getArea(final Mat mat) {
if (mat.empty())
return null;
// Identify all contours
MatVector contours = new MatVector();
Mat hierarchy = new Mat();
opencv_imgproc.findContours(mat, contours, hierarchy, opencv_imgproc.RETR_TREE, opencv_imgproc.CHAIN_APPROX_SIMPLE);
if (contours.empty()) {
hierarchy.release();
return null;
}
Area area = new Area();
updateArea(contours, hierarchy, area, 0, 0);
hierarchy.release();
return area;
}
public static void updateArea(final MatVector contours, final Mat hierarchy, final Area area, int row, int depth) {
IntIndexer indexer = hierarchy.createIndexer();
while (row >= 0) {
int[] data = new int[4];
// TODO: Check indexing after switch to JavaCPP!!!
indexer.get(0, row, data);
// hierarchy.get(0, row, data);
Mat contour = contours.get(row);
// Don't include isolated pixels - otherwise add or remove, as required
if (contour.rows() > 2) {
Path2D path = getContour(contour);
if (depth % 2 == 0)
area.add(new Area(path));
else
area.subtract(new Area(path));
}
// Deal with any sub-contours
if (data[2] >= 0)
updateArea(contours, hierarchy, area, data[2], depth+1);
// Move to next contour in this hierarchy level
row = data[0];
}
}
public static Path2D getContour(Mat contour) {
// Create a path for the contour
Path2D path = new Path2D.Float();
boolean firstPoint = true;
Indexer indexer = contour.createIndexer();
for (int r = 0; r < contour.rows(); r++) {
double px = indexer.getDouble(r, 0L, 0L);
double py = indexer.getDouble(r, 0L, 1L);
if (firstPoint) {
path.moveTo(px, py);
firstPoint = false;
} else {
path.lineTo(px, py);
}
}
return path;
}
@Override
public ParameterList getDefaultParameterList(final ImageData<BufferedImage> imageData) {
String stain2Name = imageData.getColorDeconvolutionStains() == null ? "DAB" : imageData.getColorDeconvolutionStains().getStain(2).getName();
String stain2Prompt = stain2Name + " threshold";
ParameterList params = new ParameterList()
.addIntParameter("downsampleFactor", "Downsample factor", 4, "", 1, 32, "Amount to downsample image prior to detection - higher values lead to smaller images (and faster but less accurate processing)")
.addDoubleParameter("gaussianSigmaMicrons", "Gaussian sigma", 5, GeneralTools.micrometerSymbol(), "Gaussian filter size - higher values give a smoother (less-detailed) result")
.addDoubleParameter("thresholdTissue", "Tissue threshold", 0.1, "OD units", "Threshold to use for tissue detection (used to create stroma annotation) - if zero, no stroma annotation is created")
.addDoubleParameter("thresholdDAB", stain2Prompt, 0.25, "OD units", "Threshold to use for cytokeratin detection (used to create tumor annotation) - if zero, no tumor annotation is created")
.addDoubleParameter("separationDistanceMicrons", "Separation distance", 0.5, GeneralTools.micrometerSymbol(), "Approximate space to create between tumour & stroma classes when they occur side-by-side");
// double thresholdTissue = 0.1;
// double thresholdDAB = 0.1;
// double gaussianSigmaMicrons = 5;
// int separationRadius = 1;
// TODO: Support parameters properly!
//
// if (imageData.getServer().hasPixelSizeMicrons()) {
// String um = GeneralTools.micrometerSymbol();
// params.addDoubleParameter("medianRadius", "Median radius", 1, um).
// addDoubleParameter("gaussianSigma", "Gaussian sigma", 1.5, um).
// addDoubleParameter("openingRadius", "Opening radius", 8, um).
// addDoubleParameter("threshold", "Threshold", 0.1, null, 0, 1.0).
// addDoubleParameter("minArea", "Minimum area", 25, um+"^2");
// } else {
// params.addDoubleParameter("medianRadius", "Median radius", 1, "px").
// addDoubleParameter("gaussianSigma", "Gaussian sigma", 2, "px").
// addDoubleParameter("openingRadius", "Opening radius", 20, "px").
// addDoubleParameter("threshold", "Threshold", 0.1, null, 0, 1.0).
// addDoubleParameter("minArea", "Minimum area", 100, "px^2");
// }
// params.addBooleanParameter("splitShape", "Split by shape", true);
return params;
}
@Override
public String getName() {
return "Cytokeratin annotation creation (TMA, IHC)";
}
@Override
public String getDescription() {
return "Create tumor/non-tumor annotations by thresholding a cytokeratin staining";
}
@Override
public String getLastResultsDescription() {
return detector == null ? "" : detector.getLastResultsDescription();
}
@Override
protected void addRunnableTasks(ImageData<BufferedImage> imageData, PathObject parentObject, List<Runnable> tasks) {
// if (detector == null)
detector = new CytokeratinDetector();
tasks.add(DetectionPluginTools.createRunnableTask(detector, getParameterList(imageData), imageData, parentObject));
}
}