private void SetValues(PixelSpacing pixelSpacing, NormalizedPixelSpacingCalibrationType calibrationType, string calibrationDetails) { Row = pixelSpacing.Row; Column = pixelSpacing.Column; CalibrationType = !pixelSpacing.IsNull ? calibrationType : NormalizedPixelSpacingCalibrationType.None; CalibrationDetails = !pixelSpacing.IsNull ? calibrationDetails : String.Empty; }
/// <summary> /// Constructs a new region of interest, specifying an <see cref="IPresentationImage"/> as the source of the pixel data. /// </summary> /// <param name="presentationImage">The image containing the source pixel data.</param> protected Roi(IPresentationImage presentationImage) { IImageGraphicProvider provider = presentationImage as IImageGraphicProvider; if (provider == null) return; _imageRows = provider.ImageGraphic.Rows; _imageColumns = provider.ImageGraphic.Columns; _presentationImage = presentationImage; _pixelData = provider.ImageGraphic.PixelData; if (presentationImage is IModalityLutProvider) _modalityLut = ((IModalityLutProvider) presentationImage).ModalityLut; if (presentationImage is IImageSopProvider) { Frame frame = ((IImageSopProvider) presentationImage).Frame; _normalizedPixelSpacing = frame.NormalizedPixelSpacing; _pixelAspectRatio = frame.PixelAspectRatio; _modality = frame.ParentImageSop.Modality; _modalityLutUnits = frame.RescaleUnits; _subnormalModalityLut = frame.IsSubnormalRescale; } else { _normalizedPixelSpacing = new PixelSpacing(0, 0); _pixelAspectRatio = new PixelAspectRatio(0, 0); _modalityLutUnits = RescaleUnits.None; _subnormalModalityLut = false; } }
/// <summary> /// Converts an area in pixels into the given units given some particular pixel spacing. /// </summary> /// <param name="area">The area of pixels to be converted.</param> /// <param name="units">The units into which the area should be converted.</param> /// <param name="pixelSpacing">The pixel spacing information available.</param> /// <returns>The equivalent area in the units of <paramref name="units"/>.</returns> /// <exception cref="ArgumentException">Thrown if <paramref name="units"/> is a physical unit of measurement and <paramref name="pixelSpacing"/> is not calibrated.</exception> protected static double ConvertFromSquarePixels(double area, Units units, PixelSpacing pixelSpacing) { if (!ValidateUnits(units, pixelSpacing)) { throw new ArgumentException("Pixel spacing must be calibrated in order to compute physical units.", "units"); } double factor = 1; switch (units) { case Units.Pixels: factor = 1; break; case Units.Centimeters: factor = pixelSpacing.Column * pixelSpacing.Row / 100; break; case Units.Millimeters: default: factor = pixelSpacing.Column * pixelSpacing.Row; break; } return(area * factor); }
public PixelDataInfo(DicomAttributeCollection dataset) { dataset.LoadDicomFields(this); AspectRatio = PixelAspectRatio.FromString(dataset[DicomTags.PixelAspectRatio].ToString()) ?? new PixelAspectRatio(0, 0); PixelSpacing = PixelSpacing.FromString(dataset[DicomTags.PixelSpacing].ToString()) ?? new PixelSpacing(0, 0); ImagerPixelSpacing = PixelSpacing.FromString(dataset[DicomTags.ImagerPixelSpacing].ToString()) ?? new PixelSpacing(0, 0); }
private static void AssertVolumeSlice(VolumeSlice actualSlice, int expectedRows, int expectedColumns, Vector3D expectedPositionPatient, Vector3D expectedRowOrientationPatient, Vector3D expectedColumnOrientationPatient, float expectedRowSpacing, float expectedColumnSpacing, float expectedSliceThickness, float?expectedSpacingBetweenSlices, string message = null, params object[] args) { const float tolerance = 1e-5f; var msg = !string.IsNullOrEmpty(message) ? "{0} (" + string.Format(message, args) + ")" : "{0}"; Assert.AreEqual(expectedRows, actualSlice.Rows, msg, "Rows"); Assert.AreEqual(expectedColumns, actualSlice.Columns, msg, "Columns"); var imagePositionPatient = ImagePositionPatient.FromString(actualSlice.ImagePositionPatient); Assert.IsNotNull(imagePositionPatient, "ImagePositionPatient"); var actualPositionPatient = new Vector3D((float)imagePositionPatient.X, (float)imagePositionPatient.Y, (float)imagePositionPatient.Z); if (!Vector3D.AreEqual(expectedPositionPatient, actualPositionPatient, tolerance)) { Assert.AreEqual(expectedPositionPatient, actualPositionPatient, msg, "ImagePositionPatient"); } var imageOrientationPatient = ImageOrientationPatient.FromString(actualSlice.ImageOrientationPatient); Assert.IsNotNull(imageOrientationPatient, "ImageOrientationPatient"); var actualRowOrientationPatient = new Vector3D((float)imageOrientationPatient.RowX, (float)imageOrientationPatient.RowY, (float)imageOrientationPatient.RowZ); var actualColumnOrientationPatient = new Vector3D((float)imageOrientationPatient.ColumnX, (float)imageOrientationPatient.ColumnY, (float)imageOrientationPatient.ColumnZ); if (!Vector3D.AreEqual(expectedRowOrientationPatient, actualRowOrientationPatient, tolerance)) { Assert.AreEqual(expectedRowOrientationPatient, actualRowOrientationPatient, msg, "ImageOrientationPatient.Row"); } if (!Vector3D.AreEqual(expectedColumnOrientationPatient, actualColumnOrientationPatient, tolerance)) { Assert.AreEqual(expectedColumnOrientationPatient, actualColumnOrientationPatient, msg, "ImageOrientationPatient.Column"); } var actualPixelSpacing = PixelSpacing.FromString(actualSlice.PixelSpacing); Assert.IsNotNull(actualPixelSpacing, "PixelSpacing"); Assert.AreEqual(expectedRowSpacing, actualPixelSpacing.Row, tolerance, msg, "PixelSpacing.Row"); Assert.AreEqual(expectedColumnSpacing, actualPixelSpacing.Column, tolerance, msg, "PixelSpacing.Column"); Assert.AreEqual(expectedSliceThickness, float.Parse(actualSlice.SliceThickness), tolerance, msg, "SliceThickness"); if (expectedSpacingBetweenSlices.HasValue) { Assert.AreEqual(expectedSpacingBetweenSlices.Value, float.Parse(actualSlice.SpacingBetweenSlices), tolerance, msg, "SpacingBetweenSlices"); } else { Assert.IsEmpty(actualSlice.SpacingBetweenSlices, msg, "SpacingBetweenSlices"); } }
public SizeF?GetRealDimensions() { PixelSpacing spacing = GetPixelSpacing(); if (spacing.IsNull) { return(null); } return(new SizeF((float)spacing.Column * Columns, (float)spacing.Row * Rows)); }
/// <summary> /// Constructor. /// </summary> public ImagePlaneHelper(ImagePositionPatient imagePositionPatient, ImageOrientationPatient imageOrientationPatient, PixelSpacing pixelSpacing) { Platform.CheckForNullReference(imagePositionPatient, "imagePositionPatient"); Platform.CheckForNullReference(imageOrientationPatient, "imageOrientationPatient"); Platform.CheckForNullReference(pixelSpacing, "pixelSpacing"); PixelSpacing = pixelSpacing; ImageOrientationPatient = imageOrientationPatient; ImagePositionPatient = imagePositionPatient; ImagePositionPatientVector = new Vector3D((float)ImagePositionPatient.X, (float)ImagePositionPatient.Y, (float)ImagePositionPatient.Z); }
/// <summary> /// Initializes a new instance of <see cref="ImagePlaneHelper"/> from the specified image plane details. /// </summary> public ImagePlaneHelper(ImagePositionPatient imagePositionPatient, ImageOrientationPatient imageOrientationPatient, PixelSpacing pixelSpacing, int rows, int columns) { Platform.CheckForNullReference(imagePositionPatient, "imagePositionPatient"); Platform.CheckForNullReference(imageOrientationPatient, "imageOrientationPatient"); Platform.CheckForNullReference(pixelSpacing, "pixelSpacing"); Platform.CheckPositive(rows, "rows"); Platform.CheckPositive(columns, "columns"); _rows = rows; _columns = columns; _pixelSpacing = pixelSpacing; _imageOrientationPatient = imageOrientationPatient; _imagePositionPatient = imagePositionPatient; }
public Vector3D ConvertToPatient(PointF positionPixels, int z) { ImagePositionPatient pos = this._sourceFrame.ImagePositionPatient; Vector3D _imagePositionPatient = new Vector3D((float)pos.X, (float)pos.Y, (float)pos.Z); ImageOrientationPatient orientation = this._sourceFrame.ImageOrientationPatient; Vector3D left = new Vector3D((float)orientation.RowX, (float)orientation.RowY, (float)orientation.RowZ); Vector3D normal = left.Cross(new Vector3D((float)orientation.ColumnX, (float)orientation.ColumnY, (float)orientation.ColumnZ)); PixelSpacing pixelSpacing = this._sourceFrame.PixelSpacing; if (orientation.IsNull || pixelSpacing.IsNull) { return(null); } Vector3D position = _imagePositionPatient; if (positionPixels.X == 0F && positionPixels.Y == 0F) { return(position); } Matrix _pixelToPatientTransform = new Matrix(4, 4); _pixelToPatientTransform.SetColumn(0, (float)(orientation.RowX * pixelSpacing.Column), (float)(orientation.RowY * pixelSpacing.Column), (float)(orientation.RowZ * pixelSpacing.Column), 0); _pixelToPatientTransform.SetColumn(1, (float)(orientation.ColumnX * pixelSpacing.Row), (float)(orientation.ColumnY * pixelSpacing.Row), (float)(orientation.ColumnZ * pixelSpacing.Row), 0); _pixelToPatientTransform.SetColumn(2, (float)(normal.X * pixelSpacing.Row), (float)(normal.Y * pixelSpacing.Row), (float)(normal.Z * pixelSpacing.Row), 0); _pixelToPatientTransform.SetColumn(3, position.X, position.Y, position.Z, 1F); Matrix columnMatrix = new Matrix(4, 1); columnMatrix.SetColumn(0, positionPixels.X, positionPixels.Y, z, 1F); Matrix result = _pixelToPatientTransform * columnMatrix; return(new Vector3D(result[0, 0], result[1, 0], result[2, 0])); }
/// <summary> /// Initializes a new instance of <see cref="ImagePlaneHelper"/> from the specified image plane details. /// </summary> public ImagePlaneHelper(ImagePositionPatient imagePositionPatient, ImageOrientationPatient imageOrientationPatient, PixelSpacing pixelSpacing, int rows, int columns) { Platform.CheckForNullReference(imagePositionPatient, "imagePositionPatient"); Platform.CheckForNullReference(imageOrientationPatient, "imageOrientationPatient"); Platform.CheckForNullReference(pixelSpacing, "pixelSpacing"); try { Platform.CheckPositive(rows, "rows"); Platform.CheckPositive(columns, "columns"); } catch (Exception ex) { Platform.Log(LogLevel.Error, "the row is " + rows.ToString()); } _rows = rows; _columns = columns; _pixelSpacing = pixelSpacing; _imageOrientationPatient = imageOrientationPatient; _imagePositionPatient = imagePositionPatient; }
/// <summary> /// Helper method to compute the physical distance between two pixels. /// </summary> /// <param name="point1">The first point.</param> /// <param name="point2">The second point.</param> /// <param name="normalizedPixelSpacing">The normalized pixel spacing of the image.</param> /// <param name="units">The units in which the resultant distance is given, passed by reference. If <paramref name="normalizedPixelSpacing"/> is not calibrated, then the passed variable will change to <see cref="RoiGraphics.Units.Pixels"/>.</param> /// <returns>The distance between the two points, in units of <paramref name="units"/>.</returns> public static double CalculateLength( PointF point1, PointF point2, PixelSpacing normalizedPixelSpacing, ref Units units) { if (normalizedPixelSpacing.IsNull) { units = Units.Pixels; } double widthInPixels = point2.X - point1.X; double heightInPixels = point2.Y - point1.Y; double length; if (units == Units.Pixels) { length = Math.Sqrt(widthInPixels * widthInPixels + heightInPixels * heightInPixels); } else { double widthInMm = widthInPixels * normalizedPixelSpacing.Column; double heightInMm = heightInPixels * normalizedPixelSpacing.Row; double lengthInMm = Math.Sqrt(widthInMm * widthInMm + heightInMm * heightInMm); if (units == Units.Millimeters) { length = lengthInMm; } else { length = lengthInMm / 10; } } return(length); }
public void UpdateDataSet(DicomAttributeCollection dataset) { dataset.SaveDicomFields(this); if (IsColor) { dataset[DicomTags.PixelRepresentation].SetEmptyValue();; } else { dataset[DicomTags.PlanarConfiguration].SetEmptyValue();; } if (PixelSpacing.IsNull && ImagerPixelSpacing.IsNull) { dataset[DicomTags.PixelAspectRatio].SetStringValue(AspectRatio.ToString()); } if (!PixelSpacing.IsNull) { dataset[DicomTags.PixelSpacing].SetStringValue(PixelSpacing.ToString()); } else { dataset[DicomTags.PixelSpacing].SetEmptyValue(); } if (!ImagerPixelSpacing.IsNull) { dataset[DicomTags.ImagerPixelSpacing].SetStringValue(ImagerPixelSpacing.ToString()); } else { dataset[DicomTags.ImagerPixelSpacing].SetEmptyValue(); } }
private void SetValues(PixelSpacing pixelSpacing, NormalizedPixelSpacingCalibrationType calibrationType, string calibrationDetails) { Row = pixelSpacing.Row; Column = pixelSpacing.Column; CalibrationType = !pixelSpacing.IsNull ? calibrationType : NormalizedPixelSpacingCalibrationType.None; CalibrationDetails = !pixelSpacing.IsNull ? calibrationDetails : string.Empty; }
public override string GetAnnotationText(IPresentationImage presentationImage) { if (presentationImage == null) { return(String.Empty); } IImageSopProvider imageSopProvider = presentationImage as IImageSopProvider; if (imageSopProvider == null) { return(String.Empty); } ISpatialTransformProvider spatialTransformProvider = presentationImage as ISpatialTransformProvider; if (spatialTransformProvider == null) { return(String.Empty); } ImageSpatialTransform transform = spatialTransformProvider.SpatialTransform as ImageSpatialTransform; if (transform == null) { return(String.Empty); } if (transform.RotationXY % 90 != 0) { return(SR.ValueNotApplicable); } Frame frame = imageSopProvider.Frame; PixelSpacing normalizedPixelSpacing = frame.NormalizedPixelSpacing; if (normalizedPixelSpacing.IsNull) { return(String.Empty); } RectangleF sourceRectangle = new RectangleF(0, 0, frame.Columns, frame.Rows); RectangleF destinationRectangle = transform.ConvertToDestination(sourceRectangle); destinationRectangle = RectangleUtilities.Intersect(destinationRectangle, presentationImage.ClientRectangle); //Convert the displayed width and height to source dimensions SizeF widthInSource = transform.ConvertToSource(new SizeF(destinationRectangle.Width, 0)); SizeF heightInSource = transform.ConvertToSource(new SizeF(0, destinationRectangle.Height)); //The displayed FOV is given by the magnitude of each line in source coordinates, but //for each of the 2 lines, one of x or y will be zero, so we can optimize. float x1 = Math.Abs(widthInSource.Width); float y1 = Math.Abs(widthInSource.Height); float x2 = Math.Abs(heightInSource.Width); float y2 = Math.Abs(heightInSource.Height); double displayedFieldOfViewX, displayedFieldOfViewY; if (x1 > y1) //the image is not rotated { displayedFieldOfViewX = x1 * normalizedPixelSpacing.Column / 10; displayedFieldOfViewY = y2 * normalizedPixelSpacing.Row / 10; } else //the image is rotated by 90 or 270 degrees { displayedFieldOfViewX = x2 * normalizedPixelSpacing.Column / 10; displayedFieldOfViewY = y1 * normalizedPixelSpacing.Row / 10; } return(String.Format(SR.FormatCentimeters, String.Format(SR.Format2Dimensions, displayedFieldOfViewX.ToString("F1"), displayedFieldOfViewY.ToString("F1")))); }
/// <summary> /// Helper method to compute the physical distance between two pixels. /// </summary> /// <param name="point1">The first point.</param> /// <param name="point2">The second point.</param> /// <param name="normalizedPixelSpacing">The normalized pixel spacing of the image.</param> /// <param name="units">The units in which the resultant distance is given, passed by reference. If <paramref name="normalizedPixelSpacing"/> is not calibrated, then the passed variable will change to <see cref="RoiGraphics.Units.Pixels"/>.</param> /// <returns>The distance between the two points, in units of <paramref name="units"/>.</returns> public static double CalculateLength( PointF point1, PointF point2, PixelSpacing normalizedPixelSpacing, ref Units units) { if (normalizedPixelSpacing.IsNull) units = Units.Pixels; double widthInPixels = point2.X - point1.X; double heightInPixels = point2.Y - point1.Y; double length; if (units == Units.Pixels) { length = Math.Sqrt(widthInPixels*widthInPixels + heightInPixels*heightInPixels); } else { double widthInMm = widthInPixels*normalizedPixelSpacing.Column; double heightInMm = heightInPixels*normalizedPixelSpacing.Row; double lengthInMm = Math.Sqrt(widthInMm*widthInMm + heightInMm*heightInMm); if (units == Units.Millimeters) length = lengthInMm; else length = lengthInMm/10; } return length; }
/// <summary> /// Checks if operations in the given <paramref name="units"/> are possible with the <paramref name="pixelSpacing"/> information available. /// </summary> /// <returns>True if such operations are valid; False if no such operation is possible.</returns> protected static bool ValidateUnits(Units units, PixelSpacing pixelSpacing) { return (units == Units.Pixels || !pixelSpacing.IsNull); }
public bool CheckPETFusionSeries(Series series, out string error) { if (!IsValidPETFusionSeries(series)) { error = SR.MessageInvalidSeries; return(false); } var frames = GetFrames(series.Sops); if (!AssertSameSeriesFrameOfReference(frames)) { error = SR.MessageInconsistentFramesOfReference; return(false); } // ensure all frames have the same orientation ImageOrientationPatient orient = frames[0].ImageOrientationPatient; double minColumnSpacing = double.MaxValue, minRowSpacing = double.MaxValue; double maxColumnSpacing = double.MinValue, maxRowSpacing = double.MinValue; foreach (Frame frame in frames) { if (frame.ImageOrientationPatient.IsNull) { error = SR.MessageMissingImageOrientation; return(false); } if (!frame.ImageOrientationPatient.EqualsWithinTolerance(orient, _orientationTolerance)) { error = SR.MessageInconsistentImageOrientation; return(false); } PixelSpacing pixelSpacing = frame.NormalizedPixelSpacing; if (pixelSpacing.IsNull) { error = SR.MessageMissingPixelSpacing; return(false); } minColumnSpacing = Math.Min(minColumnSpacing, pixelSpacing.Column); maxColumnSpacing = Math.Max(maxColumnSpacing, pixelSpacing.Column); minRowSpacing = Math.Min(minRowSpacing, pixelSpacing.Row); maxRowSpacing = Math.Max(maxRowSpacing, pixelSpacing.Row); } //Aren't many of these rules taken from MPR? If so, we should create a rule object. // ensure all frames have consistent pixel spacing if (maxColumnSpacing - minColumnSpacing > _sliceSpacingTolerance || maxRowSpacing - minRowSpacing > _sliceSpacingTolerance) { error = SR.MessageInconsistentPixelSpacing; return(false); } // ensure all frames are sorted by slice location frames.Sort(new SliceLocationComparer().Compare); // ensure all frames are equally spaced float?nominalSpacing = null; for (int i = 1; i < frames.Count; i++) { float currentSpacing = CalcSpaceBetweenPlanes(frames[i], frames[i - 1]); if (currentSpacing < _minimumSliceSpacing) { error = SR.MessageInconsistentImageSpacing; return(false); } if (!nominalSpacing.HasValue) { nominalSpacing = currentSpacing; } if (!FloatComparer.AreEqual(currentSpacing, nominalSpacing.Value, _sliceSpacingTolerance)) { error = SR.MessageInconsistentImageSpacing; return(false); } } // ensure frames are not tilted about unsupposed axis combinations (the gantry correction algorithm only supports rotations about X) if (!IsSupportedGantryTilt(frames)) // suffices to check first one... they're all co-planar now!! { error = SR.MessageUnsupportedGantryTilt; return(false); } error = null; return(true); }
/// <summary> /// Converts an area in pixels into the given units given some particular pixel spacing. /// </summary> /// <param name="area">The area of pixels to be converted.</param> /// <param name="units">The units into which the area should be converted.</param> /// <param name="pixelSpacing">The pixel spacing information available.</param> /// <returns>The equivalent area in the units of <paramref name="units"/>.</returns> /// <exception cref="ArgumentException">Thrown if <paramref name="units"/> is a physical unit of measurement and <paramref name="pixelSpacing"/> is not calibrated.</exception> protected static double ConvertFromSquarePixels(double area, Units units, PixelSpacing pixelSpacing) { if (!ValidateUnits(units, pixelSpacing)) throw new ArgumentException("Pixel spacing must be calibrated in order to compute physical units.", "units"); double factor = 1; switch (units) { case Units.Pixels: factor = 1; break; case Units.Centimeters: factor = pixelSpacing.Column*pixelSpacing.Row/100; break; case Units.Millimeters: default: factor = pixelSpacing.Column*pixelSpacing.Row; break; } return area*factor; }
/// <summary> /// Checks if operations in the given <paramref name="units"/> are possible with the <paramref name="pixelSpacing"/> information available. /// </summary> /// <returns>True if such operations are valid; False if no such operation is possible.</returns> protected static bool ValidateUnits(Units units, PixelSpacing pixelSpacing) { return(units == Units.Pixels || !pixelSpacing.IsNull); }