Exemplo n.º 1
0
        public void TestGetDCTReverseImageWithCompression()
        {
            var      image    = ImageFile.LoadBmpImage("..\\..\\..\\TestImages\\lenna_gray.bmp");
            var      dct      = new DCTCompression(image, 15);
            BmpImage dctImage = dct.GetDCTImage();

            ImageFile.SaveBmpImage(dctImage, "..\\..\\..\\TestImages\\lenna_dct_comp_modified.bmp");
            BmpImage reverseImage = dct.GetDCTReverseImage(8.0);

            ImageFile.SaveBmpImage(reverseImage, "..\\..\\..\\TestImages\\lenna_dct_reverse_comp_modified.bmp");
        }
Exemplo n.º 2
0
        private static void ModuleThree(BmpImage image)
        {
            /*
             * 图像压缩模块
             *  1.无损预测编码+符号编码
             *  2.均匀量化
             *  3.DCT变换及DCT反变换
             */

            int photoFormat = CheckPhoto(image);

            if (photoFormat == 1)
            {
                System.Console.WriteLine("\n检测图片为8bit灰度图像");
                System.Console.WriteLine("可选择的压缩模式:\n1.无损预测编码\n2.均匀量化\n3.DCT变换及DCT反变换\n0.返回");
                bool inputNum = false;
                do
                {
                    try
                    {
                        int num = Convert.ToInt32(System.Console.ReadLine());
                        if (num < 4 && num > -1)
                        {
                            inputNum = true;
                            //1.无损预测编码
                            if (num == 1)
                            {
                                System.Console.WriteLine("\n输如文件保存的路径:");
                                string          CompressPath  = System.Console.ReadLine();
                                string          savedFilePath = Path.GetDirectoryName(CompressPath);
                                string          fileName      = Path.GetFileNameWithoutExtension(CompressPath);
                                LinearPredictor lp            = new LinearPredictor(image, savedFilePath, fileName);
                                lp.predicate();
                                //system.console.writeline("{0},{1}", savedfilepath, filename);
                                System.Console.WriteLine("***************保存中***************");
                                System.Console.WriteLine("压缩文件已保存至{0}\n", CompressPath);
                                System.Console.WriteLine("完成无损预测编码!");
                            }
                            //2.均匀量化
                            else if (num == 2)
                            {
                                System.Console.WriteLine("\n压缩比(输如0为IGS量化):");
                                double CompressRatio = Convert.ToDouble(System.Console.ReadLine());
                                if (CompressRatio == 0)
                                {
                                    UniformQuantizing igs = new UniformQuantizing(image);
                                    image = igs.InverseUniformQuantizaing();
                                }
                                else
                                {
                                    UniformQuantizing uq = new UniformQuantizing(image, CompressRatio);
                                    image = uq.InverseUniformQuantizaing();
                                }
                                System.Console.WriteLine("完成均匀量化!");
                            }
                            //3.DCT变换及DCT反变换
                            else if (num == 3)
                            {
                                System.Console.WriteLine("\n设置分块大小:");
                                int blockSize = Convert.ToInt32(System.Console.ReadLine());
                                var dct       = new DCTCompression(image, blockSize);
                                image = dct.GetDCTImage();
                                //DCT反变换
                                BmpImage reverseImage = dct.GetDCTReverseImage(1.0);

                                //即将50%的高频系数用0代替 DCT反变换
                                BmpImage reverseHalfImage = dct.GetDCTReverseImage(2.0);

                                System.Console.WriteLine("\n把DCT反变换的结果存储至:");
                                string reverseImagePath = System.Console.ReadLine();
                                ImageFile.SaveBmpImage(reverseImage, @reverseImagePath);
                                System.Console.WriteLine("显示图片?(1 or 2)");
                                int display1 = Convert.ToInt32(System.Console.ReadLine());
                                if (display1 == 1)
                                {
                                    showPhoto(reverseImagePath);
                                }

                                System.Console.WriteLine("把DCT反变换(50%)的结果存储至:");
                                string reverseHalfImagePath = System.Console.ReadLine();
                                ImageFile.SaveBmpImage(reverseHalfImage, @reverseHalfImagePath);
                                System.Console.WriteLine("显示图片?(1 or 2)");
                                int display2 = Convert.ToInt32(System.Console.ReadLine());
                                if (display2 == 1)
                                {
                                    showPhoto(reverseHalfImagePath);
                                }
                                System.Console.WriteLine("完成DCT变换及反变换!");
                            }
                            else
                            {
                                ;
                            }
                        }
                        else
                        {
                            System.Console.Write("输入错误请重新输入:");
                        }
                    }
                    catch (OverflowException)
                    {
                        System.Console.WriteLine("err:转化的不是一个int型数据");
                    }
                    catch (FormatException)
                    {
                        System.Console.WriteLine("err:格式错误");
                    }
                    catch (ArgumentNullException)
                    {
                        System.Console.WriteLine("err:null");
                    }
                } while (inputNum == false);
            }
            else
            {
                System.Console.Write("\n图片形式不支持图像压缩哟~\n");
            }
        }