当前位置: 首页 > news >正文

【盘古100Pro+开发板实验例程】FPGA学习 | 3X3图像矩阵生成 | 图像实验指导手册

本原创文章由深圳市小眼睛科技有限公司创作,版权归本公司所有,如需转载,需授权并注明出处(www.meyesemi.com)

1. 实验简介

实验目的:

      实现 3X3 图像矩阵对应 9 个像素点图像数据的读取。

实验环境:

      Window11

      PDS2022.SP6.4

      Modelsim10.6c

      MatlabR2023b(可以用别的版本)

硬件环境:

      MES2L676-100HP

2. 实验原理

      在数字图像处理中,部分算法需根据每个像素点及其周围一定范围内的像素点数据进行计算,图像矩阵的 生成至关重要。本系列图像处理实验主要采用 3X3 图像矩阵实现对应图像处理算法。

      例如一幅完整的 5X5 图像如下图所示,每个方格表示一个像素点,方格内的数字表示对应像素点的像素数据。

      3X3 图像矩阵的行对应图像一行中三个连续的像素点,3X3 图像矩阵的列对应图像一列中三个连续的像素点,矩阵的元素对应图像的像素数据。在 5X5 的图像中,按图像的扫描方式从左到右,从上到下地获取对应 3X3 图像矩阵的数据

      在图像处理算法中,若通过每个 3X3 图像矩阵计算中心像素点的像素数据,为保证图像分辨率不变,在图 像左侧两列和上方两行的位置对像素数据补“0”操作,如下图所示:

      基于以上分析,在图像数据按行输入的情况下,为获取 3X3 图像矩阵需同时输出三行图像中的数据,因此 采用 FPGA 的缓存资源(fifo/ram)将接收的每一行数据进行缓存,并且在输出 3X3 图像矩阵数据时输出当前 第 n 行对应位置的数据、第 n-1 行对应位置的数据以及第 n-2 行对应位置的数据。另外,为缓存第 n-1 行图像 数据和第 n-2 行图像数据,需调用 2 个 fifo 缓存模块,并且实现当前数据缓存到第 n-1 行图像数据缓存 fifo, 从第 n-1 行图像数据缓存 fifo 读出后再缓存到第 n-2 行图像数据缓存 fifo,确保 3X3 矩阵输出时读取两个 fifo可同时获得第 n-1 行和第 n-2 行的数据。FPGA 实现 3X3 图像矩阵具体方案框图如下:

按行输入的图像数据 video_data 在数据有效期间缓存到 fifo1,同时 fifo1 读出一行数据进入 fifo2 缓存, 且 fifo2 同时读出一行数据,若 fifo1 或 fifo2 为空则输出 0。数据输出过程如下图所示:

此外,实现同时输出连续三行图像数据后,需注意所输出的三个数据对应在一行中的位置保持一致。

3. 接口列表

以下为 matrix_3x3.v 模块的接口列表。

4. 工程说明

4.1. 代码模块说明

3X3 图像矩阵生成模块代码如下所示:

// 3x3 矩阵生成
module matrix_3x3
#(parameter IMG_WIDTH = 11'd1920,parameter IMG_HEIGHT = 11'd1080
)
(input wire video_clk,input wire rst_n,input wire video_vs,input wire video_de,input wire [7:0] video_data,  // 3x3 矩阵输出output wire matrix_de,output reg [7:0] matrix11,output reg [7:0] matrix12,output reg [7:0] matrix13,output reg [7:0] matrix21,output reg [7:0] matrix22,output reg [7:0] matrix23,output reg [7:0] matrix31,output reg [7:0] matrix32,output reg [7:0] matrix33
);// wire definewire [7:0] line3_data;  // 第三行数据wire [7:0] line2_data;  // 第二行数据wire [7:0] line1_data;  // 第一行数据wire wr_fifo_en;        // 写 FIFO 使能wire rd_fifo_en;        // 读 FIFO 使能// reg definereg [10:0] x_cnt;reg [10:0] y_cnt;// 列计数always @(posedge video_clk or negedge rst_n) beginif (!rst_n)x_cnt <= 11'd0;else if (x_cnt == IMG_WIDTH - 1)  // 计数一行x_cnt <= 11'd0;else if (video_de)  // 数据有效x_cnt <= x_cnt + 1'b1;elsex_cnt <= x_cnt;end// 行计数always @(posedge video_clk or negedge rst_n) beginif (!rst_n)y_cnt <= 11'd0;else if (y_cnt == IMG_HEIGHT - 1 && x_cnt == IMG_WIDTH - 1)  // 计数一帧y_cnt <= 11'd0;else if (x_cnt == IMG_WIDTH - 1)  // 数据有效y_cnt <= y_cnt + 1'b1;elsey_cnt <= y_cnt;end

以上代码根据模块接口列表,定义模块端口及对输入数据进行行/列计数

// 3x3 矩阵 第一行和最后一行无法构成
assign wr_fifo_en = video_de && (y_cnt < IMG_HEIGHT-1);
assign rd_fifo_en = video_de && (y_cnt > 0);reg wr_fifo_en_1d;
reg [7:0] video_data_1d;always @(posedge video_clk or negedge rst_n) beginif (!rst_n) beginwr_fifo_en_1d <= 1'd0;video_data_1d <= 8'd0;endelse beginwr_fifo_en_1d <= wr_fifo_en;video_data_1d <= video_data;end
end// 通过两个 FIFO 与当前的输入一起构成 3x3 矩阵
assign line3_data = video_data_1d;// fifo1
fifo_line_buffer u1_fifo_line_buffer (.wr_clk      (video_clk),    // input.wr_rst      (~rst_n),       // input.wr_en       (wr_fifo_en),   // input.wr_data     (video_data),   // input [7:0].wr_full     (),             // output.almost_full (),             // output.rd_clk      (video_clk),    // input.rd_rst      (~rst_n),       // input.rd_en       (rd_fifo_en),   // input.rd_data     (line2_data),   // output [7:0].rd_empty    (u1_empty),     // output.almost_empty()              // output
);// fifo2
fifo_line_buffer u2_fifo_line_buffer (.wr_clk      (video_clk),    // input.wr_rst      (~rst_n),       // input.wr_en       (wr_fifo_en_1d),// input.wr_data     (line2_data),   // input [7:0].wr_full     (),             // output.almost_full (),             // output.rd_clk      (video_clk),    // input.rd_rst      (~rst_n),       // input.rd_en       (rd_fifo_en),   // input.rd_data     (line1_data),   // output [7:0].rd_empty    (),             // output.almost_empty()              // output
);

以上代码实现两级 fifo 对图像数据按行缓存。为确保三行数据输出保持同步,需注意 fifo 读出数据相较于 fifo 读使能信号延时 1 个时钟周期,因此 line3_data 数据需将当前输入数据延迟 1 个时钟周期,与 fifo 输出的 line2_data 和 line1_data 保持同步,并且 fifo1 输出数据写入 fifo2 时也需要将 fifo1 的读使能信号延时 1 个 时钟周期再作为 fifo2 的写使能信号。

// 数据延迟 de 延迟
reg video_de_d0;
reg video_de_d1;always @(posedge video_clk or negedge rst_n) beginif (!rst_n) beginvideo_de_d0 <= 1'd0;video_de_d1 <= 1'd0;endelse beginvideo_de_d0 <= video_de;video_de_d1 <= video_de_d0;end
end// 矩阵数据生成
always @(posedge video_clk or negedge rst_n) beginif (!rst_n) begin{matrix11, matrix12, matrix13} <= 24'd0;{matrix21, matrix22, matrix23} <= 24'd0;{matrix31, matrix32, matrix33} <= 24'd0;endelse if (video_de_d0) begin{matrix11, matrix12, matrix13} <= {matrix12, matrix13, line1_data};{matrix21, matrix22, matrix23} <= {matrix22, matrix23, line2_data};{matrix31, matrix32, matrix33} <= {matrix32, matrix33, line3_data};endelse begin{matrix11, matrix12, matrix13} <= 24'd0;{matrix21, matrix22, matrix23} <= 24'd0;{matrix31, matrix32, matrix33} <= 24'd0;end
end// 矩阵数据有效输出
assign matrix_de = video_de_d1;
assign matrix_vs = video_vs;endmodule

以上代码实现 3X3 图像矩阵的生成。三行数据同时输出,每一行的数据按“打拍”方式移位实现矩阵的位 置的移动,需注意输出数据在生成矩阵时产生 1 个时钟周期的延时,因此最终数据的矩阵有效信号 matrix_de 也需延时 1 个时钟周期。

4.2. 代码仿真

      在 modelsim 平台,matrix_3x3 模块的仿真激励模块 matrix_tb.v 生成 5x5 的图像数据源,仿真分析结果如下:

5x5 的图像数据源符合以下图像示意图:

3X3 图像矩阵的生成符合预期。

http://www.dtcms.com/a/314376.html

相关文章:

  • Exporters | 安装mysqld_exporter
  • SpringCloud相关知识
  • 晨控CK-GW08S与汇川AC系列PLC配置Ethernet/IP通讯连接手册
  • DevOps平台大比拼:Gitee、Jenkins与CircleCI如何选型?
  • 乐思 AI 智能识别平台(基于 YOLO,.NET+Vue3 开发)开源指南
  • 【秋招笔试】2025.08.03-拼多多笔试真题-第二题
  • 自然语言理解领域算法模型演进图谱
  • 2025最新、UI媲美豆包、DeepSeek等AI大厂的AIGC系统 - IMYAI源码部署教程
  • 多级表头的导出
  • 人大金仓数据库常见问题(持续更新)
  • SJW-app-1
  • [Sensors]BMI270 FIFO的使用
  • 对于类似std::shared_ptr但有可能空悬的指针使用std::weak_ptr: Effective Modern C++ 条款20
  • Shell 脚本发送信号给 C 应用程序,让 C 应用程序回收线程资源后自行退出。
  • Linux服务器管理MySQL数据库的常见命
  • Spring AI 系列之三十三 - Spring AI Alibaba-Graph框架之人类反馈
  • 区块链基础之Merkle B+树
  • 【Spring】SpringBoot自动注入原理分析,@SpringBootApplication、@EnableAutoConfiguration详解
  • Java类与对象练习题
  • 运动想象 (MI) 分类学习系列 (18) : MSVTNet
  • 一(1)关于单链表中的疑问
  • Spring AI实战:SpringBoot项目结合Spring AI开发——提示词(Prompt)技术与工程实战详解
  • SAP-ABAP:ABAP Open SQL 深度解析:核心特性、性能优化与实践指南
  • 设计模式 -> 策略模式(Strategy Pattern)
  • 2025年8月4日私鱼创作平台v1.0.4公测版更新发布-完成大部分功能包含关注创作者以及发布作品及合集功能优雅草科技
  • 06 基于sklearn的机械学习-欠拟合、过拟合、正则化、逻辑回归
  • 线程互斥锁:守护临界区的关键
  • 可编辑190页PPT | 某科技集团数字化转型SAP解决方案
  • Vue 3 版本的 JWT 单点登录 (SSO) 实现
  • 国家科学技术奖答辩PPT案例_科技进步奖ppt制作_技术发明奖ppt设计美化_自然科学奖ppt模板 | WordinPPT