def test_wikipedia(self): """Test with example values from Wikipedia https://en.wikipedia.org/wiki/Bilinear_interpolation#Application_in_image_processing""" data = np.array([[91, 210], [162, 95]]) self.assertEqual(bilinear_interpolation(0.5, 0, data), 150.5) self.assertEqual(bilinear_interpolation(0.5, 1, data), 128.5) self.assertAlmostEqual(bilinear_interpolation(0.5, 0.2, data), 146.1)
def test_opencv(self): """Test using opencv resize()""" np.random.seed(1) img_np = np.random.random([2, 2]) img_np = np.round(np.multiply(img_np, 100)) print(img_np) img_cv = cv2.resize(img_np, (3, 3), interpolation=cv2.INTER_LINEAR) print(img_cv) self.assertAlmostEqual(img_cv[0, 0], bilinear_interpolation(0, 0, img_np)) self.assertAlmostEqual(img_cv[0, 1], bilinear_interpolation(0.5, 0, img_np)) self.assertAlmostEqual(img_cv[0, 2], bilinear_interpolation(1, 0, img_np)) self.assertAlmostEqual(img_cv[1, 0], bilinear_interpolation(0, 0.5, img_np)) self.assertAlmostEqual(img_cv[1, 1], bilinear_interpolation(0.5, 0.5, img_np)) self.assertAlmostEqual(img_cv[1, 2], bilinear_interpolation(1, 0.5, img_np)) self.assertAlmostEqual(img_cv[2, 0], bilinear_interpolation(0, 1, img_np)) self.assertAlmostEqual(img_cv[2, 1], bilinear_interpolation(0.5, 1, img_np)) self.assertAlmostEqual(img_cv[2, 2], bilinear_interpolation(1, 1, img_np))
def test_interpolation_x_y_direction(self): """Test interpolation in x- and y-direction.""" self.assertEqual(bilinear_interpolation(0.5, 0.5, self.image), 2) self.assertEqual(bilinear_interpolation(1.5, 0.5, self.image), 3) self.assertEqual(bilinear_interpolation(0.5, 1.5, self.image), 5) self.assertEqual(bilinear_interpolation(1.5, 1.5, self.image), 6) self.assertAlmostEqual(bilinear_interpolation(0.1, 0.1, self.image), 0.4) self.assertEqual(bilinear_interpolation(1.9, 1.9, self.image), 7.6)
def test_interpolation_outside_image(self): """Test if error is raised when the position is outside of the image.""" with self.assertRaises(ValueError): bilinear_interpolation(2.1, 0, self.image) with self.assertRaises(ValueError): bilinear_interpolation(0, 2.1, self.image)
def test_interpolation_error_negativ(self): """Test if errors are properly raised.""" with self.assertRaises(ValueError): bilinear_interpolation(-1, 0, self.image) with self.assertRaises(ValueError): bilinear_interpolation(0, -1, self.image)
def test_interpolation_y_direction(self): """Test interpolation in y-direction.""" self.assertEqual(bilinear_interpolation(0, 0.5, self.image), 1.5) self.assertEqual(bilinear_interpolation(0, 1.5, self.image), 4.5) self.assertEqual(bilinear_interpolation(2, 0.6, self.image), 3.8) self.assertEqual(bilinear_interpolation(2, 1.5, self.image), 6.5)
def test_interpolation_x_direction(self): """Test interpolation in x-direction.""" self.assertEqual(bilinear_interpolation(0.5, 0, self.image), 0.5) self.assertEqual(bilinear_interpolation(1.4, 0, self.image), 1.4) self.assertEqual(bilinear_interpolation(0.5, 2, self.image), 6.5) self.assertEqual(bilinear_interpolation(1.4, 2, self.image), 7.4)
def test_no_interpolation(self): """Test that no scaling produces no effect.""" self.assertEqual(bilinear_interpolation(0, 0, self.image), 0) self.assertEqual(bilinear_interpolation(1, 1, self.image), 4) self.assertEqual(bilinear_interpolation(2, 2, self.image), 8)