def create_test_camera() -> Tuple[Path, UUID]:
    """ Helper function to create a test camera folder structure with images.
    """
    testing.needs_internet()

    # Get a temporary folder
    path, id = testing.get_tmp_folder()

    try:
        # Create folder structure
        for folder in FOLDERS:
            try:
                (path / folder).mkdir()
            except FileExistsError:
                pass

        img_file = testing.get_remote_file(TEST_IMG_FILENAME)
        img = imageio.imread(img_file)

        # Mosaic
        img = mosaic(img)

        # Save test image, rotated image and BW image
        imageio.imsave(path / FILES[0], img)
        imageio.imsave(path / FILES[1], np.flipud(img))
        imageio.imsave(path / FILES[2], (255 * img).astype(np.uint8))
        imageio.imsave(path / FILES[3], img)
        imageio.imsave(path / FILES[4], np.flipud(img))
        imageio.imsave(path / FILES[5], (255 * img).astype(np.uint8))

    except:
        delete_test_camera(path, id)

    return path, id
Пример #2
0
def test_from_mat():
    testing.needs_internet()

    # Get a temporary folder
    path, id = testing.get_tmp_folder()

    try:
        path = path / "test_hsi.mat"

        hsi_file = testing.get_remote_file(TEST_HSI_FILENAME)
        hsi = np.load(hsi_file)

        mdict = dict(data=hsi)
        scipy.io.savemat(path, mdict)

        # Try loading with and without key
        for key in [None, 'data']:
            tmp = SpectralImage.from_mat_file(path=path, key=key)

            assert np.allclose(hsi, tmp)

    finally:
        # Cleanup temporary folder
        testing.remove_tmp_folder(id)

    return
def test_rgb_camera():
    testing.needs_internet()

    try:
        # Create camera folder structure
        path, id = create_test_camera()

        test_camera = RgbCamera(path, bayer_pattern=PATTERN)

        img_file = testing.get_remote_file(TEST_IMG_FILENAME)
        img = imageio.imread(img_file)
        img = mosaic(img)
        img = demosaic.get_demosaiced(img,
                                      pattern=PATTERN,
                                      method='malvar2004')
        img = np.clip(img, 0, 255) / 255.0

        assert test_camera._bayerPattern == PATTERN

        test_camera.load_sensor_image(0)
        test_camera.decode_sensor_image(0)
        tmp = test_camera.get_decoded_image(0)

        # Test that demosaiced image is close to original
        assert img.shape == tmp.shape
        assert np.allclose(img, tmp, atol=0.05)

    finally:
        # Always clean up
        delete_test_camera(path, id)

    return
Пример #4
0
def test_get_rgb_image():
    """Test conversion of true RGB from multispectral image"""
    testing.needs_internet()

    hsi_file = testing.get_remote_file(TEST_HSI_FILENAME)
    hsi = SpectralArray(np.load(hsi_file), dtype=np.float32)

    rgb_file = testing.get_remote_file(TEST_RGB_FILENAME)
    rgb = (imageio.imread(rgb_file) / 255.0).astype(np.float64)

    grey_file = testing.get_remote_file(TEST_GREY_FILENAME)
    grey = (imageio.imread(grey_file) / 255.0).astype(np.float64)[..., np.newaxis]

    assert np.allclose(rgb, hsi.get_rgb(), atol=0.01)

    # Test 1D real spectrum conversion
    for x, y in zip([0, 10, 20], [10, 100, 200]):
        spectrum = hsi[x, y, :]
        rgb_point = rgb[x, y, :]
        assert np.allclose(rgb_point, spectrum.get_rgb(), atol=0.01)

    # Calculate black and white from rgb
    grey_from_hsi = hsi.get_grey(weighted=True)
    grey_from_rgb = SpectralArray(rgb, dtype=np.float32).get_grey(weighted=True)
    grey_from_grey = SpectralArray(grey, dtype=np.float32).get_grey(weighted=True)

    assert np.allclose(grey, grey_from_grey, atol=0.01)
    assert np.allclose(grey, grey_from_rgb, atol=0.01)
    assert np.allclose(grey, grey_from_hsi, atol=0.01)

    return
Пример #5
0
def test_get_edges():
    """This only tests for failure, not for correct edge detection..."""

    testing.needs_internet()
    im_file = testing.get_remote_file(TEST_GREY_FILENAME)
    im = imageio.imread(im_file)

    methods = ["scharr", "sobel", "dog", "log", "gradient"]

    for method in methods:
        edge = images.get_edges(im, method=method)

    # Test kwargs options for DOG, LOG
    edge = images.get_edges(im, method='log', sigma=4)
    edge = images.get_edges(im, method='dog', sigma=4)

    # Color input
    im_file = testing.get_remote_file(TEST_RGB_FILENAME)
    im = imageio.imread(im_file)

    with raises(ValueError) as cm:
        images.get_gradients(im, method='scharr')

    assert "Gradient calculation only works on 2D images right now. " \
           "For multi-dimensional arrays, use numpy's gradient() instead." == str(cm.value)

    return
def create_test_camera():
    """Download the Raytracer test camera, if not already present."""
    testing.needs_internet()

    for ext in [".png", ".json"]:
        testing.get_remote_file("cameras/RayTracerSmall/Calibration/cal_img" + ext)
        testing.get_remote_file("cameras/RayTracerSmall/Images/example_img" + ext)

    return
Пример #7
0
def create_test_lytro_camera_small():
    """Download the Lytro test camera, if not already present."""
    testing.needs_internet()

    for file in ["MOD_0001", "MOD_0016", "MOD_0033"]:
        testing.get_remote_file("cameras/LytroIllumSmall/Calibration/" + file + ".bsdf")

    testing.get_remote_file("cameras/LytroIllumSmall/Images/IIIT.bsdf")

    return
Пример #8
0
def create_test_lytro_camera():
    """Download the Lytro test camera, if not already present."""
    testing.needs_internet()

    for file in ["MOD_0033"]:
        testing.get_remote_file("cameras/LytroIllum/Calibration/" + file + ".RAW")
        testing.get_remote_file("cameras/LytroIllum/Calibration/" + file + ".TXT")

    testing.get_remote_file("cameras/LytroIllum/Images/IIIT.LFR")

    return
Пример #9
0
def test_from_file_collection():
    testing.needs_internet()

    for i in range(1, 32):
        fname = TEST_FILE_COLLECTION + f"balloons_ms_{i:02d}.png"
        testing.get_remote_file(fname)

    # Read from file collection
    path = testing.appdata_dir(APPNAME) / TEST_FILE_COLLECTION
    tmp = SpectralImage.from_file_collection(path=path)

    hsi_file = testing.get_remote_file(TEST_HSI_FILENAME)
    hsi = np.load(hsi_file)

    assert np.allclose(hsi, tmp)

    return
Пример #10
0
def test_from_file():
    """Test from_file() classmethod. Read RGB image."""

    testing.needs_internet()

    rgb_file = testing.get_remote_file(TEST_RGB_FILENAME)
    rgb = (imageio.imread(rgb_file) / 255.0).astype(np.float32)
    tmp = SpectralImage.from_file(rgb_file, format='PNG', dtype=np.float32)

    assert np.allclose(rgb, tmp)

    grey_file = testing.get_remote_file(TEST_GREY_FILENAME)
    grey = (imageio.imread(grey_file) / 255.0).astype(np.float32)

    assert grey.ndim == 2
    tmp = SpectralImage.from_file(grey_file, format='PNG', dtype=np.float32)

    assert tmp.ndim == 3

    return
Пример #11
0
def test_band_info_envi():
    """Test ``from_envi`` class method.
    """
    testing.needs_internet()

    envi_file = testing.get_remote_file(TEST_ENVI_HDR)
    d = SpectralArray.read_envi_header(envi_file)

    band_info = BandInfo.from_envi_header(d)
    centers_exp = np.asarray([400.6, 450.3, 500, 551.7, 600.666, 760.9])
    bandwiths_exp = np.asarray([1.1, 1.2, 1.3, 1.4, 1.5, 1.6])

    assert 6 == band_info.num_channels
    assert band_info.type is None
    assert band_info.centers_std is None
    assert band_info.bandwidths_std is None
    assert np.array_equal(centers_exp, band_info.centers)
    assert np.array_equal(bandwiths_exp, band_info.bandwidths)

    return
Пример #12
0
def create_test_camera() -> Tuple[Path, UUID]:
    """ Helper function to create a test camera folder structure with images.
    """
    testing.needs_internet()

    # Get a temporary folder
    path, id = testing.get_tmp_folder()

    try:
        # Create folder structure
        for folder in FOLDERS:
            try:
                (path / folder).mkdir()
            except FileExistsError:
                pass

        img_file = testing.get_remote_file(TEST_IMG_FILENAME)
        img = imread(img_file)

        # Save test image
        imsave(path / FILES[0], img)

        # Create greyscale whiteimages
        whiteimg_1 = (255 * np.ones(
            (img.shape[0], img.shape[1]))).astype(np.uint8)
        whiteimg_2 = (127 * np.ones(
            (img.shape[0], img.shape[1]))).astype(np.uint8)

        # Save whiteimages
        imsave(path / FILES[1], whiteimg_1)
        imsave(path / FILES[2], whiteimg_2)

    except:
        delete_test_camera(path, id)

    return path, id