Mercurial > hg > orthanc-java
comparison Samples/MammographyDeepLearning/src/main/java/ImageProcessing.java @ 28:43923934e934
added sample: deep learning for mammography
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 12 Jun 2024 13:58:29 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
27:4a750ca9461e | 28:43923934e934 |
---|---|
1 /** | |
2 * SPDX-FileCopyrightText: 2023-2024 Sebastien Jodogne, UCLouvain, Belgium | |
3 * SPDX-License-Identifier: GPL-3.0-or-later | |
4 **/ | |
5 | |
6 /** | |
7 * Java plugin for Orthanc | |
8 * Copyright (C) 2023-2024 Sebastien Jodogne, UCLouvain, Belgium | |
9 * | |
10 * This program is free software: you can redistribute it and/or | |
11 * modify it under the terms of the GNU General Public License as | |
12 * published by the Free Software Foundation, either version 3 of the | |
13 * License, or (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, but | |
16 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 * General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 **/ | |
23 | |
24 | |
25 import ai.djl.ndarray.NDArray; | |
26 import ai.djl.ndarray.NDManager; | |
27 import ai.djl.ndarray.types.DataType; | |
28 import ai.djl.ndarray.types.Shape; | |
29 | |
30 import java.awt.*; | |
31 import java.awt.image.BufferedImage; | |
32 import java.awt.image.DataBuffer; | |
33 | |
34 public class ImageProcessing { | |
35 static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) { | |
36 BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, originalImage.getType()); | |
37 Graphics2D graphics2D = resizedImage.createGraphics(); | |
38 graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); | |
39 graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
40 graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null); | |
41 graphics2D.dispose(); | |
42 return resizedImage; | |
43 } | |
44 | |
45 static NDArray imageToTensor(NDManager manager, | |
46 BufferedImage image) { | |
47 if (image.getType() != image.TYPE_USHORT_GRAY /* 16 bpp */ && | |
48 image.getType() != image.TYPE_BYTE_GRAY /* 8 bpp */) { | |
49 throw new IllegalArgumentException(); | |
50 } | |
51 | |
52 float pixels[] = new float[image.getHeight() * image.getWidth()]; | |
53 | |
54 DataBuffer db = image.getData().getDataBuffer(); | |
55 | |
56 int pos = 0; | |
57 for (int y = 0; y < image.getHeight(); y++) { | |
58 for (int x = 0; x < image.getWidth(); x++, pos++) { | |
59 pixels[pos] = db.getElemFloat(pos); | |
60 } | |
61 } | |
62 | |
63 return manager.create(pixels, new Shape(1, image.getHeight(), image.getWidth())); | |
64 } | |
65 | |
66 static NDArray standardize(NDArray image) { | |
67 if (image.getDataType() != DataType.FLOAT32 || | |
68 image.getShape().dimension() != 3 || | |
69 (image.getShape().get(0) != 1 && | |
70 image.getShape().get(0) != 3)) { | |
71 throw new IllegalArgumentException(); | |
72 } | |
73 | |
74 // Standardize the image to zero mean and 1 standard deviation | |
75 NDArray doubleImage = image.toType(DataType.FLOAT64, false); | |
76 NDArray squared = doubleImage.mul(doubleImage); | |
77 | |
78 double asum = doubleImage.sum().getDouble(); | |
79 double asumOfSquares = squared.sum().getDouble(); | |
80 double n = doubleImage.getShape().size(); | |
81 double amean = asum / n; | |
82 double astd = Math.sqrt((asumOfSquares - asum * asum / n) / n); | |
83 | |
84 return image.add(-amean).div(astd); | |
85 } | |
86 } |