File: test_gapi_imgproc.py

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (79 lines) | stat: -rw-r--r-- 2,716 bytes parent folder | download
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
#!/usr/bin/env python

import numpy as np
import cv2 as cv
import os

from tests_common import NewOpenCVTests


# Plaidml is an optional backend
pkgs = [
           ('ocl'    , cv.gapi.core.ocl.kernels()),
           ('cpu'    , cv.gapi.core.cpu.kernels()),
           ('fluid'  , cv.gapi.core.fluid.kernels())
           # ('plaidml', cv.gapi.core.plaidml.kernels())
       ]


class gapi_imgproc_test(NewOpenCVTests):

    def test_good_features_to_track(self):
        # TODO: Extend to use any type and size here
        img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')])
        in1 = cv.cvtColor(cv.imread(img_path), cv.COLOR_RGB2GRAY)

        # NB: goodFeaturesToTrack configuration
        max_corners         = 50
        quality_lvl         = 0.01
        min_distance        = 10
        block_sz            = 3
        use_harris_detector = True
        k                   = 0.04
        mask                = None

        # OpenCV
        expected = cv.goodFeaturesToTrack(in1, max_corners, quality_lvl,
                                          min_distance, mask=mask,
                                          blockSize=block_sz, useHarrisDetector=use_harris_detector, k=k)

        # G-API
        g_in = cv.GMat()
        g_out = cv.gapi.goodFeaturesToTrack(g_in, max_corners, quality_lvl,
                                            min_distance, mask, block_sz, use_harris_detector, k)

        comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out))

        for pkg_name, pkg in pkgs:
            actual = comp.apply(cv.gin(in1), args=cv.compile_args(pkg))
            # NB: OpenCV & G-API have different output shapes:
            # OpenCV - (num_points, 1, 2)
            # G-API  - (num_points, 2)
            # Comparison
            self.assertEqual(0.0, cv.norm(expected.flatten(), actual.flatten(), cv.NORM_INF),
                             'Failed on ' + pkg_name + ' backend')


    def test_rgb2gray(self):
        # TODO: Extend to use any type and size here
        img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')])
        in1 = cv.imread(img_path)

        # OpenCV
        expected = cv.cvtColor(in1, cv.COLOR_RGB2GRAY)

        # G-API
        g_in = cv.GMat()
        g_out = cv.gapi.RGB2Gray(g_in)

        comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out))

        for pkg_name, pkg in pkgs:
            actual = comp.apply(cv.gin(in1), args=cv.compile_args(pkg))
            # Comparison
            self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF),
                             'Failed on ' + pkg_name + ' backend')


if __name__ == '__main__':
    NewOpenCVTests.bootstrap()