Beispiel #1
0
def test_img_to_graph():
    x, y = np.mgrid[:4, :4] - 10
    grad_x = img_to_graph(x)
    grad_y = img_to_graph(y)
    assert_equal(grad_x.nnz, grad_y.nnz)
    # Negative elements are the diagonal: the elements of the original
    # image. Positive elements are the values of the gradient, they
    # should all be equal on grad_x and grad_y
    np.testing.assert_array_equal(grad_x.data[grad_x.data > 0],
                                  grad_y.data[grad_y.data > 0])
Beispiel #2
0
    def DBSCAN_cluster(d_array, epsilon=4.0, mini_samples=4):
        """
        Uses ready-made clustering algorithm that infers the number of clusters
        :return: 
        """
        adj_mat = img_to_graph(d_array)
        db = DBSCAN(eps=epsilon,
                    min_samples=mini_samples,
                    metric="precomputed").fit(adj_mat)
        labels = db.labels_
        print(labels)

        # Number of clusters in labels, ignoring noise if present.
        n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
        print(n_clusters_)
        n_noise_ = list(labels).count(-1)
        print(n_noise_)

        a = np.zeros(d_array.shape)

        for clust, tup in zip(labels, np.ndindex(a.shape)):
            a[tup] = clust

        GridEnsemble.plot_cluster(a)
        return a
Beispiel #3
0
def cluster(raster, pieces):
    raster.to_hsv()
    graph = image.img_to_graph(raster.get_tiered())

    beta = 15
    graph.data = exp(-beta * graph.data / raster.get_opaque().std())

    labels = []
    success = False

    while not success:
        try:
            labels = spectral_clustering(graph,
                                         n_clusters=pieces,
                                         assign_labels='discretize',
                                         random_state=1)
            success = True
        except (LinAlgError, ValueError):
            pieces -= 1

            # If clustering is non-convergent, return single cluster
            if pieces == 0:
                return np.zeros(np.product(raster.shape))

    return labels.reshape(raster.with_alpha().shape)[:, 0]
Beispiel #4
0
def image_features_labels(img,n_clusters,maxPixel):
     # X is the feature vector with one row of features per image
     #
     imageSize=maxPixel*maxPixel
     img = resize(img, (maxPixel, maxPixel))
     mask = img.astype(bool)
     # Convert the image into a graph with the value of the gradient on the
     # edges.
     graph = s_im.img_to_graph(img, mask=mask)

     # Take a decreasing function of the gradient: we take it weakly
     # dependent from the gradient the segmentation is close to a voronoi
     graph.data = np.exp(-graph.data / graph.data.std())

     # Force the solver to be arpack, since amg is numerically
     # unstable on this example
     labels = spectral_clustering(graph, n_clusters, eigen_solver='arpack')
     label_im = -np.ones(mask.shape)
     label_im[mask] = labels

     X=np.zeros(imageSize, dtype=float)

     # Store the rescaled image pixels
     X[0:imageSize] = np.reshape(label_im,(1, imageSize))
     return X
Beispiel #5
0
def image_features_labels(img,n_clusters,maxPixel):
     # X is the feature vector with one row of features per image
     #
     imageSize=maxPixel*maxPixel
     img = resize(img, (maxPixel, maxPixel))
     mask = img.astype(bool)
     # Convert the image into a graph with the value of the gradient on the
     # edges.
     graph = s_im.img_to_graph(img, mask=mask)

     # Take a decreasing function of the gradient: we take it weakly
     # dependent from the gradient the segmentation is close to a voronoi
     graph.data = np.exp(-graph.data / graph.data.std())

     # Force the solver to be arpack, since amg is numerically
     # unstable on this example
     labels = spectral_clustering(graph, n_clusters, eigen_solver='arpack')
     label_im = -np.ones(mask.shape)
     label_im[mask] = labels

     X=np.zeros(imageSize, dtype=float)

     # Store the rescaled image pixels
     X[0:imageSize] = np.reshape(label_im,(1, imageSize))
     return X
Beispiel #6
0
    def spectral_clustering(self, img):
        mask = img.astype(bool)
        img = img.astype(float)

        img += 1 + 0.2 * np.random.randn(*img.shape)
        print("start image to graph")

        graph = image.img_to_graph(img, mask=mask)
        print("finished image to graph")

        graph.data = np.exp(-graph.data / graph.data.std())
        print("start spectral_cluster")

        labels = spectral_clustering(graph,
                                     n_clusters=2,
                                     eigen_solver='arpack')
        print("finished spectral_cluster")

        label_im = np.full(mask.shape, -1.)
        label_im[mask] = labels

        cv2.imshow("img", img)
        cv2.imshow("label_im", label_im)
        cv2.waitKey(0)

        return
Beispiel #7
0
def test_connect_regions():
    face = sp.misc.face(gray=True)
    # subsample by 4 to reduce run time
    face = face[::4, ::4]
    for thr in (50, 150):
        mask = face > thr
        graph = img_to_graph(face, mask=mask)
        assert ndimage.label(mask)[1] == connected_components(graph)[0]
Beispiel #8
0
def get_label_v2(data, feature, index, clusters):
    all_data = data.sum().sum() + np.sum(feature, axis=-1)
    mask = all_data.astype(bool)
    from sklearn.feature_extraction import image
    graph = image.img_to_graph(all_data, mask=mask)
    graph.data = np.exp(-graph.data / graph.data.std())

    labels = cluster.spectral_clustering(graph, n_clusters=clusters, eigen_solver='arpack')
    return labels
def spectralClusteringTest01():
	import numpy as np
	import matplotlib.pyplot as plt

	from sklearn.feature_extraction import image
	from sklearn.cluster import spectral_clustering

	l = 100
	x,y = np.indices((l, l)) #x,y 都是二维矩阵, 表示了某点的x 和 y的坐标


	center1 = (28, 24)
	center2 = (40, 50)
	center3 = (67, 58)
	center4 = (24, 70)

	radius1, radius2, radius3, radius4 = 16, 14, 15, 14

	circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2
	circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2
	circle3 = (x - center3[0]) ** 2 + (y - center3[1]) ** 2 < radius3 ** 2
	circle4 = (x - center4[0]) ** 2 + (y - center4[1]) ** 2 < radius4 ** 2


	img = circle1 + circle2 + circle3 + circle4
	mask = img.astype(bool)
	img = img.astype(float)

	img += 1 + 0.2 * np.random.randn(*img.shape)

	#Convert the image into a graph with the value of the gradient on the edges

	#img就是一个100 * 100的图片
	#mask是一个bool型的100 * 100模板
	#graph是一个稀疏矩阵 -- 不过为什么是2678 * 2678 ?
	#估计这一步里面计算了梯度
	graph = image.img_to_graph(img, mask = mask)

	print graph.shape
	graph.data = np.exp(-graph.data / graph.data.std())

	#这里还是指定了聚类的中心数目
	#这里是只对mask内的点进行聚类
	labels = spectral_clustering(graph, n_clusters = 4, eigen_solver = "arpack")


	print labels

	label_im = -np.ones(mask.shape)
	label_im[mask] = labels

	plt.matshow(img)
	plt.matshow(label_im)

	plt.show()
Beispiel #10
0
def plot_coin_segmentation():
    # these were introduced in skimage-0.14
    if parse_version(skimage.__version__) >= parse_version('0.14'):
        rescale_params = {'anti_aliasing': False, 'multichannel': False}
    else:
        rescale_params = {}

    # load the coins as a numpy array
    orig_coins = coins()

    # Resize it to 20% of the original size to speed up the processing
    # Applying a Gaussian filter for smoothing prior to down-scaling
    # reduces aliasing artifacts.
    smoothened_coins = gaussian_filter(orig_coins, sigma=2)
    rescaled_coins = rescale(smoothened_coins,
                             0.2,
                             mode="reflect",
                             **rescale_params)

    # Convert the image into a graph with the value of the gradient on the
    # edges.
    graph = image.img_to_graph(rescaled_coins)

    # Take a decreasing function of the gradient: an exponential
    # The smaller beta is, the more independent the segmentation is of the
    # actual image. For beta=1, the segmentation is close to a voronoi
    beta = 10
    eps = 1e-6
    graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

    # Apply spectral clustering (this step goes much faster if you have pyamg
    # installed)
    N_REGIONS = 25
    """ Visualize the resulting regions """
    for assign_labels in ('kmeans', 'discretize'):
        t0 = time.time()
        labels = spectral_clustering(graph,
                                     n_clusters=N_REGIONS,
                                     assign_labels=assign_labels,
                                     random_state=42)
        t1 = time.time()
        labels = labels.reshape(rescaled_coins.shape)

        plt.figure(figsize=(5, 5))
        plt.imshow(rescaled_coins, cmap=plt.cm.gray)
        for l in range(N_REGIONS):
            plt.contour(labels == l,
                        colors=[plt.cm.nipy_spectral(l / float(N_REGIONS))])
        plt.xticks(())
        plt.yticks(())
        title = 'Spectral clustering: %s, %.2fs' % (assign_labels, (t1 - t0))
        print(title)
        plt.title(title)
    plt.show()
Beispiel #11
0
def test_connect_regions():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        # Newer versions of scipy have face in misc
        from scipy import misc
        face = misc.face(gray=True)
    for thr in (50, 150):
        mask = face > thr
        graph = img_to_graph(face, mask)
        assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])
def test_connect_regions():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        # Newer versions of scipy have face in misc
        from scipy import misc
        face = misc.face(gray=True)
    for thr in (50, 150):
        mask = face > thr
        graph = img_to_graph(face, mask)
        assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])
Beispiel #13
0
    def __init__(self, img=None, dic=None):
        # img = misc.imread(image_file, flatten=True)
        # print img.shape


        if dic != None:
            self.graph = copy.deepcopy(dic)
            self.volume = 0
            for i in self.graph.values():
                self.volume += np.sum(map(lambda a: a[1], i))
        else:
            # We use a mask that limits to the foreground: the problem that we are
            # interested in here is not separating the objects from the background,
            # but separating them one from the other.
            mask = img.astype(bool)

            img = img.astype(float)
            img += 1 + 0.2 * np.random.randn(*img.shape)

            # Convert the image into a graph with the value of the gradient on the
            # edges.
            self.graph = image.img_to_graph(img, mask=mask)

            # Take a decreasing function of the gradient: we take it weakly
            # dependent from the gradient the segmentation is close to a voronoi
            self.graph.data = np.exp(-self.graph.data / self.graph.data.std())

            self.orig = copy.deepcopy(self.graph)
            # plt.matshow(self.graph.toarray())
            # plt.show()

            # clustering = SpectralClustering().fit(img.reshape(img.shape[0] * img.shape[1], 1))
            # X = clustering.affinity_matrix_
            # print X, X.shape
            # # X[X < 0.00001] = 0
            # self.graph = coo_matrix(X)
            # # plt.matshow(X)
            # # plt.show()

            # remove self loops
            self.graph.setdiag(np.zeros(self.graph.shape[0]))

            self.graph = self.graph.todok()

            kast = dict()
            self.volume = 0
            for i in self.graph.items():
                if i[0][0] in kast:
                    kast[i[0][0]].append(i)
                else:
                    kast[i[0][0]] = [i]
                self.volume += i[1]
            self.graph = kast
Beispiel #14
0
def test_img_to_graph_sparse():
    # Check that the edges are in the right position
    #  when using a sparse image with a singleton component
    mask = np.zeros((2, 3), dtype=bool)
    mask[0, 0] = 1
    mask[:, 2] = 1
    x = np.zeros((2, 3))
    x[0, 0] = 1
    x[0, 2] = -1
    x[1, 2] = -2
    grad_x = img_to_graph(x, mask=mask).todense()
    desired = np.array([[1, 0, 0], [0, -1, 1], [0, 1, -2]])
    np.testing.assert_array_equal(grad_x, desired)
Beispiel #15
0
def sp_clustering(img):
    graph = image.img_to_graph(img)

    # Take a decreasing function of the gradient: we take it weakly
    # dependent from the gradient the segmentation is close to a voronoi
    graph.data = np.exp(-graph.data / graph.data.std())

    # Force the solver to be arpack, since amg is numerically
    # unstable on this example
    labels = spectral_clustering(graph, n_clusters=64, eigen_solver='arpack')

    plt.matshow(img)
    plt.matshow(labels)
Beispiel #16
0
def test_connect_regions():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        # Newer versions of scipy have face in misc
        from scipy import misc
        face = misc.face(gray=True)
    # subsample by 4 to reduce run time
    face = face[::4, ::4]
    for thr in (50, 150):
        mask = face > thr
        graph = img_to_graph(face, mask)
        assert ndimage.label(mask)[1] == connected_components(graph)[0]
Beispiel #17
0
    def LSTClustering(self):
        # 参考“Segmenting the picture of greek coins in regions”方法,Author: Gael Varoquaux <*****@*****.**>, Brian Cheung
        # License: BSD 3 clause
        orig_coins = self.LST
        # these were introduced in skimage-0.14
        if LooseVersion(skimage.__version__) >= '0.14':
            rescale_params = {'anti_aliasing': False, 'multichannel': False}
        else:
            rescale_params = {}
        smoothened_coins = gaussian_filter(orig_coins, sigma=2)
        rescaled_coins = rescale(smoothened_coins,
                                 0.2,
                                 mode="reflect",
                                 **rescale_params)
        # Convert the image into a graph with the value of the gradient on the
        # edges.
        graph = image.img_to_graph(rescaled_coins)
        # Take a decreasing function of the gradient: an exponential
        # The smaller beta is, the more independent the segmentation is of the
        # actual image. For beta=1, the segmentation is close to a voronoi
        beta = 10
        eps = 1e-6
        graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps
        # Apply spectral clustering (this step goes much faster if you have pyamg
        # installed)
        N_REGIONS = 200
        for assign_labels in ('discretize', ):
            #        for assign_labels in ('kmeans', 'discretize'):
            t0 = time.time()
            labels = spectral_clustering(graph,
                                         n_clusters=N_REGIONS,
                                         assign_labels=assign_labels,
                                         random_state=42)
            t1 = time.time()
            labels = labels.reshape(rescaled_coins.shape)

            plt.figure(figsize=(5 * 3, 5 * 3))
            plt.imshow(rescaled_coins, cmap=plt.cm.gray)
            for l in range(N_REGIONS):
                plt.contour(
                    labels == l,
                    colors=[plt.cm.nipy_spectral(l / float(N_REGIONS))])
            plt.xticks(())
            plt.yticks(())
            title = 'Spectral clustering: %s, %.2fs' % (assign_labels,
                                                        (t1 - t0))
            print(title)
            plt.title(title)
        plt.show()
Beispiel #18
0
    def _spectral_clustering(self,samples):
        if sp_version < (0, 12):
            raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
                   "thus does not include the scipy.misc.face() image.")

        # Convert the image into a graph with the value of the gradient on the
        # edges.
        graph = image.img_to_graph(samples)


        # Take a decreasing function of the gradient: an exponential
        # The smaller beta is, the more independent the segmentation is of the
        # actual image. For beta=1, the segmentation is close to a voronoi
        beta = 5
        eps = 1e-6
        graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

        # Apply spectral clustering (this step goes much faster if you have pyamg
        # installed)
        N_REGIONS = 4

        #############################################################################
        # Visualize the resulting regions

        for assign_labels in ('kmeans', 'discretize'):
            t0 = time.time()
            labels = spectral_clustering(graph, n_clusters=N_REGIONS,
                                         assign_labels=assign_labels, random_state=1)
            sample=pd.DataFrame(labels)
            sample.to_csv(os.path.join(OUTPUT_DIR, "spectral_result.csv"),sep=",")
            t1 = time.time()
            #classif=labels.fit(samples)
            #print classif
            print labels
            print sample
            
            labels = labels.reshape(samples.shape)

            plt.figure(figsize=(5, 5))
            plt.imshow(samples, cmap=plt.cm.gray)
            for l in range(N_REGIONS):
                plt.contour(labels == l, contours=1,
                            colors=[plt.cm.spectral(l / float(N_REGIONS))])
            plt.xticks(())
            plt.yticks(())
            title = 'Spectral clustering: %s, %.2fs' % (assign_labels, (t1 - t0))
            print(title)
            plt.title(title)
        plt.show() 
Beispiel #19
0
def readImg(path):
    """
    读取图片,并将其转换为邻接矩阵
    """
    # 对于彩色照片,只使用其中一个维度的色彩
    im = sp.misc.imread(path)[:, :, 2]
    im = im / 255.
    # 若运算速度太慢,可使用如下的语句来缩减图片的大小
    # im = sp.misc.imresize(im, 0.10) / 255.
    # 计算图片的梯度,既相邻像素点之差
    graph = image.img_to_graph(im)
    beta = 20
    # 计算邻接矩阵
    graph.data = np.exp(-beta * graph.data / graph.data.std())
    return im, graph
Beispiel #20
0
def Discretize_Clustering(twoDimg, N_REGIONS):
    """Put clustering code"""
    graph = imp.img_to_graph(twoDimg)
    beta = 1
    eps = 1e-1
    graph.data = np.exp(-beta * graph.data / twoDimg.std()) + eps

    t0 = time.time()
    labels = spectral_clustering(graph,
                                 n_clusters=N_REGIONS,
                                 assign_labels='discretize',
                                 random_state=1)
    t1 = time.time()
    labels = labels.reshape(twoDimg.shape)
    print('time taken', t1 - t0)

    return labels, N_REGIONS
def SpectralClusterImage(input_image, beta=5, eps=1e-6, n_regions=11, assign_labels='discretize',downsample_factor=np.NaN, order=3):
    """ Spectral Cluster an image
        Inputs:
            input_image: ndarray of image
            beta: Take a decreasing function of the gradient: an exponential
                The smaller beta is, the more independent the segmentation is of 
                the acutal image. For beta=1, the segmentation is close to a 
                voronoi. Default is 5.
            eps: error term. Default is 1E-6
            n_regions: number of regions to decompose into. Default is 11.
            assign_labels: ways of decomposition. Selecting from 'discretize' and 
                'kmeans'. Default is 'discretize'.
            downsample_factor: downsampling before spectral decomposition. Default
                is to keep the original sampling. Enter a single number to apply
                the kernel for both dimensions of the image, or enter as a sequence
                to apply different kernel for each dimension
            order: downsampling method, order of B-spline interpolation
    """
    # Downsample the image
    if not np.isnan(downsample_factor):
        zoom(input_image, zoom=downsample_factor, order=order)
    # Convert the image into a graph with the value of the gradient on the edges
    graph = image.img_to_graph(input_image)
    # Take a decreasing function of the gradient: an exponential
    # The smaller beta is, the more independent the segmentation is of the
    # acutal image. For beta=1, the segmentation is close to a voronoi
    graph.data = np.exp(-beta * graph.data / input_image.std()) + eps 
    # Apply spectral clustering  (this step goes much faster if yuo have pyamg 
    # installed) 
    labels = spectral_clustering(graph, n_clusters=n_regions,
                                 assign_labels='discretize')
    labels = labels.reshape(input_image.shape)
    # Visualizing the resulting regions
    pl.figure(figsize=(5,5))
    pl.imshow(input_image, cmap=pl.cm.gray)
    for lb in range(n_regions):
        pl.contour(labels == lb, contour=1,
                   color=[pl.cm.spectral(lb / float(n_regions)), ])
    # Get rid of x, y tick marks
    pl.xticks(())
    pl.yticks(())



                            
def spectral_segmentation(image_a, bet, N_REGIONS=20):

    # Convert the image into a graph with the value of the gradient on the edges
    graph = image.img_to_graph(image_a)

    # Take a decreasing function of the gradient: an exponential
    # The smaller beta is, the more independent the segmentation is of the
    # actual image. For beta=1, the segmentation is close to a voronoi
    beta = bet
    eps = 1e-6
    graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

    # Apply spectral clustering
    reg_range = np.arange(N_REGIONS)

    assign_labels = 'discretize'

    #labels = spectral_clustering(graph, n_clusters=N_REGIONS,
    #							 assign_labels=assign_labels, random_state=1)

    labels = spectral_clustering(graph,
                                 n_clusters=N_REGIONS,
                                 random_state=1,
                                 assign_labels=assign_labels)

    new_label_ordering = []

    for n in labels:

        if n not in new_label_ordering:

            new_label_ordering.append(n)

    for j in range(len(labels)):

        current_label = labels[j]
        new_label = reg_range[new_label_ordering.index(current_label)]

        labels[j] = new_label

    labels = labels.reshape(image_a.shape)

    return labels
def defficient_spectral_clustring(name_dict, z_depth, shape_dict):
    # requires to re-implement the distance definition on sparse images.

    z_stack = next(name_dict.itervalues())

    _3D_chan1 = np.zeros((shape_dict[1][0], shape_dict[1][1], z_depth))
    _3D_chan2 = np.zeros((shape_dict[2][0], shape_dict[2][1], z_depth))

    for depth, bi_image in z_stack.iteritems():
        img1 = bi_image[1]
        img2 = bi_image[2]

        _3D_chan1[:, :, depth-1] = img1
        _3D_chan2[:, :, depth-1] = img2

    # mlab.pipeline.volume(mlab.pipeline.scalar_field(_3D_chan1), vmin=0.1)
    # mlab.show()

    _3D_chan2[_3D_chan2<0.04] = 0

    mlab.pipeline.volume(mlab.pipeline.scalar_field(_3D_chan2))
    mlab.show()

    mask = _3D_chan2.astype(bool)
    img = _3D_chan2.astype(float)

    graph = image.img_to_graph(img, mask=mask)
    graph.data = np.exp(-graph.data / graph.data.std())

    print graph.shape
    print len(graph.nonzero()[0])

    clusters = 4
    labels = spectral_clustering(graph, n_clusters = clusters, eigen_solver='arpack')
    label_im = -np.ones(mask.shape)
    label_im[mask] = labels

    for i in range(0, clusters):
        re_img = copy(_3D_chan2)
        re_img[label_im!=i] = 0
        mlab.pipeline.volume(mlab.pipeline.scalar_field(re_img))
        mlab.show()
Beispiel #24
0
def spectral_clusters_scikit(image, clusters=3):
    """Performs spectral clustering for input image. Not very suitable for cartilage segmentation.

    Parameters
    ----------
    image : ndarray
        Input image to be clustered
    clusters : int
        Number of clusters.
    Returns
    -------
    Clustered image.
    """
    image = zoom(image, (0.125, 0.125))
    plt.imshow(image)
    plt.show()

    # Remove background
    mask = image.astype(bool)
    # Add random noise
    img = image.astype(float)
    img += np.random.randn(*img.shape)

    # Convert image to graph (gradient on the edges)
    graph = im.img_to_graph(img, mask=mask)

    # Decreasing function of the gradient
    graph.data = np.exp(-graph.data / graph.data.std())

    # Solve using arpack
    labels = spectral_clustering(graph,
                                 n_clusters=clusters,
                                 eigen_solver='arpack')
    label_im = np.full(mask.shape, -1.)
    label_im[mask] = labels

    plt.imshow(img)
    plt.imshow(label_im)
    plt.show()
Beispiel #25
0
def cluster(face):
    # Convert the image into a graph with the value of the gradient on the
    # edges.
    graph = image.img_to_graph(face)

    # Take a decreasing function of the gradient: an exponential
    # The smaller beta is, the more independent the segmentation is of the
    # actual image. For beta=1, the segmentation is close to a voronoi
    beta = 5
    eps = 1e-6
    graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

    # Apply spectral clustering (this step goes much faster if you have pyamg
    # installed)
    N_REGIONS = 25

    for assign_labels in ('kmeans', 'discretize'):
        t0 = time.time()
        labels = spectral_clustering(graph,
                                     n_clusters=N_REGIONS,
                                     assign_labels=assign_labels,
                                     random_state=1)
        t1 = time.time()
        labels = labels.reshape(face.shape)

        plt.figure(figsize=(5, 5))
        plt.imshow(face)
        for l in range(N_REGIONS):
            plt.contour(
                labels == l,
                contours=1,
            )
        plt.xticks(())
        plt.yticks(())
        title = 'Spectral clustering: %s, %.2fs' % (assign_labels, (t1 - t0))
        print(title)
        plt.title(title)
    plt.show()
    def spectral_clustering_func(self, img, K):

        mask = img.astype(bool)

        img_f = img.astype(float)
        img_f += 1 + 0.2 * np.random.randn(*img.shape)

        graph = image.img_to_graph(img_f, mask=mask)

        labels = spectral_clustering(graph,
                                     n_clusters=K,
                                     eigen_solver='arpack')

        label_im = -np.ones(mask.shape)
        label_im[mask] = labels

        Width = 281
        Height = 281
        for i in range(0, Height):
            for j in range(0, Width):
                label_im[i][j] += 1

        return label_im.astype(np.uint16)
    def run(self):

        from sklearn.feature_extraction import image
        from sklearn.cluster import spectral_clustering

        # 生成原始图片信息
        l = 100
        x, y = np.indices((l, l))

        center1 = (28, 24)
        center2 = (40, 50)
        center3 = (77, 58)

        radius1, radius2, radius3 = 16, 14, 15

        circle1 = (x - center1[0])**2 + (y - center1[1])**2 < radius1**2
        circle2 = (x - center2[0])**2 + (y - center2[1])**2 < radius2**2
        circle3 = (x - center3[0])**2 + (y - center3[1])**2 < radius3**2

        # 生成包括3个圆的图片
        img = circle1 + circle2 + circle3
        mask = img.astype(bool)
        img = img.astype(float)

        img += 1 + 0.2 * np.random.randn(*img.shape)
        graph = image.img_to_graph(img)
        graph.data = np.exp(-graph.data / graph.data.std())

        # 聚类输出
        labels = spectral_clustering(graph, n_clusters=4)
        label_im = -np.ones(mask.shape)
        #label_im[mask] = labels
        label_im = np.reshape(labels, (100, 100))
        import matplotlib.pyplot as plt
        plt.matshow(img)
        plt.matshow(label_im)
        plt.show()
Beispiel #28
0
    def fit(self, image):
        """
        Compute parameters of the model.

        Args:
            image: a ndarray representing an image (x, y, color_dimension)

        Returns:
            Nothing
        """
        f_dim = image.shape[-1] if len(image.shape) > 2 else 1
        X = image.reshape(-1, f_dim)

        if self.algorithm == 'aglo':
            connectivity = img_to_graph(image)
            self.model = AgglomerativeClustering(n_clusters=self.n_clusters,
                                                 affinity='euclidean',
                                                 connectivity=connectivity,
                                                 compute_full_tree=False,
                                                 linkage='average')
        elif self.algorithm == 'spectral':
            return None

        self.model.fit(X)
Beispiel #29
0
for i in range (0, input_image.shape[0]):
    for j in range (0, input_image.shape[1]):
        input_image_gray[i, j] = 0
        '''
        input_image_gray[i, j] += input_image[i, j, 2] * 0.299
        input_image_gray[i, j] += input_image[i, j, 1] * 0.587
        input_image_gray[i, j] += input_image[i, j, 0] * 0.114
        '''
        
        input_image_gray[i, j] += input_image[i, j, 0] * 0.2125
        input_image_gray[i, j] += input_image[i, j, 1] * 0.7154
        input_image_gray[i, j] += input_image[i, j, 2] * 0.0721

print("GRAYSCALED")

graph = image.img_to_graph(input_image_gray)

print("GRAPHED")
print(graph.shape)
print(type(graph.data))

# Take a decreasing function of the gradient: an exponential
# The smaller beta is, the more independent the segmentation is of the
# actual image. For beta=1, the segmentation is close to a voronoi
beta = 5 
eps = 1e-6
graph.data = np.exp(-beta * graph.data / (graph.data.std() + eps)) + eps

print("EXPONENTIALED")

# Apply spectral clustering (this step goes much faster if you have pyamg
if __name__ == "__main__":
    matplotlib.rcParams['font.sans-serif'] = ['SimHei']
    matplotlib.rcParams['axes.unicode_minus'] = False

    pic = Image.open('..\\Chrome.png')
    pic = pic.convert('L')
    data = np.array(pic).astype(np.float) / 255

    plt.figure(figsize=(10, 5), facecolor='w')
    plt.subplot(121)
    plt.imshow(pic, cmap=plt.cm.gray, interpolation='nearest')
    plt.title('原始图片', fontsize=18)
    n_clusters = 15

    affinity = image.img_to_graph(data)
    beta = 3
    affinity.data = np.exp(-beta * affinity.data / affinity.data.std()) + 10e-5
    # a = affinity.toarray()
    # b = np.diag(a.diagonal())
    # a -= b
    print('开始谱聚类...')
    y = spectral_clustering(affinity, n_clusters=n_clusters, assign_labels='kmeans', random_state=1)
    print('谱聚类完成...')
    y = y.reshape(data.shape)
    for n in range(n_clusters):
        data[y == n] = n
    plt.subplot(122)
    clrs = []
    for c in np.linspace(16776960, 16711935, n_clusters, dtype=int):
        clrs.append('#%06x' % c)
# Downsample the image by a factor of 4
# lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
# lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
print lena.shape
lena = lena.astype(float)

# lena = lena[:5, :5]
print lena.shape

# Convert the image into a graph with the value of the gradient on the
# edges.
import random

if True:
    graph = image.img_to_graph(lena, return_as=np.ndarray)
    print 'ttt'
    graph1 = image.img_to_graph(lena)
    graph2 = graph
    print type(graph1.data)
    print type(graph2.data)
    # graph2.data = np.exp(-beta * graph2.data / lena.std()) + eps

    print graph
    print graph.shape

    row = []
    col = []
    data = []

    for i in xrange(graph.shape[0]):
# load the raccoon face as a numpy array
try:  # SciPy >= 0.16 have face in misc
    from scipy.misc import face
    orig_face = img_as_float(face(gray=True))
except ImportError:
    orig_face = img_as_float(sp.face(gray=True))

# Resize it to 10% of the original size to speed up the processing
# Applying a Gaussian filter for smoothing prior to down-scaling
# reduces aliasing artifacts.
smoothened_face = gaussian_filter(orig_face, sigma=4.5)
rescaled_face = rescale(smoothened_face, 0.1, mode="reflect")

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(rescaled_face)

# Take a decreasing function of the gradient: an exponential
# The smaller beta is, the more independent the segmentation is of the
# actual image. For beta=1, the segmentation is close to a voronoi
beta = 5
eps = 1e-6
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

# Apply spectral clustering (this step goes much faster if you have pyamg
# installed)
N_REGIONS = 25

#############################################################################
# Visualize the resulting regions
prefix = 'Data/Unlabeled/lesion_c4_raw_seg_combine/'

for i in range(len(us_frames)):

    try:

        img = Image.open(us_frames[i]).convert('L')
        img_arr = np.asarray(img)

        # Resize it to 10% of the original size to speed up the processing
        us = sp.misc.imresize(img_arr, 0.10) / 255.

        # Convert the image into a graph with the value of the gradient on the
        # edges.
        graph = image.img_to_graph(us)

        # Take a decreasing function of the gradient: an exponential
        # The smaller beta is, the more independent the segmentation is of the
        # actual image. For beta=1, the segmentation is close to a voronoi
        beta = 5
        eps = 1e-6
        graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

        # Apply spectral clustering (this step goes much faster if you have pyamg
        # installed)
        N_REGIONS = 8
        reg_range = np.arange(N_REGIONS)

        #for assign_labels in ('discretize'):
        #'kmeans'
from sklearn.feature_extraction import image
from sklearn.cluster import spectral_clustering


# load the coins as a numpy array
orig_coins = coins()

# Resize it to 20% of the original size to speed up the processing
# Applying a Gaussian filter for smoothing prior to down-scaling
# reduces aliasing artifacts.
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect")

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(rescaled_coins)

# Take a decreasing function of the gradient: an exponential
# The smaller beta is, the more independent the segmentation is of the
# actual image. For beta=1, the segmentation is close to a voronoi
beta = 10
eps = 1e-6
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

# Apply spectral clustering (this step goes much faster if you have pyamg
# installed)
N_REGIONS = 25

#############################################################################
# Visualize the resulting regions
Beispiel #35
0
radius1, radius2, radius3 = 16, 14, 15

circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2
circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2
circle3 = (x - center3[0]) ** 2 + (y - center3[1]) ** 2 < radius3 ** 2

# 生成包括3个圆的图片
img = circle1 + circle2 + circle3
print ("img_processing.size=",img.size)
mask = img.astype(bool)
img = img.astype(float)
print ("img_processing.size=",img.size)
img += 1 + 0.2 * np.random.randn(*img.shape)
print ("img_processing.size=",img.size)

graph = image.img_to_graph(img, mask=mask)
graph.data = np.exp(-graph.data / graph.data.std())
#########################################################
##########################################################
#imgrobot operation

print ("robotsize=",img_ndarray.size)
mask_robot = img_ndarray.astype(bool)
img_robot = img_ndarray.astype(float)
print ("robotsize=",img_ndarray.size)

graph_robot = image.img_to_graph(img_ndarray, mask=mask_robot)
#graph_robot.data = np.exp(-img_ndarray.data / img_ndarray.data.std())


Beispiel #36
0
# #############################################################################
# 4 circles
img = circle1 + circle2 + circle3 + circle4

# We use a mask that limits to the foreground: the problem that we are
# interested in here is not separating the objects from the background,
# but separating them one from the other.
mask = img.astype(bool)

img = img.astype(float)
img += 1 + 0.2 * np.random.randn(*img.shape)

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image_.img_to_graph(img, mask=mask)

# Take a decreasing function of the gradient: we take it weakly
# dependent from the gradient the segmentation is close to a voronoi
graph.data = np.exp(-graph.data / graph.data.std())

# Force the solver to be arpack, since amg is numerically
# unstable on this example
labels = spectral_clustering(graph, n_clusters=4, eigen_solver='arpack')
label_im = -np.ones(mask.shape)
label_im[mask] = labels

plt.matshow(img)
plt.matshow(label_im)

# #############################################################################
import numpy as np
import scipy as sp
import pylab as pl

from sklearn.feature_extraction import image
from sklearn.cluster import spectral_clustering

lena = sp.misc.lena()
# Downsample the image by a factor of 4
lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(lena)

# Take a decreasing function of the gradient: an exponential
# The smaller beta is, the more independent the segmentation is of the
# actual image. For beta=1, the segmentation is close to a voronoi
beta = 5
eps = 1e-6
graph.data = np.exp(-beta * graph.data / lena.std()) + eps

# Apply spectral clustering (this step goes much faster if you have pyamg
# installed)
N_REGIONS = 11

###############################################################################
# Visualize the resulting regions
# load the coins as a numpy array
orig_coins = coins()

# Resize it to 20% of the original size to speed up the processing
# Applying a Gaussian filter for smoothing prior to down-scaling
# reduces aliasing artifacts.
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
rescaled_coins = rescale(smoothened_coins,
                         0.2,
                         mode="reflect",
                         **rescale_params)

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(rescaled_coins)

# Take a decreasing function of the gradient: an exponential
# The smaller beta is, the more independent the segmentation is of the
# actual image. For beta=1, the segmentation is close to a voronoi
beta = 10
eps = 1e-6
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

# Apply spectral clustering (this step goes much faster if you have pyamg
# installed)
N_REGIONS = 25

#############################################################################
# Visualize the resulting regions
from PIL import Image

matplotlib.rcParams['font.sans-serif'] = [u'SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False

pic = Image.open('../Total_Data/data/Chrome.png')
pic = pic.convert('L')
data = np.array(pic).astype(np.float) / 255

plt.figure(figsize=(10, 5), facecolor='w')
plt.subplot(121)
plt.imshow(pic, cmap=plt.cm.gray, interpolation='nearest')
plt.title(u'原始图片', fontsize=18)
n_clusters = 15

affinity = image.img_to_graph(data)
beta = 3
affinity.data = np.exp(-beta * affinity.data / affinity.data.std()) + 10e-5
# a = affinity.toarray()
# b = np.diag(a.diagonal())
# a -= b
print('开始谱聚类...')
y = spectral_clustering(affinity,
                        n_clusters=n_clusters,
                        assign_labels='kmeans',
                        random_state=1)
print('谱聚类完成...')
y = y.reshape(data.shape)
for n in range(n_clusters):
    data[y == n] = n
plt.subplot(122)
Beispiel #40
0
#!/usr/bin/env python
__author__ = 'ienek'

from skimage import io, filter, feature
from skimage.color import rgb2gray

from sklearn.feature_extraction import image
# test to load any image
test = io.imread('n02854926_28_0.jpg')

# loading to array
img_arr = image.img_to_graph(test)

patch_g = image.extract_patches(test, (4, 4, 2))

patch_g2 = image.extract_patches_2d(test, (4, 4))

print("Patch 1: %s " % str(patch_g.shape))
print("Patch 2: %s " % str(patch_g2.shape))

# grayscaling
gray_test = rgb2gray(test)

# building hog
hog_ = feature.hog(gray_test)

# building surf
# ORB: An efficient alternative to SIFT and SURF
surf_ = feature.ORB()
surf_.detect_and_extract(gray_test)
def test_connect_regions():
    lena = sp.misc.lena()
    for thr in (50, 150):
        mask = lena > thr
        graph = img_to_graph(lena, mask)
        assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])
from sklearn.feature_extraction import image
from sklearn.cluster import spectral_clustering

# load the raccoon face as a numpy array
try:  # SciPy >= 0.16 have face in misc
    from scipy.misc import face
    face = face(gray=True)
except ImportError:
    face = sp.face(gray=True)

# Resize it to 10% of the original size to speed up the processing
face = sp.misc.imresize(face, 0.10) / 255.

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(face)

# Take a decreasing function of the gradient: an exponential
# The smaller beta is, the more independent the segmentation is of the
# actual image. For beta=1, the segmentation is close to a voronoi
beta = 5
eps = 1e-6
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

# Apply spectral clustering (this step goes much faster if you have pyamg
# installed)
N_REGIONS = 25

for assign_labels in ('kmeans', 'discretize'):
    t0 = time.time()
    labels = spectral_clustering(graph,
#!/usr/bin/env python
#!encoding=utf8

from SimpleCV import Image
from sklearn.feature_extraction.image import img_to_graph

img = Image('./digits.bmp')
img_np = img.getNumpy()
graph = img_to_graph(img_np)
print graph

# load the raccoon face as a numpy array
try:
    face = sp.face(gray=True)
except AttributeError:
    # Newer versions of scipy have face in misc
    from scipy import misc
    face = misc.face(gray=True)

# Resize it to 10% of the original size to speed up the processing
face = sp.misc.imresize(face, 0.10) / 255.

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(face)

# Take a decreasing function of the gradient: an exponential
# The smaller beta is, the more independent the segmentation is of the
# actual image. For beta=1, the segmentation is close to a voronoi
beta = 5
eps = 1e-6
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

# Apply spectral clustering (this step goes much faster if you have pyamg
# installed)
N_REGIONS = 25

#############################################################################
# Visualize the resulting regions
def build_image(gray):

    graph = image.img_to_graph(gray)
    print type(graph)
    graph.data = np.exp(-graph.data / graph.data.std())
    return graph
def compression (lena):
    lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
    return  lena


# gray_img=compression(gray_img)
# gray_img=compression(gray_img)
# gray_img=compression(gray_img)

print("have compression gray_img.shape",gray_img.shape,type(gray_img))

# Convert the image into a graph with the value of the gradient on the
# edges.
# graph = image.img_to_graph(lena)
graph2 = image.img_to_graph(gray_img)
# graph3 = image.img_to_graph(rgb)



# Take a decreasing function of the gradient: an exponential
# The smaller beta is, the more independent the segmentation is of the
# actual image. For beta=1, the segmentation is close to a voronoi
beta = 5
eps = 1e-6
# graph.data = np.exp(-beta * graph.data / lena.std()) + eps
graph2.data = np.exp(-beta * graph2.data / gray_img.std()) + eps
# graph3.data = np.exp(-beta * graph3.data / rgb.std()) + eps

# Apply spectral clustering (this step goes much faster if you have pyamg
# installed)
Beispiel #47
0
    s = img.shape[0] * img.shape[1]
    img_wide = img.reshape(1, s, 4)

    return img_wide[0]

whale = sp.misc.imread('test_whale_square.png', flatten=True)

# plt.imshow(whale)
# plt.show()
print whale.shape

# downsize by a factor of 4
whale = whale[::2, ::2] + whale[1::2, ::2] + whale[::2, 1::2] + whale[1::2, 1::2]
whale = whale[::2, ::2] + whale[1::2, ::2] + whale[::2, 1::2] + whale[1::2, 1::2]

graph = image.img_to_graph(whale)
beta = 5
eps = 1e-6
graph.data = np.exp(-beta * graph.data / whale.std()) # + eps
N_REGIONS = 11
print 'loop go'

# for assign_labels in ('kmeans','discretize'):
t0 = time.time()
print 't0 %.2fs' % t0
labels = spectral_clustering(graph, n_clusters = N_REGIONS,
                             assign_labels='discretize', 
                             random_state=1, 
                             eigen_solver='arpack')

t1 = time.time()
circle1 = (x - center1[0])**2 + (y - center1[1])**2 < radius1**2
circle2 = (x - center2[0])**2 + (y - center2[1])**2 < radius2**2
circle3 = (x - center3[0])**2 + (y - center3[1])**2 < radius3**2
circle4 = (x - center4[0])**2 + (y - center4[1])**2 < radius4**2

###############################################################################
# 4 circles
img = circle1 + circle2 + circle3 + circle4
mask = img.astype(bool)
img = img.astype(float)

img += 1 + 0.2 * np.random.randn(*img.shape)

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(img, mask=mask)

# Take a decreasing function of the gradient: we take it weakly
# dependant from the gradient the segmentation is close to a voronoi
graph.data = np.exp(-graph.data / graph.data.std())

# Force the solver to be arpack, since amg is numerically
# unstable on this example
labels = spectral_clustering(graph, n_clusters=4, eigen_solver='arpack')
label_im = -np.ones(mask.shape)
label_im[mask] = labels

pl.matshow(img)
pl.matshow(label_im)

###############################################################################
circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2
circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2
circle3 = (x - center3[0]) ** 2 + (y - center3[1]) ** 2 < radius3 ** 2
circle4 = (x - center4[0]) ** 2 + (y - center4[1]) ** 2 < radius4 ** 2

###############################################################################
# 4 circles
img = circle1 + circle2 + circle3 + circle4
mask = img.astype(bool)
img = img.astype(float)

img += 1 + 0.2 * np.random.randn(*img.shape)

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(img, mask=None)

# Take a decreasing function of the gradient: we take it weakly
# dependent from the gradient the segmentation is close to a voronoi
graph.data = np.exp(-graph.data / graph.data.std())

# Force the solver to be arpack, since amg is numerically
# unstable on this example
labels = spectral_clustering(graph, n_clusters=4, eigen_solver='arpack')
# label_im = -np.ones(mask.shape)
label_im = -np.ones(img.shape)
# label_im[mask] = labels
label_im = labels.reshape((100, 100))
plt.matshow(img)
plt.matshow(label_im)
Beispiel #50
0
import numpy as np
import scipy as sp
import pylab as pl

from sklearn.feature_extraction import image
from sklearn.cluster import spectral_clustering

lena = sp.misc.lena()
# Downsample the image by a factor of 4
lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]

# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(lena)

# Take a decreasing function of the gradient: an exponential
# The smaller beta is, the more independant the segmentation is of the
# actual image. For beta=1, the segmentation is close to a voronoi
beta = 5
eps = 1e-6
graph.data = np.exp(-beta * graph.data / lena.std()) + eps

# Apply spectral clustering (this step goes much faster if you have pyamg
# installed)
N_REGIONS = 11
labels = spectral_clustering(graph, k=N_REGIONS)
labels = labels.reshape(lena.shape)

################################################################################
	def scan_received(self, msg):
		""" Returns an occupancy grid to publish data to map"""	
		if len(msg.ranges) != 360:
			return

		#make a pose stamp that relates to the odom of the robot
		p = PoseStamped(header=Header(stamp=msg.header.stamp,frame_id="base_link"), pose=Pose())
		self.odom_pose = self.tf_listener.transformPose("odom", p)
		# convert the odom pose to the tuple (x,y,theta)
		self.odom_pose = TransformHelpers.convert_pose_to_xy_and_theta(self.odom_pose.pose)

		time_past = 0

		for degree in range(360):
			if msg.ranges[degree] > 0.0 and msg.ranges[degree] < 5.0:
				#gets the position of the laser data points
				data_x = self.odom_pose[0] + msg.ranges[degree]*math.cos(degree*math.pi/180.0 + self.odom_pose[2])
				data_y = self.odom_pose[1] + msg.ranges[degree]*math.sin(degree*math.pi/180+self.odom_pose[2])

				#maps laser data to a pixel in the map
				datax_pixel = int((data_x - self.origin[0])/self.resolution)
				datay_pixel = int((data_y - self.origin[1])/self.resolution)

				#maps the robot to a position
				robot = (data_x - self.odom_pose[0], data_y - self.odom_pose[1])

				#finds how far away the point is from the robot
				magnitude = math.sqrt(robot[0]**2 + robot[1]**2)

				#converts magnitude and robot position to pixels in the map
				n_steps = max([1, int(math.ceil(magnitude/self.resolution))])
				robot_step = (robot[0]/(n_steps-1), robot[1]/(n_steps-1))
				marked = set()

				for pixel in range(n_steps):
					curr_x = self.odom_pose[0] + pixel*robot_step[0]
					curr_y = self.odom_pose[1] + pixel*robot_step[1]
					if (self.is_in_map(curr_x, curr_y)):
						#make sure its in the map
						break

					x_ind = int((curr_x - self.origin[0])/self.resolution)
					y_ind = int((curr_y - self.origin[1])/self.resolution)
					if x_ind == datax_pixel and y_ind==datay_pixel and self.odds_ratios[datax_pixel, datay_pixel] >= 1/60.0:
						#set odds ratio equal to past odds ratio
						if time_past % 5 == 0:
							self.past_odds_ratios[datax_pixel, datay_pixel]=self.odds_ratios[datax_pixel, datay_pixel]
							time_past += 1
						self.odds_ratios[datax_pixel, datay_pixel] *= self.p_occ[datax_pixel, datay_pixel]/(1-self.p_occ[datax_pixel, datay_pixel]) * self.odds_ratio_hit
					if not((x_ind, y_ind) in marked) and self.odds_ratios[x_ind, y_ind] >= 1/60.0:
						#If point isn't marked, update the odds of missing and add to the map
						if time_past % 5 == 0:
							self.past_odds_ratios[x_ind, y_ind]=self.odds_ratios[x_ind, y_ind]
							time_past += 1
						self.odds_ratios[x_ind, y_ind] *= self.p_occ[x_ind, y_ind] / (1-self.p_occ[x_ind, y_ind]) * self.odds_ratio_miss
						#self.p_occ[x_ind, y_ind] *= self.p_occ[x_ind, y_ind] * self.odds_ratio_miss/self.odds_ratio_hit
						marked.add((x_ind, y_ind))
						#print 'New Point'
				if not(self.is_in_map(data_x, data_y)) and self.odds_ratios[data_x, datay_pixel] >= 1/60.0:
					#if it is not in the map, update the odds of hitting it
					if time_past % 5 == 0:
						self.past_odds_ratios[datax_pixel, datay_pixel]=self.odds_ratios[datax_pixel, datay_pixel]
						time_past += 1
					self.odds_ratios[datax_pixel, datay_pixel] *= self.p_occ[datax_pixel, datay_pixel]/(1-self.p_occ[datax_pixel, datay_pixel]) * self.odds_ratio_hit


		self.seq += 1
		if self.seq % 10 == 0:
			map = OccupancyGrid() #this is a nav msg class
			map.header.seq = self.seq
			map.header.stamp = msg.header.stamp
			map.header.frame_id = "map"
			map.header.frame_id = "past_map"
			map.info.origin.position.x = self.origin[0]
			map.info.origin.position.y = self.origin[1]
			map.info.width = self.n
			map.info.height = self.n
			map.info.resolution = self.resolution
			map.data = [0]*self.n**2 #the zero is a formatter, not a multiple of 0
			for i in range(self.n):
				#this is giving us the i,j grid square, occupancy grid
				for j in range(self.n):
					idx = i+self.n*j #makes horizontal rows (i is x, j is y)
					if self.odds_ratios[i,j] < 1/5.0:
						map.data[idx] = 0 #makes the gray
					elif self.odds_ratios[i,j] >= 1/5.0 < 0.5:
						map.data[idx] = 25
					elif self.odds_ratios[i,j] > 0.5:
						map.data[idx] = 100 #makes the black walls
					else:
						map.data[idx] = -1 #makes unknown
			self.pub.publish(map)
			#TODO: Change display such that we're not just looking at the ratio, but mapping the dynamic archive and current readings

		image1 = np.zeros((self.odds_ratios.shape[0], self.odds_ratios.shape[1],3))
		image2 = np.zeros((self.odds_ratios.shape[0], self.odds_ratios.shape[1],3))

		self.counter+=1

		x_odom_index = int((self.odom_pose[0] - self.origin[0])/self.resolution)
		y_odom_index = int((self.odom_pose[1] - self.origin[1])/self.resolution)

		self.pose.append((x_odom_index, y_odom_index))

		#.shape() comes from being related to the np class
		for i in range(image1.shape[0]):
			for j in range(image1.shape[1]):
				#print self.past_odds_ratios[i,j]
				#print self.odds_ratios[i,j]
				#the thing that just rapidly appeared, disappeared!
				delta = (self.odds_ratios[i,j]-self.past_odds_ratios[i,j])
				if (delta < 0.0) and (i,j) in self.rapid_appear:
					self.dyn_obs.append((i,j,self.counter))

				#whoa buddy, a thing just appeared
				if delta > 1000.0 and (i,j) not in self.rapid_appear:
					self.rapid_appear.add((i,j))

				if self.odds_ratios[i,j] < 1/50.0:
					image1[i,j,:] = 1.0 #makes open space
				elif self.odds_ratios[i,j] >= 1/50.0 and self.odds_ratios[i,j] <4/5.0:
					image1[i,j,:] = (0, 255, 0)
				elif self.odds_ratios[i,j] > 50.0:
					image1[i,j,:] = (0, 0, 255) #makes walls
				else:
					image1[i,j,:] = 0.5 #not read
					

		if len(self.dyn_obs)>0:
			for point in self.dyn_obs:
				if (self.counter-point[2])<=100:
					image2[point[0],point[1]] = (255,0,255) #makes old/dynamic shapes on other map
			graph = image.img_to_graph(image1)
			beta = 5
			eps = 1e-6
			graph.data = np.exp(-beta*graph.data/image1.std())+eps

			N_REGIONS = 5

			for assign_labels in ('kmeans', 'discretize'):
				t0 = time.time()
	    		labels = spectral_clustering(graph, n_clusters=N_REGIONS, assign_labels=assign_labels,random_state=1)
	    		t1 = time.time()
	    		labels = labels.reshape(lena.shape)

	    		plt.figure(figsize=(5, 5))
	    		plt.imshow(image1,   cmap=plt.cm.gray)
	    		for l in range(N_REGIONS):
	        		plt.contour(labels == l, contours=1, colors=[plt.cm.spectral(l / float(N_REGIONS)), ])
	    		plt.xticks(())
	    		plt.yticks(())
	    		plt.title('Spectral clustering: %s, %.2fs' % (assign_labels, (t1 - t0)))

			plt.show()


		for point in self.pose:
			image1[point[0], point[1]] = (255, 0, 0)



		#draw it!
		cv2.circle(image1,(y_odom_index, x_odom_index), 2,(255,0,0))
		cv2.imshow("map", cv2.resize(image1,(500,500)))
		cv2.imshow("past_map", cv2.resize(image2,(500,500)))
		cv2.waitKey(20) #effectively a delay