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

Posted valderfields3

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PIE SDK应用掩膜中将二值图像的0和1值进行转换相关的知识,希望对你有一定的参考价值。

在做水体掩膜的时候,调用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:DataWorld.tif";
        info.m_strFileTypeCode = "GTiff";

        //先将二值图像World7.tif从tif格式转成jpg格式
        string jpegFileName = @"D:DataWorld7.jpg";
        var img = Image.FromFile(@"D:DataWorld7.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	ransformation.jpg"); //保存转换的结果图
        //MessageBox.Show("二值图像转换成功!");


        //再将转换结果图transformation.jpg从jpg格式转成tif格式
        string Path2 = @"D:Data	ransformation.jpg";
        string tiffFileName = @"D:DataWorld9.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:Dataworld8.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:Dataworld8.tif");
        m_HookHelper.ActiveView.FocusMap.AddLayer(layer);
        m_HookHelper.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll);`

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













以上是关于PIE SDK应用掩膜中将二值图像的0和1值进行转换的主要内容,如果未能解决你的问题,请参考以下文章

matlab中将灰度图转化为二值图后,有如何转化为灰度图

PIE SDK微分锐化

envi classic根据shp文件制作栅格二值(0/1)掩膜

opencv中加载掩膜为啥是灰度图

PIE SDK图像旋转

PIE SDK彩色空间变换