python - HOG Feature extraction -
i want extract hog features of line images of arabic handwriting. code follows. , want regarding how input image , how output features . can please me regarding this.
import numpy np scipy import sqrt, pi, arctan2, cos, sin scipy.ndimage import uniform_filter def hog(image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(3, 3), visualise=false, normalise=false): """extract histogram of oriented gradients (hog) given image. compute histogram of oriented gradients (hog) 1. (optional) global image normalisation 2. computing gradient image in x , y 3. computing gradient histograms 4. normalising across blocks 5. flattening feature vector parameters ---------- image : (m, n) ndarray input image (greyscale). orientations : int number of orientation bins. pixels_per_cell : 2 tuple (int, int) size (in pixels) of cell. cells_per_block : 2 tuple (int,int) number of cells in each block. visualise : bool, optional return image of hog. normalise : bool, optional apply power law compression normalise image before processing. returns ------- newarr : ndarray hog image 1d (flattened) array. hog_image : ndarray (if visualise=true) visualisation of hog image. references ---------- * http://en.wikipedia.org/wiki/histogram_of_oriented_gradients * dalal, n , triggs, b, histograms of oriented gradients human detection, ieee computer society conference on computer vision , pattern recognition 2005 san diego, ca, usa """ image = np.atleast_2d(image) """ first stage applies optional global image normalisation equalisation designed reduce influence of illumination effects. in practice use gamma (power law) compression, either computing square root or log of each colour channel. image texture strength typically proportional local surface illumination compression helps reduce effects of local shadowing , illumination variations. """ if image.ndim > 3: raise valueerror("currently supports grey-level images") if normalise: image = sqrt(image) """ second stage computes first order image gradients. these capture contour, silhouette , texture information, while providing further resistance illumination variations. locally dominant colour channel used, provides colour invariance large extent. variant methods may include second order image derivatives, act primitive bar detectors - useful feature capturing, e.g. bar structures in bicycles , limbs in humans. """ gx = np.zeros(image.shape) gy = np.zeros(image.shape) gx[:, :-1] = np.diff(image, n=1, axis=1) gy[:-1, :] = np.diff(image, n=1, axis=0) """ third stage aims produce encoding sensitive local image content while remaining resistant small changes in pose or appearance. adopted method pools gradient orientation information locally in same way sift [lowe 2004] feature. image window divided small spatial regions, called "cells". each cell accumulate local 1-d histogram of gradient or edge orientations on pixels in cell. combined cell-level 1-d histogram forms basic "orientation histogram" representation. each orientation histogram divides gradient angle range fixed number of predetermined bins. gradient magnitudes of pixels in cell used vote orientation histogram. """ magnitude = sqrt(gx ** 2 + gy ** 2) orientation = arctan2(gy, (gx + 1e-15)) * (180 / pi) + 90 sy, sx = image.shape cx, cy = pixels_per_cell bx, = cells_per_block n_cellsx = int(np.floor(sx // cx)) # number of cells in x n_cellsy = int(np.floor(sy // cy)) # number of cells in y # compute orientations integral images orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations)) in range(orientations): #create new integral image orientation # isolate orientations in range temp_ori = np.where(orientation < 180 / orientations * (i + 1), orientation, 0) temp_ori = np.where(orientation >= 180 / orientations * i, temp_ori, 0) # select magnitudes orientations cond2 = temp_ori > 0 temp_mag = np.where(cond2, magnitude, 0) orientation_histogram[:,:,i] = uniform_filter(temp_mag, size=(cy, cx))[cy/2::cy, cx/2::cx] # each cell, compute histogram #orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations)) radius = min(cx, cy) // 2 - 1 hog_image = none if visualise: hog_image = np.zeros((sy, sx), dtype=float) if visualise: skimage import draw x in range(n_cellsx): y in range(n_cellsy): o in range(orientations): centre = tuple([y * cy + cy // 2, x * cx + cx // 2]) dx = radius * cos(float(o) / orientations * np.pi) dy = radius * sin(float(o) / orientations * np.pi) rr, cc = draw.bresenham(centre[0] - dx, centre[1] - dy, centre[0] + dx, centre[1] + dy) hog_image[rr, cc] += orientation_histogram[y, x, o] """ fourth stage computes normalisation, takes local groups of cells , contrast normalises overall responses before passing next stage. normalisation introduces better invariance illumination, shadowing, , edge contrast. performed accumulating measure of local histogram "energy" on local groups of cells call "blocks". result used normalise each cell in block. typically each individual cell shared between several blocks, normalisations block dependent , different. cell appears several times in final output vector different normalisations. may seem redundant improves performance. refer normalised block descriptors histogram of oriented gradient (hog) descriptors. """ n_blocksx = (n_cellsx - bx) + 1 n_blocksy = (n_cellsy - by) + 1 normalised_blocks = np.zeros((n_blocksy, n_blocksx, by, bx, orientations)) x in range(n_blocksx): y in range(n_blocksy): block = orientation_histogram[y:y + by, x:x + bx, :] eps = 1e-5 normalised_blocks[y, x, :] = block / sqrt(block.sum() ** 2 + eps) """ final step collects hog descriptors blocks of dense overlapping grid of blocks covering detection window combined feature vector use in window classifier. """ if visualise: return normalised_blocks.ravel(), hog_image else: return normalised_blocks.ravel()
you can use opencv library read image files numpy arrays.
Comments
Post a Comment