PIE SDK应用掩膜中将二值图像的0和1值进行转换

技术文章 6个月前 完美者
1,218 0

标签:out   mask   form   技术   ref   reg   调用   exchange   instance   

在做水体掩膜的时候,调用PIE代码发现掩膜部分和自己想要的掩膜相反,本是掩膜去除水体变成了掩膜去除水体以外的部分,于是在应用掩膜的部分做了改动,将创建掩膜的结果(一个二值图像)中的0和1值调换,再进行应用掩膜就可以得到去除水体的效果。
创建掩膜代码原网址:
https://www.cnblogs.com/PIESat/p/10157305.html
应用掩膜代码原网址:
https://www.cnblogs.com/PIESat/p/10157375.html
大概就是在执行应用掩膜的代码中间加上一段转换0和1值的代码,将掩膜文件(二值图像)转换完成之后再执行应用掩膜,得到结果。
原创建掩膜结果二值图像如下:
技术图片
转换后的二值图像结果如下:
技术图片

`///

///应用掩膜算法测试,本算法实现了先将二值图像World7.tif进行0和1值的转化得到World9.tif,再将World.tif应用World9.tif掩模文件生成World8.tif文件
///

        #region
        //1.设置参数
        PIE.CommonAlgo.MaskApplicationExchange_Info info = new PIE.CommonAlgo.MaskApplicationExchange_Info();
        info.m_strInputFile = @"D:\Data\World.tif";
        info.m_strFileTypeCode = "GTiff";

        //先将二值图像World7.tif从tif格式转成jpg格式
        string jpegFileName = @"D:\Data\World7.jpg";
        var img = Image.FromFile(@"D:\Data\World7.tif");
        var count = img.GetFrameCount(FrameDimension.Page);
        for (int i = 0; i < count; i++)
        {
            img.SelectActiveFrame(FrameDimension.Page, i);
            img.Save(jpegFileName + ".part" + i + ".jpg");
        }
        int imageWidth = img.Width;
        int imageHeight = img.Height * count;
        Bitmap joinedBitmap = new Bitmap(imageWidth, imageHeight);
        Graphics graphics = Graphics.FromImage(joinedBitmap);
        for (int i = 0; i < count; i++)
        {
            var partImageFileName = jpegFileName + ".part" + i + ".jpg";
            Image partImage = Image.FromFile(partImageFileName);
            graphics.DrawImage(partImage, 0, partImage.Height * i, partImage.Width, partImage.Height);
            partImage.Dispose();
            //File.Delete(partImageFileName);
        }
        joinedBitmap.Save(jpegFileName);//保存jpg文件
        //MessageBox.Show("tif转jpg成功!");

        graphics.Dispose();
        joinedBitmap.Dispose();
        img.Dispose();

        string Path = jpegFileName;
        Bitmap bmp1 = new Bitmap(Path);
        for (int i = 0; i < bmp1.Width; i++)
        {
            for (int j = 0; j < bmp1.Height; j++)
            {
                Color color = bmp1.GetPixel(i, j);
                Color newColor = Color.FromArgb(1 - color.R, 1 - color.G, 1 - color.B);
                bmp1.SetPixel(i, j, newColor);
                //imgtarget.Save(Path);

                //MessageBox.Show("二值图像转换成功!")*/            
            }
        }
        bmp1.Save(@"D:\Data\transformation.jpg"); //保存转换的结果图
        //MessageBox.Show("二值图像转换成功!");


        //再将转换结果图transformation.jpg从jpg格式转成tif格式
        string Path2 = @"D:\Data\transformation.jpg";
        string tiffFileName = @"D:\Data\World9.tif";
        var tif = Image.FromFile(Path2);
        var count1 = tif.GetFrameCount(FrameDimension.Page);
        for (int i = 0; i < count; i++)
        {
            tif.SelectActiveFrame(FrameDimension.Page, i);
            tif.Save(Path2 + ".part" + i + ".tif");
        }
        int tifWidth = tif.Width;
        int tifHeight = tif.Height * count;
        Bitmap joinedBitmap1 = new Bitmap(tifWidth, tifHeight);
        Graphics graphics1 = Graphics.FromImage(joinedBitmap1);
        for (int i = 0; i < count1; i++)
        {
            var partImageFileName = Path2 + ".part" + i + ".tif";
            Image partImage1 = Image.FromFile(partImageFileName);
            graphics1.DrawImage(partImage1, 0, partImage1.Height * i, partImage1.Width, partImage1.Height);
            partImage1.Dispose();
            //File.Delete(partImageFileName);
        }
        joinedBitmap1.Save(tiffFileName);//保存tif文件
        //MessageBox.Show("jpg转tif成功!");

        graphics1.Dispose();
        joinedBitmap1.Dispose();
        tif.Dispose();
        info.m_strMaskFile = tiffFileName;
        info.m_strOutputFile = @"D:\Data\world8.tif";

        PIE.SystemAlgo.ISystemAlgo algo = PIE.SystemAlgo.AlgoFactory.Instance().CreateAlgo("PIE.CommonAlgo.dll", "PIE.CommonAlgo.MaskApplicationAlgo");
        if (algo == null) return;

        #endregion
        //2、算法执行
        PIE.SystemAlgo.ISystemAlgoEvents algoEvents = algo as PIE.SystemAlgo.ISystemAlgoEvents;
        algo.Name = "应用掩模";
        algo.Params = info; PIE.SystemAlgo.AlgoFactory.Instance().ExecuteAlgo(algo);

        ////3、结果显示
        ILayer layer = PIE.Carto.LayerFactory.CreateDefaultLayer(@"D:\Data\world8.tif");
        m_HookHelper.ActiveView.FocusMap.AddLayer(layer);
        m_HookHelper.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll);`

执行结果如下图:
技术图片

PIE SDK应用掩膜中将二值图像的0和1值进行转换

标签:out   mask   form   技术   ref   reg   调用   exchange   instance   

原文地址:https://www.cnblogs.com/valderfields3/p/13944442.html

版权声明:完美者 发表于 2020-11-08 18:02:40。
转载请注明:PIE SDK应用掩膜中将二值图像的0和1值进行转换 | 完美导航

暂无评论

暂无评论...