Пример #1
0
    def test_threshold(self):
        img_path = self.find_file('cv/face/david2.jpg',
                                  [os.environ.get('OPENCV_TEST_DATA_PATH')])
        in_mat = cv.cvtColor(cv.imread(img_path), cv.COLOR_RGB2GRAY)
        maxv = (30, 30)

        # OpenCV
        expected_thresh, expected_mat = cv.threshold(in_mat, maxv[0], maxv[0],
                                                     cv.THRESH_TRIANGLE)

        # G-API
        g_in = cv.GMat()
        g_sc = cv.GScalar()
        mat, threshold = cv.gapi.threshold(g_in, g_sc, cv.THRESH_TRIANGLE)
        comp = cv.GComputation(cv.GIn(g_in, g_sc), cv.GOut(mat, threshold))

        for pkg_name, pkg in pkgs:
            actual_mat, actual_thresh = comp.apply(cv.gin(in_mat, maxv),
                                                   args=cv.compile_args(pkg))
            # Comparison
            self.assertEqual(0.0, cv.norm(expected_mat, actual_mat,
                                          cv.NORM_INF),
                             'Failed on ' + pkg_name + ' backend')
            self.assertEqual(expected_mat.dtype, actual_mat.dtype,
                             'Failed on ' + pkg_name + ' backend')
            self.assertEqual(expected_thresh, actual_thresh[0],
                             'Failed on ' + pkg_name + ' backend')
        def test_custom_op_addC(self):
            sz = (3, 3, 3)
            in_mat = np.full(sz, 45, dtype=np.uint8)
            sc = (50, 10, 20)

            # Numpy reference, make array from sc to keep uint8 dtype.
            expected = in_mat + np.array(sc, dtype=np.uint8)

            # G-API
            g_in  = cv.GMat()
            g_sc  = cv.GScalar()
            g_out = GAddC.on(g_in, g_sc, cv.CV_8UC1)
            comp  = cv.GComputation(cv.GIn(g_in, g_sc), cv.GOut(g_out))

            pkg = cv.gapi.kernels(GAddCImpl)
            actual = comp.apply(cv.gin(in_mat, sc), args=cv.gapi.compile_args(pkg))

            self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
Пример #3
0
    def test_threshold(self):
        sz = (1280, 720)
        in_mat = np.random.randint(0, 100, sz).astype(np.uint8)
        rand_int = np.random.randint(0, 50)
        maxv = (rand_int, rand_int)

        # OpenCV
        expected_thresh, expected_mat = cv.threshold(in_mat, maxv[0], maxv[0], cv.THRESH_TRIANGLE)

        # G-API
        g_in = cv.GMat()
        g_sc = cv.GScalar()
        mat, threshold = cv.gapi.threshold(g_in, g_sc, cv.THRESH_TRIANGLE)
        comp = cv.GComputation(cv.GIn(g_in, g_sc), cv.GOut(mat, threshold))

        for pkg in pkgs:
            actual_mat, actual_thresh = comp.apply(cv.gin(in_mat, maxv), args=cv.compile_args(pkg))
            # Comparison
            self.assertEqual(0.0, cv.norm(expected_mat, actual_mat, cv.NORM_INF))
            self.assertEqual(expected_thresh, actual_thresh[0])
        def test_invalid_op_input(self):
            # NB: Check GMat/GScalar
            with self.assertRaises(Exception): create_op([cv.GMat]   , [cv.GScalar]).on(cv.GScalar())
            with self.assertRaises(Exception): create_op([cv.GScalar], [cv.GScalar]).on(cv.GMat())

            # NB: Check GOpaque
            op = create_op([cv.GOpaque.Rect], [cv.GMat])
            with self.assertRaises(Exception): op.on(cv.GOpaque.Bool())
            with self.assertRaises(Exception): op.on(cv.GOpaque.Int())
            with self.assertRaises(Exception): op.on(cv.GOpaque.Double())
            with self.assertRaises(Exception): op.on(cv.GOpaque.Float())
            with self.assertRaises(Exception): op.on(cv.GOpaque.String())
            with self.assertRaises(Exception): op.on(cv.GOpaque.Point())
            with self.assertRaises(Exception): op.on(cv.GOpaque.Point2f())
            with self.assertRaises(Exception): op.on(cv.GOpaque.Size())

            # NB: Check GArray
            op = create_op([cv.GArray.Rect], [cv.GMat])
            with self.assertRaises(Exception): op.on(cv.GArray.Bool())
            with self.assertRaises(Exception): op.on(cv.GArray.Int())
            with self.assertRaises(Exception): op.on(cv.GArray.Double())
            with self.assertRaises(Exception): op.on(cv.GArray.Float())
            with self.assertRaises(Exception): op.on(cv.GArray.String())
            with self.assertRaises(Exception): op.on(cv.GArray.Point())
            with self.assertRaises(Exception): op.on(cv.GArray.Point2f())
            with self.assertRaises(Exception): op.on(cv.GArray.Size())

            # Check other possible invalid options
            with self.assertRaises(Exception): op.on(cv.GMat())
            with self.assertRaises(Exception): op.on(cv.GScalar())

            with self.assertRaises(Exception): op.on(1)
            with self.assertRaises(Exception): op.on('foo')
            with self.assertRaises(Exception): op.on(False)

            with self.assertRaises(Exception): create_op([cv.GMat, int], [cv.GMat]).on(cv.GMat(), 'foo')
            with self.assertRaises(Exception): create_op([cv.GMat, int], [cv.GMat]).on(cv.GMat())