Exemplo n.º 1
0
        private static void DoApplyWavelet(double[,] channel, WaveletParameters waveletParameters)
        {
            var lowerCoefs  = GetCoefsByWaveletType(waveletParameters.WaveletType);
            var higherCoefs = GetHighPassFilter(lowerCoefs);
            var height      = channel.GetLength(0);
            var width       = channel.GetLength(1);
            var result      = new double[height, width];

            for (var counter = 0; counter < waveletParameters.IterationsCount; ++counter)
            {
                var line = new double[width];
                for (var i = 0; i < height; ++i)
                {
                    for (var j = 0; j < width; ++j)
                    {
                        line[j] = channel[i, j];
                    }
                    var resultLine = PairConvolution(line, lowerCoefs, higherCoefs);
                    for (var j = 0; j < width; ++j)
                    {
                        result[i, j] = resultLine[j];
                    }
                }
                line = new double[height];
                for (var j = 0; j < width; ++j)
                {
                    for (var i = 0; i < height; ++i)
                    {
                        line[i] = result[i, j];
                    }
                    var resultLine = PairConvolution(line, lowerCoefs, higherCoefs);
                    for (var i = 0; i < height; ++i)
                    {
                        result[i, j] = resultLine[i];
                    }
                }
                for (var i = 0; i < height; ++i)
                {
                    for (var j = 0; j < width; ++j)
                    {
                        channel[i % 2 == 0 ? i / 2 : height / 2 + i / 2, j % 2 == 0 ? j / 2 : width / 2 + j / 2] = result[i, j];
                    }
                }
                height >>= 1;
                width  >>= 1;
            }
            height = channel.GetLength(0);
            width  = channel.GetLength(1);
            for (var i = 0; i < height; ++i)
            {
                for (var j = 0; j < width; ++j)
                {
                    channel[i, j] = Math.Abs(channel[i, j]) < waveletParameters.Threshold ? 0.0 : channel[i, j] * 255.0;
                }
            }
        }
Exemplo n.º 2
0
 private static void SaveWavelet(BitmapSource bitmap, WaveletParameters parameters, Vector <double[, ]> channels)
 {
     using (var outputStream = File.Open(parameters.SavePath, FileMode.Create))
         using (var encodeStream = new LzmaEncodeStream(outputStream))
             using (var writer = new BinaryWriter(encodeStream))
             {
                 writer.Write(bitmap.PixelHeight);
                 writer.Write(bitmap.PixelWidth);
                 writer.Write(bitmap.DpiX);
                 writer.Write(bitmap.DpiY);
                 writer.Write((byte)parameters.WaveletType);
                 writer.Write(parameters.IterationsCount);
                 writer.Write(channels.Length);
                 for (var i = 0; i < channels.Length; ++i)
                 {
                     WriteChannel(writer, channels[i]);
                 }
             }
 }
Exemplo n.º 3
0
        public static BitmapSource ApplyWavelet(BitmapSource bitmap, WaveletParameters waveletParameters)
        {
            if (bitmap.Format != PixelFormats.Gray8)
            {
                var colors = bitmap.GetColors();
                //var yCrCb = Transformation.RGBToYCbCr(colors);
                var channels = GetChannels(colors, bitmap.PixelHeight, bitmap.PixelWidth);
                for (var channel = 0; channel < 3; ++channel)
                {
                    DoApplyWavelet(channels[channel], waveletParameters);
                }
                SaveWavelet(bitmap, waveletParameters, channels);
                return(bitmap.Create(ToColors(channels)));
            }
            var bytes  = bitmap.GetBytes();
            var channl = GetChannel(bytes, bitmap.PixelHeight, bitmap.PixelWidth);

            DoApplyWavelet(channl, waveletParameters);
            SaveWavelet(bitmap, waveletParameters, new Vector <double[, ]>(new[] { channl }));
            return(bitmap.Create(ToGrey(channl)));
        }