Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
test_numpy_image.py
35
36from typing import Any, List, Dict
37from visp.core import ImageGray, ImageRGBa, ImageRGBf, RGBa, RGBf
38
39import numpy as np
40import pytest
41
42def get_data_dicts() -> List[Dict[str, Any]]:
43 h, w = 20, 20
44 return [
45 {
46 'instance': ImageGray(h, w, 0),
47 'base_value': 0,
48 'value': 255,
49 'np_value': 255,
50 'shape': (h, w),
51 'dtype': np.uint8
52 },
53 {
54 'instance': ImageRGBa(h, w, RGBa(0, 50, 75, 255)),
55 'base_value': RGBa(0, 50, 75, 255),
56 'value': RGBa(255, 128, 0, 100),
57 'np_value': np.asarray([255, 128, 0, 100], dtype=np.uint8),
58 'shape': (h, w, 4),
59 'dtype': np.uint8
60 },
61 {
62 'instance': ImageRGBf(h, w, RGBf(0.0, 0.0, 0.0)),
63 'base_value': RGBf(0.0, 0.0, 0.0),
64 'value': RGBf(255, 0, 0),
65 'np_value': np.asarray([255, 0, 0], dtype=np.float32),
66 'shape': (h, w, 3),
67 'dtype': np.float32
68
69 },
70
71 ]
72
73
75 '''
76 Tests buffer definition, shape and dtype
77 '''
78 for test_dict in get_data_dicts():
79 np_array = np.array(test_dict['instance'], copy=False)
80 assert np_array.shape == test_dict['shape']
81 assert np_array.dtype == test_dict['dtype']
82
84 '''
85 Tests converting to a numpy array by calling our defined function
86 '''
87 for test_dict in get_data_dicts():
88 np_array = test_dict['instance'].numpy()
89 assert np_array.shape == test_dict['shape']
90 assert np_array.dtype == test_dict['dtype']
91
93 '''
94 Tests 2D pixel indexing and correspondance between visp pixel reps and numpy reps
95 '''
96 for test_dict in get_data_dicts():
97 vp_image = test_dict['instance']
98 np_array = np.array(vp_image, copy=False)
99 np_array[::2, ::2] = test_dict['np_value']
100 for i in range(vp_image.getHeight()):
101 for j in range(vp_image.getWidth()):
102 if i % 2 == 0 and j % 2 == 0:
103 assert vp_image[i, j] == test_dict['value']
104 assert vp_image[-i, -j] == test_dict['value']
105 else:
106 assert vp_image[i, j] == test_dict['base_value']
107 assert vp_image[-i, -j] == test_dict['base_value']
108
109 with pytest.raises(RuntimeError):
110 vp_image[vp_image.getHeight()]
111 with pytest.raises(RuntimeError):
112 vp_image[0, vp_image.getWidth()]
113 with pytest.raises(RuntimeError):
114 vp_image[-vp_image.getHeight() - 1]
115 with pytest.raises(RuntimeError):
116 vp_image[0, -vp_image.getWidth() - 1]
117
118
120 for test_dict in get_data_dicts():
121 vp_image = test_dict['instance']
122 # 2D indexing (basic)
123 vp_image[0, 0] = test_dict['base_value']
124 assert vp_image[0, 0] == test_dict['base_value']
125 vp_image[0, 0] = test_dict['value']
126 assert vp_image[0, 0] == test_dict['value']
127
128 # Replace a row
129 vp_image[1] = test_dict['value']
130 for i in range(vp_image.getCols()):
131 assert vp_image[1, i] == test_dict['value']
132
133
134 # Replace a row
135 vp_image[:] = test_dict['value']
136 for i in range(vp_image.getRows()):
137 for j in range(vp_image.getCols()):
138 assert vp_image[i, j] == test_dict['value']
139
140 # Replace rows with a slice
141 vp_image[:] = test_dict['base_value']
142 vp_image[::2] = test_dict['value']
143 for i in range(vp_image.getRows()):
144 v = test_dict['base_value'] if i % 2 == 1 else test_dict['value']
145 for j in range(vp_image.getCols()):
146 assert vp_image[i, j] == v
147
148 vp_image[:] = test_dict['base_value']
149 vp_image[2:-2:2] = test_dict['value']
150 for i in range(vp_image.getRows()):
151 v = test_dict['base_value'] if i % 2 == 1 or i >= vp_image.getRows() - 2 or i < 2 else test_dict['value']
152 for j in range(vp_image.getCols()):
153 assert vp_image[i, j] == v
154
155 vp_image[:, :] = test_dict['base_value']
156 for i in range(vp_image.getRows()):
157 for j in range(vp_image.getCols()):
158 assert vp_image[i, j] == test_dict['base_value']
159
160 # Indexing with two slices
161 vp_image[2:-2:2, 3:-3] = test_dict['value']
162 for i in range(vp_image.getRows()):
163 is_v = i >= 2 and i % 2 == 0 and i < vp_image.getRows() - 2
164 for j in range(vp_image.getCols()):
165 is_vj = is_v and j >= 3 and j < vp_image.getCols() - 3
166 v = test_dict['value'] if is_vj else test_dict['base_value']
167 assert vp_image[i, j] == v
168
169
170
171 # Negative step not supported
172 with pytest.raises(RuntimeError):
173 vp_image[::-1] = test_dict['value']
174 with pytest.raises(RuntimeError):
175 vp_image[:, ::-1] = test_dict['value']
176
177 # Wrong start and end values
178 with pytest.raises(RuntimeError):
179 vp_image[2:1] = test_dict['value']
180 with pytest.raises(RuntimeError):
181 vp_image[:, 3:2] = test_dict['value']
182
183
185 h, w = 30, 20
186 I = ImageGray(h, w, 0)
187 single_row = np.ones((w, ), dtype=np.uint8) * 255
188
189 I[2] = single_row
190 assert not np.any(np.equal(I.numpy()[list(set(range(h)) - {2})], single_row))
191 assert np.all(np.equal(I.numpy()[2], single_row))
192
193 I[:] = 0
194 I[1:-2] = single_row
195 assert np.all(np.equal(I.numpy()[list(set(range(h)) - {0, h - 2, h - 1})], single_row))
196 assert np.all(np.equal(I.numpy()[[0, h - 2, h - 1]], 0))
List[Dict[str, Any]] get_data_dicts()