def get_features(im): f = np.zeros((im.shape[0], im.shape[1], 28)) if len(im.shape) == 2: f[:, :, 0] = im - 0.5 f[1:-1, 1:-1, 1:] = pyhog.features_pedro(color.gray2rgb(im), 1)[:, :, :27] else: f[:, :, 0] = color.rgb2gray(im_patch) - 0.5 f[1:-1, 1:-1, 1:] = pyhog.features_pedro(color.gray2rgb(im), 1)[:, :, :27] return f
def get_feature_map(im_patch): out = np.zeros((im_patch.shape[0], im_patch.shape[1], 28)) if len(im_patch.shape) == 2: out[:, :, 0] = im_patch - 0.5 temp = pyhog.features_pedro(skic.gray2rgb(im_patch), 1) out[1:-1, 1:-1, 1:] = temp[:, :, :27] else: out[:, :, 0] = skic.rgb2gray(im_patch) - 0.5 temp = pyhog.features_pedro(skic.gray2rgb(im_patch), 1) out[1:-1, 1:-1, 1:] = temp[:, :, :27] return out
def get_scale_sample(im, pos, base_target_size, scale_factors, cos_window, scale_model_size): n = len(scale_factors) for i in range(n): patch_size = np.floor(base_target_size * scale_factors[i]) xs = np.floor(pos[1]) + np.arange(patch_size[1]) - np.floor( patch_size[1] / 2) ys = np.floor(pos[0]) + np.arange(patch_size[0]) - np.floor( patch_size[0] / 2) x_start = np.maximum(0, np.floor(pos[1]) - np.floor(patch_size[1] / 2)) y_start = np.maximum(0, np.floor(pos[0]) - np.floor(patch_size[0] / 2)) x_stop = np.minimum( im.shape[1] - 1, np.floor(pos[1]) + patch_size[1] - np.floor(patch_size[1] / 2)) y_stop = np.minimum( im.shape[0] - 1, np.floor(pos[0]) + patch_size[0] - np.floor(patch_size[0] / 2)) im_patch = im[y_start.astype(np.int32):y_stop.astype(np.int32), x_start.astype(np.int32):x_stop.astype(np.int32)] im_patch_resized = transform.resize(im_patch, scale_model_size) temp_hog = pyhog.features_pedro( repeat_to_third_dim(im_patch_resized, 3), 4) temp = temp_hog[:, :, :32] if i == 0: features = np.zeros((temp.size, n)) features[:, i] = temp.ravel() * cos_window[i] return features
def get_scale_sample(im, pos, base_target_sz, scaleFactors, scale_window, scale_model_sz): nScales = len(scaleFactors) for s in range(nScales): patch_sz = np.floor(base_target_sz * scaleFactors[s]) xs = np.floor(pos[1]) + np.arange(patch_sz[1]) - \ np.floor(patch_sz[1] / 2) ys = np.floor(pos[0]) + np.arange(patch_sz[0]) - \ np.floor(patch_sz[0] / 2) # # check for out-of-bounds coordinates, and set them to the values at # # the borders # xs[xs < 0] = 0 # ys[ys < 0] = 0 # xs[xs > im.shape[1] - 1] = im.shape[1] - 1 # ys[ys > im.shape[0] - 1] = im.shape[0] - 1 # # # extract image # im_patch = im[ys, xs, :] x_start = np.maximum(0, np.floor(pos[1]) - np.floor(patch_sz[1] / 2)) y_start = np.maximum(0, np.floor(pos[0]) - np.floor(patch_sz[0] / 2)) x_stop = np.minimum( im.shape[1] - 1, np.floor(pos[1]) + patch_sz[1] - np.floor(patch_sz[1] / 2)) y_stop = np.minimum( im.shape[0] - 1, np.floor(pos[0]) + patch_sz[0] - np.floor(patch_sz[0] / 2)) # extract image im_patch = im[x_start.astype(np.int32):x_stop.astype(np.int32), y_start.astype(np.int32):y_stop.astype(np.int32)] # resize image to model size im_patch_resized = skit.resize(im_patch, scale_model_sz) temp_hog = pyhog.features_pedro( repeat_to_third_dim(im_patch_resized, 3), 4) temp = temp_hog[:, :, :32] if s == 0: out = np.zeros((temp.size, nScales)) # window out[:, s] = temp.ravel() * scale_window[s] return out
def get_scale_sample(im, pos, base_target_sz, scaleFactors, scale_window, scale_model_sz): nScales = len(scaleFactors) for s in range(nScales): patch_sz = np.floor(base_target_sz * scaleFactors[s]) xs = np.floor(pos[1]) + np.arange(patch_sz[1]) - \ np.floor(patch_sz[1] / 2) ys = np.floor(pos[0]) + np.arange(patch_sz[0]) - \ np.floor(patch_sz[0] / 2) x_start = np.maximum(0, np.floor(pos[1]) - np.floor(patch_sz[1] / 2)) y_start = np.maximum(0, np.floor(pos[0]) - np.floor(patch_sz[0] / 2)) x_stop = np.minimum(im.shape[1] - 1, np.floor(pos[1]) + patch_sz[1] - np.floor(patch_sz[1] / 2)) y_stop = np.minimum(im.shape[0] - 1, np.floor(pos[0]) + patch_sz[0] - np.floor(patch_sz[0] / 2)) # extract image im_patch = im[y_start.astype(np.int32):y_stop.astype(np.int32), x_start.astype(np.int32):x_stop.astype(np.int32)] # resize image to model size im_patch_resized = skit.resize(im_patch, scale_model_sz) temp_hog = pyhog.features_pedro( repeat_to_third_dim(im_patch_resized, 3), 4) temp = temp_hog[:, :, :32] if s == 0: out = np.zeros((temp.size, nScales)) # window out[:, s] = temp.ravel() * scale_window[s] return out
if not args.hog: # normalize the image print "Average grayvalue: %f" % (np.mean(imgraw,axis=None)) img = np.zeros(imgraw.shape) if img.ndim==3: img[:,:,0] = (imgraw[:,:,0] - np.mean(imgraw[:,:,0],axis=None)) / 255.0 img[:,:,1] = (imgraw[:,:,1] - np.mean(imgraw[:,:,1],axis=None)) / 255.0 img[:,:,2] = (imgraw[:,:,2] - np.mean(imgraw[:,:,2],axis=None)) / 255.0 else: img = (imgraw - np.mean(imgraw,axis=None)) / 255.0 else: # use HOG image img = pyhog.features_pedro(imgraw, 8) print "Dimensions of the image: ", img.shape m = re.match('^(\d+):(\d+):(\d+)$', args.ps) if m: p = np.arange ( int(m.group(1)), int(m.group(3)), int(m.group(2)) ) patchSizes = np.vstack ( (p, p) ).T else: m = re.match('^(\d+)x(\d+)$', args.ps) if m: patchSizes = np.zeros([1,2]) patchSizes[0,0] = int(m.group(1)) patchSizes[0,1] = int(m.group(2)) else: print "Wrong format given for the patch size"
def feature_pyramid(img): """ Compute feature pyramid for different image scales Returns: ------- tuple -list, each element is a np.array which represents HoG feature -np.array, each element is double-precision number which represents scale """ #import scipy.ndimage.interpolation as npimage #it seems that using npimage.zoom brings some artifitials, #Unfortunately, scipy.misc.imresize only return uint8 array. from scipy.misc import imresize #however, still use imresize, #since it's 30x faster from pyhog import features_pedro #change data type to float64 #img = img.astype(np.float64, copy=False)/255.0 MAXLEVELS = 200 MINIDIMENSION = 5 interval = detect_levels_per_octave sc = 2 ** (1 / interval) feat = [] scale = np.zeros((MAXLEVELS), dtype=float) #Start at detect_max_scale, and keep going down by the increment sc, until #we reach MAXLEVELS or detect_min_scale for i in range(MAXLEVELS): scaler = detect_max_scale / (sc ** (i)) if scaler < detect_min_scale: return (feat, scale) scale[i] = scaler scaled = imresize(img, scaler).astype(np.float64, copy=False)/255.0 #if minimum dimensions is less than or equal to 5, return if min(scaled.shape[0], scaled.shape[1]) <= MINIDIMENSION: scale = scale[np.where(scale > 0.0)] return (feat, scale) feature = features_pedro(scaled, sbin) #if we get zero size feature, backtrack one, and dont produce any #more levels if min(feature.shape[0], feature.shape[1]) == 0: scale = scale[:,:i] return (feat, scale) #recover lost bin feature = np.lib.pad(feature,((1,1),(1,1),(0,0)), 'constant',constant_values=(0.0)) feat.append(feature) #if max dimensions is less than or equal to 5, return if max(feature.shape[0], feature.shape[1]) <= MINIDIMENSION: scale = scale[np.where(scale > 0.0)] return (feat, scale) return (feat, scale)
# -*- coding: utf-8 -*- """ Created on Fri Apr 1 11:02:37 2016 @author: liangfu """ import pyhog import numpy as np import matplotlib.pyplot as pl from scipy.misc import imread, imrotate import features_pedro_py img = (imread('../VOC2007/JPEGImages/000015.jpg').astype(np.float64)/255.0) pl.imshow(img) pl.axis('off') hog = pyhog.features_pedro(img, 30) hog.shape
def main(): import pdb pdb.set_trace() im = Image.open("testImage.jpg") hogbin = pyhog.features_pedro(im, 20) pyhog.hog_picture(hogbin)