xref: /aosp_15_r20/cts/apps/CameraITS/utils/opencv_processing_utils_tests.py (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1# Copyright 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Tests for opencv_processing_utils."""
15
16
17import math
18import os
19import unittest
20
21import cv2
22
23import opencv_processing_utils
24
25
26class Cv2ImageProcessingUtilsTests(unittest.TestCase):
27  """Unit tests for this module."""
28
29  def test_get_angle_identify_rotated_chessboard_angle(self):
30    """Unit test to check extracted angles from images."""
31    # Array of the image files and angles containing rotated chessboards.
32    test_cases = [
33        ('', 0),
34        ('_15_ccw', -15),
35        ('_30_ccw', -30),
36        ('_45_ccw', -45),
37        ('_60_ccw', -60),
38        ('_75_ccw', -75),
39    ]
40    test_fails = ''
41
42    # For each rotated image pair (normal, wide), check angle against expected.
43    for suffix, angle in test_cases:
44      # Define image paths.
45      normal_img_path = os.path.join(
46          opencv_processing_utils.TEST_IMG_DIR,
47          f'rotated_chessboards/normal{suffix}.jpg')
48      wide_img_path = os.path.join(
49          opencv_processing_utils.TEST_IMG_DIR,
50          f'rotated_chessboards/wide{suffix}.jpg')
51
52      # Load and color-convert images.
53      normal_img = cv2.cvtColor(cv2.imread(normal_img_path), cv2.COLOR_BGR2GRAY)
54      wide_img = cv2.cvtColor(cv2.imread(wide_img_path), cv2.COLOR_BGR2GRAY)
55
56      # Assert angle as expected.
57      normal = opencv_processing_utils.get_angle(normal_img)
58      wide = opencv_processing_utils.get_angle(wide_img)
59      valid_angles = (angle, angle+90)  # try both angle & +90 due to squares
60      e_msg = (f'\n Rotation angle test failed: {angle}, extracted normal: '
61               f'{normal:.2f}, wide: {wide:.2f}, valid_angles: {valid_angles}')
62      matched_angles = False
63      for a in valid_angles:
64        if (math.isclose(normal, a,
65                         abs_tol=opencv_processing_utils.ANGLE_CHECK_TOL) and
66            math.isclose(wide, a,
67                         abs_tol=opencv_processing_utils.ANGLE_CHECK_TOL)):
68          matched_angles = True
69
70      if not matched_angles:
71        test_fails += e_msg
72
73    self.assertEqual(len(test_fails), 0, test_fails)
74
75
76if __name__ == '__main__':
77  unittest.main()
78