首頁 > 軟體

Python Opencv中獲取折積核的實現程式碼

2022-07-04 14:01:47

1.cv2.getStructuringElement(shape,ksize,anchor=(-1,-1))

該函數構造並返回可進一步傳遞給createMorphologyFilter()、Correase()、Explate()或morphologyEx()的結構元素。但您也可以自己構造任意的二進位制掩碼,並將其用作結構元素。

2.函數講解

getStructuringElement(shape, ksize, anchor=None):

Shape:結構單元使用的型別;
Ksize:折積核的大小,如(3,3),(5,5)……。
Anchor:折積核的錨點位置,預設為中心位置,其值為(-1,-1);

3.程式碼實戰

import os
import cv2
import numpy as np
def ErodeFilterRect(img_path='images/lenna.png'):
    img_src=cv2.imread(img_path)
    img=cv2.resize(src=img_src,dsize=(500,500))
    img=cv2.cvtColor(src=img,code=cv2.COLOR_BGR2GRAY)
    # kernel=np.ones((7,7),dtype=np.uint8)
    kernel=cv2.getStructuringElement(shape=cv2.MORPH_RECT,ksize=(3,3))
    print(kernel)
    img=cv2.erode(src=img,kernel=kernel,iterations=1)
    cv2.imshow('img_src',img_src)
    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
def ErodeFilterELLIPSE(img_path='images/lenna.png'):
    img_src=cv2.imread(img_path)
    img=cv2.resize(src=img_src,dsize=(500,500))
    img=cv2.cvtColor(src=img,code=cv2.COLOR_BGR2GRAY)
    # kernel=np.ones((7,7),dtype=np.uint8)
    kernel=cv2.getStructuringElement(shape=cv2.MORPH_ELLIPSE,ksize=(3,3))
    print(kernel)
    img=cv2.erode(src=img,kernel=kernel,iterations=1)
    cv2.imshow('img_src',img_src)
    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
def ErodeFilterCROSS(img_path='images/lenna.png'):
    img_src=cv2.imread(img_path)
    img=cv2.resize(src=img_src,dsize=(500,500))
    img=cv2.cvtColor(src=img,code=cv2.COLOR_BGR2GRAY)
    # kernel=np.ones((7,7),dtype=np.uint8)
    kernel=cv2.getStructuringElement(shape=cv2.MORPH_CROSS,ksize=(3,3))
    print(kernel)
    img=cv2.erode(src=img,kernel=kernel,iterations=1)
    cv2.imshow('img_src',img_src)
    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
if __name__ == '__main__':
    print('Pycharm')
    # ErodeFilterRect()
    # ErodeFilterELLIPSE()
    ErodeFilterCROSS()

到此這篇關於Python Opencv中獲取折積核的實現程式碼的文章就介紹到這了,更多相關Python Opencv折積核內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com