Image Processing On FPGA Using Verilog HDL - FPGA4student
Image Processing On FPGA Using Verilog HDL - FPGA4student
In this FPGA Verilog project, some simple processing operations are implemented
in Verilog such as inversion, brightness control and threshold operations. The
image processing operation is selected by a "parameter.v" file and then, the
processed image data are written to a bitmap image output.bmp for verification
purposes. The image reading Verilog code operates as a Verilog model of an
image sensor/ camera, which can be really helpful for functional verifications in
real-time FPGA image processing projects. The image writing part is also
extremely useful for testing as well when you want to see the output image in BMP
format. In this project, I added some simple image processing code into the
reading part to make an example of image processing, but you can easily remove
it to get raw image data.
First of all, Verilog cannot read images directly. To read the .bmp image on in
Verilog, the image is required to be converted from the bitmap format to the
hexadecimal format. Below is a Matlab example code to convert a bitmap image to
a .hex file. The input image size is 768x512 and the image .hex file includes R, G,
B data of the bitmap image.
b=imread('kodim24.bmp'); % 24-bit BMP image RGB888
k=1;
for i=512:-1:1
for j=1:768
a(k)=b(i,j,1);
a(k+1)=b(i,j,2);
a(k+2)=b(i,j,3);
1 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
k=k+3;
end
end
fid = fopen('kodim24.hex', 'wt');
fprintf(fid, '%x\n', a);
disp('Text file write done');disp(' ');
fclose(fid);
% fpga4student.com FPGA projects, Verilog projects, VHDL projects
To read the image hexadecimal data file, Verilog uses this command: $readmemh
or $readmemb if the image data is in a binary text file. After reading the image .hex
file, the RGB image data are saved into memory and read out for processing.
Below is the Verilog code to the image reading and processing part:
/*******************************************************************
/****************** 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
// Just create and is not used, will be used once a video or many i
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)
2 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
3 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
temp_BMP[i] = total_memory[i+0][7:0];
end
4 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
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 v
ST_HSYNC: begin ctrl_hsync_run = 1; end // trigger counting for hs
ST_DATA: begin ctrl_data_run = 1; end // trigger counting for dat
endcase
end
// counters for vsync, hsync
always@(posedge HCLK, negedge HRESETn)
begin
5 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
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
6 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
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
7 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
8 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
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
DATA_R0=255-value2;
DATA_G0=255-value2;
DATA_B0=255-value2;
value4 = (org_B[WIDTH * row + col+1 ] + org_R[WIDTH * row
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 ]
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
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
9 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
endmodule
The image processing operation is selected in the following "parameter.v" file. To
change the processing operation, just switch the comment line.
/***************************************/
/****************** Definition file ********/
/************** **********************************************/
`define INPUTFILENAME "your_image.hex" // Input file name
`define OUTPUTFILENAME "output.bmp" // Output file name
// Choose the operation of code by delete
// in the beginning of the selected line
//`define BRIGHTNESS_OPERATION
`define INVERT_OPERATION
//`define THRESHOLD_OPERATION
// fpga4student.com FPGA projects, Verilog projects, VHDL projects
The "parameter.v" file is also to define paths and names of the input and output
file. After processing the image, it is needed to write the processed data to an
output image for verifications.
The following Verilog code is to write the processed image data to
a bitmap image for verification:
/****************** Module for writing .bmp image *************/
/***********************************************************/
// fpga4student.com FPGA projects, Verilog projects, VHDL projects
// Verilog project: Image processing in Verilog
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-----//
//-------------------------------------//
10 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
11 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
12 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
.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
Finally, we have everything to run a simulation to verify the image processing code.
Let's use the following image as the input bitmap file:
13 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
14 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
by block memory (RAMs) and design address generators to read image data from
the block memory.
After receiving so many questions related to this project, followings are the
answers to your questions:
1. Explanation and tutorial on how to run the simulation:
2. The full Verilog code for this image processing project can be downloaded
here. Run the simulation about 6ms and close the simulation, then you will be able
to see the output image.
3. The reading part operates as a Verilog model of an image sensor/camera
(output RGB data, HSYNC, VSYNC, HCLK). The Verilog image reading code is
extremely useful for functional verification in real-time FPGA image/video projects.
4. In this project, I added the image processing part to make an example of image
enhancement. You can easily remove the processing part to get only raw image
data in the case that you want to use the image sensor model only for verifying
your image processing design.
5. You should not synthesize this code because it is not designed for running on
FPGA, but rather for functional verification purposes. If you really want to
synthesize this code (read and process) and load the image into FPGA for
processing directly on FPGA, there are two ways: 1. write a RAM code and
initialize the image data into the memory using $readmemh; 2. generate a block
memory using either Xilinx Core Generator or Altera MegaFunction and load the
image data into the initial values of memory (.coe file for Xilinx Core Gen. and .mif
for Altera MegaFunction), then read the image data from memory and process it
(FSM Design).
6. In this project, two even and old pixels are read at the same time for speeding
up the processing, but you can change the number of pixels being read depending
on your design.
7. The writing Verilog code is also very helpful for testing purposes as you can see
the output in BMP format.
8. If you want to do real-time image processing, you can check this for the camera
15 de 16 20/08/2018 14:43
Image processing on FPGA using Verilog HDL - FPGA4student.com https://www.fpga4student.com/2016/11/image-processing-on-fpga ...
16 de 16 20/08/2018 14:43