Académique Documents
Professionnel Documents
Culture Documents
2D Convolution in Hardware
SystemVerilog implementation of Edge Detection
The Sobel operator is one of the most widely known methods for edge
detection in image processing. The edges in an image are the points where
the pixel intensity have the maximum variation in the spatial distribution. In
the frequency domain, the edges are obtained ltering out the low
frequencies in the spatial distribution [1].
https://sistenix.com/sobel.html 1/13
9/13/2018 2D Convolution in Hardware
(img/grad_eq1.svg)
Equation (1)
(img/grad_eq2.svg)
Equation (2)
A practical example
To understand the effect of the Solbel lter, we will use the Figure (1).
https://sistenix.com/sobel.html 2/13
9/13/2018 2D Convolution in Hardware
(img/lena.bmp)
Figure (1): The classic picture of Lena
Using MATLAB (or its open source version Octave with Image Processing
package) we can do the Sobel Operation with the following lines of code:
The rst step for edge detection using Octave is to convert the image to gray
scale. It can be achieved with the following lines of code:
https://sistenix.com/sobel.html 3/13
9/13/2018 2D Convolution in Hardware
(img/img_gray.jpg)
Figure (2): Lena's picture in gray scale
To detect the edges of the image using Octave just use this lines of code:
https://sistenix.com/sobel.html 4/13
9/13/2018 2D Convolution in Hardware
The edges of the classic picture of Lena can be seen in Figure (3):
(img/edge_img.jpg)
Figure (3): Edges of the classic picture of Lena
Hardware implementation
https://sistenix.com/sobel.html 5/13
9/13/2018 2D Convolution in Hardware
(img/sliding_window.svg)
Figure (4): Convolution of a 3x3 kernel with a image with WIDTH=9 pixels per line
The pixel's datapath in the convolution can ben seen in Figure (5) and it can
be represented mathematically as a function of the BUFFER_SIZE acccording
to equation (3), where the matrix SLIDING_WINDOW3x3(pixel(t)), represents
the 3x3 kernel that convolves the pixels in function of the time t and as a
function of BUFFER_SIZE.
(img/grad_eq3.svg)
https://sistenix.com/sobel.html 6/13
9/13/2018 2D Convolution in Hardware
Equation (3)
input
(img/circular_buffer.svg)
Figure (5): The pixel's datapath in the convolution
The SystemVerilog code of the Sliding Window for the pixel streams can be
seen below:
https://sistenix.com/sobel.html 7/13
9/13/2018 2D Convolution in Hardware
parameter WORD_SIZE = 8;
parameter ROW_SIZE = 10;
parameter BUFFER_SIZE = 3;
https://sistenix.com/sobel.html 8/13
9/13/2018 2D Convolution in Hardware
The SystemVerilog code of the 3x3 kernel for Sobel Operation and its
testbench can be seen below:
https://sistenix.com/sobel.html 9/13
9/13/2018 2D Convolution in Hardware
localparam BUFFER_SIZE=3;
logic [WORD_SIZE+2:0] g;
https://sistenix.com/sobel.html 10/13
9/13/2018 2D Convolution in Hardware
endmodule
`include "sobel.sv"
module top();
logic clock, reset;
logic [4:0] i, k;
logic [WORD_SIZE-1:0] inputPixel;
logic [WORD_SIZE-1:0] outputPixel;
initial begin
$display("testing ...");
clock = 0;
$monitor("%d %d %d", i, inputPixel, outputPixel);
for(i=0; i<31; i++)begin
#0 @(posedge clock)
inputPixel=$random;
end
#100;
$finish;
end
always #5 clock = !clock;
https://sistenix.com/sobel.html 11/13
9/13/2018 2D Convolution in Hardware
References:
[1] http://homepages.inf.ed.ac.uk/rbf/HIPR2/sobel.htm
(http://homepages.inf.ed.ac.uk/rbf/HIPR2/sobel.htm)
[2] Gradient Filter implementation on FPGA : Part 2 Implementing gradient
Filter (https://www.element14.com/community/groups/fpga-
group/blog/2015/05/27/gradient- lter-implementation-on-fpga-part2-
rst-modules)
[3] VIVADO HLS 2D Convolution on hardware - part 1
(https://www.youtube.com/watch?v=38lj0VQci7E&t=44s)
[4] LAD Projetos (http://lad.dsc.ufcg.edu.br/lad/pmwiki.php?n=Lad.Projetos)
(mailto:nelsoncscampos@gmail.com)
(https://twitter.com/ne1_50n)
(https://br.linkedin.com/in/nelsoncsc)
(https://github.com/nelsoncsc)
https://sistenix.com/sobel.html 12/13
9/13/2018 2D Convolution in Hardware
https://sistenix.com/sobel.html 13/13