def __init__(self, image, label, strength, parent=None): super(GrowCutWidget, self).__init__(parent) self.image = image self.strength = strength self.label = label self.coordinates = automata.formSamples( self.image.shape, neighbours=automata.CONNECT_4 ) # Itterating? self.paused = True self.editLabel = 1 self.resize(640, 480) self.layout = QtGui.QHBoxLayout(self) self.setMouseTracking(True) # Place a visualization of the image. self.imageScene = QtGui.QGraphicsScene(self) self.imageView = GrowCutImageView(self.imageScene) self.imageView.fitInView(QtCore.QRectF(0, 0, image.shape[1], image.shape[0]), QtCore.Qt.KeepAspectRatio) self.layout.addWidget(self.imageView) self.displayedImage = QtGui.QGraphicsPixmapItem() self.imageScene.addItem(self.displayedImage) self.imageView.centerOn(self.displayedImage) self.displayedImage.setPixmap( QtGui.QPixmap.fromImage(ndarrayToQtImage(self.image)) ) # Place a visualization of the label. self.labelScene = QtGui.QGraphicsScene(self) self.labelView = GrowCutImageView(self.labelScene) self.labelView.fitInView(QtCore.QRectF(0, 0, image.shape[1], image.shape[0]), QtCore.Qt.KeepAspectRatio) self.layout.addWidget(self.labelView) self.displayedLabel = QtGui.QGraphicsPixmapItem() self.labelScene.addItem(self.displayedLabel) self.labelView.centerOn(self.displayedLabel) self.displayedLabel.setPixmap( QtGui.QPixmap.fromImage(ndarrayToQtImage(self.label)) ) self.labelView.mouseDragEvent.connect(self.labelUpdate) self.imageView.mouseDragEvent.connect(self.labelUpdate) # self.labelView.wheelEvent.connect(self.wheelEvent) # Form a timer for growcut automations self.timer = QtCore.QTimer() self.timer.timeout.connect(self.timedUpdate) self.redPen = QtGui.QPen(QtGui.QColor(*RED)) self.greenPen = QtGui.QPen(QtGui.QColor(*GREEN))
def test_numpy_automate(): shape = (100, 100) cx, cy = shape[1] / 2.0, shape[0] / 2.0 r = 20 y, x = np.ogrid[0:shape[0], 0:shape[1]] mask = (np.power((y - cy), 2) + np.power((x - cx), 2)) < np.power(r, 2) field = np.zeros(mask.shape) field[mask] = 1.0 label = np.zeros_like(field) theta = np.zeros_like(field) label[:] = 0 label[0:(r / 4), 0:(r / 4)] = -1 theta[0:(r / 4), 0:(r / 4)] = 1.0 label[cy - (r / 4):cy + (r / 4), cx - (r / 4):cx + (r / 4)] = 1 theta[cy - (r / 4):cy + (r / 4), cx - (r / 4):cx + (r / 4)] = 1.0 # Plot the image and the label map. # import matplotlib.pyplot as plt # fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # ax1.imshow(field, interpolation='nearest', cmap='gray') # ax1.axis('off') # ax2.imshow(label, interpolation='nearest', cmap='jet') # ax2.axis('off') coordinates = automata.formSamples(field.shape, neighbours=automata.CONNECT_8) # Automate to update the labels for itteration in range(100): theta, label = growcut.numpyAutomate(coordinates, field, theta, label) # fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # ax1.imshow(field, interpolation='nearest', cmap='gray') # ax1.axis('off') # ax2.imshow(label, interpolation='nearest', cmap='jet') # ax2.axis('off') # plt.show() # Assert that the label map looks like the mask assert np.allclose(label[mask], 1.0) & np.allclose(label[~mask], -1), \ "Segmentation did not converge after {} iterations".format(itteration)
def test_formSamples(): """ Assert that the correct pattern of samples is generated using both the fast and slow sampling methods. """ grid, _ = np.mgrid[0:100, 0:100] gridIterator = automata.iterGrid(grid, automata.CONNECT_4) # Slow approach iteratorValues = [] for _, values in gridIterator: iteratorValues.append(values) iteratorValues = np.array(iteratorValues) # Fast approach coordinates = automata.formSamples(grid.shape, automata.CONNECT_4) sampledValues = grid[coordinates] # Assert equivalence of approaches assert np.allclose(sampledValues, iteratorValues)
def imageUpdate(self, fname): lum = plt.imread(fname) self.image = np.average(lum, 2) self.label = np.zeros_like(self.image, dtype=np.int) self.strength = np.zeros_like(self.image, dtype=np.float64) self.displayedImage.setPixmap( QtGui.QPixmap.fromImage(ndarrayToQtImage(self.image)) ) self.imageView.centerOn(self.displayedImage) self.displayedLabel.setPixmap( QtGui.QPixmap.fromImage(ndarrayToQtImage(self.label)) ) self.labelView.centerOn(self.displayedLabel) self.coordinates = automata.formSamples( self.image.shape, neighbours=automata.CONNECT_4 )
import numpy as np from growcut import automata from matplotlib import pyplot as plt from matplotlib import animation state = np.random.randint(0, 2, (250, 250)).astype(np.bool) coordinates = automata.formSamples(state.shape, neighbours=automata.CONNECT_8) fig = plt.figure(figsize=(10, 10)) img = plt.imshow(state, interpolation='nearest', origin='lower', cmap='binary') plt.axis('off') def init(): img.set_data(state) return img, def animate(i): state[:] = automata.numpyGameOfLife(state, coordinates)[:] img.set_data(state) return img, # call the animator. blit=True means only re-draw the parts that have changed. anim = animation.FuncAnimation( fig, animate, init_func=init,
# Form a label grid (0: no label, 1: foreground, 2: background) label = np.zeros_like(lum, dtype=np.int) label[:] = -1 label[75:90, 100:110] = 1 label[110:120, 150:160] = 1 label[50:55, 160:165] = 1 label[50:55, 180:185] = 0 label[0:10, 0:10] = 0 label[75:90, 0:10] = 0 label[0:10, 200:210] = 0 label[75:90, 200:210] = 0 # Form a strength grid. strength = np.zeros_like(lum, dtype=np.float64) strength[label != -1] = 1.0 t0 = time.time() coordinates = automata.formSamples(lum.shape, neighbours=automata.CONNECT_4) strength, label = growcut.numpyAutomate(coordinates, lum, strength, label) print "Numpy vectorized: " + str(1000 * (time.time() - t0)) + " ms" t0 = time.time() strength, label = automate_cy(lum, strength, label, connectivity=4) print "Cython: " + str(1000 * (time.time() - t0)) + " ms" t0 = time.time() strength, label = growcut.automate(lum, strength, label) print "Pure Python: " + str(1000 * (time.time() - t0)) + " ms"