计算机视觉课程要用K聚类和EM算法完成图像分割任务,先贴别人的代码,有空补注释或者自己rebuild
1.Gabor
是一种滤波器,用来提取图片的纹理特征。
import cv2,os
import numpy as np
import matplotlib.pyplot as plt
def get_img(input_Path):
img_paths = []
for (path, dirs, files) in os.walk(input_Path):
for filename in files:
if filename.endswith(('.jpg','.png','.bmp')):
img_paths.append(path+'/'+filename)
return img_paths
#构建Gabor滤波器
def build_filters():
filters = []
ksize = [7,9,11,13,15,17] # gabor尺度,6个
lamda = np.pi/2.0 # 波长
for theta in np.arange(0, np.pi, np.pi / 4): #gabor方向,0°,45°,90°,135°,共四个
for K in range(6):
kern = cv2.getGaborKernel((ksize[K], ksize[K]), 1.0, theta, lamda, 0.5, 0, ktype=cv2.CV_32F)
kern /= 1.5*kern.sum()
filters.append(kern)
plt.figure(1)