Sobel简介及代码实现

描述

 

一. Sobel简介

  •  一句话可以概况为,分别求水平与竖直梯度,然后求平方和再开方(近似的话就直接求绝对值之和),最后与设定的阈值进行比较,大于的话就赋值为0,小于的话就赋值为255。

  • x方向梯度dx的求法:3*3的图像矩阵与下面的矩阵在对应位置相乘然后相加

    sobel

     

  • y方向梯度dy的求法:同上

    sobel

     

     

二. 代码实现

  •  

这里采用近似计算G = |dx|  + |dy|, 正负号分开计算,然后用大的数减去小的数  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
reg[10:0]  Sobel_px ,Sobel_nx;reg[10:0]  Sobel_py ,Sobel_ny;
wire[10:0]  Sobel_x;wire[10:0]  Sobel_y;
wire[7:0]  Sobel_data;
//x方向的梯度assign Sobel_x = (Sobel_px > Sobel_nx) ? (Sobel_px - Sobel_nx) : (Sobel_nx - Sobel_px);//y方向的梯度assign Sobel_y = (Sobel_py > Sobel_ny) ? (Sobel_py - Sobel_ny) : (Sobel_ny - Sobel_py);assign Sobel_data = (Sobel_x + Sobel_y > 'd135) ? 'd0 : 'd255;


always@(posedge clk_9M or negedge rst)begin  if(rst == 1'b0)    begin      Sobel_px <= 'd0;      Sobel_nx <= 'd0;    end  else if(cur_x >= 'd100 && cur_x <= 'd199 && cur_y >= 'd50)    begin      Sobel_nx <= data_line_11 + data_line_21 + data_line_21 + data_line_31;      Sobel_px <= data_line_13 + data_line_23 + data_line_23 + data_line_33;    end  else    begin    Sobel_nx <= 'd0;    Sobel_px <= 'd0;    endend

always@(posedge clk_9M or negedge rst)begin  if(rst == 1'b0)    begin      Sobel_py <= 'd0;      Sobel_ny <= 'd0;    end  else if(cur_x >= 'd100 && cur_x <= 'd199 && cur_y >= 'd50)    begin      Sobel_py <= data_line_11 + data_line_12 + data_line_12 + data_line_13;      Sobel_ny <= data_line_31 + data_line_32 + data_line_32+ data_line_33;    end  else    begin      Sobel_ny <=  'd0;      Sobel_py <=  'd0;    endend
   

  审核编辑:汤梓红


打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分