分享

c# opencvsharp学习笔记(2)...

 行走在理想边缘 2020-11-13
Mat src = new Mat("lenna.png", ImreadModes.AnyColor);//src就是source源,dst destination,目的地。
Mat src = Cv2.ImRead("lenna.png", ImreadModes.GrayScale);

这两个读取图片最常用的方式,个人比较喜欢用前者。

这是imreadmodes的几个枚举,记住第4个是转成灰度图就行了,具体的看下面

 public enum ImreadModes
    {
        //
        // 摘要:
        //     If set, return the loaded image as is (with alpha channel, otherwise it gets
        //     cropped).
        Unchanged = -1,
        //
        // 摘要:
        //     If set, always convert image to the single channel grayscale image.
        GrayScale = 0,
        //
        // 摘要:
        //     If set, always convert image to the 3 channel BGR color image.
        Color = 1,
        //
        // 摘要:
        //     If set, return 16-bit/32-bit image when the input has the corresponding depth,
        //     otherwise convert it to 8-bit.
        AnyDepth = 2,
        //
        // 摘要:
        //     If set, the image is read in any possible color format.
        AnyColor = 4,
        //
        // 摘要:
        //     If set, use the gdal driver for loading the image.
        LoadGdal = 8
    }

2.显示图片

Cv2.ImShow("src image", src);
using (new Window("src image", src)) 

两种效果一样,基本上都是使用第一种,如果要在两个不同的窗口显示图片,窗口名字不能取一样,否则会直接在那个窗口上更新图片。

3.保存图片

 Cv2.ImWrite("lenna1.png", dst);//和exe在同一个文件夹下

4.flip翻转图像

  Cv2.Flip(src,dst,  FlipMode.XY);//水平垂直翻转

5.写字

  Cv2.PutText(src,                                      //目标图像
                        "lenna",                        //字符串
                        new Point(0,80),                //位置,注意这是字符串左下角的位置
                        HersheyFonts.HersheyComplex,    //字体类型
                        5,                              //字体大小   
                        Scalar.White);                  //颜色

还有一个重载,第6个是参数thinkness,也就是厚度默认1,下面圆也有。

6.圆

  //空心圆
            Cv2.Circle(src,                 //目标图像
                        new Point(80, 80),  //中心点坐标
                        60,                 //半径
                        Scalar.White);      //颜色
            //实心圆
            Cv2.Circle(dst, new Point(80, 80), 30, Scalar.White,65);//多一个厚度,后面还有两个参数是LineTypes平滑程度,和shift缩小倍率

因为线条宽度是向半径两边展开的,所以要画实心圆要把半径变成原来的一半。

7.线

Cv2.Line(src, 0, 0, 80, 80, Scalar.White);//目标图像,点1x,y,点2x,y,颜色

有多个重载,同样有平滑程度和缩小倍率。

8,框

Rect rect = new Rect(0, 0, 100, 100);
            Cv2.Rectangle(src, rect, Scalar.White);
有多个重载,同样有平滑程度和缩小倍率。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约