zoukankan      html  css  js  c++  java
  • 实现Sobel算子滤波、Robers算子滤波、Laplace算子滤波

    前几天,老师布置了这样一个任务,读取图片并显示,反色后进行显示;进行Sobel算子滤波,然后反色,进行显示;进行Robers算子滤波,然后反色,进行显示。我最后加上了Laplace算子滤波,进行了比较。下面我来讲一下我的实现方法:

    一、实现过程

    思路:先完成每种函数的算法,接下来是反色函数,最后实现。

    import cv2

    import numpy as np

    # robert 算子[[-1,-1],[1,1]]

    def robert_suanzi(img):

      r, c = img.shape

      r_sunnzi = [[-1,-1],[1,1]]

      for x in range(r):

        for y in range(c):

          if (y + 2 <= c) and (x + 2 <= r):

            imgChild = img[x:x+2, y:y+2]

            list_robert = r_sunnzi*imgChild

            img[x, y] = abs(list_robert.sum())   # 求和加绝对值

      return img

            

    # # sobel算子的实现

    def sobel_suanzi(img):

      r, c = img.shape

      new_image = np.zeros((r, c))

      new_imageX = np.zeros(img.shape)

      new_imageY = np.zeros(img.shape)

      s_suanziX = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])   # X方向

      s_suanziY = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])  

      for i in range(r-2):

        for j in range(c-2):

          new_imageX[i+1, j+1] = abs(np.sum(img[i:i+3, j:j+3] * s_suanziX))

          new_imageY[i+1, j+1] = abs(np.sum(img[i:i+3, j:j+3] * s_suanziY))

          new_image[i+1, j+1] = (new_imageX[i+1, j+1]*new_imageX[i+1,j+1] + new_imageY[i+1, j+1]*new_imageY[i+1,j+1])**0.5

      # return np.uint8(new_imageX)

      # return np.uint8(new_imageY)

      return np.uint8(new_image) # 无方向算子处理的图像

    # Laplace算子

    # 常用的Laplace算子模板 [[0,1,0],[1,-4,1],[0,1,0]]  [[1,1,1],[1,-8,1],[1,1,1]]

    def Laplace_suanzi(img):

      r, c = img.shape

      new_image = np.zeros((r, c))

      L_sunnzi = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])  

      # L_sunnzi = np.array([[1,1,1],[1,-8,1],[1,1,1]])  

      for i in range(r-2):

        for j in range(c-2):

          new_image[i+1, j+1] = abs(np.sum(img[i:i+3, j:j+3] * L_sunnzi))

      return np.uint8(new_image)

    #反色函数

    def inverse_color(img):

      height,width = img.shape

      img2 = img.copy()

      for i in range(height):

        for j in range(width):

            img2[i,j] = (255-img[i,j])

      return img2

    img = cv2.imread('E:/test3.bmp', cv2.IMREAD_GRAYSCALE)

    cv2.imshow('image', img)

    img2 = inverse_color(img)

    cv2.imshow('image2',img2)

    # # robers算子

    out_robert = robert_suanzi(img)

    out_robert = inverse_color(out_robert)

    cv2.imshow('robert_image', out_robert)

    # sobel 算子

    out_sobel = sobel_suanzi(img)

    out_sobel = inverse_color(out_sobel)

    cv2.imshow('sobel_image', out_sobel)

    # Laplace算子

    out_laplace = Laplace_suanzi(img)

    out_laplace = inverse_color(out_laplace)

    cv2.imshow('laplace_image', out_laplace)

    cv2.waitKey(0)

    cv2.destroyAllWindows()

    二、运行效果

    原图

     反色后

     Sobel算子滤波

     robert算子滤波

     laplace算子滤波

     三、问题及解决办法

    1.出现找不到文件的情况

     

    这是第一次实验就遇到的问题,我以为还是符号的问题(就是文件前面要用/),但发现改了之后仍然出现问题,然后发现是我把图片的后缀记错了,如下图,test3的后缀是bmp,而我代码里出了问题,后来改正了就可以了。

     

    2.运行结果没有进行反色处理。

     

    解决方法:其实就是忘记了题目的要求,然后后来加上了反色的代码,结果就是题目要求的了。代码如下:

  • 相关阅读:
    (转)MP4文件两种格式AVC1和H264的区别及利用FFMPEG demux为h264码流事项
    (转)【多媒体封装格式详解】--- AAC ADTS格式分析
    (转)使用FFMPEG类库分离出多媒体文件中的H.264码流
    (转)ffmpeg 从mp4上提取H264的nalu
    (原)hisi3531立体声pcm实现播放方式
    (转)关于yuv 格式:planar和semi-planar格式
    (转)LCD:LCD常用接口原理篇
    Instrumentation 两种方法 premain Agent
    解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variabl
    Java反射机制获取Class文件
  • 原文地址:https://www.cnblogs.com/qq991025/p/12492578.html
Copyright © 2011-2022 走看看