def test_extract_region(): R, C = 4, 6 x = np.arange(R*C) x.shape = (R, C) for r in range(R): for c in range(C): s = util.extract_region_safe(x, r, c, 0, 17) assert_equal(s.shape, (1, 1)) assert_equal(s[0, 0], x[r, c]) # upper left s = util.extract_region_safe(x, 0, 0, 1, 17) assert_equal(s.shape, (3, 3)) assert_array_equal(s[0, :], 17) assert_array_equal(s[:, 0], 17) assert_array_equal(s[1:,1:], x[:2, :2]) # upper right s = util.extract_region_safe(x, 0, 5, 1, 17) assert_equal(s.shape, (3, 3)) assert_array_equal(s[0, :], 17) assert_array_equal(s[:, 2], 17) assert_array_equal(s[1:,:2], x[:2, 4:]) # lower left s = util.extract_region_safe(x, 3, 0, 1, 17) assert_equal(s.shape, (3, 3)) assert_array_equal(s[2, :], 17) assert_array_equal(s[:, 0], 17) assert_array_equal(s[:2,1:], x[2:, :2]) # lower right s = util.extract_region_safe(x, 3, 5, 1, 17) assert_equal(s.shape, (3, 3)) assert_array_equal(s[2, :], 17) assert_array_equal(s[:, 2], 17) assert_array_equal(s[:2,:2], x[2:, 4:]) # now we move the window around for a variety of edge sizes, # save the middle pixel, and reconstruct the matrix and compare for p in range(4): newx= np.zeros_like(x) for r in range(R): for c in range(C): s = util.extract_region_safe(x, r, c, p, 17) newx[r, c] = s[p, p] assert_array_equal(newx, x)
def test_extract_region(): R, C = 4, 6 x = np.arange(R * C) x.shape = (R, C) for r in range(R): for c in range(C): s = util.extract_region_safe(x, r, c, 0, 17) assert_equal(s.shape, (1, 1)) assert_equal(s[0, 0], x[r, c]) # upper left s = util.extract_region_safe(x, 0, 0, 1, 17) assert_equal(s.shape, (3, 3)) assert_array_equal(s[0, :], 17) assert_array_equal(s[:, 0], 17) assert_array_equal(s[1:, 1:], x[:2, :2]) # upper right s = util.extract_region_safe(x, 0, 5, 1, 17) assert_equal(s.shape, (3, 3)) assert_array_equal(s[0, :], 17) assert_array_equal(s[:, 2], 17) assert_array_equal(s[1:, :2], x[:2, 4:]) # lower left s = util.extract_region_safe(x, 3, 0, 1, 17) assert_equal(s.shape, (3, 3)) assert_array_equal(s[2, :], 17) assert_array_equal(s[:, 0], 17) assert_array_equal(s[:2, 1:], x[2:, :2]) # lower right s = util.extract_region_safe(x, 3, 5, 1, 17) assert_equal(s.shape, (3, 3)) assert_array_equal(s[2, :], 17) assert_array_equal(s[:, 2], 17) assert_array_equal(s[:2, :2], x[2:, 4:]) # now we move the window around for a variety of edge sizes, # save the middle pixel, and reconstruct the matrix and compare for p in range(4): newx = np.zeros_like(x) for r in range(R): for c in range(C): s = util.extract_region_safe(x, r, c, p, 17) newx[r, c] = s[p, p] assert_array_equal(newx, x)
PIX_REGION = 24 # num pixels on either side ledimgs = np.zeros( (len(valid_frames), 2, PIX_REGION * 2 + 1, PIX_REGION * 2 + 1), dtype=np.uint8) framepos = 0 for frame_chunk in util.chunk(valid_frames, 100): frames = organizedata.get_frames(basedir, frame_chunk) for frame_idx, frame in zip(frame_chunk, frames): for led, field in [(0, 'led_front'), (1, 'led_back')]: real_pos = positions_cleaned[field][frame_idx] x, y = env.gc.real_to_image(real_pos[0], real_pos[1]) ledimgs[framepos, led, :, :] = util.extract_region_safe( frame, int(y), int(x), PIX_REGION, 0) framepos += 1 ledimgs_mean = np.mean(ledimgs.astype(np.float32), axis=0) subsamp = 60 led_params_dict = { 'ledimgs_mean': ledimgs_mean, 'dist': compute_led_sep(positions[valid_frames]), 'subsampled_frames': ledimgs[::subsamp] } pickle.dump(led_params_dict, open(led_params, 'w')) def led_measure(img_row):
PIX_REGION = 24 # num pixels on either side ledimgs = np.zeros((len(valid_frames), 2, PIX_REGION*2+1, PIX_REGION*2+1), dtype = np.uint8) framepos = 0 for frame_chunk in util.chunk(valid_frames, 100): frames = organizedata.get_frames(basedir, frame_chunk) for frame_idx, frame in zip(frame_chunk, frames): for led, field in [(0, 'led_front'), (1, 'led_back')]: real_pos = positions_cleaned[field][frame_idx] x, y = env.gc.real_to_image(real_pos[0], real_pos[1]) ledimgs[framepos, led, :, :] = util.extract_region_safe(frame, int(y), int(x), PIX_REGION, 0) framepos +=1 ledimgs_mean = np.mean(ledimgs.astype(np.float32), axis=0) subsamp = 60 led_params_dict = {'ledimgs_mean' : ledimgs_mean, 'dist' : compute_led_sep(positions[valid_frames]), 'subsampled_frames' : ledimgs[::subsamp]} pickle.dump(led_params_dict, open(led_params, 'w')) def led_measure(img_row): import scipy.stats midval = (np.max(img_row) - np.min(img_row)) / 2.0 + np.min(img_row)