def test_setting_value_in_buffer(self): mat = Mat(rows=1, cols=2, dtype=CV_32FC3) array1 = mat.asarray() array1[0, 0, :] = [0, 1, 2] array2 = mat.asarray() assert np.array_equal(array2[0, 0, :], np.array([0, 1, 2])) del array1 del array2
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_asarray_uint(self): mat = Mat(rows=1, cols=2, dtype=CV_8UC3) array = mat.asarray() assert array.dtype == np.uint8 assert array.shape == (1, 2, 3)
def test_repr(self): mat = Mat(rows=1, cols=2) assert repr(mat) == "Mat(rows=1, cols=2, dtype=16)"
def test_non_empty_array_is_true(self): mat = Mat(rows=1, cols=1) assert bool(mat)
def test_empty_array_is_false(self): mat = Mat() assert bool(mat) == False
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 test_channels(self): mat = Mat(rows=5, cols=5, dtype=CV_8UC3) assert mat.channels == 3
def test_empty(self): mat = Mat(rows=0, cols=0) assert mat.empty
def test_cols_property(self): n_cols = 20 mat = Mat(rows=10, cols=n_cols) assert mat.cols == n_cols
def test_default_dtype_is_CV_8UC3(self): mat = Mat(rows=5, cols=5) assert mat.dtype == CV_8UC3
def test_rows_property(self): n_rows = 10 mat = Mat(rows=n_rows, cols=20) assert mat.rows == n_rows
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 test_creation(self): Mat()
def test_ndim(self): mat = Mat(rows=0, cols=0, dtype=CV_8UC3) assert mat.ndim == 3
def test_asarray_float(self): mat = Mat(rows=1, cols=2, dtype=CV_32FC2) array = mat.asarray() assert array.dtype == np.float32 assert array.shape == (1, 2, 2) del array
def test_depth(self): mat = Mat(rows=1, cols=1, dtype=CV_64FC3) assert mat.depth == CV_64F
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_shape(self): mat = Mat(rows=10, cols=20, dtype=CV_8UC3) assert mat.shape == (10, 20, 3)
def test_non_emtpy_creation(self): Mat(rows=10, cols=20)
def make_random_uint8_mat(rows, cols, channels): return Mat.fromarray((np.random.rand(rows, cols, channels) * 255).astype( np.uint8), copy=True)