问答 店铺
热搜: ZYNQ FPGA discuz

QQ登录

只需一步,快速开始

微信登录

微信扫码,快速开始

微信扫一扫 分享朋友圈

已有 84 人浏览分享

Scan me!
开启左侧

[MILIANPAI-F01-EG4D]FPGA图像入门连载_09FPGA实现图像sobel算子边缘提取处理

[复制链接]
84 0
UT发布  工单组 发表于  7 天前 | 显示全部楼层 |阅读模式
安路-FPGA课程
安路课程: 图像算法 » 图像新手入门实验
安路系列: EG4
本帖最后由 UT发布 于 2025-4-7 15:06 编辑

软件版本:Anlogic -TD5.6.1-64bit
操作系统:WIN10 64bit
硬件平台:适用安路(Anlogic)FPGA
登录米联客”FPGA社区-www.uisrc.com视频课程、答疑解惑!
1概述

本文简述了图像sobel算子边缘提取的算法,讲解如何进行Verilog的算法实现,并进行上板实验。

2算法原理简介

所谓边缘是指其周围像素灰度急剧变化的那些象素的集合,它是图像最基本的特征。边缘存在于目标、背景和区域之间,所以,它是图像分割所依赖的最重要的依据。由于边缘是位置的标志,对灰度的变化不敏感,因此,边缘也是图像匹配的重要的特征。

Soble边缘检测通常带有方向性,可以只检测竖直边缘或垂直边缘或都检测。

所以我们先定义两个梯度方向的系数:


kx=0;ky=1;% horizontal

kx=1;ky=0;% vertical

kx=1;ky=1;% both


然后我们来计算梯度图像,我们知道边缘点其实就是图像中灰度跳变剧烈的点,所以先计算梯度图像,然后将梯度图像中较亮的那一部分提取出来就是简单的边缘部分。

Sobel算子使用3*3的模板如下:

image.jpg
3算法仿真
3.1Matlab算法仿真
3.1.1Matlab算法代码分析

源代码如下:

  1. clear;clear all;clc;
  2. image_in = imread('lena_1280x720.jpg');
  3. image_gray = rgb2gray(image_in);
  4. [row,col] = size(image_gray);
  5. image_gray  = im2double(image_gray);
  6. sobel_image = zeros(size(image_gray), 'like', image_gray);
  7. for i = 2:1:row-1
  8.         for j = 2:1:col-1
  9.                 sobel_image_x=...
  10.                 image_gray(i-1,j+1)   +...
  11.                 image_gray(i  ,j+1)*2 +...
  12.                 image_gray(i+1,j+1)   -...
  13.                 image_gray(i-1,j-1)   -...
  14.                 image_gray(i  ,j-1)*2 -...
  15.                 image_gray(i+1,j-1)         ;
  16.                
  17.                 sobel_image_y=...
  18.                 image_gray(i-1,j-1)   +...
  19.                 image_gray(i-1,j  )*2 +...
  20.                 image_gray(i-1,j+1)   -...
  21.                 image_gray(i+1,j-1)   -...
  22.                 image_gray(i+1,j  )*2 -...
  23.                 image_gray(i+1,j+1)         ;
  24.                
  25.                 sobel = abs(sobel_image_x)+abs(sobel_image_y);
  26.                 if(sobel > 95/255)
  27.                         sobel_image(i,j) = 0;
  28.                 else
  29.                         sobel_image(i,j) = 1;
  30.                 end
  31.                
  32.         end
  33. end
  34. figure
  35. subplot(121);
  36. imshow(image_gray ), title('the original gray image');
  37. subplot(122);
  38. imshow(sobel_image), title('the lap image image');
复制代码
3.1.2Matlab实验结果
image.jpg
3.2Verilog算法仿真
3.2.1Modelsim仿真
3.2.1.1仿真执行

在件夹Algorithm_simulation下进行算法的仿真,分为simsrctb三个子文件夹。在sim文件夹下有win系统的快捷执行文件sim.bat,可以一键进行仿真,src文件下放的是Verilog的核心图像算法及其顶层与输入图像激励,tb文件下放的是测试激励文件及输出图像的保存。

双击执行sim文件夹下sim.bat,自动打开Modelsim仿真,自动添加仿真波形,执行完成后自动保存图像,仿真波形如图所示:

image.jpg
3.2.1.2仿真关键部分代码解析

Sim.do执行仿真代码,文件内容如下:

  1. #
  2. # Create work library
  3. #
  4. vlib work
  5. #
  6. # Compile sources
  7. #
  8. vlog "../src/*.v"
  9. vlog "../tb/*.v"
  10. #
  11. # Call vsim to invoke simulator
  12. #
  13. vsim -voptargs=+acc work.top_tb
  14. #
  15. # Add waves
  16. #
  17. do wave.do
  18. #
  19. # Run simulation
  20. #
  21. run -all
  22. #
  23. # End
复制代码

图像输入代码部分:

  1. reg                 en;
  2. reg [12:0]         h_syn_cnt = 'd0;
  3. reg [12:0]         v_syn_cnt = 'd0;
  4. reg [23:0]         image [0 : H_ACTIVE*V_ACTIVE-1];
  5. reg [31:0]         image_cnt = 'd0;
  6. //读取txt文件到image数组中
  7. initial begin
  8.         $readmemh("../matlab_src/image_720_1280_3.txt", image);
  9. end
  10. // 行扫描计数器
  11. always@(posedge i_clk)
  12. begin
  13.         if(h_syn_cnt == H_TOTAL_TIME-1)
  14.         h_syn_cnt <= 0;
  15.     else
  16.         h_syn_cnt <= h_syn_cnt + 1;
  17. end
  18. // 列扫描计数器
  19. always@(posedge i_clk)
  20. begin
  21.         if(h_syn_cnt == H_TOTAL_TIME-1)
  22.         begin
  23.         if(v_syn_cnt == V_TOTAL_TIME-1)
  24.             v_syn_cnt <= 0;
  25.         else
  26.             v_syn_cnt <= v_syn_cnt + 1;
  27.         end
  28. end
  29. // 行同步控制
  30. always@(posedge i_clk)
  31. begin
  32.     if(h_syn_cnt < H_SYNC_TIME)
  33.         o_hsyn <= 0;
  34.     else
  35.         o_hsyn <= 1;
  36. end
  37. // 场同步控制
  38. always@(posedge i_clk)
  39. begin
  40.     if(v_syn_cnt < V_SYNC_TIME)
  41.         o_vsyn <= 0;
  42.     else
  43.         o_vsyn <= 1;
  44. end
  45. // 坐标使能.
  46. always@(posedge i_clk)
  47. begin
  48.     if(v_syn_cnt >= V_SYNC_TIME + V_BACK_PORCH && v_syn_cnt < V_SYNC_TIME + V_BACK_PORCH + V_ACTIVE)
  49.     begin
  50.         if(h_syn_cnt >= H_SYNC_TIME + H_BACK_PORCH && h_syn_cnt < H_SYNC_TIME + H_BACK_PORCH + H_ACTIVE)
  51.             en <= 1;
  52.         else
  53.             en <= 0;
  54.     end
  55.     else
  56.         en <= 0;
  57. end
  58. always@(posedge i_clk)
  59. begin
  60.     if(en)
  61.         begin
  62.                 o_r                 <= image[image_cnt][23:16];
  63.                 o_g                 <= image[image_cnt][15:8];
  64.                 o_b                 <= image[image_cnt][7:0];
  65.             image_cnt         <= image_cnt + 1;
  66.         end
  67.         else if(image_cnt == H_ACTIVE*V_ACTIVE)
  68.         begin
  69.                 o_r                 <= 8'h00;
  70.                 o_g                 <= 8'h00;
  71.                 o_b                 <= 8'h00;
  72.             image_cnt         <= 'd0;
  73.         end        
  74.     else
  75.         begin
  76.                 o_r                 <= 8'h00;
  77.                 o_g                 <= 8'h00;
  78.                 o_b                 <= 8'h00;
  79.             image_cnt         <= image_cnt;
  80.         end        
  81. end
  82. always@(posedge i_clk)
  83. begin
  84.         // if(image_cnt >= H_ACTIVE*V_ACTIVE)
  85.                 // o_en <= 0;
  86.         // else
  87.                 o_en <= en;
  88. end
复制代码

图像输出保存代码部分:

  1. reg             clk;
  2. reg             rst_n;
  3. integer                 image_txt;
  4. reg [31:0]                 pixel_cnt;
  5. wire[23:0]          data;
  6. wire            de;
  7. top u_top
  8. (
  9.     .i_clk                              (clk                ),
  10.     .i_rst_n                      (rst_n              ),
  11.     .o_gray_data             (data               ),
  12.     .o_gray_de               (de                 )
  13. );
  14. always #(1) clk = ~clk;
  15. initial
  16. begin
  17.         clk   = 1;
  18.     rst_n = 0;         
  19.         #100
  20.     rst_n = 1;
  21.         
  22. end
  23. glbl glbl();
  24. initial
  25. begin
  26.     image_txt = $fopen("../matlab_src/image_720_1280_3_out.txt");
  27. end
  28. always@(posedge clk or negedge rst_n)
  29. begin
  30.     if(!rst_n)
  31.         begin
  32.         pixel_cnt <= 0;
  33.     end
  34.     else if(de)
  35.         begin
  36.         pixel_cnt = pixel_cnt + 1;
  37.         $fwrite(image_txt,"%h\n",data);
  38.     end
  39. end
  40. always@(posedge clk )
  41. begin
  42.         if(pixel_cnt == 720*1280)
  43.         begin
  44.                 $display("*******************************************************************************");               
  45.                 $display("*** Success:image_720_1280_3_out.txt is output complete! %t", $realtime, "ps***");
  46.                 $display("*******************************************************************************");
  47.                         $fclose(image_txt);
  48.                 $stop;
  49.         end        
  50. end
复制代码
3.2.2Modelsim实验结果

matlab查看输入输出的图像代码部分:

  1. clear;clear all;clc;
  2. row = 720;  
  3. col = 1280;  
  4. n   = 3;   
  5. image_sim_pass = uint8(zeros(row,col,n));
  6. fid = fopen('image_720_1280_3_out.txt','r');
  7. for x = 1:row
  8.     for y = 1:col
  9.         RGB = fscanf(fid,'%s',1);
  10.         image_sim_pass(x,y,1) = uint8(hex2dec(RGB(1:2)));
  11.         image_sim_pass(x,y,2) = uint8(hex2dec(RGB(3:4)));
  12.         image_sim_pass(x,y,3) = uint8(hex2dec(RGB(5:6)));              
  13.     end
  14. end
  15. fclose(fid);
  16. image_1 = imread('lena_1280x720.jpg');
  17. subplot(121);
  18. imshow(image_1), title('The original image');
  19. subplot(122);
  20. imshow(image_sim_pass),title('After processing images');
  21. imwrite(image_sim_pass,'lena_720x128_sim_pass.jpg');   
复制代码
image.jpg
4工程实现
4.1Verilog代码分析
变量声明
  1. reg        [2:0]                i_hsyn_d;
  2. reg        [2:0]                i_vsyn_d;
  3. reg        [2:0]                i_en_d;
  4. reg [11:0]                r_x1;
  5. reg [11:0]                r_x2;
  6. reg [11:0]                r_y1;
  7. reg [11:0]                r_y2;
  8. reg [11:0]                sobel_x;
  9. reg [11:0]                sobel_y;
  10. wire [7:0]        r_temp_11;
  11. wire [7:0]        r_temp_12;
  12. wire [7:0]        r_temp_13;
  13. wire [7:0]        r_temp_21;
  14. wire [7:0]        r_temp_22;
  15. wire [7:0]        r_temp_23;
  16. wire [7:0]        r_temp_31;
  17. wire [7:0]        r_temp_32;
  18. wire [7:0]        r_temp_33;
复制代码
输出赋值
  1. assign o_hs = i_hsyn_d[2];
  2. assign o_vs = i_vsyn_d[2];
  3. assign o_en = i_en_d[2];
复制代码
阈值赋值
  1. assign o_r        = (sobel_x + sobel_y) > threshold ? 255 : 0;
  2. assign o_g        = (sobel_x + sobel_y) > threshold ? 255 : 0;
  3. assign o_b        = (sobel_x + sobel_y) > threshold ? 255 : 0;
复制代码
信号同步化
  1. always@(posedge i_clk )
  2. begin
  3.         i_hsyn_d <=        {i_hsyn_d[1:0],i_hsyn};
  4.         i_vsyn_d <=        {i_vsyn_d[1:0],i_vsyn};
  5.         i_en_d         <=        {i_en_d[1:0],i_en};
  6.             
  7. end
复制代码
调用3x3模板
  1. image_template u_r_template
  2. (
  3.         .i_clk                        (i_clk                                ),
  4.         .i_rst_n                (i_rst_n                        ),
  5.         .i_en                        (i_en                                ),
  6.         .i_data                        (i_r                                ),
  7.         .o_en                        (                                        ),
  8.         .o_temp_11                (r_temp_11                        ),
  9.         .o_temp_12                (r_temp_12                        ),
  10.         .o_temp_13                (r_temp_13                        ),        
  11.         .o_temp_21                (r_temp_21                        ),
  12.         .o_temp_22                (r_temp_22                        ),
  13.         .o_temp_23                (r_temp_23                        ),               
  14.         .o_temp_31                (r_temp_31                        ),
  15.         .o_temp_32                (r_temp_32                        ),
  16.         .o_temp_33                (r_temp_33                        )
  17. );
复制代码
执行加法运算
  1. always@(posedge i_clk or negedge i_rst_n)
  2. begin
  3.     if(!i_rst_n)
  4.         begin
  5.         r_x1 <= 12'd0;
  6.         r_x2 <= 12'd0;
  7.     end
  8.     else
  9.         begin
  10.         r_x1 <= r_temp_11 + r_temp_21 + r_temp_21 + r_temp_31;
  11.         r_x2 <= r_temp_13 + r_temp_23 + r_temp_23 + r_temp_33;
  12.     end
  13. end
复制代码
执行加法运算
  1. always@(posedge i_clk or negedge i_rst_n)
  2. begin
  3.     if(!i_rst_n)
  4.         begin
  5.         r_y1 <= 12'd0;
  6.                 r_y2 <= 12'd0;
  7.     end
  8.     else
  9.         begin
  10.         r_y1 <= r_temp_11 + r_temp_12 + r_temp_12 + r_temp_13;
  11.                 r_y2 <= r_temp_31 + r_temp_32 + r_temp_32 + r_temp_33;
  12.     end
  13. end
复制代码
x方向算子求解
  1. lways@(posedge i_clk or negedge i_rst_n)
  2. begin
  3.     if(!i_rst_n)
  4.         begin
  5.         sobel_x <= 'd0;
  6.     end
  7.     else if(r_x1 >= r_x2)
  8.         begin
  9.         sobel_x <= r_x1 - r_x2;
  10.     end
  11.         else
  12.         begin
  13.         sobel_x <= r_x2 - r_x1;
  14.     end        
  15. end
复制代码
y方向算子求解
  1. always@(posedge i_clk or negedge i_rst_n)
  2. begin
  3.     if(!i_rst_n)
  4.         begin
  5.         sobel_y <= 'd0;
  6.     end
  7.     else if(r_y1 >= r_y2)
  8.         begin
  9.         sobel_y <= r_y1 - r_y2;
  10.     end
  11.         else
  12.         begin
  13.         sobel_y <= r_y2 - r_y1;
  14.     end        
  15. end
复制代码
4.2工程结构

工程结构如图所示:

image.jpg

图像数据通过摄像头采集进来,先缓存在fifo中,然后通过写状态机,将图像数据送进DDR进行缓存,缓存后的图像数据从DDR中取出,通过读状态机送出到fifo中,然后算法处理模块在fifo中取出数据,完成数据处理后送到LCD进行显示输出。

5上板实验
image.jpg

点击下载后,可以看到正常的输出如下所示,摄像头的分辨率为640x480

image.jpg




您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


  • 微信公众平台

  • 扫描访问手机版