def test_load_image(datadirs): h, w = 213, 320 _, formatsdir, arraydir, _ = datadirs pathpattern = formatsdir + '*' for filepath in glob(pathpattern): img = ni.load_image(filepath) is_color = 'color' in filepath assert img.shape == (h, w, 3) if is_color else (h, w) assert isinstance(img, np.ndarray) assert img.dtype == np.uint8 assert np.max(img) <= 255 assert np.min(img) >= 0 for filepath in glob(pathpattern): img = ni.load_image(filepath, as_grey=True) assert img.shape == (h, w) assert np.max(img) <= 255 assert np.min(img) >= 0 for filepath in glob(pathpattern): img = ni.load_image(filepath) fdir, fname = op.split(filepath) arr = np.load(arraydir + fname + '.npy') nt.assert_allclose(img, arr)
def test_distort_elastic(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.distort_elastic(img_arr, 10, 5000) imagepath = processeddir + 'nut_color_elastic.bmp' assert_equal_image(imagepath, new_img) img_arr = ni.load_image(imagedir + 'nut_grayscale.bmp') new_img = ni.distort_elastic(img_arr, 10, 5000) imagepath = processeddir + 'nut_grayscale_elastic.bmp' assert_equal_image(imagepath, new_img)
def test_ImageWriter(): samples = [('nut_color', 1), ('nut_grayscale', 2)] inpath = 'tests/data/img_formats/*.bmp' img_samples = samples >> ReadImage(0, inpath) >> Collect() imagepath = 'tests/data/test_*.bmp' names = samples >> Get(0) >> Collect() img_samples >> WriteImage(0, imagepath, names) >> Consume() for sample, name in zip(img_samples, names): filepath = 'tests/data/test_{}.bmp'.format(name) arr = load_image(filepath) assert np.array_equal(arr, sample[0]) os.remove(filepath) pathfunc = lambda sample, name: 'tests/data/test_{}.jpg'.format(name) img_samples >> WriteImage(0, pathfunc) >> Consume() for i, sample in enumerate(img_samples): filepath = 'tests/data/test_{}.jpg'.format(i) os.path.exists(filepath) os.remove(filepath) with pytest.raises(ValueError) as ex: img_samples >> WriteImage(0, ()) >> Consume() assert str(ex.value).startswith('Expect path or function')
def test_save_image(datadirs): _, formatsdir, _, _ = datadirs formats = ['gif', 'png', 'jpg', 'bmp', 'tif', 'npy'] for format in formats: inpath = formatsdir + 'nut_color.' + format outpath = formatsdir + 'temp_nut_color.' + format image = ni.load_image(inpath) ni.save_image(outpath, image) loaded = ni.load_image(outpath) os.remove(outpath) # Saved and loaded JPG images can vary greatly (lossy format) when using # skimage and a direct comparision fails under Windows 7, Anaconda. # Therefore, only shape and mean value are verified for JPG images. if format == 'jpg': assert abs(np.mean(image) - np.mean(loaded)) < 0.1 assert loaded.shape == (213, 320, 3) else: nt.assert_allclose(image, loaded)
def test_rotate(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.rotate(img_arr, 45, order=1) imagepath = processeddir + 'nut_color_rotated.bmp' assert_equal_image(imagepath, new_img, rtol=0.1, atol=0.1) img_arr = np.eye(4, dtype=np.uint8) exp_img = np.array([[0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]]) new_img = ni.rotate(img_arr, 90) nt.assert_allclose(new_img, exp_img)
def DISABLED_test_plotlines(): filepath = 'tests/data/temp_plotlines.png' xs = np.arange(0, 6.3, 1.2) ysin, ycos = np.sin(xs), np.cos(xs) data = zip(xs, ysin, ycos) out = data >> pl.PlotLines(1, 0, filepath=filepath) >> Collect() assert out == data expected = 'tests/data/img/plotlines.png' image = ni.load_image(filepath) os.remove(filepath) assert_equal_image(expected, image)
def test_normalize_histo(datadirs): imagedir, _, arraydir, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.normalize_histo(img_arr) imagepath = processeddir + 'nut_color_normalize_histo.bmp' assert_equal_image(imagepath, new_img)
def assert_equal_image(imagepath, image, rtol=0.01, atol=0.01): if CREATE_DATA: ni.save_image(imagepath, image) expected = ni.load_image(imagepath) nt.assert_allclose(expected, image, rtol=rtol, atol=atol)
def test_shear(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.shear(img_arr, 0.5) imagepath = processeddir + 'nut_color_sheared.bmp' assert_equal_image(imagepath, new_img)
def test_translate(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.translate(img_arr, 50, 100) imagepath = processeddir + 'nut_color_translated.bmp' assert_equal_image(imagepath, new_img)
def test_rgb2gray(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.rgb2gray(img_arr) imagepath = processeddir + 'nut_color_2gray.bmp' assert_equal_image(imagepath, new_img)
def test_gray2rgb(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_grayscale.bmp') new_img = ni.gray2rgb(img_arr) imagepath = processeddir + 'nut_grayscale_2rgb.bmp' assert_equal_image(imagepath, new_img)
def test_change_color(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.change_color(img_arr, 2.0) imagepath = processeddir + 'nut_color_color.bmp' assert_equal_image(imagepath, new_img)
def test_sharpness(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.change_sharpness(img_arr, 0.5) imagepath = processeddir + 'nut_color_sharpness.bmp' assert_equal_image(imagepath, new_img, rtol=0, atol=10)
def test_brightness(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.change_brightness(img_arr, 0.5) imagepath = processeddir + 'nut_color_brightness.bmp' assert_equal_image(imagepath, new_img)
def test_rotate(datadirs): imagedir, _, _, processeddir = datadirs img_arr = ni.load_image(imagedir + 'nut_color.bmp') new_img = ni.rotate(img_arr, 45) imagepath = processeddir + 'nut_color_rotated.bmp' assert_equal_image(imagepath, new_img, rtol=0.1, atol=0.1)