该fpga项目旨在详细展示如何使用verilog处理图像,从verilog中读取输入位图图像(.bmp),处理并将处理结果写入verilog中的输出位图图像。提供了用于读取图像、图像处理和写入图像的完整 verilog 代码 。
在这个fpga verilog项目中,一些简单的处理操作都是在verilog中实现的,比如反相、亮度控制和阈值操作。图像处理操作由“parameter.v”文件选择,然后将处理后的图像数据写入位图图像 output.bmp 以供验证。所述图像读取verilog代码作为图像传感器/摄像机的模型的verilog,它可以是用于在实时的功能验证真正有用的操作fpga图像处理项目。当您想查看 bmp 格式的输出图像时,图像写入部分对于测试也非常有用。在这个项目中,我在阅读部分添加了一些简单的图像处理代码来制作图像处理的示例,但是您可以轻松地将其删除以获得原始图像数据。学生提出的所有相关问题都在本文底部得到解答。首先,verilog 不能直接读取图像。要在 verilog 中读取 .bmp 图像,需要将图像从位图格式转换为十六进制格式。下面是将位图图像转换为 .hex 文件的 matlab 示例代码。输入图像大小为 768x512,图像 .hex 文件包括位图图像的 r、g、b 数据。
b=imread(‘kodim24.bmp’); % 24-bit bmp image rgb888
k=1;
for i=5121 % image is written from the last row to the first row
for j=1:768
a(k)=b(i,j,1);
a(k+1)=b(i,j,2);
a(k+2)=b(i,j,3);
k=k+3;
end
end
fid = fopen(‘kodim24.hex’, ‘wt’);
fprintf(fid, ‘%x
’, a);
disp(‘text file write done’);disp(‘ ’);
fclose(fid);
要读取图像十六进制数据文件,verilog 使用以下命令:readmemb 如果图像数据在二进制文本文件中。读取图像.hex 文件后,将rgb 图像数据保存到内存中并读出进行处理。
下面是图像读取和处理部分的verilog代码:
/***************************************************** *****************************/
/******************** 模块用于读取和处理图像 **************/
/***************************** ****************************************************/
/******************************************************************************/
/****************** module for reading and processing image **************/
/******************************************************************************/
`include “parameter.v” // include definition file
// fpga4student.com: fpga projects for students
// fpga project: image processing in verilog
module image_read
#(
parameter width = 768, // image width
height = 512, // image height
infile = “。/img/kodim01.hex”, // image file
start_up_delay = 100, // delay during start up time
hsync_delay = 160,// delay between hsync pulses
value= 100, // value for brightness operation
threshold= 90, // threshold value for threshold operation
sign=1 // sign value using for brightness operation
// sign = 0: brightness subtraction
// sign = 1: brightness addition
)
(
input hclk, // clock
input hresetn, // reset (active low)
output vsync, // vertical synchronous pulse
// this signal is often a way to indicate that one entire image is transmitted.
// just create and is not used, will be used once a video or many images are transmitted.
output reg hsync, // horizontal synchronous pulse
// an hsync indicates that one line of the image is transmitted.
// used to be a horizontal synchronous signals for writing bmp file.
output reg [7:0] data_r0, // 8 bit red data (even)
output reg [7:0] data_g0, // 8 bit green data (even)
output reg [7:0] data_b0, // 8 bit blue data (even)
output reg [7:0] data_r1, // 8 bit red data (odd)
output reg [7:0] data_g1, // 8 bit green data (odd)
output reg [7:0] data_b1, // 8 bit blue data (odd)
// process and transmit 2 pixels in parallel to make the process faster, you can modify to transmit 1 pixels or more if needed
output ctrl_done // done flag
);
//-------------------------------------------------
// internal signals
//-------------------------------------------------
parameter sizeofwidth = 8; // data width
parameter sizeoflengthreal = 1179648; // image data : 1179648 bytes: 512 * 768 *3
// local parameters for fsm
localparam st_idle = 2‘b00,// idle state
st_vsync = 2’b01,// state for creating vsync
st_hsync = 2‘b10,// state for creating hsync
st_data = 2’b11;// state for data processing
reg [1:0] cstate, // current state
nstate; // next state
reg start; // start signal: trigger finite state machine beginning to operate
reg hresetn_d; // delayed reset signal: use to create start signal
reg ctrl_vsync_run; // control signal for vsync counter
reg [8:0] ctrl_vsync_cnt; // counter for vsync
reg ctrl_hsync_run; // control signal for hsync counter
reg [8:0] ctrl_hsync_cnt; // counter for hsync
reg ctrl_data_run; // control signal for data processing
reg [7 : 0] total_memory [0 : sizeoflengthreal-1];// memory to store 8-bit data image
// temporary memory to save image data : size will be width*height*3
integer temp_bmp [0 : width*height*3 - 1];
integer org_r [0 : width*height - 1]; // temporary storage for r component
integer org_g [0 : width*height - 1]; // temporary storage for g component
integer org_b [0 : width*height - 1]; // temporary storage for b component
// counting variables
integer i, j;
// temporary signals for calculation: details in the paper.
integer tempr0,tempr1,tempg0,tempg1,tempb0,tempb1; // temporary variables in contrast and brightness operation
integer value,value1,value2,value4;// temporary variables in invert and threshold operation
reg [ 9:0] row; // row index of the image
reg [10:0] col; // column index of the image
reg [18:0] data_count; // data counting for entire pixels of the image
//-------------------------------------------------//
// -------- reading data from input file ----------//
//-------------------------------------------------//
initial begin
$readmemh(infile,total_memory,0,sizeoflengthreal-1); // read file from infile
end
// use 3 intermediate signals rgb to save image data
always@(start) begin
if(start == 1‘b1) begin
for(i=0; i《width*height*3 ; i=i+1) begin
temp_bmp[i] = total_memory[i+0][7:0];
end
for(i=0; i《height; i=i+1) begin
for(j=0; j《width; j=j+1) begin
// matlab code writes image from the last row to the first row
// verilog code does the same in reading to correctly save image pixels into 3 separate rgb mem
org_r[width*i+j] = temp_bmp[width*3*(height-i-1)+3*j+0]; // save red component
org_g[width*i+j] = temp_bmp[width*3*(height-i-1)+3*j+1];// save green component
org_b[width*i+j] = temp_bmp[width*3*(height-i-1)+3*j+2];// save blue component
end
end
end
end
//----------------------------------------------------//
// ---begin to read image file once reset was high ---//
// ---by creating a starting pulse (start)------------//
//----------------------------------------------------//
always@(posedge hclk, negedge hresetn)
begin
if(!hresetn) begin
start 《= 0;
hresetn_d 《= 0;
end
else begin // ______
hresetn_d 《= hresetn; // | |
if(hresetn == 1’b1 && hresetn_d == 1‘b0) // __0___| 1 |___0____ : starting pulse
start 《= 1’b1;
else
start 《= 1‘b0;
end
end
//-----------------------------------------------------------------------------------------------//
// finite state machine for reading rgb888 data from memory and creating hsync and vsync pulses --//
//-----------------------------------------------------------------------------------------------//
always@(posedge hclk, negedge hresetn)
begin
if(~hresetn) begin
cstate 《= st_idle;
end
else begin
cstate 《= nstate; // update next state
end
end
//-----------------------------------------//
//--------- state transition --------------//
//-----------------------------------------//
// idle 。 vsync 。 hsync 。 data
always @(*) begin
case(cstate)
st_idle: begin
if(start)
nstate = st_vsync;
else
nstate = st_idle;
end
st_vsync: begin
if(ctrl_vsync_cnt == start_up_delay)
nstate = st_hsync;
else
nstate = st_vsync;
end
st_hsync: begin
if(ctrl_hsync_cnt == hsync_delay)
nstate = st_data;
else
nstate = st_hsync;
end
st_data: begin
if(ctrl_done)
nstate = st_idle;
else begin
if(col == width - 2)
nstate = st_hsync;
else
nstate = st_data;
end
end
endcase
end
// ------------------------------------------------------------------- //
// --- counting for time period of vsync, hsync, data processing ---- //
// ------------------------------------------------------------------- //
always @(*) begin
ctrl_vsync_run = 0;
ctrl_hsync_run = 0;
ctrl_data_run = 0;
case(cstate)
st_vsync: begin ctrl_vsync_run = 1; end // trigger counting for vsync
st_hsync: begin ctrl_hsync_run = 1; end // trigger counting for hsync
st_data: begin ctrl_data_run = 1; end // trigger counting for data processing
endcase
end
// counters for vsync, hsync
always@(posedge hclk, negedge hresetn)
begin
if(~hresetn) begin
ctrl_vsync_cnt 《= 0;
ctrl_hsync_cnt 《= 0;
end
else begin
if(ctrl_vsync_run)
ctrl_vsync_cnt 《= ctrl_vsync_cnt + 1; // counting for vsync
else
ctrl_vsync_cnt 《= 0;
if(ctrl_hsync_run)
ctrl_hsync_cnt 《= ctrl_hsync_cnt + 1; // counting for hsync
else
ctrl_hsync_cnt 《= 0;
end
end
// counting column and row index for reading memory
always@(posedge hclk, negedge hresetn)
begin
if(~hresetn) begin
row 《= 0;
col 《= 0;
end
else begin
if(ctrl_data_run) begin
if(col == width - 2) begin
row 《= row + 1;
end
if(col == width - 2)
col 《= 0;
else
col 《= col + 2; // reading 2 pixels in parallel
end
end
end
//-------------------------------------------------//
//----------------data counting---------- ---------//
//-------------------------------------------------//
always@(posedge hclk, negedge hresetn)
begin
if(~hresetn) begin
data_count 《= 0;
end
else begin
if(ctrl_data_run)
data_count 《= data_count + 1;
end
end
assign vsync = ctrl_vsync_run;
assign ctrl_done = (data_count == 196607)? 1’b1: 1‘b0; // done flag
//-------------------------------------------------//
//------------- image processing ---------------//
//-------------------------------------------------//
always @(*) begin
hsync = 1’b0;
data_r0 = 0;
data_g0 = 0;
data_b0 = 0;
data_r1 = 0;
data_g1 = 0;
data_b1 = 0;
if(ctrl_data_run) begin
hsync = 1‘b1;
`ifdef brightness_operation
/**************************************/
/* brightness addition operation */
/**************************************/
if(sign == 1) begin
// r0
tempr0 = org_r[width * row + col ] + value;
if (tempr0 》 255)
data_r0 = 255;
else
data_r0 = org_r[width * row + col ] + value;
// r1
tempr1 = org_r[width * row + col+1 ] + value;
if (tempr1 》 255)
data_r1 = 255;
else
data_r1 = org_r[width * row + col+1 ] + value;
// g0
tempg0 = org_g[width * row + col ] + value;
if (tempg0 》 255)
data_g0 = 255;
else
data_g0 = org_g[width * row + col ] + value;
tempg1 = org_g[width * row + col+1 ] + value;
if (tempg1 》 255)
data_g1 = 255;
else
data_g1 = org_g[width * row + col+1 ] + value;
// b
tempb0 = org_b[width * row + col ] + value;
if (tempb0 》 255)
data_b0 = 255;
else
data_b0 = org_b[width * row + col ] + value;
tempb1 = org_b[width * row + col+1 ] + value;
if (tempb1 》 255)
data_b1 = 255;
else
data_b1 = org_b[width * row + col+1 ] + value;
end
else begin
/**************************************/
/* brightness subtraction operation */
/**************************************/
// r0
tempr0 = org_r[width * row + col ] - value;
if (tempr0 《 0)
data_r0 = 0;
else
data_r0 = org_r[width * row + col ] - value;
// r1
tempr1 = org_r[width * row + col+1 ] - value;
if (tempr1 《 0)
data_r1 = 0;
else
data_r1 = org_r[width * row + col+1 ] - value;
// g0
tempg0 = org_g[width * row + col ] - value;
if (tempg0 《 0)
data_g0 = 0;
else
data_g0 = org_g[width * row + col ] - value;
tempg1 = org_g[width * row + col+1 ] - value;
if (tempg1 《 0)
data_g1 = 0;
else
data_g1 = org_g[width * row + col+1 ] - value;
// b
tempb0 = org_b[width * row + col ] - value;
if (tempb0 《 0)
data_b0 = 0;
else
data_b0 = org_b[width * row + col ] - value;
tempb1 = org_b[width * row + col+1 ] - value;
if (tempb1 《 0)
data_b1 = 0;
else
data_b1 = org_b[width * row + col+1 ] - value;
end
`endif
/**************************************/
/* invert_operation */
/**************************************/
`ifdef invert_operation
value2 = (org_b[width * row + col ] + org_r[width * row + col ] +org_g[width * row + col ])/3;
data_r0=255-value2;
data_g0=255-value2;
data_b0=255-value2;
value4 = (org_b[width * row + col+1 ] + org_r[width * row + col+1 ] +org_g[width * row + col+1 ])/3;
data_r1=255-value4;
data_g1=255-value4;
data_b1=255-value4;
`endif
/**************************************/
/********threshold operation *********/
/**************************************/
`ifdef threshold_operation
value = (org_r[width * row + col ]+org_g[width * row + col ]+org_b[width * row + col ])/3;
if(value 》 threshold) begin
data_r0=255;
data_g0=255;
data_b0=255;
end
else begin
data_r0=0;
data_g0=0;
data_b0=0;
end
value1 = (org_r[width * row + col+1 ]+org_g[width * row + col+1 ]+org_b[width * row + col+1 ])/3;
if(value1 》 threshold) begin
data_r1=255;
data_g1=255;
data_b1=255;
end
else begin
data_r1=0;
data_g1=0;
data_b1=0;
end
`endif
end
end
endmodule
“parameter.v”文件也是定义输入输出文件的路径和名称。对图像进行处理后,需要将处理后的数据写入输出图像进行验证。
以下verilog代码是将处理后的图像数据写入位图图像进行验证:
/******************** 写入.bmp图像的模块************/
/********** ****************************************************/
module image_write #(parameter
width = 768, // image width
height = 512, // image height
infile = “output.bmp”, // output image
bmp_header_num = 54 // header for bmp image
)
(
input hclk, // clock input
hresetn, // reset active low
input hsync, // hsync pulse
input [7:0] data_write_r0, // red 8-bit data (odd)
input [7:0] data_write_g0, // green 8-bit data (odd)
input [7:0] data_write_b0, // blue 8-bit data (odd)
input [7:0] data_write_r1, // red 8-bit data (even)
input [7:0] data_write_g1, // green 8-bit data (even)
input [7:0] data_write_b1, // blue 8-bit data (even)
output reg write_done
);
// fpga4student.com fpga projects, verilog projects, vhdl projects
//-----------------------------------//
//-------header data for bmp image-----//
//-------------------------------------//
// windows bmp files begin with a 54-byte header
initial begin
bmp_header[ 0] = 66;bmp_header[28] =24;
bmp_header[ 1] = 77;bmp_header[29] = 0;
bmp_header[ 2] = 54;bmp_header[30] = 0;
bmp_header[ 3] = 0;bmp_header[31] = 0;
bmp_header[ 4] = 18;bmp_header[32] = 0;
bmp_header[ 5] = 0;bmp_header[33] = 0;
bmp_header[ 6] = 0;bmp_header[34] = 0;
bmp_header[ 7] = 0;bmp_header[35] = 0;
bmp_header[ 8] = 0;bmp_header[36] = 0;
bmp_header[ 9] = 0;bmp_header[37] = 0;
bmp_header[10] = 54;bmp_header[38] = 0;
bmp_header[11] = 0;bmp_header[39] = 0;
bmp_header[12] = 0;bmp_header[40] = 0;
bmp_header[13] = 0;bmp_header[41] = 0;
bmp_header[14] = 40;bmp_header[42] = 0;
bmp_header[15] = 0;bmp_header[43] = 0;
bmp_header[16] = 0;bmp_header[44] = 0;
bmp_header[17] = 0;bmp_header[45] = 0;
bmp_header[18] = 0;bmp_header[46] = 0;
bmp_header[19] = 3;bmp_header[47] = 0;
bmp_header[20] = 0;bmp_header[48] = 0;
bmp_header[21] = 0;bmp_header[49] = 0;
bmp_header[22] = 0;bmp_header[50] = 0;
bmp_header[23] = 2;bmp_header[51] = 0;
bmp_header[24] = 0;bmp_header[52] = 0;
bmp_header[25] = 0;bmp_header[53] = 0;
bmp_header[26] = 1; bmp_header[27] = 0;
end
//---------------------------------------------------------//
//--------------write .bmp file ----------------------//
//----------------------------------------------------------//
initial begin
fd = $fopen(infile, “wb+”);
end
always@(write_done) begin // once the processing was done, bmp image will be created
if(write_done == 1’b1) begin
for(i=0; i《bmp_header_num; i=i+1) begin
$fwrite(fd, “%c”, bmp_header[i][7:0]); // write the header
end
for(i=0; i《width*height*3; i=i+6) begin
// write r0b0g0 and r1b1g1 (6 bytes) in a loop
$fwrite(fd, “%c”, out_bmp[i ][7:0]);
$fwrite(fd, “%c”, out_bmp[i+1][7:0]);
$fwrite(fd, “%c”, out_bmp[i+2][7:0]);
$fwrite(fd, “%c”, out_bmp[i+3][7:0]);
$fwrite(fd, “%c”, out_bmp[i+4][7:0]);
$fwrite(fd, “%c”, out_bmp[i+5][7:0]);
end
end
end
位图图像的标头数据非常重要,在这里发布。如果没有标题数据,则无法正确显示写入的图像。在 verilog hdl 中,$fwrite 命令用于将数据写入文件。
接下来,让我们编写一个测试平台 verilog 代码来验证图像处理操作。
`timescale 1ns/1ps /**************************************************/
/******* testbench for simulation ****************/
/*********************************************/
// fpga4student.com fpga projects, verilog projects, vhdl projects
// verilog project: image processing in verilog
`include “parameter.v” // include definition file module tb_simulation;
//------------------ // internal signals
//-------------------------------------------------
reg hclk, hresetn;
wire vsync;
wire hsync;
wire [ 7 : 0] data_r0;
wire [ 7 : 0] data_g0;
wire [ 7 : 0] data_b0;
wire [ 7 : 0] data_r1;
wire [ 7 : 0] data_g1;
wire [ 7 : 0] data_b1;
wire enc_done;
image_read #(.infile(`inputfilename))
u_image_read
( .hclk (hclk ),
.hresetn (hresetn ),
.vsync (vsync ),
.hsync (hsync ),
.data_r0 (data_r0 ),
.data_g0 (data_g0 ),
.data_b0 (data_b0 ),
.data_r1 (data_r1 ),
.data_g1 (data_g1 ),
.data_b1 (data_b1 ),
.ctrl_done (enc_done)
);
image_write #(.infile(`outputfilename))
u_image_write (
.hclk(hclk),
.hresetn(hresetn),
.hsync(hsync),
.data_write_r0(data_r0),
.data_write_g0(data_g0),
.data_write_b0(data_b0),
.data_write_r1(data_r1),
.data_write_g1(data_g1),
.data_write_b1(data_b1),
.write_done()
);
//------------- // test vectors
//-------------------------------------
initial
begin
hclk = 0;
forever #10 hclk = ~hclk;
end
initial
begin
hresetn = 0;
#25 hresetn = 1;
end endmodule
最后,我们拥有一切来运行模拟来验证图像处理代码。让我们使用下图作为输入位图文件:
运行仿真 6ms,关闭仿真并打开输出图像以检查结果。以下是参数.v中选定操作处理的输出图像:
由于读取代码是为了模拟目的而对图像传感器/相机进行建模,因此建议不要合成代码。如果你真的想综合处理代码并直接在fpga上运行,你需要将代码中的这些图像数组(total_memory、temp_bmp、org_r、org_b、org_g)替换为块存储器(ram)和设计地址生成器来读取图像块内存中的数据。
问题的答案:
此图像处理项目的完整 verilog 代码可在此处下载。运行模拟大约 6ms 并关闭模拟,然后您将能够看到输出图像。
https://github.com/suisuisi/fpgaandimage/blob/main/image/012_others/verilog_image_processing.zip
读取部分作为图像传感器/相机的 verilog 模型运行(输出 rgb 数据、hsync、vsync、hclk)。verilog 图像读取代码对于实时 fpga 图像/视频项目中的功能验证非常有用。
3.在这个项目中,我添加了图像处理部分,做一个图像增强的例子。如果您只想使用图像传感器模型来验证您的图像处理设计,您可以轻松移除处理部分以仅获取原始图像数据。
4.图像保存到三个独立的rgb mem中:由于matlab代码是将图像16进制文件从最后一行写到第一行,所以rgb保存代码(org_r, org_b, org_g)在读取temp_bmp内存时也是这样做的,保存rgb数据正确。如果您想以不同的方式进行,您可以相应地更改它。
如果您想更改图像大小,您可能会发现以下对 bmp 标题的解释很有用:
图像大小 = 768*512*3= 1179648 字节
bmp 标题 = 54 字节
bmp 文件大小 = 图像大小 + bmp 标题 = 1179702 字节
将其转换为十六进制数:1179702 in decimal = 120036 in hexade
然后 bmp 文件的 4 字节大小:00h, 12 in hexa = 18 decimal, 00h, 36 in hexa = 54 decimal
这就是我们如何得到以下值:
bmp_header[2] = 54;
bmp_header[3] = 0;
bmp_header[4] = 18;
bmp_header[5] = 0;
图像宽度 = 768 =》 十六进制:0x0300。图像宽度的 4 个字节是
0、3、0、0。这就是您获得以下值的方式:bmp_header[18] = 0;
bmp_header[19] = 3;
bmp_header[20] = 0;
bmp_header[21] = 0;
图像高度 = 512 =》 十六进制:0x0200。图像宽度的 4 个字节是 0, 2, 0, 0。这就是我们如何得到以下值:
bmp_header[22] = 0;
bmp_header[23] = 2;
bmp_header[24] = 0;
bmp_header[25] = 0;
您不应综合此代码,因为它不是为在 fpga 上运行而设计的,而是用于功能验证目的。 如果你真的想合成这段代码(读取和处理)并将图像加载到 fpga中直接在 fpga 上处理,请将所有 temp. 变量 (org_r, org_b, org_g, tmp_bmp = total_memory) 并生成地址以读取图像数据(删除 always @(start) 和所有“for 循环” - 这些用于模拟目的)。有两种方式:1. 写一段ram代码,用$readmemh将图像数据初始化到内存中;2. 使用 xilinx core generator 或 altera megafunction 生成块存储器并将图像数据加载到存储器的初始值(xilinx core gen. 为 .coe 文件,altera megafunction 为 .mif 文件),然后从存储器中读取图像数据并处理它(fsm 设计)。
在这个项目中,为了加快处理速度,同时读取两个偶数和旧像素,但您可以根据您的设计更改正在读取的像素数。
编写verilog代码对于测试目的也非常有帮助,因为您可以看到bmp 格式的输出。
9.如果要做实时图像处理,可以查看camera接口代码这个:https://www.fpga4student.com/2018/08/basys-3-fpga-ov7670-camera.html
如何将HMI数据记录保存至单个文件?
PIC系列单片机应用设计与实例
半导体收购:Marvell正式宣布将以 100 亿美元收购Inphi
诺基亚可能使用Windows Phone 7(WP7)手机操作系统
可穿戴设备的排名情况分析
FPGA中如何使用Verilog处理图像
英恒科技正式发布CAELUS自动驾驶域控制器开源计划
400W大功率稳压逆变器电路图,原理图
ADI携智能解决方案亮相embedded world 2023,助力加快实现可持续发展
双11再战:天猫精灵、小度又打起擂台
rt-thread 驱动篇(七)GPIO驱动
基于工业物联网的中药生产过程监控和故障监测系统
水质在线电导率仪/酸碱盐浓度变送器介绍
晶圆厂为何要进攻先进封装?
从车流到数据流 | 智慧交通时代,Hisan智能激光屏推动交通大数据化发展
Xilinx ISE Design Suite 12.3 下
618真无线蓝牙耳机/TWS主动降噪耳机有哪些推荐?
工业相机在机器视觉系统中的地位和作用
手机如何才能防止泄密四种方法详细说明
三星s8什么时候降价?三星s8面部识别,照片就能骗?三星s8因为这个或将停产?