Esempio n. 1
0
    def test_basic(self):
        img = np.random.randint(255, size=(200, 200))

        pulses = lulu.decompose(img)
        img_, areas, area_count = lulu.reconstruct(pulses, img.shape)

        # Write assert this way so that we can see how many
        # pixels mismatch as a percent of the total nr of pixels
        assert_array_equal(img_, img)
        assert_equal(np.sum(img_ != img) / float(np.prod(img.shape)) * 100,
                     0, "Percentage mismatch =")
Esempio n. 2
0
    def test_basic(self):
        img = np.random.randint(255, size=(200, 200))

        pulses = lulu.decompose(img)
        img_, areas, area_count = lulu.reconstruct(pulses, img.shape)

        # Write assert this way so that we can see how many
        # pixels mismatch as a percent of the total nr of pixels
        assert_array_equal(img_, img)
        assert_equal(
            np.sum(img_ != img) / float(np.prod(img.shape)) * 100, 0,
            "Percentage mismatch =")
Esempio n. 3
0
def lulu_filter(noisy,sf):
    prev_smoothed = noisy
    pulses = lulu.decompose(img=noisy,quiet=False,operator='LU')
    smoothness_inc = 0.01
    pulse_len = len(pulses.keys())
    pulses
    i=2
    while 1-smoothness_inc > sf and i < pulse_len:
        lulu_smoothed, areas, area_count = lulu.reconstruct(dict((k, pulses[k]) for k in pulses.keys()[0:i]), noisy.shape)
        lulu_smoothed = noisy - lulu_smoothed
        smoothness_inc = np.var(lulu_smoothed)/np.var(prev_smoothed)
        prev_smoothed = lulu_smoothed
        i = i + 1
    return lulu_smoothed
Esempio n. 4
0
            bracket(t)
            for t in ('amplitude_threshold', 'area_threshold',
                      'volume_threshold', 'circularity', 'lifetime'))

        for tname in ('absolute_sum', 'amplitudes_one', 'replace', 'subtract',
                      'output_threshold', 'pulses_used'):
            out += ('\n%s: %s' % (pptrait(tname), getattr(self, tname)))

        return out


if __name__ == "__main__":
    import sys
    if len(sys.argv) >= 2 and '-UL' in sys.argv:
        operator = 'UL'
        sys.argv.remove('-UL')
    else:
        operator = 'LU'

    image = load_image()

    print("Decomposing using the %s operator." % operator)
    if operator == 'LU':
        print("Use the '-UL' flag to switch to UL.")

    print()
    pulses = lulu.decompose(image, operator=operator)

    viewer = Viewer(pulses=pulses, image=image)
    viewer.configure_traits()
Esempio n. 5
0
        self.plot_data.set_data('reconstruction', self.result[0])
        self.new.request_redraw()



if __name__ == "__main__":
    import sys
    if len(sys.argv) >= 2 and '-UL' in sys.argv:
        operator = 'UL'
        sys.argv.remove('-UL')
    else:
        operator = 'LU'

    image = (np.sin(np.linspace(-np.pi, np.pi, 100))*100).astype(int)

    mask = np.random.random((100,)) > 0.8
    noise = np.random.random((100,))*100 - 50

    image += (mask * noise).astype(image.dtype)
    image = image.reshape((1,-1))

    print("Decomposing using the %s operator." % operator)
    if operator == 'LU':
        print("Use the '-UL' flag to switch to UL.")

    print()
    pulses = lulu.decompose(image, operator=operator)

    viewer = Viewer1D(pulses=pulses, image=image)
    viewer.configure_traits()
Esempio n. 6
0
import sys
sys.path.insert(0, '..')

from demo import load_image

import time
import lulu

img = load_image()

print "Decomposing a %s matrix." % str(img.shape)

tic = time.time()
regions = lulu.decompose(img)
toc = time.time()

print "Execution time: %.2fs" % (toc - tic)
Esempio n. 7
0
import numpy as np
import matplotlib.pyplot as plt

import os
import time

import lulu
import lulu.connected_region_handler as crh

img = load_image()

print "Decomposing a %s matrix." % str(img.shape)

tic = time.time()
regions = lulu.decompose(img.copy())
toc = time.time()

print "Execution time: %.2fs" % (toc - tic)

print "-" * 78
print "Reconstructing image...",
out, areas, area_count = lulu.reconstruct(regions, img.shape)
print "done."
print "Reconstructed from %d pulses." % sum(area_count)
print "-" * 78

plt.subplot(2, 2, 1)
plt.imshow(img, interpolation='nearest', cmap=plt.cm.gray)
plt.title('Original')
plt.subplot(2, 2, 2)
Esempio n. 8
0
beta = 1.5 # Smoothing
min_area = 500
# -----------------------------------

from demo import load_image

import numpy as np
from scipy.cluster.vq import kmeans2
import matplotlib.pyplot as plt

import lulu
import lulu.connected_region_handler as crh

img = load_image('truck_and_apcs_small.jpg')

pulses = lulu.decompose(img)

impulse_strength = np.zeros(img.shape, dtype=int)
for area in pulses:
    if area > min_area:
        for cr in pulses[area]:
            crh.set_array(impulse_strength, cr,
                          np.abs(crh.get_value(cr)), 'add')

def ICM(data, N, beta):
    print("Performing ICM segmentation...")

    # Initialise segmentation using kmeans
    print("K-means initialisation...")
    clusters, labels = kmeans2(np.ravel(data).astype(float), N)
Esempio n. 9
0
beta = 1.5  # Smoothing
min_area = 500
# -----------------------------------

from demo import load_image

import numpy as np
from scipy.cluster.vq import kmeans2
import matplotlib.pyplot as plt

import lulu
import lulu.connected_region_handler as crh

img = load_image('truck_and_apcs_small.jpg')

pulses = lulu.decompose(img)

impulse_strength = np.zeros(img.shape, dtype=int)
for area in pulses:
    if area > min_area:
        for cr in pulses[area]:
            crh.set_array(impulse_strength, cr, np.abs(crh.get_value(cr)),
                          'add')


def ICM(data, N, beta):
    print("Performing ICM segmentation...")

    # Initialise segmentation using kmeans
    print("K-means initialisation...")
    clusters, labels = kmeans2(np.ravel(data).astype(float), N)
Esempio n. 10
0
import sys
sys.path.insert(0, '..')

import numpy as np
import matplotlib.pyplot as plt

import time
import lulu

sizes = []
times = []
for n in [16, 32, 64, 128, 256, 512, 1024]:
    print "DPT of size (%s, %s)" % (n, n)
    x = np.random.randint(255, size=(n, n))
    tic = time.time()
    lulu.decompose(x)
    toc = time.time()

    sizes.append(n * n)
    times.append(toc - tic)

plt.plot(sizes, times, '-x')
plt.title('Execution times for 2D DPT')
plt.xlabel('Size, i.e. NM for an NxM image')
plt.ylabel('Execution time')

plt.show()
Esempio n. 11
0
                crh.set_array(self.pulse_strength, cr, 1, 'add')

        self.result = self.pulse_strength.copy()

        tmin = int(self.pulse_strength.min())
        tmax = int(self.pulse_strength.max())

        self.add_trait('threshold_min',
                       Range(value=0, low=tmin, high=tmax, mode='slider'))
        self.add_trait('threshold_max',
                       Range(value=tmax, low=tmin, high=tmax, mode='slider'))

    @on_trait_change('threshold_min, threshold_max')
    def reconstruct(self):
        self.result = self.pulse_strength.copy()

        mask = (self.result < self.threshold_min) | \
               (self.result > self.threshold_max)

        self.result[mask] = 0

        self.plot_data.set_data('reconstruction', self.result)
        self.new.request_redraw()


image = load_image()
pulses = lulu.decompose(image)

viewer = StrengthViewer(pulses=pulses, image=image)
viewer.configure_traits()
Esempio n. 12
0
import sys
sys.path.insert(0, '..')

import numpy as np
import matplotlib.pyplot as plt

import time
import lulu


sizes = []
times = []
for n in [16, 32, 64, 128, 256, 512, 1024]:
    print("DPT of size (%s, %s)" % (n, n))
    x = np.random.randint(255, size=(n, n))
    tic = time.time()
    lulu.decompose(x)
    toc = time.time()

    sizes.append(n*n)
    times.append(toc - tic)

plt.plot(sizes, times, '-x')
plt.title('Execution times for 2D DPT')
plt.xlabel('Size, i.e. NM for an NxM image')
plt.ylabel('Execution time')

plt.show()
Esempio n. 13
0
import numpy as np
import matplotlib.pyplot as plt

import os
import time

import lulu
import lulu.connected_region_handler as crh

img = load_image()

print "Decomposing a %s matrix." % str(img.shape)

tic = time.time()
regions = lulu.decompose(img.copy())
toc = time.time()

print "Execution time: %.2fs" % (toc - tic)

print "-"*78
print "Reconstructing image...",
out, areas, area_count = lulu.reconstruct(regions, img.shape)
print "done."
print "Reconstructed from %d pulses." % sum(area_count)
print "-"*78

plt.subplot(2, 2, 1)
plt.imshow(img, interpolation='nearest', cmap=plt.cm.gray)
plt.title('Original')
plt.subplot(2, 2, 2)
Esempio n. 14
0
            for cr in self.pulses[area]:
                crh.set_array(self.pulse_strength, cr, 1, 'add')

        self.result = self.pulse_strength.copy()

        tmin = int(self.pulse_strength.min())
        tmax = int(self.pulse_strength.max())

        self.add_trait('threshold_min',
                       Range(value=0, low=tmin, high=tmax, mode='slider'))
        self.add_trait('threshold_max',
                       Range(value=tmax, low=tmin, high=tmax, mode='slider'))

    @on_trait_change('threshold_min, threshold_max')
    def reconstruct(self):
        self.result = self.pulse_strength.copy()

        mask = (self.result < self.threshold_min) | \
               (self.result > self.threshold_max)

        self.result[mask] = 0

        self.plot_data.set_data('reconstruction', self.result)
        self.new.request_redraw()

image = load_image()
pulses = lulu.decompose(image)

viewer = StrengthViewer(pulses=pulses, image=image)
viewer.configure_traits()