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

news/2025/2/13 21:39:19/

目录

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();

效果:


http://www.ppmy.cn/news/1255579.html

相关文章

解决vue3项目打包发布到服务器后访问页面显示空白问题

1.在 vite.config.ts 文件中 加入 base:./ 当你将 base 设置为 / 时&#xff0c;它表示你的应用程序将部署在服务器的根路径上&#xff0c;&#xff08;将 base 设置为 / 表示你的应用程序部署在服务器的根路径上&#xff0c;并且 Vite 会相应地处理资源和路由的路径…

QT 中使用 QTableView 和 QStandardItemModel 实现将数据导出到Excel 和 从Excel导入到 QTableView 的功能

简介 在Qt中&#xff0c;使用QTableView和QStandardItemModel来实现将数据导出到Excel和从Excel导入到QTableView的功能&#xff0c;而不使用第三方库&#xff08;如QXlsx&#xff09;。 效果 将 QTableView 中的数据导出到Excel //从tableview 导出到 EXcle void MainInterfa…

基于英特尔平台及OpenVINO2023工具套件优化文生图任务

当今&#xff0c;文生图技术在很多领域都得到了广泛的应用。这种技术可以将文本直接转换为逼真的图像&#xff0c;具有很高的实用性和应用前景。然而&#xff0c;由于文生成图任务通常需要大量的计算资源和时间&#xff0c;如何在英特尔平台上高效地完成这些计算是一个重要的挑…

C++ Easyx 三子棋

目录 思路 框架​编辑 读取操作 数据操作 绘制画面 游戏的数据结构 用二维数组来模拟棋盘格 赢的情况 平局情况 Code 代码细节部分 &#xff08;1&#xff09;初始化棋盘格 &#xff08;2&#xff09; 初始化棋子类型​编辑 事件处理部分 落子 框架内代码的完善 数据处…

分页助手入门以及小bug,报sql语法错误

导入坐标 5版本以上的分页助手 可以不用手动指定数据库语言&#xff0c;它会自动识别 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.3.2</version> </dependency&g…

【2023.11.29练习】希尔排序的实现

题目描述&#xff1a; 本题要求实现一趟希尔排序函数&#xff0c;待排序列的长度1<n<1000。 函数接口定义&#xff1a; void ShellInsert(SqList L,int dk); 其中L是待排序表&#xff0c;使排序后的数据从小到大排列。 ###类型定义&#xff1a; typedef int KeyTy…

MySQL进阶部分

存储引擎 MySQL体系结构图&#xff1a; 连接层&#xff1a; 最上层是一些客户端连接服务&#xff0c;主要完成一些类似于连接处理 &#xff0c;授权认证及相关的安全方案。服务器也会为安全接入的每个用户端验证它所具有的操作权限。 服务层&#xff1a; 第二层架构主要完成大…

App测试之App日志收集及adb常用命令

文章目录 前言一、adb是什么1.APP测试收集手机日志常用的工具2.adb下载与安装3.ADT/SDK/ADB是什么4.adb连接真机 二、adb常用命令三、android系统日志文件1.logcat日志文件2.logcat日志文件分析 四、分析crash & ANR 日志1.发生crash如何分析2.发生ANR如何分析 总结扩展&am…