1. 程式人生 > >Gabor小波變換處理眼部影象

Gabor小波變換處理眼部影象


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