Development of image sobel edge extraction algorithm based on FPGA, including tb test files and matlab verification code

Table of contents

1. Preview of algorithm operation renderings

2.Algorithm running software version

3. Some core programs

4. Overview of algorithm theory

5. Algorithm complete program engineering


1. Preview of algorithm operation renderings

2.Algorithm running software version

vivado2019.2

matlab2022a

3. Some core programs

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2023/07/31
// Design Name: 
// Module Name: sobel
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module tops(
input i_clk,
input i_rst,
input[7:0]i_I,
output reg[7:0]o_sobel
);
    
parameter LEN = 256;  
parameter th  = 255;      

........................................................

 
   
 
reg signed[10:0]x1;
reg signed[10:0]x2;

reg signed[10:0]y1;
reg signed[10:0]y2;

reg signed[11:0]x12;
reg signed[11:0]y12;

reg signed[11:0]x_;  
reg signed[11:0]y_;  
  
reg signed[12:0]edge_;  

always @(posedge i_clk or posedge i_rst)
begin
     if(i_rst)
     begin
     x1 <=11'd0;
     x2 <=11'd0;

     y1 <=11'd0;
     y2 <=11'd0;

     x12<=12'd0;
     y12<=12'd0;

     x_<=11'd0;
     y_<=11'd0;
  
     edge_ <=13'd0;
     end
else begin
.........................................................
  
     edge_<= x_ +  y_;  // 计算Sobel算子响应的绝对值和
     end
end 
    
    
    
always @(posedge i_clk or posedge i_rst)
begin
     if(i_rst)
     begin
     o_sobel <= 8'd0;
     end
else begin

          if(edge_>=th) //判断绝对值和是否大于阈值
          o_sobel <= 8'd255;
          else
          o_sobel <= 8'd0; 
 
     end
end  
    
    
endmodule
0X_001m

4. Overview of algorithm theory

        Image edge detection greatly reduces the amount of data, eliminates information that can be considered irrelevant, and retains important structural attributes of the image. There are many methods for edge detection, and most of them can be divided into two categories: one based on search and one based on zero crossing. Search-based methods detect boundaries by finding the maximum and minimum values ​​in the first derivative of the image, typically locating the boundary in the direction of maximum gradient. The method based on zero crossing finds the boundary by looking for the zero crossing of the second derivative of the image, which is usually the Laplacian zero crossing point or the zero crossing point of the nonlinear difference representation.

       Soble edge detection algorithm is relatively simple. In practical applications, it is more efficient than canny edge detection, but the edge is not as accurate as canny detection. However, in many practical applications, sobel edge is the first choice, especially when efficiency requirements are high, and for When fine grain is less of a concern. Soble edge detection is usually directional and can only detect vertical edges or vertical edges or both. So we first define the coefficients of the two gradient directions:

        Then we calculate the gradient image. We know that edge points are actually points in the image where the grayscale jumps violently, so we first calculate the gradient image, and then extract the brighter part of the gradient image to become the simple edge part.

        The Sobel operator uses a 3*3 filter to filter the image to obtain the gradient image. How to perform filtering and their meaning will not be described in detail here.

Filter in the vertical direction: y_mask=op = [-1 -2 -1;0 0 0;1 2 1]/8;

Horizontal filter: transpose of op: x_mask=op';

After defining the filter, we start to find the gradient images in the vertical and vertical directions respectively. Just convolve the filter with the image:

bx = abs(filter2(x_mask,a)); 
by = abs(filter2(y_mask,a));

Above, bx is the gradient image in the horizontal direction, and by is the gradient image in the vertical direction. In order to explain the algorithm process more clearly, the gradient image of an example image is given below.

5. Algorithm complete program engineering

OOOOO

OOO

O

Guess you like

Origin blog.csdn.net/aycd1234/article/details/132587803