温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么样修改和保存OpenCvSharp图像

发布时间:2020-11-18 16:13:26 来源:亿速云 阅读:219 作者:Leah 栏目:开发技术

这篇文章给大家介绍怎么样修改和保存OpenCvSharp图像,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一 :图像的颜色空间转换

在OpenCvSharp中颜色转换函数为:Cv2.CvtColor()

参数:

参数说明
src:源图像,8位无符号,16位无符号或单精度浮点
dst:输出图像,具有与src相同的大小和深度
code:颜色空间转换代码:(ColorConversionCodes)枚举类型

代码:

static void Main(string[] args)
    {
      Mat src = new Mat(@"C:\Users\whx\Desktop\opcvImage\s1.jpg ", ImreadModes.Color);

      if (src == null) //上面的加载方式如果找不到指定的文件也会报错
      {
        Console.WriteLine("加载图像失败");
        return;
      }

      Mat outImage = new Mat(); //声明一个容器,装载改变后的图像

      //参数:1 原图矩阵容器 2:保存图像的矩阵容器 3:颜色转换通道(很多,查手册)
      Cv2.CvtColor(src, outImage, ColorConversionCodes.RGB2GRAY); //转为灰度空间图像,

      //参数:1 要保存图片的路径 2:图像的矩阵容器 ,(图片保存格式个根据自定义的后缀名)
      Cv2.ImWrite(@"C:\Users\whx\Desktop\out.png", outImage);//保存到桌面

      using (new Window("src", WindowMode.Normal, src))
      using (new Window("out", WindowMode.Normal, outImage))
      {
        Cv2.WaitKey();
      }
    }

怎么样修改和保存OpenCvSharp图像

左边是源图像,右边是转为灰度空间的图像,保存路径在桌面。
转为灰度空间的类型在 OpenCvSharp 中的 ColorConversionCodes.RGB2GRAY 与 ColorConversionCodes.BRR2GRAY 都能实现,OpenCvSharp 加载进来的图像是哪一种?

代码

static void Main(string[] args)
    {
      #region 
      //自定义一张全红色的图片
      Mat src = new Mat(100,100,MatType.CV_8UC3,new Scalar(0,0,255));
      Vec3b vec3B = new Vec3b();
      //获取第一个像素的三通道像素值
      vec3B.Item0 = src.At<Vec3b>(0, 0)[0];
      vec3B.Item1 = src.At<Vec3b>(0, 0)[1];
      vec3B.Item2 = src.At<Vec3b>(0, 0)[2];
      Console.WriteLine("0 :"+vec3B.Item0); //控制台输出
      Console.WriteLine("1 :"+vec3B.Item1);
      Console.WriteLine("2 :"+vec3B.Item2);

      using (new Window("src image", WindowMode.FreeRatio, src)) //创建一个新窗口显示图像
      {
        Cv2.WaitKey();
      }
      #endregion

    }

怎么样修改和保存OpenCvSharp图像

根据输出像素值(0,0,255)可以看出 OpenCvSharp 三通道像素值排列为:BGR

二: 掩膜操作,提高图像对比度

使用Cv2.Filter2D函数:

参数说明
src:输入的源图像
dst:输出图像,一个Mat 对象,与原图图像具有相同的大小和图像深度
ddepth:目标图像的所需深度。如果它是负的,它就是与src.depth()相同,不确定时就填 -1
kernel:卷积核
anchor:内核的锚点,表示经过过滤的点的相对位置. (- 1,-1)表示锚位于内核中心
delta:在卷积过程中,该值会加到每个像素上。默认情况下,这个值为 0 。相当于一个增益值
borderType:指定边缘处理的方法,比较复杂,选择默认值即可。是枚举类型

代码:

 static void Main(string[] args)
    {
      using (Mat src = new Mat(@"C:\Users\whx\Desktop\opcvImage\m4.jpg", ImreadModes.AnyColor | ImreadModes.AnyDepth))
      using (Mat dst = new Mat())
      {
        //定义一个掩膜矩阵
        InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } });

        //进行掩膜操作,提高图片亮度
        Cv2.Filter2D(src, dst, -1, kernel, new Point(-1, 1), 0, BorderTypes.Default);

        using (new Window("OutputImage", WindowMode.Normal, dst))
        using (new Window("InputImage",WindowMode.Normal,src))
        {
          Cv2.WaitKey(0);
        }
      }
    }

怎么样修改和保存OpenCvSharp图像

从上图可以看出,OutputImage 比 Inputimage 的亮度明显增强。

三:利用指针修改图像像素值,进行图像对比度处理

代码:

unsafe static void Main(string[] args)
    {
      Mat src, dst;
      src = Cv2.ImRead(@"C:\Users\whx\Desktop\opcvImage\m4.jpg"); //读取图片
      if (src.Data == null)
      {
        Console.WriteLine("加载图像失败");
        return;
      }
      #region 
      /*
       * 两种判断方法都可以
       */

      //if(src.Empty()) //如果数组没有元素,则返回true。
      //{
      //  Console.WriteLine("加载图像失败");
      //  return;
      //}

      //显示方式2
      //new Window("Input Image", WindowMode.FreeRatio);
      //Cv2.ImShow("Input Image",src);
      //Cv2.WaitKey(0); 
      #endregion

      #region 指针操作增加饱和度
      int clos = (src.Cols - 1) * src.Channels(); //RGB有三个通道,(图像的列(长度) * 图像的通道数)
      int rows = src.Rows; //行(高度)
      int offsetx = src.Channels();

      dst = new Mat(src.Size(), src.Type()); //初始化

      for (int row = 1; row < rows - 1; row++)
      {
        IntPtr current = src.Ptr(row); //当前行
        byte* curr = (byte*)current.ToPointer();

        IntPtr upRow = src.Ptr(row - 1);//上一行
        byte* up = (byte*)upRow.ToPointer();

        IntPtr nextRow = src.Ptr(row + 1);//下一行
        byte* next = (byte*)nextRow.ToPointer();

        IntPtr outPut = dst.Ptr(row); //输出
        byte* opt = (byte*)outPut.ToPointer();

        for (int col = offsetx; col < clos; col++)
        {
          opt[col] = (byte)Saturate.Saturate_cast((5 * curr[col] - (curr[col - offsetx] + curr[col + offsetx] + up[col] + next[col])));
        }
      }
      #endregion

      
      using (new Window("OutputImage", WindowMode.FreeRatio, dst))
      using (new Window("InputImage", WindowMode.FreeRatio, src))
      {
        Cv2.WaitKey(0);
      }
    }
unsafe static void Main(string[] args)
    {
      Mat src, dst;
      src = Cv2.ImRead(@"C:\Users\whx\Desktop\opcvImage\m4.jpg"); //读取图片
      if (src.Data == null)
      {
        Console.WriteLine("加载图像失败");
        return;
      }
      #region 
      /*
       * 两种判断方法都可以
       */

      //if(src.Empty()) //如果数组没有元素,则返回true。
      //{
      //  Console.WriteLine("加载图像失败");
      //  return;
      //}

      //显示方式2
      //new Window("Input Image", WindowMode.FreeRatio);
      //Cv2.ImShow("Input Image",src);
      //Cv2.WaitKey(0); 
      #endregion

      #region 指针操作增加饱和度
      int clos = (src.Cols - 1) * src.Channels(); //RGB有三个通道,(图像的列(长度) * 图像的通道数)
      int rows = src.Rows; //行(高度)
      int offsetx = src.Channels();

      dst = new Mat(src.Size(), src.Type()); //初始化

      for (int row = 1; row < rows - 1; row++)
      {
        IntPtr current = src.Ptr(row); //当前行
        byte* curr = (byte*)current.ToPointer();

        IntPtr upRow = src.Ptr(row - 1);//上一行
        byte* up = (byte*)upRow.ToPointer();

        IntPtr nextRow = src.Ptr(row + 1);//下一行
        byte* next = (byte*)nextRow.ToPointer();

        IntPtr outPut = dst.Ptr(row); //输出
        byte* opt = (byte*)outPut.ToPointer();

        for (int col = offsetx; col < clos; col++)
        {
          opt[col] = (byte)Saturate.Saturate_cast((5 * curr[col] - (curr[col - offsetx] + curr[col + offsetx] + up[col] + next[col])));
        }
      }
      #endregion

      
      using (new Window("OutputImage", WindowMode.FreeRatio, dst))
      using (new Window("InputImage", WindowMode.FreeRatio, src))
      {
        Cv2.WaitKey(0);
      }
    }

怎么样修改和保存OpenCvSharp图像

效果与上面使用API操作基本一致。这里是由一个计算公式:

怎么样修改和保存OpenCvSharp图像

对应这行代码:

 opt[col] = (byte)Saturate.Saturate_cast((5 * curr[col] - (curr[col - offsetx] + curr[col + offsetx] + up[col] + next[col])));

四:减少图像亮度

代码:

static void Main(string[] args)
    {
      Mat src, dst;
      src = Cv2.ImRead(@"C:\Users\whx\Desktop\opcvImage\m4.jpg"); //读取图片
      dst = new Mat(src.Size(), src.Type()); //初始化
      src.CopyTo(dst); //把原图像拷贝到 dst 中
      for (int i = 0; i < src.Rows; i++)
      {
        for (int j = 0; j < src.Cols; j++)
        {
          Vec3b color = new Vec3b();//新建Vec3b对象(字节的三元组(System.Byte))
          color.Item0 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item0 - 50));// B 读取原来的通道值并减50
          color.Item1 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item1 - 50)); // G
          color.Item2 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item2 - 50)); // R
          src.Set(i, j, color);
        }
      }

      using (new Window("Input", WindowMode.FreeRatio, dst))
      using (new Window("Output", WindowMode.FreeRatio, src))
      {
        Cv2.WaitKey(0);
      }
    }

怎么样修改和保存OpenCvSharp图像

输出图像明显比输入的亮度下降。

代码:

static void Main(string[] args)
    {
      Mat src, dst;
      src = Cv2.ImRead(@"C:\Users\whx\Desktop\opcvImage\m4.jpg"); //读取图片
      dst = new Mat(src.Size(), src.Type()); //初始化
      src.CopyTo(dst); //把原图像拷贝到 dst 中
      for (int i = 0; i < src.Rows; i++)
      {
        for (int j = 0; j < src.Cols; j++)
        {
          Vec3b color = new Vec3b();//新建Vec3b对象(字节的三元组(System.Byte))
          color.Item0 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item0 + 50));//读取原来的通道值并加50
          color.Item1 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item1 + 50));
          color.Item2 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item2 + 50));
          src.Set(i, j, color);
        }
      }
      using (new Window("Input", WindowMode.FreeRatio, dst))
      using (new Window("Output", WindowMode.FreeRatio, src))
      {
        Cv2.WaitKey(0);
      }
    }

怎么样修改和保存OpenCvSharp图像

输出图像明显比输入的亮度提高很多。

关于怎么样修改和保存OpenCvSharp图像就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI