Пример #1
0
 def test_value(self):
     c = ConnectedRegion(shape=(2, 2))
     assert_equal(crh.get_value(c), 0)
     crh.set_value(c, 5)
     assert_equal(crh.get_value(c), 5)
     crh.set_value(c, 0)
     assert_equal(crh.get_value(c), 0)
Пример #2
0
 def test_value(self):
     c = ConnectedRegion(shape=(2, 2))
     assert_equal(crh.get_value(c), 0)
     crh.set_value(c, 5)
     assert_equal(crh.get_value(c), 5)
     crh.set_value(c, 0)
     assert_equal(crh.get_value(c), 0)
Пример #3
0
    def __init__(self, **kwargs):
        BaseViewer.__init__(self, **kwargs)

        # Calculate maximum amplitude, area and volume
        areas = self.pulses.keys()
        amplitudes = []
        volumes = []
        for area in areas:
            self.pulses_used += len(self.pulses[area])
            for cr in self.pulses[area]:
                value = abs(crh.get_value(cr))
                amplitudes.append(value)
                volumes.append(area * value)

        max_amplitude = max(amplitudes)
        max_volume = max(volumes)
        max_area = max(areas)

        # Calculate lifetimes
        life_starts = np.zeros_like(self.image)
        for area in reversed(sorted(self.pulses.keys())):
            for cr in self.pulses[area]:
                crh.set_array(life_starts, cr, area)

        life_ends = np.zeros_like(self.image)
        for area in sorted(self.pulses.keys()):
            for cr in self.pulses[area]:
                crh.set_array(life_ends, cr, area)

        self.lifetimes = life_ends - life_starts

        self.add_trait('amplitude_threshold_min',
                       Range(value=1, low=1, high=max_amplitude)),
        self.add_trait('amplitude_threshold_max',
                       Range(value=max_amplitude, low=1, high=max_amplitude))

        max_area = max(self.pulses.keys())
        self.add_trait('area_threshold_min',
                       Range(value=1, low=1, high=max_area))
        self.add_trait('area_threshold_max',
                       Range(value=max_area, low=1, high=max_area))

        self.add_trait('volume_threshold_min',
                       Range(value=1, low=1, high=max_volume))
        self.add_trait('volume_threshold_max',
                       Range(value=max_volume, low=1, high=max_volume))

        self.add_trait('circularity_min', Range(value=0, low=0, high=1.0))
        self.add_trait('circularity_max', Range(value=1.0, low=0, high=1.0))

        self.add_trait(
            'lifetime_min',
            Range(value=int(self.lifetimes.min()),
                  low=int(self.lifetimes.min()),
                  high=int(self.lifetimes.max())))
        self.add_trait(
            'lifetime_max',
            Range(value=int(self.lifetimes.max()),
                  low=int(self.lifetimes.min()),
                  high=int(self.lifetimes.max())))

        self.add_trait('output_threshold', Range(value=0, low=0, high=255))

        self.result = self.image.copy()
Пример #4
0
    def reconstruct(self):
        self.result.fill(0)
        pulses = 0

        # Reconstruct only from pulses inside the thresholds
        for area in sorted(self.pulses.keys()):
            if area < self.area_threshold_min or \
               area > self.area_threshold_max:
                continue

            for cr in self.pulses[area]:
                value = crh.get_value(cr)
                aval = abs(value)
                if aval < self.amplitude_threshold_min or \
                   aval > self.amplitude_threshold_max:
                    continue

                volume = aval * area
                if volume < self.volume_threshold_min or \
                   volume > self.volume_threshold_max:
                    continue

                ## # See:
                ## #
                ## # Measuring rectangularity by Paul L. Rosin
                ## # Machine Vision and Applications, Vol 11, No 4, December 1999
                ## # http://www.springerlink.com/content/xb9klcax8ytnwth1/
                ## #
                ## # for more information on computing rectangularity.
                ## #
                ## r0, c0, r1, c1 = crh.bounding_box(cr)
                ## if c0 == c1 or r0 == r1:
                ##     rectangularity = 1
                ## else:
                ##     rectangularity = area / float((c1 - c0 + 1) * (r1 - r0 + 1))

                ## if rectangularity < self.rectangularity_min or \
                ##    rectangularity > self.rectangularity_max:
                ##     continue

                r0, c0, r1, c1 = crh.bounding_box(cr)
                if (c0 == c1) and (r0 == r1):
                    circularity = 1
                else:
                    max_dim = max(abs(r0 - r1), abs(c0 - c1))
                    circularity = crh.nnz(cr) / \
                                  (np.pi / 4 * (max_dim + 2)**2)
                    # We add 2 to max_dim to allow for pixel overlap
                    # with circumscribing circle
                if circularity < self.circularity_min or \
                   circularity > self.circularity_max:
                    continue

                if self.absolute_sum:
                    value = aval

                if self.amplitudes_one:
                    value = 1

                if self.replace:
                    crh.set_array(self.result, cr, value)
                else:
                    crh.set_array(self.result, cr, value, 'add')

                pulses += 1

        mask = (self.lifetimes > self.lifetime_max) | \
               (self.lifetimes < self.lifetime_min)
        self.result[mask] = 0

        if self.subtract:
            self.result = np.abs(self.image - self.result)

        if self.output_threshold != 0:
            self.result[self.result <= self.output_threshold] = 0

        self.pulses_used = pulses

        self.update_plot()
Пример #5
0
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)

    print("Iterative segmentation...")
    f = data.copy()

    def _minimise_cluster_distance(data, labels, N, beta):
        data_flat = np.ravel(data)
        cluster_means = np.array(
            [np.mean(data_flat[labels == k]) for k in range(N)]
Пример #6
0
import lulu.connected_region_handler as crh

img = load_image('chelsea_small.jpg')

print "Decomposing a %s image." % str(img.shape)
regions = lulu.decompose(img)

value_maxes = []
height = 0
for area in sorted(regions.keys()):
    pulses = regions[area]

    if len(pulses) == 0 or area < 280 or area > 300:
        continue

    values = [crh.get_value(p) for p in pulses]
    height_diff = max(values) - min(values)
    value_maxes.append(height_diff)
    centre = height + height_diff / 2.0

    pulse_values = np.zeros_like(img)
    for p in pulses:
        crh.set_array(pulse_values, p, crh.get_value(p))

    y, x = np.where(pulse_values)
    s = pulse_values[y, x]

    mlab.barchart(x, y, [height + centre] * len(s), s,
                  opacity=1.0, scale_factor=1.5)

    height += height_diff + 0.5
Пример #7
0
    def reconstruct(self):
        self.result.fill(0)
        pulses = 0

        # Reconstruct only from pulses inside the thresholds
        for area in sorted(self.pulses.keys()):
            if area < self.area_threshold_min or \
               area > self.area_threshold_max:
                continue

            for cr in self.pulses[area]:
                value = crh.get_value(cr)
                aval = abs(value)
                if aval < self.amplitude_threshold_min or \
                   aval > self.amplitude_threshold_max:
                    continue

                volume = aval * area
                if volume < self.volume_threshold_min or \
                   volume > self.volume_threshold_max:
                    continue

                ## # See:
                ## #
                ## # Measuring rectangularity by Paul L. Rosin
                ## # Machine Vision and Applications, Vol 11, No 4, December 1999
                ## # http://www.springerlink.com/content/xb9klcax8ytnwth1/
                ## #
                ## # for more information on computing rectangularity.
                ## #
                ## r0, c0, r1, c1 = crh.bounding_box(cr)
                ## if c0 == c1 or r0 == r1:
                ##     rectangularity = 1
                ## else:
                ##     rectangularity = area / float((c1 - c0 + 1) * (r1 - r0 + 1))

                ## if rectangularity < self.rectangularity_min or \
                ##    rectangularity > self.rectangularity_max:
                ##     continue

                r0, c0, r1, c1 = crh.bounding_box(cr)
                if (c0 == c1) and (r0 == r1):
                    circularity = 1
                else:
                    max_dim = max(abs(r0 - r1), abs(c0 - c1))
                    circularity = crh.nnz(cr) / \
                                  (np.pi / 4 * (max_dim + 2)**2)
                    # We add 2 to max_dim to allow for pixel overlap
                    # with circumscribing circle
                if circularity < self.circularity_min or \
                   circularity > self.circularity_max:
                    continue

                if self.absolute_sum:
                    value = aval

                if self.amplitudes_one:
                    value = 1

                if self.replace:
                    crh.set_array(self.result, cr, value)
                else:
                    crh.set_array(self.result, cr, value, 'add')

                pulses += 1

        mask = (self.lifetimes > self.lifetime_max) | \
               (self.lifetimes < self.lifetime_min)
        self.result[mask] = 0

        if self.subtract:
            self.result = np.abs(self.image - self.result)

        if self.output_threshold != 0:
            self.result[self.result <= self.output_threshold] = 0

        self.pulses_used = pulses

        self.update_plot()
Пример #8
0
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)

    print("Iterative segmentation...")
    f = data.copy()

    def _minimise_cluster_distance(data, labels, N, beta):
        data_flat = np.ravel(data)
Пример #9
0
    def __init__(self, **kwargs):
        BaseViewer.__init__(self, **kwargs)

        # Calculate maximum amplitude, area and volume
        areas = self.pulses.keys()
        amplitudes = []
        volumes = []
        for area in areas:
            self.pulses_used += len(self.pulses[area])
            for cr in self.pulses[area]:
                value = abs(crh.get_value(cr))
                amplitudes.append(value)
                volumes.append(area * value)

        max_amplitude = max(amplitudes)
        max_volume = max(volumes)
        max_area = max(areas)

        # Calculate lifetimes
        life_starts = np.zeros_like(self.image)
        for area in reversed(sorted(self.pulses.keys())):
            for cr in self.pulses[area]:
                crh.set_array(life_starts, cr, area)

        life_ends = np.zeros_like(self.image)
        for area in sorted(self.pulses.keys()):
            for cr in self.pulses[area]:
                crh.set_array(life_ends, cr, area)

        self.lifetimes = life_ends - life_starts

        self.add_trait('amplitude_threshold_min',
                       Range(value=1, low=1, high=max_amplitude)),
        self.add_trait('amplitude_threshold_max',
                       Range(value=max_amplitude, low=1, high=max_amplitude))

        max_area = max(self.pulses.keys())
        self.add_trait('area_threshold_min',
                       Range(value=1, low=1, high=max_area))
        self.add_trait('area_threshold_max',
                       Range(value=max_area, low=1, high=max_area))

        self.add_trait('volume_threshold_min',
                       Range(value=1, low=1, high=max_volume))
        self.add_trait('volume_threshold_max',
                       Range(value=max_volume, low=1, high=max_volume))

        self.add_trait('circularity_min',
                       Range(value=0, low=0, high=1.0))
        self.add_trait('circularity_max',
                       Range(value=1.0, low=0, high=1.0))

        self.add_trait('lifetime_min',
                       Range(value=int(self.lifetimes.min()),
                             low=int(self.lifetimes.min()),
                             high=int(self.lifetimes.max())))
        self.add_trait('lifetime_max',
                       Range(value=int(self.lifetimes.max()),
                             low=int(self.lifetimes.min()),
                             high=int(self.lifetimes.max())))

        self.add_trait('output_threshold',
                       Range(value=0,
                             low=0,
                             high=255))

        self.result = self.image.copy()
Пример #10
0
import lulu
import lulu.connected_region_handler as crh

img = load_image("truck_and_apcs_small.jpg")

pulses = lulu.decompose(img)

areas = sorted(pulses.keys())
cumulative_volume = []
volumes = []
reconstruction = np.zeros_like(img)
for area in areas:
    area_volume = 0
    for cr in pulses[area]:
        area_volume += crh.nnz(cr) * abs(crh.get_value(cr))
        crh.set_array(reconstruction, cr, abs(crh.get_value(cr)), "add")
    cumulative_volume.append(np.sum(reconstruction))
    volumes.append(area_volume)

total_volume = np.sum(reconstruction)
cumulative_volume = np.array(cumulative_volume)
cumulative_volume = 1 - cumulative_volume / float(total_volume)

plt.subplot(1, 3, 1)
plt.imshow(img, interpolation="nearest", cmap=plt.cm.gray)

plt.subplot(1, 3, 2)
plt.plot(areas[:-10], volumes[:-10], "x-")
# plt.xlim(plt.xlim()[::-1])
plt.title("Level Volumes")
Пример #11
0
import lulu.connected_region_handler as crh

img = load_image('chelsea_small.jpg')

print "Decomposing a %s image." % str(img.shape)
regions = lulu.decompose(img)

value_maxes = []
height = 0
for area in sorted(regions.keys()):
    pulses = regions[area]

    if len(pulses) == 0 or area < 280 or area > 300:
        continue

    values = [crh.get_value(p) for p in pulses]
    height_diff = max(values) - min(values)
    value_maxes.append(height_diff)
    centre = height + height_diff / 2.0

    pulse_values = np.zeros_like(img)
    for p in pulses:
        crh.set_array(pulse_values, p, crh.get_value(p))

    y, x = np.where(pulse_values)
    s = pulse_values[y, x]

    mlab.barchart(x,
                  y, [height + centre] * len(s),
                  s,
                  opacity=1.0,
Пример #12
0
import lulu
import lulu.connected_region_handler as crh

img = load_image('truck_and_apcs_small.jpg')

pulses = lulu.decompose(img)

areas = sorted(pulses.keys())
cumulative_volume = []
volumes = []
reconstruction = np.zeros_like(img)
for area in areas:
    area_volume = 0
    for cr in pulses[area]:
        area_volume += crh.nnz(cr) * abs(crh.get_value(cr))
        crh.set_array(reconstruction, cr, abs(crh.get_value(cr)), 'add')
    cumulative_volume.append(np.sum(reconstruction))
    volumes.append(area_volume)

total_volume = np.sum(reconstruction)
cumulative_volume = np.array(cumulative_volume)
cumulative_volume = 1 - cumulative_volume / float(total_volume)

plt.subplot(1, 3, 1)
plt.imshow(img, interpolation='nearest', cmap=plt.cm.gray)

plt.subplot(1, 3, 2)
plt.plot(areas[:-10], volumes[:-10], 'x-')
#plt.xlim(plt.xlim()[::-1])
plt.title('Level Volumes')
Пример #13
0
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), N)

    print "Iterative segmentation..."
    f = data.copy()

    def _minimise_cluster_distance(data, labels, N, beta):
        data_flat = np.ravel(data)
        cluster_means = np.array(
            [np.mean(data_flat[labels == k]) for k in range(N)]