File: test_numpy_image.py

package info (click to toggle)
visp 3.7.0-7
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 166,380 kB
  • sloc: cpp: 392,705; ansic: 224,448; xml: 23,444; python: 13,701; java: 4,792; sh: 206; objc: 145; makefile: 118
file content (196 lines) | stat: -rw-r--r-- 6,485 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#############################################################################
#
# ViSP, open source Visual Servoing Platform software.
# Copyright (C) 2005 - 2025 by Inria. All rights reserved.
#
# This software is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# See the file LICENSE.txt at the root directory of this source
# distribution for additional information about the GNU GPL.
#
# For using ViSP with software that can not be combined with the GNU
# GPL, please contact Inria about acquiring a ViSP Professional
# Edition License.
#
# See https://visp.inria.fr for more information.
#
# This software was developed at:
# Inria Rennes - Bretagne Atlantique
# Campus Universitaire de Beaulieu
# 35042 Rennes Cedex
# France
#
# If you have questions regarding the use of this file, please contact
# Inria at visp@inria.fr
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Description:
# ViSP Python bindings test
#
#############################################################################

from typing import Any, List, Dict
from visp.core import ImageGray, ImageRGBa, ImageRGBf, RGBa, RGBf

import numpy as np
import pytest

def get_data_dicts() -> List[Dict[str, Any]]:
  h, w = 20, 20
  return [
    {
      'instance': ImageGray(h, w, 0),
      'base_value': 0,
      'value': 255,
      'np_value': 255,
      'shape': (h, w),
      'dtype': np.uint8
    },
    {
      'instance': ImageRGBa(h, w, RGBa(0, 50, 75, 255)),
      'base_value': RGBa(0, 50, 75, 255),
      'value': RGBa(255, 128, 0, 100),
      'np_value': np.asarray([255, 128, 0, 100], dtype=np.uint8),
      'shape': (h, w, 4),
      'dtype': np.uint8
    },
    {
      'instance': ImageRGBf(h, w, RGBf(0.0, 0.0, 0.0)),
      'base_value': RGBf(0.0, 0.0, 0.0),
      'value': RGBf(255, 0, 0),
      'np_value': np.asarray([255, 0, 0], dtype=np.float32),
      'shape': (h, w, 3),
      'dtype': np.float32

    },

  ]


def test_np_array_shape_type():
  '''
  Tests buffer definition, shape and dtype
  '''
  for test_dict in get_data_dicts():
    np_array = np.array(test_dict['instance'], copy=False)
    assert np_array.shape == test_dict['shape']
    assert np_array.dtype == test_dict['dtype']

def test_np_array_shape_type_numpy_fn():
  '''
  Tests converting to a numpy array by calling our defined function
  '''
  for test_dict in get_data_dicts():
    np_array = test_dict['instance'].numpy()
    assert np_array.shape == test_dict['shape']
    assert np_array.dtype == test_dict['dtype']

def test_np_array_replace_value():
  '''
  Tests 2D pixel indexing and correspondance between visp pixel reps and numpy reps
  '''
  for test_dict in get_data_dicts():
    vp_image = test_dict['instance']
    np_array = np.array(vp_image, copy=False)
    np_array[::2, ::2] = test_dict['np_value']
    for i in range(vp_image.getHeight()):
      for j in range(vp_image.getWidth()):
        if i % 2 == 0 and j % 2 == 0:
          assert vp_image[i, j] == test_dict['value']
          assert vp_image[-i, -j] == test_dict['value']
        else:
          assert vp_image[i, j] == test_dict['base_value']
          assert vp_image[-i, -j] == test_dict['base_value']

    with pytest.raises(RuntimeError):
      vp_image[vp_image.getHeight()]
    with pytest.raises(RuntimeError):
      vp_image[0, vp_image.getWidth()]
    with pytest.raises(RuntimeError):
      vp_image[-vp_image.getHeight() - 1]
    with pytest.raises(RuntimeError):
      vp_image[0, -vp_image.getWidth() - 1]


def test_setitem_with_single_value():
  for test_dict in get_data_dicts():
    vp_image = test_dict['instance']
    # 2D indexing (basic)
    vp_image[0, 0] = test_dict['base_value']
    assert vp_image[0, 0] == test_dict['base_value']
    vp_image[0, 0] = test_dict['value']
    assert vp_image[0, 0] == test_dict['value']

    # Replace a row
    vp_image[1] = test_dict['value']
    for i in range(vp_image.getCols()):
      assert vp_image[1, i] == test_dict['value']


    # Replace a row
    vp_image[:] = test_dict['value']
    for i in range(vp_image.getRows()):
      for j in range(vp_image.getCols()):
        assert vp_image[i, j] == test_dict['value']

    # Replace rows with a slice
    vp_image[:] = test_dict['base_value']
    vp_image[::2] = test_dict['value']
    for i in range(vp_image.getRows()):
      v = test_dict['base_value'] if i % 2 == 1 else test_dict['value']
      for j in range(vp_image.getCols()):
        assert vp_image[i, j] == v

    vp_image[:] = test_dict['base_value']
    vp_image[2:-2:2] = test_dict['value']
    for i in range(vp_image.getRows()):
      v = test_dict['base_value'] if i % 2 == 1 or i >= vp_image.getRows() - 2 or i < 2 else test_dict['value']
      for j in range(vp_image.getCols()):
        assert vp_image[i, j] == v

    vp_image[:, :] = test_dict['base_value']
    for i in range(vp_image.getRows()):
      for j in range(vp_image.getCols()):
        assert vp_image[i, j] == test_dict['base_value']

    # Indexing with two slices
    vp_image[2:-2:2, 3:-3] = test_dict['value']
    for i in range(vp_image.getRows()):
      is_v = i >= 2 and i % 2 == 0 and i < vp_image.getRows() - 2
      for j in range(vp_image.getCols()):
        is_vj = is_v and j >= 3 and j < vp_image.getCols() - 3
        v = test_dict['value'] if is_vj else test_dict['base_value']
        assert vp_image[i, j] == v



    # Negative step not supported
    with pytest.raises(RuntimeError):
      vp_image[::-1] = test_dict['value']
    with pytest.raises(RuntimeError):
      vp_image[:, ::-1] = test_dict['value']

    # Wrong start and end values
    with pytest.raises(RuntimeError):
      vp_image[2:1] = test_dict['value']
    with pytest.raises(RuntimeError):
      vp_image[:, 3:2] = test_dict['value']


def test_setitem_with_numpy_raw_image():
  h, w = 30, 20
  I = ImageGray(h, w, 0)
  single_row = np.ones((w, ), dtype=np.uint8) * 255

  I[2] = single_row
  assert not np.any(np.equal(I.numpy()[list(set(range(h)) - {2})], single_row))
  assert np.all(np.equal(I.numpy()[2], single_row))

  I[:] = 0
  I[1:-2] = single_row
  assert np.all(np.equal(I.numpy()[list(set(range(h)) - {0, h - 2, h - 1})], single_row))
  assert np.all(np.equal(I.numpy()[[0, h - 2, h - 1]], 0))