一 :图像的颜色空间转换 在OpenCvSharp中颜色转换函数为: Cv2.CvtColor() 参数: 参数 说明 src: 源图像,8位无符号,16位无符号或单精度浮点 dst: 输出图像,具有与src相同的大小和深度 c
          一 :图像的颜色空间转换
在OpenCvSharp中颜色转换函数为:Cv2.CvtColor()
参数:
  
    
       
  
  参数 
      说明 
    
代码:
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 中的 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
    }

根据输出像素值(0,0,255)可以看出 OpenCvSharp 三通道像素值排列为:BGR
二: 掩膜操作,提高图像对比度
使用Cv2.Filter2D函数:
  
    
       
  
  参数 
      说明 
    
代码:
 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);
        }
      }
    }

从上图可以看出,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);
      }
    }

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

对应这行代码:
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);
      }
    }

输出图像明显比输入的亮度下降。
代码:
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图像掩膜内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
