OpenCvSharp从入门到实践-(06)创建图像

目录

1、创建图像

1.1实例1-创建黑色图像

1.2实例2-创建白色图像

1.3实例3-创建随机像素的雪花点图像

2、图像拼接

2.1水平拼接图像

2.2垂直拼接图像

2.3实例4-垂直和水平两种方式拼接两张图像


在OpenCV中,黑白图像其实就是一个二维数组,彩色图像就是一个三位数组。数组中的每个元素就是图像中对应位置的像素值。

1、创建图像

在黑白图像中,像素值为0表示纯黑色,像素值为255表示纯白色

1.1实例1-创建黑色图像

创建一个100行、200列(即宽200、高100)的黑色图像,代码如下:

int width = 200;

int height = 100;

Mat img = Mat.Zeros(height, width, MatType.CV_8UC1);

Cv2.ImShow("img", img);

Cv2.WaitKey();

Cv2.DestroyAllWindows();

效果

方式二,效果同上,代码如下:

int width = 200;

int height = 100;

int[] array = new int[200 * 100];

Mat img = new Mat(height, width, MatType.CV_8UC1, array);

Cv2.ImShow("img", img);

Cv2.WaitKey();

Cv2.DestroyAllWindows();

1.2实例2-创建白色图像

创建白色图像有多种方式:

第一种:利用Mat构造函数直接创建;

第二种:利用Mat.Ones方法创建一个像素值为1的图像,然后将图像中所有像素值乘以255;

第三种:创建一个所有值都为255的数组,利用数组创建图像;

第四种:利用SetTo方法;

第一种代码如下:

int width = 200;

int height = 100;

Mat img = new Mat(new Size(width, height), MatType.CV_8UC1, Scalar.White);

Cv2.ImShow("img", img);

Cv2.WaitKey();

Cv2.DestroyAllWindows();

第二种代码如下:

int width = 200;

int height = 100;

Mat img = Mat.Ones(height, width, MatType.CV_8UC1) * 255;

Cv2.ImShow("img", img);

Cv2.WaitKey();

Cv2.DestroyAllWindows();

第三种代码如下:

int width = 200;

int height = 100;

byte[] array = new byte[width* height]; // 定义了长度为width* height的数组

for (int i = 0; i < array.Length; i++)

{

array[i] = 255; // 将每个元素赋值为255

}

Mat img = new Mat(height, width, MatType.CV_8UC1, array);

Cv2.ImShow("img", img);

Cv2.WaitKey();

Cv2.DestroyAllWindows();

第四种代码如下:

int width = 200;

int height = 100;

Mat img = new Mat(new Size(width, height), MatType.CV_8UC1);

img.SetTo(new Scalar(255, 255, 255)); // 将背景设置为白色

Cv2.ImShow("img", img);

Cv2.WaitKey();

Cv2.DestroyAllWindows();

效果:

1.3实例3-创建随机像素的雪花点图像

代码如下:

int width = 200;

int height = 100;

Mat img = new Mat(height, width, MatType.CV_8UC1);

Random random = new Random();

for (int i = 0; i < height; i++)

{

for (int j = 0; j < width; j++)

{

byte blue = (byte)(random.NextDouble() * 256);

byte green = (byte)(random.NextDouble() * 256);

byte red = (byte)(random.NextDouble() * 256);

Vec3b color = new Vec3b((byte)blue, (byte)green, (byte)red);

img.At<Vec3b>(i, j) = color;

}

}

Cv2.ImShow("img", img);

Cv2.WaitKey();

Cv2.DestroyAllWindows();

效果:

改变一行代码,创建彩色的随机图像,代码如下:

int width = 200;

int height = 100;

Mat img = new Mat(height, width, MatType.CV_8UC3);

Random random = new Random();

for (int i = 0; i < height; i++)

{

for (int j = 0; j < width; j++)

{

byte blue = (byte)(random.NextDouble() * 256);

byte green = (byte)(random.NextDouble() * 256);

byte red = (byte)(random.NextDouble() * 256);

Vec3b color = new Vec3b((byte)blue, (byte)green, (byte)red);

img.At<Vec3b>(i, j) = color;

}

}

Cv2.ImShow("img", img);

Cv2.WaitKey();

Cv2.DestroyAllWindows();

效果:

2、图像拼接

OpenCvSharp中提供Cv2.HConcat、Cv2.VConcat方法实现图像拼接。

2.1水平拼接图像

Cv2.HConcat方法可以对图像进行水平拼接(或者叫横向拼接),其函数如下:

public static void HConcat(IEnumerable<Mat> src, OutputArray dst)

说明:

摘要:

Applies horizontal concatenation to given matrices.

参数:

src:

input array or vector of matrices. all of the matrices must have the same number

of rows and the same depth.

dst:

output array. It has the same number of rows and depth as the src, and the sum

of cols of the src.

2.2垂直拼接图像

Cv2.VConcat可以对图像进行垂直拼接(或者叫纵向拼接),其函数如下:

public static void VConcat(IEnumerable<Mat> src, OutputArray dst)

说明:

摘要:

Applies vertical concatenation to given matrices.

参数:

src:

input array or vector of matrices. all of the matrices must have the same number

of cols and the same depth.

dst:

output array. It has the same number of cols and depth as the src, and the sum

of rows of the src.

2.3实例4-垂直和水平两种方式拼接两张图像

代码如下:

Mat mat = Cv2.ImRead("test01.jpg");

Cv2.ImShow("src", mat);

Mat dst = new Mat();

Cv2.VConcat(new Mat[] { mat, mat }, dst);

Cv2.ImShow("img_v", dst);

Cv2.HConcat(new Mat[] { mat, mat }, dst);

Cv2.ImShow("img_h", dst);

Cv2.WaitKey();

Cv2.DestroyAllWindows();

效果:

相关推荐
ss.li1 分钟前
TripGenie:畅游济南旅行规划助手:个人工作纪实(二十二)
javascript·人工智能·python
小天才才11 分钟前
前沿论文汇总(机器学习/深度学习/大模型/搜广推/自然语言处理)
人工智能·深度学习·机器学习·自然语言处理
新加坡内哥谈技术41 分钟前
Meta计划借助AI实现广告创作全自动化
运维·人工智能·自动化
西猫雷婶1 小时前
pytorch基本运算-导数和f-string
人工智能·pytorch·python
Johny_Zhao1 小时前
华为MAAS、阿里云PAI、亚马逊AWS SageMaker、微软Azure ML各大模型深度分析对比
linux·人工智能·ai·信息安全·云计算·系统运维
顽强卖力1 小时前
第二十八课:深度学习及pytorch简介
人工智能·pytorch·深度学习
述雾学java1 小时前
深入理解 transforms.Normalize():PyTorch 图像预处理中的关键一步
人工智能·pytorch·python
武子康1 小时前
大数据-276 Spark MLib - 基础介绍 机器学习算法 Bagging和Boosting区别 GBDT梯度提升树
大数据·人工智能·算法·机器学习·语言模型·spark-ml·boosting
要努力啊啊啊1 小时前
使用 Python + SQLAlchemy 创建知识库数据库(SQLite)—— 构建本地知识库系统的基础《一》
数据库·人工智能·python·深度学习·自然语言处理·sqlite
武子康1 小时前
大数据-277 Spark MLib - 基础介绍 机器学习算法 Gradient Boosting GBDT算法原理 高效实现
大数据·人工智能·算法·机器学习·ai·spark-ml·boosting