|
| 1 | +import cv |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +def pretty_depth(depth): |
| 6 | + """Converts depth into a 'nicer' format for display |
| 7 | +
|
| 8 | + This is abstracted to allow for experimentation with normalization |
| 9 | +
|
| 10 | + Args: |
| 11 | + depth: A numpy array with 2 bytes per pixel |
| 12 | +
|
| 13 | + Returns: |
| 14 | + A numpy array that has been processed whos datatype is unspecified |
| 15 | + """ |
| 16 | + np.clip(depth, 0, 2**10 - 1, depth) |
| 17 | + depth >>= 2 |
| 18 | + depth = depth.astype(np.uint8) |
| 19 | + return depth |
| 20 | + |
| 21 | + |
| 22 | +def pretty_depth_cv(depth): |
| 23 | + """Converts depth into a 'nicer' format for display |
| 24 | +
|
| 25 | + This is abstracted to allow for experimentation with normalization |
| 26 | +
|
| 27 | + Args: |
| 28 | + depth: A numpy array with 2 bytes per pixel |
| 29 | +
|
| 30 | + Returns: |
| 31 | + An opencv image who's datatype is unspecified |
| 32 | + """ |
| 33 | + depth = pretty_depth(depth) |
| 34 | + image = cv.CreateImageHeader((depth.shape[1], depth.shape[0]), |
| 35 | + cv.IPL_DEPTH_8U, |
| 36 | + 1) |
| 37 | + cv.SetData(image, depth.tostring(), |
| 38 | + depth.dtype.itemsize * depth.shape[1]) |
| 39 | + return image |
| 40 | + |
| 41 | + |
| 42 | +def video_cv(video): |
| 43 | + """Converts video into a BGR format for opencv |
| 44 | +
|
| 45 | + This is abstracted out to allow for experimentation |
| 46 | +
|
| 47 | + Args: |
| 48 | + video: A numpy array with 1 byte per pixel, 3 channels RGB |
| 49 | +
|
| 50 | + Returns: |
| 51 | + An opencv image who's datatype is 1 byte, 3 channel BGR |
| 52 | + """ |
| 53 | + video = video[:, :, ::-1] # RGB -> BGR |
| 54 | + image = cv.CreateImageHeader((video.shape[1], video.shape[0]), |
| 55 | + cv.IPL_DEPTH_8U, |
| 56 | + 3) |
| 57 | + cv.SetData(image, video.tostring(), |
| 58 | + video.dtype.itemsize * 3 * video.shape[1]) |
| 59 | + return image |
0 commit comments