版权声明:欢迎转载与留言提问 https://blog.csdn.net/qq_25439417/article/details/82624153
import cv2
import numpy as np
import pylab as pl
from PIL import Image
import time
#构建Gabor滤波器
def build_filters():
filters = []
ksize = [7,9,11,13,15,17] #gabor尺度 6个
lamda = np.pi/2.0 # 波长
kern = cv2.getGaborKernel((15,15),1,np.pi/2,lamda,0.5,0,ktype=cv2.CV_32F)
kern /= 1.5*kern.sum()
filters.append(kern)
return filters
#滤波过程
def process(img,filters):
accum = np.zeros_like(img)
for kern in filters:
fimg = cv2.filter2D(img,cv2.CV_8UC3,kern)
np.maximum(accum,fimg,accum)
return accum
#特征图生成并显示
def getGabor(img,filters):
res = [] #滤波结果
for i in range(len(filters)):
res1 = process(img,filters[i])
res.append(np.asarray(res1))
return res
if __name__ == '__main__':
cap = cv2.VideoCapture('WIN_20180831_13_49_39_Pro.mp4')
filters = build_filters()
iss=True
while iss:
iss,img = cap.read()
if iss:
img = np.power(img/255.,2.2)
img = np.uint8(img*255)
cv2.imshow("yuantu_gaus",img)
edgeimg = getGabor(img,filters)[0]
grayedgeimg = cv2.cvtColor(edgeimg,cv2.COLOR_BGR2GRAY)
# sn = np.histogram(grayedgeimg)
grayedgeimg[grayedgeimg>=grayedgeimg.mean()]=255
threshold,grayedgeimg = cv2.threshold(grayedgeimg,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imshow("grayedgeimg",grayedgeimg)
# time.sleep(0.1)
if cv2.waitKey(1) & 0xFF == 'q':
break