Skip to content

Commit 4fb7942

Browse files
committed
bilateral image filtering
1 parent e4ad5de commit 4fb7942

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

Assets/Scripts/Gradients/CentralDifferenceGradientComputator.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
using itk.simple;
2+
using openDicom.Image;
13
using System;
4+
using System.Runtime.InteropServices;
25
using UnityEngine;
36

47
namespace UnityVolumeRendering
58
{
69
public class CentralDifferenceGradientComputator : GradientComputator
710
{
8-
public CentralDifferenceGradientComputator(VolumeDataset dataset) : base(dataset)
11+
public CentralDifferenceGradientComputator(VolumeDataset dataset, bool smootheDataValues) : base(dataset, smootheDataValues)
912
{
1013
}
1114

Assets/Scripts/Gradients/GradientComputator.cs

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using itk.simple;
12
using System;
3+
using System.Runtime.InteropServices;
24
using UnityEngine;
35

46
namespace UnityVolumeRendering
@@ -8,14 +10,62 @@ public abstract class GradientComputator
810
protected float[] data;
911
protected int dimX, dimY, dimZ;
1012

11-
public GradientComputator(VolumeDataset dataset)
13+
public GradientComputator(VolumeDataset dataset, bool smootheDataValues)
1214
{
1315
this.data = dataset.data;
1416
this.dimX = dataset.dimX;
1517
this.dimY = dataset.dimY;
1618
this.dimZ = dataset.dimZ;
19+
20+
if (smootheDataValues)
21+
{
22+
#if UVR_USE_SIMPLEITK
23+
Image image = new Image((uint)dimX, (uint)dimY, (uint)dimZ, PixelIDValueEnum.sitkFloat32);
24+
25+
for (uint z = 0; z < dimZ; z++)
26+
{
27+
for (uint y = 0; y < dimY; y++)
28+
{
29+
for (uint x = 0; x < dimX; x++)
30+
{
31+
float value = data[x + y * dimX + z * (dimX * dimY)];
32+
image.SetPixelAsFloat(new VectorUInt32() { x, y, z }, value);
33+
}
34+
}
35+
}
36+
37+
BilateralImageFilter filter = new BilateralImageFilter();
38+
image = filter.Execute(image);
39+
40+
this.data = new float[data.Length];
41+
IntPtr imgBuffer = image.GetBufferAsFloat();
42+
Marshal.Copy(imgBuffer, data, 0, data.Length);
43+
#else
44+
Debug.LogWarning("SimpleITK is required to generate smooth gradients.");
45+
#endif
46+
}
1747
}
1848

1949
public abstract Vector3 ComputeGradient(int x, int y, int z, float minValue, float maxRange);
2050
}
51+
52+
public class GradientComputatorFactory
53+
{
54+
public static GradientComputator CreateGradientComputator(VolumeDataset dataset, GradientType gradientType)
55+
{
56+
switch (gradientType)
57+
{
58+
case GradientType.CentralDifference:
59+
return new CentralDifferenceGradientComputator(dataset, false);
60+
case GradientType.SmoothedCentralDifference:
61+
return new CentralDifferenceGradientComputator(dataset, true);
62+
case GradientType.Sobel:
63+
return new SobelGradientComputator(dataset, false);
64+
case GradientType.SmoothedSobel:
65+
return new SobelGradientComputator(dataset, true);
66+
default:
67+
throw new NotImplementedException();
68+
}
69+
}
70+
}
2171
}

Assets/Scripts/Gradients/GradientType.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ namespace UnityVolumeRendering
66
public enum GradientType
77
{
88
CentralDifference,
9-
Sobel
9+
SmoothedCentralDifference,
10+
Sobel,
11+
SmoothedSobel
1012
}
1113

1214
public class GradientTypeUtils
@@ -15,7 +17,6 @@ public static GradientType GetDefaultGradientType()
1517
{
1618
GradientType gradientType = GradientType.CentralDifference;
1719
Enum.TryParse(PlayerPrefs.GetString("DefaultGradientType"), out gradientType);
18-
Debug.Log(gradientType);
1920
return gradientType;
2021
}
2122
}

Assets/Scripts/Gradients/SobelGradientComputator.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System;
1+
using System;
2+
using System.Drawing;
23
using UnityEngine;
34

45
namespace UnityVolumeRendering
56
{
67
public class SobelGradientComputator : GradientComputator
78
{
8-
public SobelGradientComputator(VolumeDataset dataset) : base(dataset)
9+
public SobelGradientComputator(VolumeDataset dataset, bool smootheDataValues) : base(dataset, smootheDataValues)
910
{
1011
}
1112

Assets/Scripts/Importing/ImageSequenceImporter/SimpleITK/SimpleITKDICOMImporter.cs

+4
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ private void ImportSeriesInternal(VectorString dicomNames, ImageSequenceSeries s
153153
// Cast to 32-bit float
154154
image = SimpleITK.Cast(image, PixelIDValueEnum.sitkFloat32);
155155

156+
//SmoothingRecursiveGaussianImageFilter filter = new SmoothingRecursiveGaussianImageFilter();
157+
//filter.SetSigma(2);
158+
//image = filter.Execute(image);
159+
156160
size = image.GetSize();
157161

158162
int numPixels = 1;

Assets/Scripts/VolumeData/VolumeDataset.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ await Task.Run(() => {
377377
Texture3D textureTmp = new Texture3D(dimX, dimY, dimZ, texformat, false);
378378
textureTmp.wrapMode = TextureWrapMode.Clamp;
379379

380-
GradientComputator gradientComputator = gradientType == GradientType.Sobel ? new SobelGradientComputator(this) : new CentralDifferenceGradientComputator(this);
380+
GradientComputator gradientComputator = GradientComputatorFactory.CreateGradientComputator(this, gradientType);
381381

382382
for (int x = 0; x < dimX; x++)
383383
{
@@ -405,7 +405,7 @@ await Task.Run(() => {
405405

406406
progressHandler.StartStage(0.6f, "Creating gradient texture");
407407
await Task.Run(() => {
408-
GradientComputator gradientComputator = gradientType == GradientType.Sobel ? new SobelGradientComputator(this) : new CentralDifferenceGradientComputator(this);
408+
GradientComputator gradientComputator = GradientComputatorFactory.CreateGradientComputator(this, gradientType);
409409

410410
for (int z = 0; z < dimZ; z++)
411411
{

0 commit comments

Comments
 (0)