def test_merge_diagonal(self): a = ConnectedRegion(shape=(2, 4), value=1, rowptr=[0, 2, 4], colptr=[0, 1, 1, 2]) b = ConnectedRegion(shape=(2, 4), value=1, rowptr=[0, 2, 4], colptr=[2, 3, 3, 4]) crh.merge(a, b) assert_array_equal(crh.todense(a), [[1, 0, 1, 0], [0, 1, 0, 1]]) assert_equal(crh.nnz(a), 4) a = ConnectedRegion(shape=(2, 2), value=1, rowptr=[0, 2, 4], colptr=[0, 1, 1, 2]) b = ConnectedRegion(shape=(2, 2), value=1, rowptr=[0, 2, 4], colptr=[1, 2, 0, 1]) crh.merge(a, b) assert_array_equal(crh.todense(a), [[1, 1], [1, 1]]) assert_equal(crh.nnz(a), 4)
def test_nnz(self): assert_equal(crh.nnz(self.c), 8)
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()
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")
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')