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 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