def GaborFilter_(img, blockSize, wl, dire, sigma=20): imgout = np.zeros_like(img) O = block_view(imgout, (blockSize, blockSize)) B = block_view(img, (blockSize, blockSize)) for w, d, o, b in zip(wl, dire, O, B): kernel = map( lambda w, d: cv2.getGaborKernel( (blockSize, blockSize), sigma, d, w, 1), w, d) o[:, :] = np.asarray( map(lambda x, kernel: cv2.filter2D(x, -1, kernel), b, kernel)) return imgout
def GaborFilter(img, blockSize, wl, dire, sigma=20): """Gabor Filter img: input image blockSize: size of a block wl: wavelength dire: direction return: filtered image """ img = img.astype(np.float64) imgout = img.copy() kernel = np.zeros((img.shape[0] / blockSize * (blockSize + 1), img.shape[1] / blockSize * (blockSize + 1))) K = block_view(kernel, (blockSize + 1, blockSize + 1)) for k, w, d in zip(K, wl, dire): k[:, :] = np.asarray( map( lambda w, d: cv2.getGaborKernel( (blockSize + 1, blockSize + 1), sigma, d, w, 1), w, d)) for i in xrange(blockSize / 2, img.shape[0] - blockSize / 2): block_i = i / blockSize for j in xrange(blockSize / 2, img.shape[1] - blockSize / 2): block_j = j / blockSize imgout[i, j] = np.sum(K[block_i, block_j][::-1, ::-1] * img[i - blockSize / 2:i + blockSize / 2 + 1, j - blockSize / 2:j + blockSize / 2 + 1]) imgout[np.where(imgout > 255)] = 255 imgout[np.where(imgout < 0)] = 0 return imgout
def calcWlDire(img, blockSize): wl = np.zeros((img.shape[0] / blockSize, img.shape[1] / blockSize)) dire = np.zeros((img.shape[0] / blockSize, img.shape[1] / blockSize)) B = block_view(img, (blockSize, blockSize)) for w, d, b in zip(wl, dire, B): a = map(lambda x: blkWlDire(x), b) w[:] = map(lambda x: x[0], a) d[:] = map(lambda x: x[1], a) return wl, dire
def calcWl(img, blockSize): """calculation wavelength of every blocks in a given image """ wl = np.zeros((img.shape[0] / blockSize, img.shape[1] / blockSize)) B = block_view(img, (blockSize, blockSize)) for w, b in zip(wl, B): w[:] = map(lambda b: blkwl(b), b) # Gaussian smoothing gaussianBlurSigma = 4 gaussian_block = 7 wl = cv2.GaussianBlur(wl, (gaussian_block, gaussian_block), gaussianBlurSigma, gaussianBlurSigma) return wl
def segmentation(img, blockSize=8, h=352, w=288): add0 = (16 - img.shape[0] % 16) / 2 add1 = (16 - img.shape[1] % 16) / 2 img = np.vstack((255 * np.ones((add0, img.shape[1])), img, 255 * np.ones( (add0, img.shape[1])))) img = np.hstack((255 * np.ones((img.shape[0], add1)), img, 255 * np.ones( (img.shape[0], add1)))) img = np.uint8(img) ## reference: IMPROVED FINGERPRINT IMAGE SEGMENTATION USING NEW MODIFIED GRADIENT # BASED TECHNIQUE sobel_x = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) sobel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) par_x = convolve2d(img, sobel_x, mode='same') par_y = convolve2d(img, sobel_y, mode='same') #img=basic.blockproc(img,cv2.equalizeHist,(blockSize,blockSize)) stdx = blockproc(par_x, np.std, (blockSize, blockSize), True) stdy = blockproc(par_y, np.std, (blockSize, blockSize), True) grddev = stdx + stdy threshold = 90 index = grddev[1:-1, 1:-1].copy() index[np.where(index < threshold)] = 0 index[np.where(index >= threshold)] = 1 a = np.zeros(grddev.shape) a[1:-1, 1:-1] = index index = a valid = np.zeros(img.shape) valid_b = block_view(valid, (blockSize, blockSize)) valid_b[:] = index[:, :, np.newaxis, np.newaxis] kernel = np.ones((8, 8), np.uint8) # first dilate to delete the invalid value inside the fingerprint region valid = cv2.dilate(valid, kernel, iterations=3) # then erode more to delete the valid value outside the fingerprint region valid = cv2.erode(valid, kernel, iterations=10) # dilate again to increase the valid value area in compensate for the lose # due to erosion in the last step valid = cv2.dilate(valid, kernel, iterations=5) img[np.where(valid == 0)] = 255 # align the image #img=align(img, valid) return cut(img, valid, h, w)
def GaborFilterBox(img, blockSize, boxSize, wl, dire, sigma=20): """Gabor Filter img: input image blockSize: size of a block wl: wavelength dire: direction return: filtered image """ img = img.astype(np.float64) N, M = img.shape imgout = img.copy() kernel = np.zeros((img.shape[0] / boxSize * (blockSize + 1), img.shape[1] / boxSize * (blockSize + 1))) K = block_view(kernel, (blockSize + 1, blockSize + 1)) for k, w, d in zip(K, wl, dire): k[:, :] = np.asarray([ cv2.getGaborKernel((blockSize, blockSize), sigma, d_, w_, 1, 0) for w_, d_ in zip(w, d) ]) ii = -1 for i in xrange(blockSize / 2, N - blockSize / 2): ii = (i - blockSize / 2) / boxSize if ii >= N / boxSize: break a = i - blockSize / 2 b = a + blockSize + 1 img0 = img[a:b] jj = -1 for j in xrange(blockSize / 2, M - blockSize / 2): jj = (j - blockSize / 2) / boxSize if jj >= M / boxSize: break c = j - blockSize / 2 d = c + blockSize + 1 imgout[i, j] = np.sum(K[ii, jj] * img0[:, c:d]) # imgout[np.where(imgout>255)]=255;imgout[np.where(imgout<0)]=0 return imgout
blockSize = 8 img = cv2.imread(path + '105_1.tif', 0) add0 = (16 - img.shape[0] % 16) / 2 add1 = (16 - img.shape[1] % 16) / 2 img = np.vstack((255 * np.ones((add0, img.shape[1])), img, 255 * np.ones( (add0, img.shape[1])))) img = np.hstack((255 * np.ones((img.shape[0], add1)), img, 255 * np.ones( (img.shape[0], add1)))) img = np.uint8(img) # #theta=pre.calcDirection(img,blockSize) #wl=pre.calcWl(img,blockSize) #img=pre.GaborFilter(img,blockSize,wl,np.pi/2-theta) img_b = block_view(img, (32, 32)) sobel_x = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) sobel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) par_x = convolve2d(img, sobel_x, mode='same') par_y = convolve2d(img, sobel_y, mode='same') #img=basic.blockproc(img,cv2.equalizeHist,(blockSize,blockSize)) #Mx=basic.blockproc(par_x,np.mean,(8,8),True) #My=basic.blockproc(par_y,np.mean,(8,8),True) stdx = basic.blockproc(par_x, np.std, (blockSize, blockSize), True) stdy = basic.blockproc(par_y, np.std, (blockSize, blockSize), True) grddev = stdx + stdy threshold = 100 index = grddev[1:-1, 1:-1].copy()