Python Opencv卷积核
Keep_Trying_Go 人气:01.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()
加载全部内容