def elasticDistort(picture, sigma, alpha): dx, dy = generateDisplacementFields(sigma, alpha) distortion = np.zeros(Constants.pic_shape) Visualizer.plotNumber('dx', dx) Visualizer.plotNumber('dy', dy) # For each pixel use the displacement field to grab new pixel values # Hopefully it will be elastically distorted! :D # Bilinear interpolation for i in range(Constants.pic_height): for j in range(Constants.pic_width): x = i + dx[i, j] y = j + dy[i, j] # If the new location we're trying to grab is outside the picture # boundaries, then leave the pixel value as zero if x < 0 or x > (Constants.pic_width - 1): continue if y < 0 or y > (Constants.pic_height - 1): continue distortion[i, j] = bilinearInterpolation(picture, x, y) return distortion
def main(): from Visualizer import Visualizer #data = np.genfromtxt('Data/train.csv', delimiter=',') # Why does numpy break? Nobody knows... print 'Reading data' data = pd.read_csv('Data/train.csv') print 'Reshaping data' row = 0 label = data.iloc[row, 0] picture = np.reshape(data.iloc[row, 1:], Constants.pic_shape) print 'Plotting number' Visualizer.plotNumber(str(label), picture) print 'Transforming' sigma = 4 alpha = 8 elasticDistort(picture, sigma, alpha) return # distortion = elasticDistort(picture, sigma, alpha) print distortion print 'Plotting zoom' Visualizer.plotNumber(str(label), distortion)