FPGA 车牌识别与 Modelsim 仿真
本文介绍基于 Xilinx Artix-7 开发板的车牌识别系统设计与 Modelsim 仿真验证。使用 Vivado 2019.2 工具,FPGA 芯片为 XC7A35T,摄像头型号 OV5640。
一、硬件基础
选用 Artix-7 开发板,FPGA 芯片为 XC7A35T。连接 OV5640 摄像头和 LCD 显示屏(800×480),即可运行车牌识别程序。
二、功能实现解析
1. 图像采集
图像采集模块负责从 OV5640 获取数据。通过 Verilog 配置摄像头分辨率和帧率。
module ov5640_interface (
input wire clk,
input wire rst,
input wire vsync,
input wire href,
input wire pixel_clk,
input wire [7:0] data,
output reg [23:0] img_data,
output reg img_valid
);
always @(posedge pixel_clk or posedge rst) begin
if (rst) begin
img_data <= 24'd0;
img_valid <= 1'b0;
end else if (vsync && href) begin
img_data <= {data, data, data};
img_valid <= 1'b1;
end else begin
img_valid <= 1'b0;
end
end
endmodule
该模块利用 pixel_clk 采样像素数据 data,当 vsync 和 href 有效时输出 24 位 RGB 格式数据及有效信号。
2. RGB 转 Ycbcr
将 RGB 图像转换为 YCbCr 格式以便后续处理。
module rgb_to_ycbcr (
input wire [23:0] rgb,
output reg [7:0] y,
output reg [7:0] cb,
output reg [7:0] cr
);
always @(*) begin
y = (8'd65 * rgb[23:16] + 8'd129 * rgb[15:8] + 8'd25 * rgb[7:0] + 8'd128) >> 8;
cb = (8'd - 38 * rgb[23:16] - 8'd74 * rgb[15:8] + 8'd112 * rgb[7:0] + 8'd128) >> 8;
cr = (8'd112 * rgb[23:16] - 8'd94 * rgb[15:8] - 8'd18 * rgb[7:0] + 8'd128) >> 8;
end
endmodule
3. Sobel 边缘检测
Sobel 算子用于提取车牌轮廓。
module sobel_edge_detection (
input wire [7:0] img_in,
input wire clk,
input wire rst,
output reg [7:0] edge_out
);
reg [7:0] gx[0:2][0:2];
reg [7:0] gy[0:2][0:2];
integer i, j;
always @(posedge clk or posedge rst) begin
if (rst) begin
for (i = 0; i < 3; i = i + 1) begin
for (j = 0; j < 3; j = j + 1) begin
gx[i][j] <= 8'd0;
gy[i][j] <= 8'd0;
end
end
edge_out <= 8'd0;
end else begin
// 填充模板并计算边缘强度
edge_out = (gx[0][0] + 2 * gx[0][1] + gx[0][2] + gx[1][0] + 2 * gx[1][1] + gx[1][2] + gx[2][0] + 2 * gx[2][1] + gx[2][2]) +
(gy[0][0] + 2 * gy[0][1] + gy[0][2] + gy[1][0] + 2 * gy[1][1] + gy[1][2] + gy[2][0] + 2 * gy[2][1] + gy[2][2]);
end
end
endmodule











