1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| from scipy.ndimage import filters import stereo from PIL import Image from scipy import misc from scipy.ndimage import filters from pylab import * import numpy as np import matplotlib.pyplot as plt
def plane_sweep_ncc(iml, imr, start, steps, wid): (m, n) = iml.shape print(iml.shape) meanl = zeros((m, n)) meanr = zeros((m, n)) s = np.zeros((m, n)) s_l = np.zeros((m, n)) s_r = np.zeros((m, n)) deepmaps = np.zeros((m, n, steps)) filters.uniform_filter(iml, wid, meanl) filters.uniform_filter(imr, wid, meanr) norml = iml - meanl normr = imr - meanr for disp in range(steps): filters.uniform_filter(np.roll(norml, -disp - start) * normr, wid, s) filters.uniform_filter(np.roll(norml, -disp - start) * np.roll(norml, -disp - start), wid, s_l) filters.uniform_filter(normr * normr, wid, s_r) deepmaps[:, :, disp] = s / sqrt(s_l * s_r) return argmax(deepmaps, axis=2)
def plane_sweep_guess(iml, imr, start, steps, wid): (m, n) = iml.shape
meanl = zeros((m, n)) meanr = zeros((m, n)) s = np.zeros((m, n)) s_l = np.zeros((m, n)) s_r = np.zeros((m, n)) deepmaps = np.zeros((m, n, steps)) filters.gaussian_filter(iml, wid, 0, meanl) filters.gaussian_filter(imr, wid, 0, meanr) norml = iml - meanl normr = imr - meanr for disp in range(steps): filters.gaussian_filter(np.roll(norml, -disp - start) * normr, wid, 0, s) filters.gaussian_filter(np.roll(norml, -disp - start) * np.roll(norml, -disp - start), wid, 0, s_l) filters.gaussian_filter(normr * normr, wid, 0, s_r) deepmaps[:, :, disp] = s / sqrt(s_l * s_r) return argmax(deepmaps, axis=2) iml = np.array(Image.open('../input/stereo-sample/im0.png').convert('L'), 'f') imr = np.array(Image.open('../input/stereo-sample/im1.png').convert('L'), 'f')
steps = 50 start = 4
wid = 30 for i in range(1, wid): print('宽度{}时的滤波结果!'.format(i)) res_gaussian = plane_sweep_guess(iml, imr, start, steps, i) imsave(r'./{}.jpg'.format(i), res_gaussian)
|