Esempio n. 1
0
        internal void ReCreateBitmap(µImage image, PaletteType paletteType, ref WriteableBitmap writeableBitmap)
        {
            PixelFormat   pixelFormat;
            BitmapPalette palette;

            switch (paletteType)
            {
            case PaletteType.Default:
                palette     = null;
                pixelFormat = PixelFormats.Gray8;
                break;

            default:
                palette     = µPalette.GetPalette(paletteType);
                pixelFormat = PixelFormats.Indexed8;
                break;
            }

            if ((null == writeableBitmap) ||
                (image.Width != writeableBitmap.Width) || (image.Height != writeableBitmap.Height) ||
                (paletteType != _previousPaletteType) || (pixelFormat != _previousPixelFormat))
            {
                writeableBitmap = new WriteableBitmap(image.Width, image.Height, 96, 96, pixelFormat, palette);
            }
            ToWriteableBitmap(image, writeableBitmap, displayMapping);
            this.ApplyWriteableBitmap(writeableBitmap);
            this.ApplyµImage(image);
            _previousPaletteType = paletteType;
            _previousPixelFormat = pixelFormat;
        }
Esempio n. 2
0
 private void MainWindow_Loaded(object sender, RoutedEventArgs e)
 {
     µsrc = new µImage();
     µdst = new µImage();
     µReadFile(µsrc, "Zippo.jpg");
     µCopy(µsrc, µdst);
     MyµImage.DisplayImage(µsrc);
     DisplayHistogram(µsrc);
     Info.IsEnabled = false;
 }
Esempio n. 3
0
        internal void SetWindow(µImage image, Point start, Point end)
        {
            double min, max;

            µMinMax(image, start, end, out min, out max);
            µOutputDebugString("min = " + (int)min + "; max = " + (int)max);
            MyµImage.SetDisplayMappingData(min, max, µ.Display.DisplayMappingOption.GivenRange);
            MyµImage.Dispatcher.Invoke(() => MyµImage.DisplayImage(µdst));
            MyµImage.Dispatcher.Invoke(() => SliderMin.Value      = min);
            MyµImage.Dispatcher.Invoke(() => SliderMax.Value      = max);
            MyµImage.Dispatcher.Invoke(() => linegraph.Visibility = Visibility.Hidden);
            System.Threading.Thread.Sleep(1);
        }
Esempio n. 4
0
        internal void ShowLineProfile(µImage image, Point start, Point end)
        {
            double        Average;
            List <double> Profile;

            µLineProfile(image, start, end, out Profile, out Average);

            var x = Enumerable.Range(0, Profile.Count).Select(i => i).ToArray();
            var y = Profile.ToArray();

            MyµImage.Dispatcher.Invoke(() => linegraph.Plot(x, y));
            MyµImage.Dispatcher.Invoke(() => linegraph.Visibility = Visibility.Visible);
            System.Threading.Thread.Sleep(1);
        }
Esempio n. 5
0
        private void DisplayHistogram(µImage image)
        {
            if (null == image)
            {
                return;
            }

            double[] hist = new double[256];             //Currently 256 bins only ToDo: make flexible

            µHistogram(image, hist);

            DataContext      = null;
            SeriesCollection = new SeriesCollection {
                new ColumnSeries {
                    Title          = "Histogram",
                    Values         = new ChartValues <double>(hist),
                    MaxColumnWidth = double.PositiveInfinity,
                    ColumnPadding  = 0
                }
            };
            DataContext = this;
        }
Esempio n. 6
0
        public static void ToWriteableBitmap(µImage source, WriteableBitmap dst, DisplayMapping displayMapping)
        {
            OpenCvSharp.Mat src = source._image;

            if (src == null)
            {
                throw new ArgumentNullException(nameof(src));
            }
            if (dst == null)
            {
                throw new ArgumentNullException(nameof(dst));
            }
            if (src.Width != dst.PixelWidth || src.Height != dst.PixelHeight)
            {
                throw new ArgumentException("size of src must be equal to size of dst");
            }
            if (src.Dims > 2)
            {
                throw new ArgumentException("Mat dimensions must be 2");
            }
            int width  = src.Width;
            int height = src.Height;
            int bpp    = dst.Format.BitsPerPixel;
            //int channels = GetOptimumChannels(dst.Format);

            int channels = 1;             //single channel only - just for test

            if (src.Channels() != channels)
            {
                throw new ArgumentException("channels of dst != channels of PixelFormat", nameof(dst));
            }

            unsafe {
                byte *pSrc  = (byte *)(src.Data);
                int   sstep = (int)src.Step();
                int   dstep = dst.BackBufferStride;
                //1 bit bpp completely removed - I haven't plan to support such images. The 8 bit will be used instead.
                // Copy
                long imageSize = src.DataEnd.ToInt64() - src.Data.ToInt64();
                if (imageSize < 0)
                {
                    throw new OpenCvSharp.OpenCvSharpException("The mat has invalid data pointer");
                }
                if (imageSize > int.MaxValue)
                {
                    throw new OpenCvSharp.OpenCvSharpException("Too big mat data");
                }

                dst.Lock();
                IntPtr backBuffer = dst.BackBuffer;
                IntPtr srcPointer = src.Data;
                switch (source.imageType)
                {
                case ImageType.U8:
                    Mapping8to8(srcPointer, backBuffer, sstep, dstep, width, height,
                                (int)displayMapping.min, (int)displayMapping.max, displayMapping.displayMappingOption);
                    break;

                case ImageType.U16:
                    Mapping16to8(srcPointer, backBuffer, sstep, dstep, width, height,
                                 (int)displayMapping.min, (int)displayMapping.max, displayMapping.displayMappingOption);
                    break;
                }
                dst.AddDirtyRect(new Int32Rect(0, 0, width, height));
                dst.Unlock();
            }
        }
Esempio n. 7
0
 public void DisplayImage(µImage image, PaletteType palette)
 {
     ReCreateBitmap(image, palette, ref writeableBitmap);
 }
Esempio n. 8
0
 public void DisplayImage(µImage image)
 {
     DisplayImage(image, _previousPaletteType);
 }
Esempio n. 9
0
 public void ApplyµImage(µImage image)
 {
     µimage             = image;
     part_µImage.Width  = image.Width;
     part_µImage.Height = image.Height;
 }
Esempio n. 10
0
        public static void µReadDICONDEFile(µImage destination, string fileName)
        {
            string aFileName = fileName;

            var mIOD = (new IOD(aFileName));

            int mColumnCount = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,0011)");          //width
            int mRowCount    = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,0010)");          //height

            bool aPixelPaddingValueExist = DICONDEParserUtility.DoesDICONDEAttributeExist(mIOD.XDocument, "(0028,0120)");
            int  aPixelPaddingValue      = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,0120)");

            int aPixelBitsAllocated = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,0100)");

            ushort aPixelPaddingValue16;

            aPixelPaddingValue16 = 0;

            int aWindowCenter = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,1050)");
            int aWindowWidth  = DICONDEParserUtility.GetDICONDEAttributeAsInt(mIOD.XDocument, "(0028,1051)");

            var aPixelDataQuery = from Element in mIOD.XDocument.Descendants("DataElement")
                                  where Element.Attribute("Tag").Value.Equals("(7FE0,0010)")
                                  select Element;

            // Get the start position of the stream for the pixel data attribute
            long aStreamPosition = Convert.ToInt64(aPixelDataQuery.Last().Attribute("StreamPosition").Value);

            BinaryReader aBinaryReader = new BinaryReader(File.Open(aFileName, FileMode.Open, FileAccess.Read, FileShare.Read));

            // Set the stream position of the binary reader to first pixel
            aBinaryReader.BaseStream.Position = aStreamPosition;

            OpenCvSharp.Mat image = null;

            var size = new OpenCvSharp.Size();

            size.Width  = mColumnCount;
            size.Height = mRowCount;

            long length = aBinaryReader.BaseStream.Length;             //! Important to get length outside of the cycle, otherwise too sloooow

            if (16 == aPixelBitsAllocated)
            {
                image = new OpenCvSharp.Mat(size, OpenCvSharp.MatType.CV_16U);
                var mat16   = new OpenCvSharp.Mat <ushort>(image);
                var indexer = mat16.GetIndexer();

                for (int aRowIndex = 0; aRowIndex < mRowCount; aRowIndex++)
                {
                    for (int aColumnIndex = 0; aColumnIndex < mColumnCount; aColumnIndex++)
                    {
                        // For some images, the pixel buffer is smaller than '2Byte * RowCount * ColumnCount'
                        // That's why we need the check...
                        if (aBinaryReader.BaseStream.Position - 2 < length)
                        {
                            byte   aByte0      = aBinaryReader.ReadByte();
                            byte   aByte1      = aBinaryReader.ReadByte();
                            ushort aPixelValue = Convert.ToUInt16((aByte1 << 8) + aByte0);
                            // Check for Pixel Padding Value
                            if ((aPixelPaddingValueExist) && (aPixelValue == aPixelPaddingValue16))
                            {
                                aPixelValue = UInt16.MinValue;
                            }
                            indexer[aRowIndex, aColumnIndex] = aPixelValue;
                            // Rescale handling
                            //aPixelValue = aPixelValue * aRescaleSlope + aRescaleIntercept;
                            // Value of the voxel is stored in Hounsfield Units
                            //mHounsfieldPixelBuffer[aRowIndex, aColumnIndex] = aPixelValue;
                        }
                    }
                }
                OpenCvSharp.Cv2.CopyTo(mat16, destination._image);
            }
            else
            {
                image = new OpenCvSharp.Mat(size, OpenCvSharp.MatType.CV_8U);
                var mat8    = new OpenCvSharp.Mat <byte>(image);
                var indexer = mat8.GetIndexer();
                for (int aRowIndex = 0; aRowIndex < mRowCount; aRowIndex++)
                {
                    for (int aColumnIndex = 0; aColumnIndex < mColumnCount; aColumnIndex++)
                    {
                        if (aBinaryReader.BaseStream.Position - 2 < length)
                        {
                            byte aPixelValue = aBinaryReader.ReadByte();
                            if ((aPixelPaddingValueExist) && (aPixelValue == aPixelPaddingValue16))
                            {
                                aPixelValue = 0;
                            }
                            indexer[aRowIndex, aColumnIndex] = aPixelValue;
                        }
                    }
                }
                OpenCvSharp.Cv2.CopyTo(mat8, destination._image);
            }
        }