def test_div(self): """ Test div. """ x = np.ones((5, 4)) * 2 y = np.ones((5, 4)) * 3 img_x = CImg(x) img_y = CImg(y) img_x.div(img_y) img_expected = CImg(x / y) self.assertEqual(img_x, img_expected)
def test_mul(self): """ Test mul. """ x = np.ones((5, 4)) * 2 y = np.ones((5, 4)) * 3 img_x = CImg(x) img_y = CImg(y) img_x.mul(img_y) img_expected = CImg(x * y) self.assertEqual(img_x, img_expected)
def test_save_tiff(self): """ Test save tiff. """ img = CImg((100, 100), dtype=cimg.uint8) img.rand(0, 255) filename = self._get_testfilename() + '.tiff' img.save_tiff(filename, compression_type=cimg.C_LZW, voxel_size=0, description="Test tiff") self.assertTrue(os.path.isfile(filename)) os.remove(filename)
def test_round(self): """ Test round. """ img = CImg(np.array([2.3, -1.7, 1.5, -0.1])) img.round() img_expected = CImg(np.array([2, -2, 2, 0])) self.assertEqual(img, img_expected)
def test_noise(self): """ Test noise. """ img = CImg(np.zeros((5, 5))) img.noise(2) img_not_expected = CImg(np.zeros((5, 5))) self.assertNotEqual(img, img_not_expected)
def test_tan(self): arr = np.linspace(-0.5, 0.5, 100) img = CImg(arr) self.assertTrue(np.allclose(np.tan(arr), img.tan().asarray()))
def test_sinc(self): arr = np.linspace(-10, 10, 100) img = CImg(arr) self.assertTrue(np.allclose(np.sinc(arr / np.pi), img.sinc().asarray()))
def test_equalize(self): """ Test equalize. """ img = CImg(np.array([1, 1, 2, 2])) img.equalize(2, 1, 2) img_expected = CImg(np.array([1.5, 1.5, 2, 2])) self.assertEqual(img, img_expected)
def test_from_numpy(self): """ Test construction from numpy array. """ arr = np.ones((100, 50)) im = CImg(arr) self.assertTrue(np.allclose(arr, im.asarray().squeeze()))
def test_load_jpeg(self): """ Test loading a JPEG file. """ img = CImg() img.load_jpeg(get_test_image('jpg')) self._check_image_dimensions(img)
def test_threshold(self): """ Test threshold. """ img = CImg(np.array([0, 1, 2, 3, 4])) img.threshold(2) img_expected = CImg(np.array([0, 0, 1, 1, 1])) self.assertEqual(img, img_expected)
def test_load(self): """ Test load. """ img = CImg() self.assertRaises(RuntimeError, img.load, 'notexistent.jpg') img.load(get_test_image()) self._check_image_dimensions(img)
def test_load_bmp(self): """ Test loading a BMP file. """ img = CImg() img.load_bmp(get_test_image('bmp')) self._check_image_dimensions(img)
def test_atan(self): arr = np.random.randn(10, 5) img = CImg(arr) self.assertTrue(np.allclose(np.arctan(arr), img.atan().asarray()))
def test_acos(self): arr = -np.ones((10, 5)) img = CImg(arr) self.assertTrue(np.allclose(np.arccos(arr), img.acos().asarray()))
def test_tanh(self): arr = np.random.randn(10, 5) img = CImg(arr) self.assertTrue(np.allclose(np.tanh(arr), img.tanh().asarray()))
def test_fill(self): """ Test fill. """ img = CImg((2, 2)) img.fill(42) img_expected = CImg(np.ones((2, 2)) * 42) self.assertEqual(img, img_expected)
def test_load_png(self): """ Test loading a PNG file. """ img = CImg() img.load_png(get_test_image('png')) self._check_image_dimensions(img)
def test_quantize(self): """ Test quantize. """ img = CImg(np.array([0, 1, 2, 3, 4])) img.quantize(2, keep_range=True) img_expected = CImg(np.array([0, 0, 2, 2, 2])) self.assertEqual(img, img_expected)
def test_load_tiff(self): """ Test loading a TIFF file. """ img = CImg() img.load_tiff(get_test_image('tiff')) self._check_image_dimensions(img)
def test_histogram(self): """ Test histogram. """ img = CImg(np.array([0, 1, 2, 3, 4])) img.histogram(2, 0, 4) img_expected = CImg(np.array([2, 3])) self.assertEqual(img, img_expected)
def test_noarg(self): """ Test construction of empty image. """ im = CImg() self.assertEqual(im.shape, (0, 0, 0, 0))
def test_from_file(self): """ Test construction from file. """ im = CImg(get_test_image()) self.assertEqual(im.shape, (3, 1, 797, 1200))
def test_dot(self): """ Test dot. """ img1 = CImg(np.array([[2, -5], [0, 3]])) img2 = CImg(np.array([[2, -3], [0, 3]])) self.assertEqual(img1.dot(img2), 28)
def test_size(self): """ Test construction with size tuple. """ im = CImg((100, 50)) self.assertEqual(im.shape, (1, 1, 50, 100)) im = CImg((100)) self.assertEqual(im.shape, (1, 1, 1, 100))
def test_sqr(self): arr = np.random.randn(10, 5) img = CImg(arr) self.assertTrue(np.allclose(arr * arr, img.sqr().asarray()))
def test_linear_atXY(self): ia = CImg((2, 2)) # Create simple 2x2 checkerboard arr = ia.asarray() arr[0, 0, 0, 1] = 1 arr[0, 0, 1, 1] = 1 ia.resize(4, 4, interpolation_type=cimg.LINEAR) ib = CImg((2, 2)) arr = ib.asarray() arr[0, 0, 0, 1] = 1 arr[0, 0, 1, 1] = 1 sc = (2 - 1) / (4 - 1) ic = CImg((4, 4)) arr = ic.asarray() for x in range(ic.width): for y in range(ic.height): arr[0, 0, y, x] = ib.linear_atXY(sc * x, sc * y, 0, 0) self.assertTrue(np.allclose(ia.asarray(), ic.asarray()))
def test_save_load(self): """ Test save/load half float. """ im = CImg() arr = np.random.randn(3, 2, 500, 300) im.fromarray(arr) self.assertTrue(np.allclose(arr, im.asarray())) filename = self._get_testfilename() + '.cimg' im.save(filename) im2 = CImg() im2.load(filename) self.assertTrue(np.allclose(im2.asarray(), im.asarray())) os.remove(filename) # save/load half float filename = self._get_testfilename() + '.cimg' im.save_cimg_float16(filename) im3 = CImg() im3.load_cimg_float16(filename) self.assertTrue(np.allclose(im2.asarray(), im.asarray())) os.remove(filename)
def test_magnitude(self): """ Test magnitude. """ img = CImg(np.array([[2, -5], [0, 3]])) self.assertEqual(img.magnitude(cimg.L1_NORM), 10)
def test_invert_endianness(self): """ Test invert endianness. """ img = CImg((2, 2), dtype=cimg.uint16) img.fill(0xAAFF) img.invert_endianness() print(img.asarray()) img_expected = CImg((2, 2), dtype=cimg.uint16) img_expected.fill(0xFFAA) self.assertEqual(img, img_expected)