在上一节中可以看到一个类Mat,盲猜可以知道其用于表征一张图。Mat是Matrix的缩写,OpenCV的世界把图像抽象成矩阵。所以后续所有对于图像的操作,转变成为矩阵的数学。
对于一张图片,其矩阵的大小为 宽度高度通道数,如BGR(opencv顺序是BGR,如果要在winform/wpf或者unity中展示一定要记得需要转换成rgb)。其内存中的存储方式如下(此处以BGR三通道示例)
Lesson2-1.jpg
读取图像
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lesson1
{
class Program
{
static void Main(string[] args)
{
Mat lena = Cv2.ImRead("lena.jpg");
Mat lenaGray = Cv2.ImRead("lena.jpg", ImreadModes.Grayscale);
Cv2.ImShow("lena", lena);
Cv2.ImShow("lenaGray", lenaGray);
Cv2.WaitKey();
}
}
}
Lesson2-2.jpg
ImRead是读取图像的函数,该函数将图像加载到内存的matrix中。
//
// 摘要:
// Loads an image from a file.
//
// 参数:
// fileName:
// Name of file to be loaded.
//
// flags:
// Specifies color type of the loaded image
public static Mat ImRead(string fileName, ImreadModes flags = ImreadModes.Color);
//
// 摘要:
// Specifies colorness and Depth of the loaded image
[Flags]
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,
//
// 摘要:
// If set, always convert image to the single channel grayscale image and the image
// size reduced 1/2.
ReducedGrayscale2 = 16,
//
// 摘要:
// If set, always convert image to the 3 channel BGR color image and the image size
// reduced 1/2.
ReducedColor2 = 17,
//
// 摘要:
// If set, always convert image to the single channel grayscale image and the image
// size reduced 1/4.
ReducedGrayscale4 = 32,
//
// 摘要:
// If set, always convert image to the 3 channel BGR color image and the image size
// reduced 1/4.
ReducedColor4 = 33,
//
// 摘要:
// If set, always convert image to the single channel grayscale image and the image
// size reduced 1/8.
ReducedGrayscale8 = 64,
//
// 摘要:
// If set, always convert image to the 3 channel BGR color image and the image size
// reduced 1/8.
ReducedColor8 = 65,
//
// 摘要:
// If set, do not rotate the image according to EXIF's orientation flag.
IgnoreOrientation = 128
}
写入图像
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lesson1
{
class Program
{
static void Main(string[] args)
{
Mat lenaGray = Cv2.ImRead("lena.jpg", ImreadModes.Grayscale);
Cv2.ImWrite("lenaGray.jpg", lenaGray);
Cv2.WaitKey();
}
}
}
Lesson2-3.jpg
Imwrite是写入图像的函数。
//
// 摘要:
// Saves an image to a specified file.
//
// 参数:
// fileName:
// Name of the file.
//
// img:
// Image to be saved.
//
// prms:
// Format-specific save parameters encoded as pairs
public static bool ImWrite(string fileName, Mat img, int[]? prms = null);
其中第一个参数是目标路径,其通过扩展名来表征要保存成的图片格式,如jpeg、png等。
读取视频
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Lesson1
{
class Program
{
static void Main(string[] args)
{
FrameSource frameSrc = Cv2.CreateFrameSource_Video("bach.mp4");
Mat mat = new Mat();
Task.Run(() =>
{
frameSrc.NextFrame(mat);
while (!mat.Empty())
{
Cv2.ImShow("bach", mat);
Cv2.WaitKey(20);// 每一帧20ms
frameSrc.NextFrame(mat);
}
Cv2.DestroyWindow("bach");
});
Console.ReadKey();
}
}
}
此处读取视频使用的是CreateFrameSource_Video。视频还可以直接使用VideoCa
读取摄像头 CreateFrameSource_Camera
//
// 参数:
// deviceId:
public static FrameSource CreateFrameSource_Camera(int deviceId);
其中deviceid是摄像头的设备索引,如果只有一个摄像头,一般设置为零。