Opencvノート(3)——画像形態学処理


画像形態学的処理
(2値画像用)
文書ディレクトリ
  • 画像形態学処理
  • 一、形態学-腐食操作
  • 二、形態学-膨張操作
  • 三、演算
  • 四、常用関数

  • 一、形態学-腐食操作
    import cv2          #opencv      BGR
    import matplotlib.pyplot as plt
    import numpy as np  #         
    %matplotlib inline  #      ,    
    
    kernel = np.ones((3,3), np.uint8)
    dige_eosion = cv2.erode(img, kernel, iterations = 1)
    cv2.imshow('erosion', erosion)
    cv2.waitKey(0)
    cv2.destoryAllWindows()
    
    kernel = np.ones((30,3)0, np.uint8)
    eosion_1 = cv2.erode(img, kernel, iterations = 1)
    eosion_2 = cv2.erode(img, kernel, iterations = 1)
    eosion_3 = cv2.erode(img, kernel, iterations = 1)
    res = np.hstack((eosion_1, eosion_2, eosion_3))
    cv2.imshow('res', res)
    cv2.waitKey(0)
    cv2.destoryAllWindows()
    

    二、形態学-膨張操作
    kernel = np.ones((3,3), np.uint8)
    dige_dilate = cv2.dilate(img, kernel, iterations = 1)
    cv2.imshow('dilate', dige_dilate)
    cv2.waitKey(0)
    cv2.destoryAllWindows()
    

    三、演算
    #    :   ,   
    img = cv2.imread('dige.png')
    
    kernel = np.ones((5,5), np.uint8)
    opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
    
    cv2.imshow('opening', opening)
    cv2.waitKey(0)
    cv2.destoryAllWindows()
    
    #    :   ,   
    img = cv2.imread('dige.png')
    
    kernel = np.ones((5,5), np.uint8)
    closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
    
    cv2.imshow('closing ', closing)
    cv2.waitKey(0)
    cv2.destoryAllWindows()
    
    #     =  -  
    pie = cv2.imread('pie.png')
    kernel = np.ones((7,7), np.uint8)
    dilate = cv2.dilate(pie, kernel, iterations = 5)
    erosion = cv2.erode(pie, kernel, iterations = 5)
    
    res = np.hstack((dilate, erosion))
    gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)
    
    cv2.imshow('res', res)
    cv2.imshow('gradient ', gradient )
    cv2.waitKey(0)
    cv2.destoryAllWindows()
    
    #   =    -     
    img = cv2.imread('dige.png')
    tophat= cv2.morphologyEx(pie, cv2.MORPH_TOPHAT, kernel)
    cv2.imshow('tophat', tophat)
    cv2.waitKey(0)
    cv2.destoryAllWindows()
    
    #   =   -    
    img = cv2.imread('dige.png')
    blackhat= cv2.morphologyEx(pie, cv2.MORPH_BLACKHAT, kernel)
    cv2.imshow('blackhat', blackhat)
    cv2.waitKey(0)
    cv2.destoryAllWindows()
    

    四、常用関数
  • numpy.ones(shape, dtype=None, order=‘C’)
                      ,         1。    numpy zeros()      。
          int   int  ,         。          int  ,        。         ,          。
     dtype       ,    float。             ,  int。
                   (C  )     (Fortran  )        。
    
  • cv2.erode(img, kernel, iterations)
         
    
  • cv2.dilate(img1, kernel, iterations)
         
    
  • cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
        (      )
    
  • cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
        (      )
    
  • cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
         (  -  )
    
  • cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)
         (   -   )
    
  • cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)
         (   -   )