小工具知识网

当前位置: 首页 > 行业知识 > Python与FPGA——sobel边缘检测

行业知识

Python与FPGA——sobel边缘检测

2026-11-07 23:14:09 来源:互联网转载

文章目录

  • 前言
  • 一、sobel边缘检测
  • 二、Python sobel边缘检测
  • 三、FPGA sobel边缘检测
  • ���结

    前言

      边缘存在于目标、背景区域之间,它是图像分割所依赖的较重要的依据,也是图像匹配的重要特征。边缘检测在图像处理和计算机视觉中,尤其在图像的特征提前、对象检测以及模式识别等方面都有重要的作用。


    一、sobel边缘检测

      下图是Gx的计算,Gy的计算类似。

      为什么要乘以一个像Gx的矩阵呢?其实就是在求上图中像素6周围像素的变化率,求导的另外一种表示方法就是变化率。

    ▽ f ( x , y ) = ( ∂ f ∂ x , ∂ f ∂ y ) = ( ∂ f ∂ x ) 2 + ( ∂ f ∂ y ) 2 \bigtriangledown f(x, y) = \Big(\frac{\partial f}{\partial x}, \frac{\partial f}{\partial y}\Big) = \sqrt{\Big(\frac{\partial f}{\partial x}\Big)^2 + \Big(\frac{\partial f}{\partial y}\Big)^2} ▽f(x,y)=(∂x∂f​,∂y∂f​)=(∂x∂f​)2+(∂y∂f​)2 ​

    G x = [ − 1 0 1 − 2 0 2 − 1 0 1 ] G y = [ − 1 − 2 − 1 0 0 0 1 2 1 ] G_x = \begin{bmatrix} -1 & 0 & 1\\ -2 & 0 & 2\\ -1 & 0 & 1 \end{bmatrix} \quad\quad\quad G_y = \begin{bmatrix} -1 & -2 & -1\\ 0 & 0 & 0\\ 1 & 2 & 1 \end{bmatrix} Gx​=⎣⎡​−1−2−1​000​121​⎦⎤​Gy​=⎣⎡​−101​−202​−101​⎦⎤​

      Gx与Gy算出了过后,再进行如下运算,计算出G。设置一个阈值threshold,如果G>=threshold,就将窗口中心对应的图像像素设置为255,否则设置为0。

    G = G x 2 + G y 2 G = \sqrt{G_x^2 + G_y^2} G=Gx2​+Gy2​ ​

    二、Python sobel边缘检测

    import numpy as npimport matplotlib.pyplot as pltimg = plt.imread("lenna.png")gray = 0.299 * img[:, :, 0] + 0.587 * img[:, :, 1] + 0.114 * img[:, :, 2] gray = gray * 255#图像是[0-1]--->[0-255]def kernel_mutiply(win, kernel):    value = np.sum(np.multiply(win, kernel))    return value    def sobel_detect(gray, gx, gy, threshold):    h, w = gray.shape    n, n = gx.shape    m = int((n - 1) / 2)    sobel_image = np.zeros((h, w))    for i in range(m, h - m):        for j in range(m, w - m):            gx2 = (kernel_mutiply(gray[i - m: i + m + 1, j - m: j + m + 1], gx)) ** 2            gy2 = (kernel_mutiply(gray[i - m: i + m + 1, j - m: j + m + 1], gy)) ** 2            value = np.sqrt(gx2 + gy2)            if value >= threshold:                sobel_image[i, j] = 255            else:                sobel_image[i, j] = 0    return sobel_image.astype(np.uint8)    gx = np.array([[-1, 0, 1],               [-2, 0, 2],               [-1, 0, 1]])gy = np.array([[-1, -2, -1],               [0, 0, 0],               [1, 2, 1]])sobel_image = sobel_detect(gray, gx, gy, 95)fig = plt.figure(figsize=(8, 8))ax = fig.add_subplot(1, 2, 1)ax.set_title("gray image")ax.set_xlabel("width")ax.set_ylabel("height")plt.imshow(gray, cmap="gray")ax = fig.add_subplot(1, 2, 2)ax.set_title("sobel image")ax.set_xlabel("width")ax.set_ylabel("height")plt.imshow(sobel_image, cmap="gray")

    三、FPGA sobel边缘检测

      在FPGA的sobel边缘检测中,增加了移位寄存器IP,sqrt数学运算IP。

    //3*3图像//P11   P12   P13//P21   P22   P23//P31   P32   P33//Gx算子//-1     0     1//-2     0     2//-1     0     1//Gx = -P11 + P13 - 2*P21 + 2*P23 - P31 + P33//Gx = (P13 - P11) + 2*(P23 - P21) + (P33 - P31)//Gy算子//1      2     1//0      0     0//-1     -2    -1//Gy = P11 + 2*P12 + P13 - P31 - 2*P32 - P33//Gy = (P11 - P31) + 2*(P12 - P32) + (P13 - P33)module  ycbcr_to_sobel(inputwiresys_clk,//系统时钟,频率为50MHZinputwiresys_rst_n,//系统复位,低电平有效inputwirergb_valid,//RGB565图像显示有效信号inputwire[7:0]th_data,//阈值数据inputwire[7:0]y_data,//Y分量inputwire[11:0]pixel_x,//有效显示区域横坐标inputwire[11:0]pixel_y,//有效显示区域纵坐标outputreg[15:0]sobel_data//Sobel算法处理后的图像数据);regy_valid;//Y分量有效信号//shift ramwire[7:0]data_row1;//移位寄存器第一行数据wire[7:0]data_row2;//移位寄存器第二行数据wire[7:0]data_row3;//移位寄存器第三行数据//3*3像素数据,左上角至右下角共9个数据reg[7:0]p11;//3*3第1个像素数据reg[7:0]p12;//3*3第2个像素数据reg[7:0]p13;//3*3第3个像素数据reg[7:0]p21;//3*3第4个像素数据reg[7:0]p22;//3*3第5个像素数据reg[7:0]p23;//3*3第6个像素数据reg[7:0]p31;//3*3第7个像素数据reg[7:0]p32;//3*3第8个像素数据reg[7:0]p33;//3*3第9个像素数据//Sobel算子wire[15:0]Gx;//水平梯度值wire[15:0]Gy;//数值梯度值wire[7:0]Gxy;//总体梯度值assign  data_row3 = y_data  ;assign  Gx = (p13 - p11) + 2*(p23 - p21) + (p33 - p31)  ;assign  Gy = (p11 - p31) + 2*(p12 - p32) + (p13 - p33)  ;//设定第一行、第二行,第一列、第二列显示全白色always@(*)if((pixel_y == 12'd0)||(pixel_y == 12'd1)||(pixel_x == 12'd2)||(pixel_x == 12'd3))sobel_data = 16'hffff  ;elsesobel_data = (Gxy >= th_data) ? 16'hffff : 16'd0  ;always@(posedge sys_clk or negedge sys_rst_n)if(sys_rst_n == 1'b0)y_valid  p11,p12,p13}  

上一篇:万网怎么换域名?

下一篇:垃圾桶服务器怎么用?