def test_read_write_flo(tmpdir): flow_path = str(tmpdir / 'test.flo') rows = 20 cols = 30 channels = 2 flow_np = (np.random.rand(rows, cols, channels) * 20).astype(np.float32) flow = Mat.fromarray(flow_np, copy=True) write_flo(flow, flow_path) flow2 = np.array(read_flo(flow_path)) assert_array_equal(flow2, flow)
def test_fromarray_preserves_data(self): array = np.random.randint(0, 255, size=(4, 5, 3), dtype=np.uint8) mat = Mat.fromarray(array) mat_as_array = mat.asarray() assert_equal(array, mat_as_array)
def test_fromarray_maintains_shape(self): array = np.zeros((5, 10, 3), dtype=np.uint8) mat = Mat.fromarray(array) assert (5, 10, 3) == mat.shape
def array_to_mat(array): """Convert np.ndarray to OpenCV Mat""" return Mat.fromarray(array)
def to_rgb(grayscale_img: np.ndarray): assert grayscale_img.ndim == 2 return Mat.fromarray( np.stack([grayscale_img] * 3, axis=-1).astype(np.uint8))
def make_random_uint8_mat(rows, cols, channels): return Mat.fromarray((np.random.rand(rows, cols, channels) * 255).astype( np.uint8), copy=True)