private static void TestVolume(bool signed, VolumeFunction f, IEnumerable <IVolumeSlicerParams> slicerParams, string testName, ImageKernelFunction imageKernel, VolumeKernelFunction volumeKernel)
        {
            const int      FULL_SCALE         = 65535;
            VolumeFunction normalizedFunction = f.Normalize(100);

            using (Volume volume = normalizedFunction.CreateVolume(100, signed))
            {
                float offset = signed ? -32768 : 0;
                foreach (IVolumeSlicerParams slicing in slicerParams)
                {
                    List <double> list = new List <double>();
                    using (VolumeSlicer slicer = new VolumeSlicer(volume, slicing))
                    {
                        foreach (ISopDataSource slice in slicer.CreateSliceSops())
                        {
                            using (ImageSop imageSop = new ImageSop(slice))
                            {
                                foreach (IPresentationImage image in PresentationImageFactory.Create(imageSop))
                                {
                                    IImageSopProvider     imageSopProvider     = (IImageSopProvider)image;
                                    IImageGraphicProvider imageGraphicProvider = (IImageGraphicProvider)image;
                                    DicomImagePlane       dip = DicomImagePlane.FromImage(image);

                                    for (int y = 1; y < imageSopProvider.Frame.Rows - 1; y++)
                                    {
                                        for (int x = 1; x < imageSopProvider.Frame.Columns - 1; x++)
                                        {
                                            // pixels on the extreme sides of the volume tend to have more interpolation error due to MPR padding values
                                            Vector3D vector = dip.ConvertToPatient(new PointF(x, y));                                             // +new Vector3D(-0.5f, -0.5f, 0);
                                            if (Between(vector.X, 1, 98) && Between(vector.Y, 1, 98) && Between(vector.Z, 1, 98))
                                            {
                                                float expected = volumeKernel.Invoke(normalizedFunction, vector.X, vector.Y, vector.Z) + offset;
                                                float actual   = imageKernel.Invoke(imageGraphicProvider.ImageGraphic.PixelData, x, y);
                                                list.Add(Math.Abs(expected - actual));
                                            }
                                        }
                                    }

                                    image.Dispose();
                                }
                            }
                            slice.Dispose();
                        }
                    }

                    Statistics stats = new Statistics(list);
                    Trace.WriteLine(string.Format("Testing {0}", testName));
                    Trace.WriteLine(string.Format("\tFunction/Slicing: {0} / {1}", normalizedFunction.Name, slicing.Description));
                    Trace.WriteLine(string.Format("\t       Pixel Rep: {0}", signed ? "signed" : "unsigned"));
                    Trace.WriteLine(string.Format("\t Voxels Compared: {0}", list.Count));
                    Trace.WriteLine(string.Format("\t      Mean Delta: {0:f2} ({1:p2} of full scale)", stats.Mean, stats.Mean / FULL_SCALE));
                    Trace.WriteLine(string.Format("\t    StdDev Delta: {0:f2} ({1:p2} of full scale)", stats.StandardDeviation, stats.StandardDeviation / FULL_SCALE));
                    Assert.Less(stats.Mean, FULL_SCALE * 0.05, "Mean delta exceeds 5% of full scale ({0})", FULL_SCALE);
                    Assert.Less(stats.StandardDeviation, FULL_SCALE * 0.05, "StdDev delta exceeds 5% of full scale ({0})", FULL_SCALE);
                }
            }
        }
Esempio n. 2
0
        public ISopDataSource[] CreateSops(bool signed, Modality modality, Vector3D voxelSpacing, Vector3D sliceAxisX, Vector3D sliceAxisY, Vector3D sliceAxisZ)
        {
            var seriesInstanceUid = DicomUid.GenerateUid().UID;
            var slicerParams      = new VolumeSlicerParams(sliceAxisX, sliceAxisY, sliceAxisZ);
            var volume            = CreateVolume(signed, modality, voxelSpacing);

            using (VolumeSlicer slicer = new VolumeSlicer(volume, slicerParams))
            {
                return(new List <ISopDataSource>(slicer.CreateSliceSops(seriesInstanceUid)).ToArray());
            }
        }
Esempio n. 3
0
        protected static void ValidateVolumeSlicePoints(Volumes.Volume volume, IVolumeSlicerParams slicerParams, IList <KnownSample> expectedPoints,
                                                        double xAxialGantryTilt, double yAxialGantryTilt, bool gantryTiltInDegrees)
        {
            if (gantryTiltInDegrees)
            {
                xAxialGantryTilt *= Math.PI / 180;
                yAxialGantryTilt *= Math.PI / 180;
            }

            Trace.WriteLine(string.Format("Using slice plane: {0}", slicerParams.Description));
            using (VolumeSlicer slicer = new VolumeSlicer(volume, slicerParams))
            {
                foreach (ISopDataSource slice in slicer.CreateSliceSops())
                {
                    using (ImageSop imageSop = new ImageSop(slice))
                    {
                        foreach (IPresentationImage image in PresentationImageFactory.Create(imageSop))
                        {
                            IImageGraphicProvider imageGraphicProvider = (IImageGraphicProvider)image;
                            DicomImagePlane       dip = DicomImagePlane.FromImage(image);

                            foreach (KnownSample sample in expectedPoints)
                            {
                                Vector3D patientPoint = sample.Point;
                                if (xAxialGantryTilt != 0 && yAxialGantryTilt == 0)
                                {
                                    float cos = (float)Math.Cos(xAxialGantryTilt);
                                    float sin = (float)Math.Sin(xAxialGantryTilt);
                                    patientPoint = new Vector3D(patientPoint.X,
                                                                patientPoint.Y * cos + (xAxialGantryTilt > 0 ? 100 * sin : 0),
                                                                patientPoint.Z / cos - patientPoint.Y * sin - (xAxialGantryTilt > 0 ? 100 * sin * sin / cos : 0));
                                }
                                else if (yAxialGantryTilt != 0)
                                {
                                    Assert.Fail("Unit test not designed to work with gantry tilts about Y (i.e. slew)");
                                }

                                Vector3D slicedPoint = dip.ConvertToImagePlane(patientPoint);
                                if (slicedPoint.Z > -0.5 && slicedPoint.Z < 0.5)
                                {
                                    int actual = imageGraphicProvider.ImageGraphic.PixelData.GetPixel((int)slicedPoint.X, (int)slicedPoint.Y);
                                    Trace.WriteLine(string.Format("Sample {0} @{1} (SLICE: {2}; PATIENT: {3})", actual, FormatVector(sample.Point), FormatVector(slicedPoint), FormatVector(patientPoint)));
                                    Assert.AreEqual(sample.Value, actual, "Wrong colour sample @{0}", sample.Point);
                                }
                            }

                            image.Dispose();
                        }
                    }
                    slice.Dispose();
                }
            }
        }
Esempio n. 4
0
		protected static void ValidateVolumeSlicePoints(Volumes.Volume volume, IVolumeSlicerParams slicerParams, IList<KnownSample> expectedPoints,
		                                                double xAxialGantryTilt, double yAxialGantryTilt, bool gantryTiltInDegrees)
		{
			if (gantryTiltInDegrees)
			{
				xAxialGantryTilt *= Math.PI/180;
				yAxialGantryTilt *= Math.PI/180;
			}

			Trace.WriteLine(string.Format("Using slice plane: {0}", slicerParams.Description));
			using (VolumeSlicer slicer = new VolumeSlicer(volume, slicerParams))
			{
				foreach (ISopDataSource slice in slicer.CreateSliceSops())
				{
					using (ImageSop imageSop = new ImageSop(slice))
					{
						foreach (IPresentationImage image in PresentationImageFactory.Create(imageSop))
						{
							IImageGraphicProvider imageGraphicProvider = (IImageGraphicProvider) image;
							DicomImagePlane dip = DicomImagePlane.FromImage(image);

							foreach (KnownSample sample in expectedPoints)
							{
								Vector3D patientPoint = sample.Point;
								if (xAxialGantryTilt != 0 && yAxialGantryTilt == 0)
								{
									float cos = (float) Math.Cos(xAxialGantryTilt);
									float sin = (float) Math.Sin(xAxialGantryTilt);
									patientPoint = new Vector3D(patientPoint.X,
									                            patientPoint.Y*cos + (xAxialGantryTilt > 0 ? 100*sin : 0),
									                            patientPoint.Z/cos - patientPoint.Y*sin - (xAxialGantryTilt > 0 ? 100*sin*sin/cos : 0));
								}
								else if (yAxialGantryTilt != 0)
								{
									Assert.Fail("Unit test not designed to work with gantry tilts about Y (i.e. slew)");
								}

								Vector3D slicedPoint = dip.ConvertToImagePlane(patientPoint);
								if (slicedPoint.Z > -0.5 && slicedPoint.Z < 0.5)
								{
									int actual = imageGraphicProvider.ImageGraphic.PixelData.GetPixel((int) slicedPoint.X, (int) slicedPoint.Y);
									Trace.WriteLine(string.Format("Sample {0} @{1} (SLICE: {2}; PATIENT: {3})", actual, FormatVector(sample.Point), FormatVector(slicedPoint), FormatVector(patientPoint)));
									Assert.AreEqual(sample.Value, actual, "Wrong colour sample @{0}", sample.Point);
								}
							}

							image.Dispose();
						}
					}
					slice.Dispose();
				}
			}
		}
Esempio n. 5
0
		private static void TestVolume(bool signed, VolumeFunction f, IEnumerable<IVolumeSlicerParams> slicerParams, string testName, ImageKernelFunction imageKernel, VolumeKernelFunction volumeKernel)
		{
			const int FULL_SCALE = 65535;
			VolumeFunction normalizedFunction = f.Normalize(100);

			using (Volume volume = normalizedFunction.CreateVolume(100, signed))
			{
				float offset = signed ? -32768 : 0;
				foreach (IVolumeSlicerParams slicing in slicerParams)
				{
					List<double> list = new List<double>();
					using (VolumeSlicer slicer = new VolumeSlicer(volume, slicing))
					{
						foreach (ISopDataSource slice in slicer.CreateSliceSops())
						{
							using (ImageSop imageSop = new ImageSop(slice))
							{
								foreach (IPresentationImage image in PresentationImageFactory.Create(imageSop))
								{
									IImageSopProvider imageSopProvider = (IImageSopProvider) image;
									IImageGraphicProvider imageGraphicProvider = (IImageGraphicProvider) image;
									DicomImagePlane dip = DicomImagePlane.FromImage(image);

									for (int y = 1; y < imageSopProvider.Frame.Rows - 1; y++)
									{
										for (int x = 1; x < imageSopProvider.Frame.Columns - 1; x++)
										{
											// pixels on the extreme sides of the volume tend to have more interpolation error due to MPR padding values
											Vector3D vector = dip.ConvertToPatient(new PointF(x, y)); // +new Vector3D(-0.5f, -0.5f, 0);
											if (Between(vector.X, 1, 98) && Between(vector.Y, 1, 98) && Between(vector.Z, 1, 98))
											{
												float expected = volumeKernel.Invoke(normalizedFunction, vector.X, vector.Y, vector.Z) + offset;
												float actual = imageKernel.Invoke(imageGraphicProvider.ImageGraphic.PixelData, x, y);
												list.Add(Math.Abs(expected - actual));
											}
										}
									}

									image.Dispose();
								}
							}
							slice.Dispose();
						}
					}

					Statistics stats = new Statistics(list);
					Trace.WriteLine(string.Format("Testing {0}", testName));
					Trace.WriteLine(string.Format("\tFunction/Slicing: {0} / {1}", normalizedFunction.Name, slicing.Description));
					Trace.WriteLine(string.Format("\t       Pixel Rep: {0}", signed ? "signed" : "unsigned"));
					Trace.WriteLine(string.Format("\t Voxels Compared: {0}", list.Count));
					Trace.WriteLine(string.Format("\t      Mean Delta: {0:f2} ({1:p2} of full scale)", stats.Mean, stats.Mean/FULL_SCALE));
					Trace.WriteLine(string.Format("\t    StdDev Delta: {0:f2} ({1:p2} of full scale)", stats.StandardDeviation, stats.StandardDeviation/FULL_SCALE));
					Assert.Less(stats.Mean, FULL_SCALE*0.05, "Mean delta exceeds 5% of full scale ({0})", FULL_SCALE);
					Assert.Less(stats.StandardDeviation, FULL_SCALE*0.05, "StdDev delta exceeds 5% of full scale ({0})", FULL_SCALE);
				}
			}
		}