/// <summary>
        /// Do a horizontal pass of the kernel over the image. The results are placed in an intermediate image as other the result of each transformation would affect the values of the next kernel sample.
        /// </summary>
        /// <param name="srcImage">The image source data.</param>
        /// <param name="dstImage">The image destination data.</param>
        private void ApplyHorizontally(MonoImage <TPixel> srcImage, MonoImage <TPixel> dstImage)
        {
            int maxIntensity = (int)(Math.Pow(2, srcImage.BitDepth) - 1);

            Parallel.For(0, srcImage.Size.Height, new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            }, y =>
            {
                for (int x = 0; x < srcImage.Size.Width; x++)
                {
                    Pixel <TPixel> accumulated = default(TPixel);

                    for (int kernelX = 0, pixelX = x - _kernelRadius; kernelX < _kernelWidth; kernelX++, pixelX++)
                    {
                        int correctedX = MirrorXIfOutOfRange(pixelX, srcImage.Size.Width);

                        accumulated += (srcImage[correctedX, y] * _kernel[kernelX]);
                    }


                    accumulated = Math.Max(0, accumulated);
                    accumulated = Math.Min(maxIntensity, accumulated);

                    dstImage[x, y] = accumulated;
                }
            });
        }
示例#2
0
        public static byte[] LoadImage(string monoPath, string assemblyPath)
        {
            IntPtr pData = IntPtr.Zero;

            try
            {
                // prepare raw assembly data for native method call
                byte[] assemblyData = File.ReadAllBytes(assemblyPath);
                pData = Marshal.AllocHGlobal(assemblyData.Length);
                Marshal.Copy(assemblyData, 0, pData, assemblyData.Length);

                // load mono library and open the assembly
                LoadLibrary(monoPath);
                MonoInitImages();
                var       pImage      = MonoOpenImage(pData, (uint)assemblyData.Length, false, IntPtr.Zero, false, assemblyPath);
                MonoImage loadedImage = (MonoImage)Marshal.PtrToStructure(pImage, typeof(MonoImage));

                // dump the decoded assembly
                byte[] dumpedData = new byte[loadedImage.rawDataLen];
                Marshal.Copy(loadedImage.rawData, dumpedData, 0, loadedImage.rawDataLen);
                return(dumpedData);
            }
            finally
            {
                if (pData != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pData);
                }
            }
        }
        public void ApplyTo(MonoImage <TPixel> image)
        {
            MonoImage <TPixel> intermediateImage = image.Clone() as MonoImage <TPixel>;

            ApplyHorizontally(image, intermediateImage);
            ApplyVertically(intermediateImage, image);
        }
        /// <summary>
        /// Do a vertical pass of the kernel over the image. The results are placed in an intermediate image as other the result of each transformation would affect the values of the next kernel sample.
        /// </summary>
        /// <param name="srcImage">The image source data.</param>
        /// <param name="dstImage">The image destination data.</param>
        private void ApplyVertically(MonoImage <float> srcImage, MonoImage <float> dstImage)
        {
            int maxIntensity = (int)(Math.Pow(2, srcImage.BitDepth) - 1);

            Parallel.For(0, srcImage.Size.Height, new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            }, y =>
            {
                for (int x = 0; x < srcImage.Size.Width; x++)
                {
                    double accumulated = 0;

                    for (int kernelY = 0, pixelY = y - _kernelRadius; kernelY < _kernelWidth; kernelY++, pixelY++)
                    {
                        int correctedY = MirrorYIfOutOfRange(pixelY, srcImage.Size.Height);

                        accumulated += (_kernel[kernelY] * srcImage[x, correctedY]);
                    }

                    accumulated = Math.Max(0, accumulated);
                    accumulated = Math.Min(maxIntensity, accumulated);

                    dstImage[x, y] = (float)accumulated;
                }
            });
        }
        /// <summary>
        /// 使用 Mono 加载 Assembly 的镜像。
        /// </summary>
        /// <param name="monoPath">Mono 库的路径</param>
        /// <param name="assemblyPath">Assembly 镜像的路径</param>
        /// <returns>加载了数据的数组</returns>
        public static byte[] LoadImage(string monoPath, string assemblyPath)
        {
            IntPtr pData = IntPtr.Zero;

            try
            {
                // 准备可供调用 Unmanaged 代码的带有 Assembly 数据的指针
                byte[] assemblyData = File.ReadAllBytes(assemblyPath);
                pData = Marshal.AllocHGlobal(assemblyData.Length);
                Marshal.Copy(assemblyData, 0, pData, assemblyData.Length);

                // 加载 Mono 动态链接库并加载加壳过的 Assembly
                LoadLibrary(monoPath);
                MonoInitImages();
                var       pImage      = MonoOpenImage(pData, (uint)assemblyData.Length, false, IntPtr.Zero, false, assemblyPath);
                MonoImage loadedImage = (MonoImage)Marshal.PtrToStructure(pImage, typeof(MonoImage));

                // 准备用于储存 Managed 数据的数组,并写入已加载的数据
                byte[] dumpedData = new byte[loadedImage.rawDataLen];
                Marshal.Copy(loadedImage.rawData, dumpedData, 0, loadedImage.rawDataLen);
                return(dumpedData);
            }
            finally
            {
                if (pData != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pData);
                }
            }

            // FIXME: 检查 MonoImage 是否有被释放?
        }
        public void ApplyTo(MonoImage <float> image)
        {
            MonoImage <float> unsharpMask = image.Clone() as MonoImage <float>;

            unsharpMask.Apply(new ParallelGaussianBlur(_radius, _sigma));
            unsharpMask.Apply(new ParallelMultiplication(_weight));
            image.Apply(new ParallelImageSubtraction(unsharpMask));

            double normalisingFactor = 1 / (1 - _weight);

            image.Apply(new ParallelMultiplication(normalisingFactor));
        }
示例#7
0
        public void ApplyTo(MonoImage <float> image)
        {
            var maxIntensity = (Math.Pow(2, image.BitDepth) - 1);

            Parallel.For(0, image.Size.Height, new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            }, y =>
            {
                for (int x = 0; x < image.Size.Width; x++)
                {
                    var newValue = (image[x, y] - _image[x, y]);
                    newValue     = Math.Max(0, newValue);
                    newValue     = (float)Math.Min(maxIntensity, newValue);

                    image[x, y] = newValue;
                }
            });
        }
示例#8
0
        public MonoImage <float> ReadFloatFrame(string path, int width, int height, int bitDepth)
        {
            var img = new MonoImage <float>(width, height, bitDepth);

            using (var stream = File.OpenRead(path))
            {
                using (var reader = new BinaryReader(stream))
                {
                    for (var y = 0; y < height; y++)
                    {
                        for (var x = 0; x < width; x++)
                        {
                            img[x, y] = reader.ReadSingle();
                        }
                    }
                }
            }

            return(img);
        }
示例#9
0
        private void btnRun_Click(object sender, EventArgs e)
        {
            showInputImage_Click(sender, e);

            Sample targetSample = null;

            switch (state.chosenNetwork)
            {
            case 1:
                targetSample = hamming.Network.run(state.samples.ToArray(), new Sample("Test sample", state.pbState.imgArr));
                break;

            case 0:
                break;

            default:
                throw new NotImplementedException();
            }

            pbResult.Image    = MonoImage.getCompressedImage(targetSample.imgArr);
            labelResDesc.Text = targetSample.description;
        }
示例#10
0
 public ParallelImageSubtraction(MonoImage <float> image)
 {
     _image = image;
 }