Example #1
0
 def testCrackConnectionImage(self):
     labels = vigra.analysis.labelImageWithBackground(
         vigra.readImage("crackConvert-test1.png")[0].astype(int), 8,
         0)[0]
     cc = crackConnectionImage(labels)
     cc_ref = vigra.readImage("crackConvert-test1cc.png")
     self.assertEqual(numpy.abs(cc - cc_ref).max(), 0)
def use_ridge_regression():
    # Read the command line arguments.
    args = process_command_line()
    im_orig = numpy.squeeze(vigra.readImage("cc_90.png"))
    im = kernel_ridge_regression(im_orig, args.tau, args.rho, args.gamma)
    vigra.impex.writeImage(im, "res.png")
    im_true = numpy.squeeze(vigra.readImage("charlie-chaplin.jpg"))
    print "MSE: ", calc_mse(im, im_true)

    return 0
Example #3
0
def use_ridge_regression():
    # Read the command line arguments.
    args = process_command_line()
    im_orig = numpy.squeeze(vigra.readImage("cc_90.png"))
    im = kernel_ridge_regression(im_orig, args.tau, args.rho, args.gamma)
    vigra.impex.writeImage(im, "res.png")
    im_true = numpy.squeeze(vigra.readImage("charlie-chaplin.jpg"))
    print "MSE: ", calc_mse(im, im_true)

    return 0
Example #4
0
def bayesian_hp_optimization(Q):
    im = numpy.squeeze(vigra.readImage("cc_90.png"))
    im_orig = numpy.squeeze(vigra.readImage("charlie-chaplin.jpg"))
    P = []
    E = []
    # initialize
    f = open("cache/bayes_opt.txt", "w")
    start = time.time()
    #for i in range(20):
    #    interpolation = kernel_ridge_regression( im, Q[i,2], Q[i,0], Q[i,1] )
    #    P.append( [Q[i,0], Q[i,1], Q[i,2]] )
    #    E.append( calc_mse(im_orig, interpolation) )
    #    # save result
    #    res = str(Q[i,0]) + str(" ") + str(Q[i,1]) + str(" ") + str(Q[i,2]) + str(" ") + str(E[i]) + '\n'
    #    f.write(res)
    #    f.flush()

    save = numpy.loadtxt("cache/save.txt")
    for d in save:
        P.append([d[0], d[1], d[2]])
        E.append(d[3])

    # TODO should we remove known vals from Q ?
    # remove known values from Q
    # Q = numpy.delete(Q, numpy.arange(20), axis=0)
    # parameter for the matern regression
    sig_rho = 8. / 10
    sig_gamma = 3. / 10
    sig_tau = .9 / 10
    lambd = .3
    for i in range(20):
        mse, var = matern_regression(Q, P, E, sig_rho, sig_gamma, sig_tau,
                                     lambd)
        #utility = numpy.divide( mse, numpy.sqrt(var) )
        utility = numpy.abs(numpy.divide(mse, var))
        best_hp = numpy.nanargmin(utility)
        P.append(Q[best_hp])
        print Q[best_hp]
        print utility[best_hp], mse[best_hp], var[best_hp]
        interpolation = kernel_ridge_regression(im, Q[best_hp, 2],
                                                Q[best_hp, 0], Q[best_hp, 1])
        E.append(calc_mse(im_orig, interpolation))
        res = str(Q[best_hp, 0]) + str(" ") + str(
            Q[best_hp, 1]) + str(" ") + str(Q[best_hp, 2]) + str(" ") + str(
                E[-1]) + '\n'
        f.write(res)
        f.flush()
    stop = time.time()
    print "Bayesian parameter optimization took %.02f seconds." % (stop -
                                                                   start)
    best_hp = numpy.argmin(E)
    f.close()
    return P[best_hp], E[best_hp]
def importStack(path,fname):
	absname = path +fname
	zsize = vigra.impex.numberImages(absname)
	im =vigra.readImage(absname, index = 0, dtype='FLOAT')
	#vol = np.zeros([im.height,im.width,zsize])
	vol = np.memmap('tmpVolDat/' + fname[0:-4],dtype='float64',mode = 'w+', shape = (im.height,im.width,zsize))
	#raise('hallo')
	for i in range(zsize):
		print("importing slice " + str(i) + ' of file '+fname)
		im=np.squeeze(vigra.readImage(absname, index = i, dtype='FLOAT'))
		vol[:,:,i] = im
	vol.flush()
	return vol
def bayesian_hp_optimization(Q):
    im = numpy.squeeze(vigra.readImage("cc_90.png"))
    im_orig = numpy.squeeze(vigra.readImage("charlie-chaplin.jpg"))
    P = []
    E = []
    # initialize
    f = open("cache/bayes_opt.txt","w")
    start = time.time()
    #for i in range(20):
    #    interpolation = kernel_ridge_regression( im, Q[i,2], Q[i,0], Q[i,1] )
    #    P.append( [Q[i,0], Q[i,1], Q[i,2]] )
    #    E.append( calc_mse(im_orig, interpolation) )
    #    # save result
    #    res = str(Q[i,0]) + str(" ") + str(Q[i,1]) + str(" ") + str(Q[i,2]) + str(" ") + str(E[i]) + '\n'
    #    f.write(res)
    #    f.flush()

    save = numpy.loadtxt("cache/save.txt")
    for d in save:
        P.append( [d[0], d[1], d[2]] )
        E.append( d[3] )

    # TODO should we remove known vals from Q ?
    # remove known values from Q
    # Q = numpy.delete(Q, numpy.arange(20), axis=0)
    # parameter for the matern regression
    sig_rho     = 8. / 10
    sig_gamma   = 3. / 10
    sig_tau     = .9 / 10
    lambd       = .3
    for i in range(20):
        mse, var = matern_regression(Q, P, E, sig_rho, sig_gamma, sig_tau, lambd)
        #utility = numpy.divide( mse, numpy.sqrt(var) )
        utility = numpy.abs(numpy.divide( mse, var ) )
        best_hp = numpy.nanargmin(utility)
        P.append( Q[best_hp])
        print Q[best_hp]
        print utility[best_hp], mse[best_hp], var[best_hp]
        interpolation = kernel_ridge_regression( im, Q[best_hp,2], Q[best_hp,0], Q[best_hp,1])
        E.append( calc_mse(im_orig, interpolation))
        res = str(Q[best_hp,0]) + str(" ") + str(Q[best_hp,1]) + str(" ") + str(Q[best_hp,2]) + str(" ") + str(E[-1]) + '\n'
        f.write(res)
        f.flush()
    stop = time.time()
    print "Bayesian parameter optimization took %.02f seconds." % (stop-start)
    best_hp = numpy.argmin(E)
    f.close()
    return P[best_hp], E[best_hp]
Example #7
0
    def addBackgroundWithFrame(self,
                               bgImageFilename,
                               container=True,
                               **params):
        """fe.addBackgroundWithFrame(bgImageFilename, depth = 85, ...)

        Adds a picture object to the fig.File, framed by an additional
        rectangle.  See addROIRect() and addImage().  If no roi is
        given (via a keyword parameter), the image file is opened
        (using readImage) and its size is used to initialize a
        BoundingBox positioned at the origin.

        Returns the pair (bgImage, bgRect) of both added fig objects."""

        if not params.has_key("roi") and not self.roi:
            size = readImage(bgImageFilename).size()
            params["roi"] = Rect2D(size)
        if container == True:
            container = self.f

        if not params.has_key("depth"):
            params["depth"] = 1

        bgRect = self.addROIRect(**params)

        bgImage = fig.PictureBBox(0, 0, 1, 1, bgImageFilename)
        bgImage.points = list(bgRect.points)
        bgImage.depth = 999
        container.append(bgImage)

        return bgImage, bgRect
Example #8
0
    def load_image(self, filename=None):
        #if filename is None:
        #    filename = self.image_name

        # get input image
        self.image_name = filename
        self.set_image(vigra.readImage(filename))
    def load_image(self, filename=None):
        # if filename is None:
        #    filename = self.image_name

        # get input image
        self.image_name = filename
        self.set_image(vigra.readImage(filename))
def extractMarkedNodes(filename):
    # extracts markers from the raw images
    # returns a dict, with color as key and a list
    # of marker center coords as value

    print "processing file", filename
    im = vigra.readImage(filename)
    colored1 = im[..., 0] != im[..., 1]
    colored2 = im[..., 1] != im[..., 2]
    colored3 = im[..., 2] != im[..., 0]
    colored = numpy.logical_or(colored1, colored2)
    colored = numpy.logical_or(colored, colored3)
    cc = vigra.analysis.labelImageWithBackground(colored.astype(numpy.uint8))
    # take the center pixel for each colored square
    feats = vigra.analysis.extractRegionFeatures(colored.astype(numpy.float32), cc, ["RegionCenter"])
    center_coords = feats["RegionCenter"][1:][:].astype(numpy.uint32)
    center_coords_list = [center_coords[:, 0], center_coords[:, 1]]
    im_centers = numpy.asarray(im[center_coords_list])
    # print im_centers
    # struct = im_centers.view(dtype='f4, f4, f4')

    # colors, indices = numpy.unique(struct, return_inverse=True)
    # print colors, indices, colors.shape
    centers_by_color = {}
    for iindex in range(center_coords.shape[0]):
        center = (center_coords[iindex][0], center_coords[iindex][1])
        #print center, index
        color = colors[tuple(im_centers[iindex].astype(numpy.uint8))]
        #centers_by_color.setdefault(tuple(im_centers[iindex]), []).append(center)
        centers_by_color.setdefault(color, []).append(center)
        
    print centers_by_color
    return centers_by_color
Example #11
0
 def load_image(self, file_):
     if not self._image_cache.has_key(file_):
         image = vigra.readImage(file_)
         # numpy array convention
         image.swapaxes(0, 1)
         self._image_cache[file_] = np.squeeze(image)
     return self._image_cache[file_]
Example #12
0
    def addBackgroundWithFrame(self, bgImageFilename, container = True, **params):
        """fe.addBackgroundWithFrame(bgImageFilename, depth = 85, ...)

        Adds a picture object to the fig.File, framed by an additional
        rectangle.  See addROIRect() and addImage().  If no roi is
        given (via a keyword parameter), the image file is opened
        (using readImage) and its size is used to initialize a
        BoundingBox positioned at the origin.

        Returns the pair (bgImage, bgRect) of both added fig objects."""

        if not params.has_key("roi") and not self.roi:
            size = readImage(bgImageFilename).size()
            params["roi"] = Rect2D(size)
        if container == True:
            container = self.f

        if not params.has_key("depth"):
            params["depth"] = 1

        bgRect = self.addROIRect(**params)

        bgImage = fig.PictureBBox(0, 0, 1, 1, bgImageFilename)
        bgImage.points = list(bgRect.points)
        bgImage.depth = 999
        container.append(bgImage)

        return bgImage, bgRect
Example #13
0
def kernel_ridge_regression(tau, sigma):
    # Load the image.
    im_orig = numpy.squeeze(vigra.readImage("cc_90.png"))

    # Make a copy, so both the original and the regressed image can be shown afterwards.
    im = numpy.array(im_orig)

    # Find the known pixels and the pixels that shall be predicted.
    known_ind = numpy.where(im != 0)
    unknown_ind = numpy.where(im >= 0)
    known_x = numpy.array(known_ind).transpose()
    known_y = numpy.array(im[known_ind])
    pred_x = numpy.array(unknown_ind).transpose()

    # Train and predict with the given regressor.
    start = time.time()
    print "training..."
    r = KernelRidgeRegressor(tau, sigma)
    r.train(known_x, known_y)
    print "done training"
    # pickle.dump(r, open("regressor.p", "wb"))
    # r = pickle.load(open("regressor.p", "rb"))
    print "predicting..."
    pred_y = r.predict(pred_x)
    print "done predicting"

    # Write the predicted values back into the image and show the result.
    im[unknown_ind] = pred_y
    stop = time.time()
    print "Train and predict took %.02f seconds." % (stop - start)
    vigra.impex.writeImage(im, "res.png")
Example #14
0
 def load_image(self, file_):
     if not self._image_cache.has_key(file_):
         image = vigra.readImage(file_)
         # numpy array convention
         image.swapaxes(0, 1)
         self._image_cache[file_] = np.squeeze(image)
     return self._image_cache[file_]
Example #15
0
 def setImage(self, imagefilename, retainView=False):
     self.image = vigra.readImage(imagefilename)
     shapefactor = self.image.shape[0]/5000
     self.imagedisplay = vigra.sampling.resizeImageNoInterpolation(self.image, (self.image.shape[0]/shapefactor, self.image.shape[1]/shapefactor))
     self.imagedisplay = vigra.colors.brightness(vigra.colors.linearRangeMapping(self.imagedisplay), 35.)
     super(S57QImageViewer, self).setImage(self.imagedisplay.qimage(), retainView)
     self.geoimage = GeoImage(imagefilename)
def kernel_ridge_regression(tau, sigma):
    # Load the image.
    im_orig = numpy.squeeze(vigra.readImage("cc_90.png"))

    # Make a copy, so both the original and the regressed image can be shown afterwards.
    im = numpy.array(im_orig)

    # Find the known pixels and the pixels that shall be predicted.
    known_ind = numpy.where(im != 0)
    unknown_ind = numpy.where(im >= 0)
    known_x = numpy.array(known_ind).transpose()
    known_y = numpy.array(im[known_ind])
    pred_x = numpy.array(unknown_ind).transpose()

    # Train and predict with the given regressor.
    start = time.time()
    print "training..."
    r = KernelRidgeRegressor(tau, sigma)
    r.train(known_x, known_y)
    print "done training"
    # pickle.dump(r, open("regressor.p", "wb"))
    # r = pickle.load(open("regressor.p", "rb"))
    print "predicting..."
    pred_y = r.predict(pred_x)
    print "done predicting"

    # Write the predicted values back into the image and show the result.
    im[unknown_ind] = pred_y
    stop = time.time()
    print "Train and predict took %.02f seconds." % (stop-start)
    vigra.impex.writeImage(im, "res.png")
Example #17
0
    def __getitem__(self,index):

        if self.filetype == "image" :
            return readImage(self.files[index])
        elif self.filetype == "h5" :
            f = h5py.File(self.files[index],'r')
            value = f[self.dset].value
            f.close()
            return value
Example #18
0
    def get_data(self, data_nr):
        """Returns the dataset.

        :param data_nr: number of dataset
        :return: the dataset
        """
        if self._datatype(data_nr) == "hdf5" or self.is_internal(data_nr):
            return vigra.readHDF5(self.get_data_path(data_nr), self.get_data_key(data_nr))
        else:
            return vigra.readImage(self.get_data_path(data_nr))
Example #19
0
    def get_data(self, data_nr):
        """Returns the dataset.

        :param data_nr: number of dataset
        :return: the dataset
        """
        if self._datatype(data_nr) == "hdf5" or self.is_internal(data_nr):
            return vigra.readHDF5(self.get_data_path(data_nr),
                                  self.get_data_key(data_nr))
        else:
            return vigra.readImage(self.get_data_path(data_nr))
Example #20
0
def main(filename, biScale=1.6, saddleThreshold=0.2):
    """Creates an initial GeoMap ("level 0" of irregular pyramid) using
    subpixel watersheds on a Gaussian gradient boundary indicator and
    creates a Workspace from that."""

    import bi_utils
    img = vigra.readImage(filename)
    gm, grad = bi_utils.gaussianGradient(img, biScale)
    wsm = maputils.subpixelWatershedMap(gm, saddleThreshold=saddleThreshold)

    return Workspace(wsm, img, bi=gm)
Example #21
0
def main(filename, biScale = 1.6, saddleThreshold = 0.2):
    """Creates an initial GeoMap ("level 0" of irregular pyramid) using
    subpixel watersheds on a Gaussian gradient boundary indicator and
    creates a Workspace from that."""

    import bi_utils
    img = vigra.readImage(filename)
    gm, grad = bi_utils.gaussianGradient(img, biScale)
    wsm = maputils.subpixelWatershedMap(
        gm, saddleThreshold = saddleThreshold)

    return Workspace(wsm, img, bi = gm)
Example #22
0
 def _execute_5d(self, roi, result):
     t_start, x_start, y_start, z_start, c_start = roi.start
     t_stop, x_stop, y_stop, z_stop, c_stop = roi.stop
     
     for result_t, t in enumerate( range( t_start, t_stop ) ):
         file_name = self.fileNameList[t]
         for result_z, z in enumerate( range( z_start, z_stop ) ):
             img = vigra.readImage( file_name, index=z )
             result[result_t, :, :, result_z, :] = img[ x_start:x_stop,
                                                        y_start:y_stop,
                                                        c_start:c_stop ]
     return result
def random_hp_optimization(Q):
    f = open("cache/rand_opt.txt","w")
    image = numpy.squeeze(vigra.readImage("cc_90.png"))
    im_orig = numpy.squeeze(vigra.readImage("charlie-chaplin.jpg"))
    start = time.time()
    P = []
    E = []
    N = Q.shape[0]
    rand_indices = numpy.random.randint(0, N, size = 40)
    for i in rand_indices:
        interpolation = kernel_ridge_regression( image, Q[i,2], Q[i,0], Q[i,1] )
        P.append( [Q[i,0], Q[i,1], Q[i,2]] )
        E.append( calc_mse(im_orig, interpolation) )
        res = str(Q[i,0]) + str(" ") + str(Q[i,1]) + str(" ") + str(Q[i,2]) + str(" ") + str(E[-1]) + '\n'
        f.write(res)
        f.flush()
    rand_hp = numpy.argmin(E)
    stop = time.time()
    print "Random parameter optimization took %.02f seconds." % (stop-start)
    f.close()
    return P[rand_hp], E[rand_hp]
def volume_from_dir(dirpattern, output_filepath, offset=0, nfiles=None):
    filelist = glob.glob(dirpattern)
    filelist = sorted(filelist, key=str.lower) #mwahaha, 10000<9000
    begin = offset
    if nfiles is not None and offset+nfiles<len(filelist):
        end=offset+nfiles
    else:
        end = len(filelist)
    filelist = filelist[begin:end]
    nx, ny = vigra.readImage(filelist[0]).squeeze().shape
    dt = vigra.readImage(filelist[0]).dtype
    nz = len(filelist)
    volume = numpy.zeros((nx, ny, nz, 1), dtype=dt)
    
    for i in range(len(filelist)):
        volume[:, :, i, 0] = vigra.readImage(filelist[i]).squeeze()[:]
        
    outfile = h5py.File(output_filepath, "w")
    outfile.create_dataset("data", data=volume)
    outfile.close()
    return volume
def makeTif(pathSearch, pathSave, filename):  
    frameNum = os.path.splitext(filename)[0][-5:]; begName = os.path.splitext(filename)[0][:-5]; ext = os.path.splitext(filename)[1]
    newFrameNum = '{:0>5}'.format(int(frameNum)*30)
    try:
        im=vigra.readImage(os.path.join(pathSearch, filename))
    except IOError:
        print "File pbl with file ", filename
        return 0
    else:
        im2=vigra.Image(im, dtype=np.uint8)
        im2[im2>0]=1
        im2.writeImage(os.path.join(pathSave, 'Mask_'+begName+newFrameNum+ext))
        return 1
Example #26
0
    def _execute_5d(self, roi, result):
        # roi is in tzyxc order.
        t_start, z_start, y_start, x_start, c_start = roi.start
        t_stop, z_stop, y_stop, x_stop, c_stop = roi.stop

        # Use *enumerated* range to get global t coords and result t coords
        for result_t, t in enumerate( range( t_start, t_stop ) ):
            file_name = self.fileNameList[t]
            for result_z, z in enumerate( range( z_start, z_stop ) ):
                img = vigra.readImage( file_name, index=z )
                result[result_t, result_z, :, :, :] = img[ x_start:x_stop,
                                                           y_start:y_stop,
                                                           c_start:c_stop ].withAxes( *'yxc' )
        return result
Example #27
0
    def _execute_5d(self, roi, result):
        # roi is in tzyxc order.
        t_start, z_start, y_start, x_start, c_start = roi.start
        t_stop, z_stop, y_stop, x_stop, c_stop = roi.stop

        # Use *enumerated* range to get global t coords and result t coords
        for result_t, t in enumerate( range( t_start, t_stop ) ):
            file_name = self.fileNameList[t]
            for result_z, z in enumerate( range( z_start, z_stop ) ):
                img = vigra.readImage( file_name, index=z )
                result[result_t, result_z, :, :, :] = img[ x_start:x_stop,
                                                           y_start:y_stop,
                                                           c_start:c_stop ].withAxes( *'yxc' )
        return result
Example #28
0
def random_hp_optimization(Q):
    f = open("cache/rand_opt.txt", "w")
    image = numpy.squeeze(vigra.readImage("cc_90.png"))
    im_orig = numpy.squeeze(vigra.readImage("charlie-chaplin.jpg"))
    start = time.time()
    P = []
    E = []
    N = Q.shape[0]
    rand_indices = numpy.random.randint(0, N, size=40)
    for i in rand_indices:
        interpolation = kernel_ridge_regression(image, Q[i, 2], Q[i, 0], Q[i,
                                                                           1])
        P.append([Q[i, 0], Q[i, 1], Q[i, 2]])
        E.append(calc_mse(im_orig, interpolation))
        res = str(Q[i, 0]) + str(" ") + str(Q[i, 1]) + str(" ") + str(
            Q[i, 2]) + str(" ") + str(E[-1]) + '\n'
        f.write(res)
        f.flush()
    rand_hp = numpy.argmin(E)
    stop = time.time()
    print "Random parameter optimization took %.02f seconds." % (stop - start)
    f.close()
    return P[rand_hp], E[rand_hp]
Example #29
0
    def _execute_5d(self, roi, result):
        # Technically, t and z might be switched depending on SequenceAxis.
        # Beware these variable names for t/z might be misleading.
        t_start, z_start, y_start, x_start, c_start = roi.start
        t_stop, z_stop, y_stop, x_stop, c_stop = roi.stop

        # Use *enumerated* range to get global t coords and result t coords
        for result_t, t in enumerate( range( t_start, t_stop ) ):
            file_name = self.fileNameList[t]
            for result_z, z in enumerate( range( z_start, z_stop ) ):
                img = vigra.readImage( file_name, index=z )
                result[result_t, result_z, :, :, :] = img[ x_start:x_stop,
                                                           y_start:y_stop,
                                                           c_start:c_stop ].withAxes( *'yxc' )
        return result
Example #30
0
    def _execute_5d(self, roi, result):
        # Technically, t and z might be switched depending on SequenceAxis.
        # Beware these variable names for t/z might be misleading.
        t_start, z_start, y_start, x_start, c_start = roi.start
        t_stop, z_stop, y_stop, x_stop, c_stop = roi.stop

        # Use *enumerated* range to get global t coords and result t coords
        for result_t, t in enumerate(range(t_start, t_stop)):
            file_name = self.fileNameList[t]
            for result_z, z in enumerate(range(z_start, z_stop)):
                img = vigra.readImage(file_name, index=z)
                result[result_t, result_z, :, :, :] = img[x_start:x_stop, y_start:y_stop, c_start:c_stop].withAxes(
                    *"yxc"
                )
        return result
def dense_reconstruction_slice(probs_slice_path, skeletons_volume, rf):
    probs = vigra.readImage(probs_slice_path)
    probs = np.squeeze(probs)
    probs = np.array(probs) # get rid of axistags...
    # need to invert for watersheds
    probs = 1. - probs

    # Threshold and sigma hardcoded to values suited for the google pmaps
    seg_wsdt = watersheds_dt(probs, 0.15, 2.6)

    # this may happen for the black slices...
    if np.unique(seg_wsdt).shape[0] == 1:
        return np.zeros_like(probs, dtype = np.uint32)

    rag = vigra.graphs.regionAdjacencyGraph(
            vigra.graphs.gridGraph(seg_wsdt.shape[0:2]), seg_wsdt )

    # +1 because we dont have a zero in overseg
    merge_nodes = np.zeros(rag.nodeNum+1, dtype = np.uint32)

    gridGraphEdgeIndicator = vigra.graphs.implicitMeanEdgeMap(rag.baseGraph,
            probs)
    edge_feats = rag.accumulateEdgeStatistics(gridGraphEdgeIndicator)
    edge_feats = np.nan_to_num(edge_feats)
    edge_probs = rf.predict_proba(edge_feats)[:,1]
    probs_thresh = 0.5

    for skel_id in np.unique(skeletons_volume):
        if skel_id == 0:
            continue
        skel_c = np.where(skeletons_volume == skel_id)
        for i in xrange(skel_c[0].size):
            seg_id = seg_wsdt[ skel_c[0][i], skel_c[1][i] ]
            merge_nodes[seg_id] = skel_id
            root = rag.nodeFromId( long(seg_id) )
            nodes_for_merge = [root]
            already_merged  = [root]
            while nodes_for_merge:
                u = nodes_for_merge.pop()
                for v in rag.neighbourNodeIter(u):
                    edge = rag.findEdge(u,v)
                    #print edge_mean_probs[edge.id]
                    if edge_probs[edge.id] <= probs_thresh and not v.id in already_merged:
                        merge_nodes[v.id] = skel_id
                        nodes_for_merge.append(v)
                        already_merged.append(v.id)
    return rag.projectLabelsToBaseGraph(merge_nodes)
Example #32
0
def run_feature_generation(dataset,
                           start=0,
                           end=-1,
                           patches=False,
                           ignore_pups=False):
    loader = data.Loader()
    images = loader.load_original_images(dataset=dataset)

    if dataset == 'train':
        if patches:
            outdir = settings.DENSITY_MAP_FEATURE_CROPS_DIR
        else:
            outdir = settings.TRAIN_FEATURES_DIR
        indir = settings.TRAIN_ORIGINAL_IMAGES_DIR
    elif dataset == 'test_st1':
        outdir = settings.TEST_FEATURES_DIR
        indir = settings.TEST_ORIGINAL_IMAGES_DIR
    else:
        raise Exception('Data set not implemented: ' + dataset)

    if not os.path.exists(outdir):
        os.makedirs(outdir)

    if end == -1:
        end = len(images)

    for idx in range(start, end):
        imageid = images[idx]['m']['filename']
        settings.logger.info('Generating crops image %d (%s.jpg)...' %
                             (idx, imageid))

        image = vigra.readImage(os.path.join(indir, imageid + '.jpg'))
        image = vigra.sampling.resampleImage(image, factor=RESAMPLE_FACTOR)

        if patches:
            sea_lions = [((round(float(coord['x_coord']) * RESAMPLE_FACTOR),
                           round(float(coord['y_coord']) * RESAMPLE_FACTOR)),
                          coord['category'])
                         for coord in images[idx]['m']['coordinates']]
            crops = sliding_window_crop_generation(sea_lions, image.shape,
                                                   ignore_pups)

            generate_features(image,
                              os.path.join(outdir, imageid),
                              patches=crops)
        else:
            generate_features(image, os.path.join(outdir, imageid))
 def __call__(self, filename, out_path):
     if not os.path.exists(out_path):
         os.makedirs(out_path)
         print 'made %s' % out_path
         
     colorin = vigra.readImage(filename)
     filename_base, extension = os.path.splitext(os.path.basename(filename))
     
     col_dec = self.colorDeconv(colorin)
     channels = ['h', 'e', 'dab']
     for i in range(3):
         new_filename = os.path.join(out_path, 
                                     filename_base + '__%s' % channels[i] + extension)
         vigra.impex.writeImage(col_dec[:,:,i], new_filename) 
         print 'written %s' % new_filename
         
     return
Example #34
0
def getImageByName(imagefilename, topleft=None, bottomright=None, linearrangemapping=True):
	"""
		Returns a defined crop of the file as vigra.Image. 
	"""
	base, ext = os.path.splitext(imagefilename)
	if not ext[1:] in getVigraExts():
		raise IOError('unsupported file extension %s' % ext[1:])
	image = vigra.readImage(imagefilename)
	if topleft == None and bottomright == None:
		return image
	# determine crop size
	x_size = bottomright[0] - topleft[0]
	y_size = bottomright[1] - topleft[1]
	# crop
	out = image[topleft[0]:topleft[0]+x_size, topleft[1]:topleft[1]+y_size].copy()
	if linearrangemapping:
		out = vigra.colors.linearRangeMapping(out)
	return out
Example #35
0
def get_extensions(in_folder):

    max_width = 0
    max_height = 0    
    image_names = os.listdir(in_folder)
    image_names = sorted(filter(lambda x: os.path.splitext(x)[-1].lower() in ['.tif', '.tiff', '.png', '.jpg'], image_names))
    
    for image_name in image_names:
        img = vigra.readImage(os.path.join(in_folder, image_name))
        width = img.shape[0]
        height = img.shape[1]        
        print '%s: %i, %i' % (image_name, width, height)
        
        max_width = max(width, max_width)
        max_height = max(height, max_height)
        
    print 'maximal extensions: %i, %i' % (max_width, max_height)
    return max_width, max_height
Example #36
0
def main():
    """
    Test code
    :return:
    """
    image = vigra.readImage('C:/Users/Saqib/Desktop/Koala.jpg')
    pyramid_1 = get_gaussian_pyramid(image, 4)
    pyramid_2 = get_gaussian_pyramid_2(image, 4)
    # print str(pyramid_1) == str(pyramid_2)

    # Get Gaussian Pyramid for the image
    for i in pyramid_1:
        i.imshow()

    lap_pyramid = get_laplacian_pyramid(image, 4)
    for j in lap_pyramid:
        j.imshow()
    get_laplacian_pyramid(image, 4)
    def generate_dec_crops(self, filename, out_path, crop_size=1024, nb_positions=1):
        if not os.path.exists(out_path):
            os.makedirs(out_path)
            print 'made %s' % out_path
            
        colorin = vigra.readImage(filename)
        width = colorin.shape[0]
        height = colorin.shape[1]

        frow = np.sqrt(nb_positions)
        if np.abs(int(frow) - frow) > 1e-10:
            raise ValueError('number of positions needs to be squared.')
        frow = int(frow)               

        if frow*crop_size > width or frow*crop_size > height:
            print 'crop_size is too large (exceeds image dimensions) ... skipping %s' % filename
            return
        
        offset_x = (width - frow*crop_size) / 2
        offset_y = (height - frow*crop_size) / 2
            
        filename_base, extension = os.path.splitext(os.path.basename(filename))
        
        col_dec = self.colorDeconv(colorin)
        channels = ['h', 'e', 'dab']
        
        for i in range(3):

            position = 1
            for y in range(frow):
                for x in range(frow):
                                            
                    ref_img = col_dec[offset_x+x*crop_size:offset_x+(x+1)*crop_size, 
                                      offset_y+y*crop_size:offset_y+(y+1)*crop_size, i]
        
                    new_filename = os.path.join(out_path, 
                                                '%s__P%05i__%s%s' % (filename_base, position, channels[i], extension))
                    vigra.impex.writeImage(ref_img, new_filename)
                    print 'written %s' % new_filename
                    position += 1
                    
        return
Example #38
0
def get_extensions(in_folder):

    max_width = 0
    max_height = 0
    image_names = os.listdir(in_folder)
    image_names = sorted(
        filter(
            lambda x: os.path.splitext(x)[-1].lower() in
            ['.tif', '.tiff', '.png', '.jpg'], image_names))

    for image_name in image_names:
        img = vigra.readImage(os.path.join(in_folder, image_name))
        width = img.shape[0]
        height = img.shape[1]
        print '%s: %i, %i' % (image_name, width, height)

        max_width = max(width, max_width)
        max_height = max(height, max_height)

    print 'maximal extensions: %i, %i' % (max_width, max_height)
    return max_width, max_height
Example #39
0
def parse_image(filename, tile_x, tile_y):
    """Load the given image and parse it on a (tile_x, tile_y) grid.

    :param filename: image filename
    :param tile_x: number of tiles in x direction
    :param tile_y: number of tiles in y direction
    :return: parsed image as numpy array
    """
    out = numpy.zeros((tile_x, tile_y))
    data = vigra.readImage(filename)
    dx = data.shape[0] / float(tile_x)
    dy = data.shape[1] / float(tile_y)
    for y in xrange(tile_y):
        for x in xrange(tile_y):
            scanx = x * dx + dx/2
            scany = y * dy + dy/2
            if max(data[scanx, scany, :]) < 70:
                out[x, y] = 0
            else:
                out[x, y] = 4
    return out
Example #40
0
def adjust_images(in_folder, out_folder, max_width, max_height):
    image_names = os.listdir(in_folder)
    image_names = sorted(
        filter(
            lambda x: os.path.splitext(x)[-1].lower() in
            ['.tif', '.tiff', '.png', '.jpg'], image_names))

    ref_img = vigra.RGBImage((max_width, max_height))

    if not os.path.exists(out_folder):
        os.makedirs(out_folder)
        print 'made %s' % out_folder
    for image_name in image_names:
        img = vigra.readImage(os.path.join(in_folder, image_name))
        width = img.shape[0]
        height = img.shape[1]

        if width <= max_width and height <= max_height:
            avg_color = get_corner_color(img, 5)
            ref_img[:, :, :] = avg_color

            offset_x = (max_width - width) / 2
            offset_y = (max_height - height) / 2

            ref_img[offset_x:offset_x + width,
                    offset_y:offset_y + height, :] = img

        elif width > max_width and height > max_height:
            # in this case, we have a crop situation
            offset_x = (width - max_width) / 2
            offset_y = (height - max_height) / 2

            ref_img = img[offset_x:offset_x + max_width,
                          offset_y:offset_y + max_height, :]

        # export
        filename = os.path.join(out_folder, image_name)
        vigra.impex.writeImage(ref_img, filename)

    return
Example #41
0
def adjust_images(in_folder, out_folder, max_width, max_height):
    image_names = os.listdir(in_folder)
    image_names = sorted(filter(lambda x: os.path.splitext(x)[-1].lower() in ['.tif', '.tiff', '.png', '.jpg'], image_names))
    
    ref_img = vigra.RGBImage((max_width, max_height))
    
    if not os.path.exists(out_folder):
        os.makedirs(out_folder)
        print 'made %s' % out_folder
    for image_name in image_names:
        img = vigra.readImage(os.path.join(in_folder, image_name))
        width = img.shape[0]
        height = img.shape[1]        
        
        if width <= max_width and height <=max_height:        
            avg_color = get_corner_color(img, 5)
            ref_img[:,:,:] = avg_color
                    
            offset_x = (max_width - width) / 2
            offset_y = (max_height - height) / 2
            
            ref_img[offset_x:offset_x + width, offset_y:offset_y + height, :] = img        

        elif width > max_width and height > max_height:
            # in this case, we have a crop situation
            offset_x = (width - max_width) / 2
            offset_y = (height - max_height) / 2

            ref_img = img[offset_x:offset_x + max_width, 
                          offset_y:offset_y + max_height, :]
                       
            
        # export
        filename = os.path.join(out_folder, image_name)
        vigra.impex.writeImage(ref_img, filename)

    return
            start = old_position[0]
        else:
            slicing = slice(old_position[0]+self.cutout[0]+diff[0], old_position[0]+self.cutout[0])
            start = old_position[0] + self.cutout[0] + int(diff[0])
        self.device.drawRectangle(np.copy(self.basic_image[:,
                                                           slicing,
                                                           old_position[1]:old_position[1]+self.cutout[1]],
                                           order='F'),
                                  start,
                                  old_position[1])        
        


if __name__ == "__main__":
    import time
    image = vigra.readImage('/home/phil/Downloads/1920x1080-wallpaper.jpg')
    image_gs = np.mean(image, axis=-1)
    edges_single = vigra.filters.gaussianGradientMagnitude(image_gs, sigma=1)
    image = np.append(image, np.zeros(image.shape[:-1] + (1,), dtype=image.dtype), axis=-1)

    
    
    basic_image = np.array(image.transpose((2,0,1)), dtype=np.uint8, order='F')
    image_edges = np.zeros(basic_image.shape, dtype=np.uint8, order='F')
    for i in xrange(3):
        image_edges[i,...] = edges_single
    moving_image = np.zeros(basic_image.shape, dtype=np.uint8, order='F')
    rgb_image = np.zeros((4,600,200), dtype=np.uint8, order='F')
    rgb_image[0, :200, :] = 255
    rgb_image[1, 200:400, :] = 255
    rgb_image[2, 400:600, :] = 255
Example #43
0

import gseg









visu 	 	= True
filepath 	= '42049.jpg'
#filepath    = '156065.jpg'
img 		= vigra.readImage(filepath)#[0:200,0:200,:]
imgLab  	= vigra.colors.transform_RGB2Lab(img)
gradmag  	= vigra.filters.gaussianGradientMagnitude(img,4.0)

seg,nseg    = vigra.analysis.slicSuperpixels(imgLab,10.0,5)
#seg,nseg 	= vigra.analysis.watersheds(gradmag)

tgrid 	= cgp2d.TopologicalGrid(seg.astype(numpy.uint64))
cgp  	= cgp2d.Cgp(tgrid)


imgTopo  	= vigra.sampling.resize(imgLab,cgp.shape)
imgRGBTopo  = vigra.colors.transform_Lab2RGB(imgTopo)
gradTopo 	= vigra.filters.gaussianGradientMagnitude(imgTopo,1.0)
labelsTopo  = vigra.sampling.resize(seg.astype(numpy.float32),cgp.shape,0)
Example #44
0
import vigra
import numpy
import phist
import scipy.ndimage
import pylab

img = vigra.readImage('108073.jpg')  #[0:100,0:200,0:3]
"""

h0 = phist.histogram(image=img,r=3)
h1 = phist.histogram(image=img,sigma=1.0,r=3)
h2 = phist.histogram(image=img,sigma=[1.0,1.0],r=3)



h0 = h0.reshape([img.shape[0],img.shape[1],-1])
h1 = h1.reshape([img.shape[0],img.shape[1],-1])
h2 = h2.reshape([img.shape[0],img.shape[1],-1])


for x in range(0,h0.shape[2]):

	f = pylab.figure()

	f.add_subplot(2, 2, 1)  # this line outputs images side-by-side
	pylab.imshow(numpy.swapaxes(img/255.0,0,1),cmap='jet')

	hImg = h0[:,:,x].copy()
	hImg-=hImg.min()
	hImg/=hImg.max()
	f.add_subplot(2, 2, 2)  # this line outputs images side-by-side
Example #45
0
import opengm
import vigra
import numpy
import time
import sys

fname = "135069.jpg"
img = vigra.readImage(fname)
img = numpy.sum(img, axis=2)
img = vigra.resize(img, [s / 1 for s in img.shape])
noise = numpy.random.random(img.size).reshape(img.shape) * 255
print noise.shape
img += noise
img -= img.min()
img /= img.max()
print "shape", img.shape
vigra.imshow(img)
#vigra.show()

threshold = 0.24
labelsNaive = img > threshold
vigra.imshow(labelsNaive)
#vigra.show()

nVar = img.size
nLabelsPerVar = 2
variableSpace = numpy.ones(nVar) * nLabelsPerVar
gm = opengm.gm(variableSpace)

t0 = time.time()
# add unaries
Example #46
0
import vigra
import numpy
from collections import OrderedDict
def normByQuantile(a,qs):
    a = numpy.clip(a,qs[0],qs[1])
    a -=a.min()
    a /-a.max()
    return a

fFront = "/media/tbeier/309AF4254C0E5431/hhess_2nm/complete_dataset/2x2x2nm/2x2x2nm.0500.tif"
fMid = "/media/tbeier/309AF4254C0E5431/hhess_2nm/complete_dataset/2x2x2nm/2x2x2nm.2500.tif"
fBack = "/media/tbeier/309AF4254C0E5431/hhess_2nm/complete_dataset/2x2x2nm/2x2x2nm.3275.tif"

paths = [fFront,fMid,fBack]
imgs = [vigra.readImage(p) for p in paths]
nimgs = []
for img in imgs:
    s3 = [int(s/10) for s in img.shape[0:2]]
    print s3
    vigra.sampling.resize(img.astype('float32'), s3)

    #imgSmoothed = vigra.filters.gaussianSmoothing(img,sigma=5.0)
    imgSmoothed = img
    lq = vigra.filters.discRankOrderFilter(imgSmoothed.astype('uint8'),50, 0.01)
    hq = vigra.filters.discRankOrderFilter(imgSmoothed.astype('uint8'),50, 0.99)

    #lq = vigra.filters.gaussianSmoothing(lq.astype('float32'),sigma=10.0)
    #hq = vigra.filters.gaussianSmoothing(hq.astype('float32'),sigma=10.0)

    lq = vigra.sampling.resize(lq.astype('float32'), img.shape[0:2])
    hq = vigra.sampling.resize(hq.astype('float32'), img.shape[0:2])
Example #47
0
def batch_predict(args, ilastik_args):
    """Do the batch prediction.

    :param args: command line arguments
    :param ilastik_args: additional ilastik arguments
    """
    # Create the folder for the intermediate results.
    if not os.path.isdir(args.cache):
        os.makedirs(args.cache)

    # Find the random forest files.
    rf_files = autocontext_forests(args.batch_predict)
    n = len(rf_files)

    # Get the output format arguments.
    default_output_format = "hdf5"
    default_output_filename_format = os.path.join(args.cache,
                                                  "{nickname}_probs.h5")
    ilastik_parser = argparse.ArgumentParser()
    ilastik_parser.add_argument("--output_format",
                                type=str,
                                default=default_output_format)
    ilastik_parser.add_argument("--output_filename_format",
                                type=str,
                                default=default_output_filename_format)
    ilastik_parser.add_argument("--output_internal_path",
                                type=str,
                                default=default_export_key())
    format_args, ilastik_args = ilastik_parser.parse_known_args(ilastik_args)
    output_formats = [default_output_format] * (n - 1) + [
        format_args.output_format
    ]
    if args.no_overwrite:
        output_filename_formats = [
            default_output_filename_format[:-3] + "_%s" % str(i).zfill(2) +
            default_output_filename_format[-3:] for i in xrange(n - 1)
        ] + [format_args.output_filename_format]
    else:
        output_filename_formats = [default_output_filename_format] * (
            n - 1) + [format_args.output_filename_format]
    output_internal_paths = [default_export_key()] * (n - 1) + [
        format_args.output_internal_path
    ]

    # Reshape the data to tzyxc and move it to the cache folder.
    outfiles = []
    keep_channels = None
    for i in xrange(len(args.files)):
        # Read the data and attach axistags.
        filename = args.files[i]
        if ".h5/" in filename or ".hdf5/" in filename:
            data_key = os.path.basename(filename)
            data_path = filename[:-len(data_key) - 1]
            data = vigra.readHDF5(data_path, data_key)
        else:
            data_key = default_export_key()
            data_path_base, data_path_ext = os.path.splitext(filename)
            data_path = data_path_base + ".h5"
            data = vigra.readImage(filename)
        if not hasattr(data, "axistags"):
            default_tags = {1: "x", 2: "xy", 3: "xyz", 4: "xyzc", 5: "txyzc"}
            data = vigra.VigraArray(data,
                                    axistags=vigra.defaultAxistags(
                                        default_tags[len(data.shape)]),
                                    dtype=data.dtype)
        new_data = reshape_tzyxc(data)

        if i == 0:
            c_index = new_data.axistags.index("c")
            keep_channels = new_data.shape[c_index]

        # Save the reshaped dataset.
        output_filename = os.path.split(data_path)[1]
        output_filename = os.path.join(args.cache, output_filename)
        vigra.writeHDF5(new_data,
                        output_filename,
                        data_key,
                        compression=args.compression)
        args.files[i] = output_filename + "/" + data_key
        if args.no_overwrite:
            outfiles.append([
                os.path.splitext(output_filename)[0] +
                "_probs_%s.h5" % str(i).zfill(2) for i in xrange(n - 1)
            ])
        else:
            outfiles.append(
                [os.path.splitext(output_filename)[0] + "_probs.h5"] * (n - 1))
    assert keep_channels > 0

    # Run the batch prediction.
    for i in xrange(n):
        rf_file = rf_files[i]
        output_format = output_formats[i]
        output_filename_format = output_filename_formats[i]
        output_internal_path = output_internal_paths[i]

        filename_key = os.path.basename(args.files[0])
        filename_path = args.files[0][:-len(filename_key) - 1]

        # Quick hack to prevent the ilastik error "wrong number of channels".
        p = ILP(rf_file, args.cache, compression=args.compression)
        for j in xrange(p.data_count):
            p.set_data_path_key(j, filename_path, filename_key)

        # Call ilastik to run the batch prediction.
        cmd = [
            args.ilastik, "--headless",
            "--project=%s" % rf_file,
            "--output_format=%s" % output_format,
            "--output_filename_format=%s" % output_filename_format,
            "--output_internal_path=%s" % output_internal_path
        ]

        if args.predict_file:
            pfile = os.path.join(args.cache, "predict_file.txt")
            with open(pfile, "w") as f:
                for pf in args.files:
                    f.write(os.path.abspath(pf) + "\n")
            cmd.append("--predict_file=%s" % pfile)
        else:
            cmd += args.files

        print col.Fore.GREEN + "- Running autocontext batch prediction round %d of %d -" % (
            i + 1, n) + col.Fore.RESET
        subprocess.call(cmd, stdout=sys.stdout)

        if i < n - 1:
            # Merge the probabilities back to the original file.
            for filename, filename_out in zip(args.files, outfiles):
                filename_key = os.path.basename(filename)
                filename_path = filename[:-len(filename_key) - 1]
                merge_datasets(filename_path,
                               filename_key,
                               filename_out[i],
                               output_internal_path,
                               n=keep_channels,
                               compression=args.compression)
Example #48
0
                    f_cur = np.array([
                        feats_cur[feat_class.feats_name][label_cur]
                    ]).flatten()
                    f_next = np.array([
                        feats_next_subset_best[feat_class.feats_name]
                    ]).reshape((-1, f_cur.shape[0]))
                result[name][label_cur] = feat_class.compute(f_cur, f_next)

        return result


if __name__ == '__main__':
    import vigra
    import numpy as np

    img_cur = vigra.readImage('/home/mschiegg/tmp/segmentImage.tif')
    img_next = img_cur

    labels_cur = vigra.analysis.labelImage(img_cur)
    feats_cur = vigra.analysis.extractRegionFeatures(
        labels_cur.astype(np.float32),
        labels_cur.astype(np.uint32),
        features='all',
        ignoreLabel=0)

    feat_names = [
        'ParentChildrenRatio_Count', 'ParentChildrenRatio_Mean',
        'ChildrenRatio_Count', 'ChildrenRatio_Mean',
        'ParentChildrenAngle_RegionCenter', 'ChildrenRatio_SquaredDistances'
    ]
    fm = FeatureManager()
Example #49
0
# test copying / init from map data with disconnected contours:
import copy
om = copy.copy(map)
assert om.__getstate__()[:6] == map.__getstate__()[:6]
assert om.checkConsistency(), "map inconsistent"
assert maputils.checkLabelConsistency(om), "om.labelImage() inconsistent"

# --------------------------------------------------------------------
# 				   simple map, WatershedStatistics
# --------------------------------------------------------------------

from vigra import readImage, resizeImageSplineInterpolation, transformImage, gaussianGradientAsVector, gaussianGradientMagnitude, SplineImageView5
from geomap import SubPixelWatersheds5

img = readImage("test_blox.png")
img = resizeImageSplineInterpolation(img, Size2D(128, 128))
img.gm = gaussianGradientMagnitude(img, 1.0)
# img.grad = gaussianGradientAsVector(img, 1.0)
# img.gm = transformImage(img.grad, "\l x: norm(x)", {})
img.gm.siv = SplineImageView5(img.gm)
img.spws = SubPixelWatersheds5(img.gm)

import maputils
maxima, flowlines = maputils.subpixelWatershedData(
    img.spws,
    img.gm.siv,
    maputils.PassValueFilter(img.gm.siv, 0.7),
    perpendicularDistEpsilon=None)

map = GeoMap(maxima, [], img.size())
Example #50
0
def map_from_boundaries(boundaries_filename, cornerType = cellimage.CellType.Line):
    edgeImage = vigra.readImage(boundaries_filename) > 0
    gm = cellimage.GeoMap(edgeImage.astype(numpy.float32), 1, cornerType)
    print boundaries_filename, len(gm.nodes), len(gm.edges), len(gm.faces)
    return gm
Example #51
0
 def testHeadlineMap(self):
     labels = vigra.readImage("headline.png")[0]
     cem = crackEdgeMap(labels)
     self.assertEqual(cem.nodeCount, 11)
     self.assertEqual(cem.edgeCount, 13)
     self.assertEqual(cem.faceCount, 11)
def gtToRagGt(rag, gts):
    ragGts = []
    for gt in gts:
        ragGt,q = rag.projectBaseGraphGt(gt.astype('uint32'))
        ragGt = ragGt.astype('uint32')
        ragGts.append(ragGt[:,None])
    ragGts = numpy.concatenate(ragGts,axis=1)
    return ragGts

folder = "train"
fileNumber = "12003" 

gtFile = "/home/tbeier/datasets/BSR/BSDS500/data/groundTruth/%s/%s.mat"%(folder,fileNumber)
imageFile = "/home/tbeier/datasets/BSR/BSDS500/data/images/%s/%s.jpg"%(folder,fileNumber)

imgRgb = vigra.readImage(imageFile)
shape = imgRgb.shape[0:2]
sp,nSeg = vigra.analysis.slicSuperpixels(imgRgb, intensityScaling=50.0, seedDistance=2)
sp-=1
gg = vigra.graphs.gridGraph(shape)
rag = vigra.graphs.regionAdjacencyGraph(gg, sp)

agraphGG = agraph.gridGraph(shape)

print rag.nodeNum, rag.nodeNum**2


gts = matToGt(sio.loadmat(gtFile))
ragGts = gtToRagGt(rag, gts).astype('uint64')

# convert vigra graph to agraph
import vigra
import graph as agraph

show = False
verbose = 1

img = vigra.readImage('12074.jpg')  #[120:300,0:100,:]
shape = img.shape[0:2]
shape = [s / 2 for s in shape]

timg = vigra.sampling.resize(img, shape)

# get orientation

tensor = vigra.filters.structureTensor(timg, 1.0, 2.0)
eigenrep = vigra.filters.tensorEigenRepresentation2D(tensor)

# print get oriented repulsive edges
edgePmap = eigenrep[:, :, 0].copy()
edgePmap -= edgePmap.min()
edgePmap /= edgePmap.max()

graph = agraph.gridGraph(shape)
model = agraph.liftedMcModel(graph)

with vigra.Timer("add long range edges"):
    agraph.addLongRangeEdges(model, edgePmap, 0.5, 2, 5)

settings = agraph.settingsParallelLiftedMc(model)
solver = agraph.parallelLiftedMc(model, settings)
Example #54
0
import opengm
import vigra
import numpy
import sys

if __name__ == "__main__":
    args = sys.argv

    if len(args) != 8:
        print "Usage: ", args[
            0], " infile hdf5-outfile red green blue T lambda"
        sys.exit(0)

    img = vigra.readImage(args[1])

    if img.shape[2] != 3:
        print "Image must be RGB"
        sys.exit(0)

    T = float(args[6])
    beta = float(args[7])

    imgFlat = img.reshape([-1, 3]).view(numpy.ndarray)
    numVar = imgFlat.shape[0]

    gm = opengm.gm(numpy.ones(numVar, dtype=opengm.label_type) * 2)

    protoColor = numpy.array([args[3], args[4], args[5]],
                             dtype=opengm.value_type).reshape([3, -1])
    protoColor = numpy.repeat(protoColor, numVar, axis=1).swapaxes(0, 1)
    diffArray = numpy.sum(numpy.abs(imgFlat - protoColor), axis=1)
Example #55
0
import vigra
import numpy
import matplotlib.pyplot as plt

f = "islands.png"
cmap = plt.get_cmap("afmhot")

# Compute labeled iamge.
img = numpy.squeeze(vigra.readImage(f))
lbl = numpy.array(vigra.analysis.labelImage(img))

# Compute the eccentricity transform.
ecc = vigra.filters.eccentricityTransform(lbl)
plt.imshow(numpy.swapaxes(ecc, 1, 0), cmap=cmap)
plt.show()

# Compute the eccentricity centers and draw them into the image.
centers = vigra.filters.eccentricityCenters(lbl)
m = ecc.max()
for c in centers[1:]:
    ecc[c] = m
plt.imshow(numpy.swapaxes(ecc, 1, 0), cmap=cmap)
plt.show()

# # Compute the transformation and the centers in one step:
# ecc, centers = vigra.filters.eccentricityTransformWithCenters(lbl)
def calculate_distances():

    """
    compute distances between color markers instead of existing synapses
    markers of the same color should be in the same neuron
    """

    files_2d = glob.glob(inputdir + d2_pattern)
    files_2d = sorted(files_2d, key=str.lower)

    files_3d = glob.glob(inputdir + d3_pattern)
    files_3d = sorted(files_3d, key=str.lower)

    files_markers = glob.glob(inputdir + marker_pattern)
    files_markers = sorted(files_markers, key=str.lower)

    debug_dirs = glob.glob(debugdir)
    debug_dirs = sorted(debug_dirs, key=str.lower)

    files_raw = glob.glob(inputdir + raw_pattern)
    files_raw = sorted(files_raw, key=str.lower)

    #print files_2d, files_3d, files_markers, debug_dirs, files_raw


    first = 0
    last = 4
    all_distances_same = []
    all_distances_diff = []
    nsamesame = 0
    nsamediff = 0
    ndiffdiff = 0
    ndiffsame = 0

    for f2name, f3name, mname, ddir, rawname in zip(files_2d[first:last], files_3d[first:last],
                                                    files_markers[first:last], debug_dirs[first:last],
                                                    files_raw[first:last]):


        print "processing files:"
        print f2name
        print f3name
        print mname
        print ddir
        print rawname

        tempGraph = Graph()
        edgeIndicators = []
        instances = []

        if debug_images:
            rawim = vigra.readImage(rawname)
            vigra.impex.writeImage(rawim, ddir + "/raw.tiff")

        #print "processing files:", f2name, f3name, mname

        f2 = h5py.File(f2name)
        f3 = h5py.File(f3name)


        if use_2d_only:
            d2 = f2["exported_data"][5, :, :, 0]
            d3 = f2["exported_data"][5, :, :, 2]
        else:
            d2 = f2["exported_data"][..., 0]
            d3 = f3["exported_data"][5, :, :, 2] # 5 because we only want the central slice, there are 11 in total
            d3 = d3.swapaxes(0, 1)

        # print d2.shape, d3.shape

        combined = d2 + d3
        if use_2d_only:
            #convert to float
            combined = combined.astype(numpy.float32)
            combined = combined/255.

        markedNodes = extractMarkedNodes(mname)
        # print
        opUpsample = OpUpsampleByTwo(graph=tempGraph)
        combined = numpy.reshape(combined, combined.shape + (1,) + (1,))

        combined = combined.view(vigra.VigraArray)
        combined.axistags = vigra.defaultAxistags('xytc')
        opUpsample.Input.setValue(combined)
        upsampledMembraneProbs = opUpsample.Output[:].wait()
        # get rid of t
        upsampledMembraneProbs = upsampledMembraneProbs[:, :, 0, :]
        upsampledMembraneProbs = upsampledMembraneProbs.view(vigra.VigraArray)
        upsampledMembraneProbs.axistags = vigra.defaultAxistags('xyc')

        # try to filter
        upsampledSmoothedMembraneProbs = computeDistanceRaw(upsampledMembraneProbs, 1.6, ddir)
        upsampledMembraneProbs = filter_by_size(upsampledSmoothedMembraneProbs, ddir)
        upsampledMembraneProbs = upsampledMembraneProbs.view(vigra.VigraArray)
        upsampledMembraneProbs.axistags = vigra.defaultAxistags('xyc')


        edgeIndicators.append(computeDistanceHessian(upsampledMembraneProbs, 5.0, ddir))
        edgeIndicators.append(upsampledSmoothedMembraneProbs)

        segm = superpixels(combined.squeeze())
        if debug_images:
            vigra.impex.writeImage(segm, ddir + "/superpixels.tiff")

        gridGr = graphs.gridGraph((d2.shape[0], d2.shape[1]))  # !on original pixels

        for iind, indicator in enumerate(edgeIndicators):
            gridGraphEdgeIndicator = graphs.edgeFeaturesFromInterpolatedImage(gridGr, indicator)
            instance = vigra.graphs.ShortestPathPathDijkstra(gridGr)
            instances.append(instance)
            distances_same = []
            distances_diff = []
            for color, points in markedNodes.iteritems():
                #going over points of *same* color
                if len(points)>1:
                    print "Processing color", color
                for i in range(len(points)):
                    node = map(long, points[i])
                    sourceNode = gridGr.coordinateToNode(node)
                    instance.run(gridGraphEdgeIndicator, sourceNode, target=None)
                    distances_all = instance.distances()
                    sp_this = segm[node[0], node[1]]
                    for j in range(i + 1, len(points)):
                        # go over points of the same color

                        other_node = map(long, points[j])
                        distances_same.append(distances_all[other_node[0], other_node[1]])

                        sp_other = segm[other_node[0], other_node[1]]
                        if sp_this==sp_other:
                            nsamesame = nsamesame + 1
                            #print "same color in the same superpixel!"
                        else:
                            nsamediff += 1
                        #targetNode = gridGr.coordinateToNode(other_node)
                        #path = instance.run(gridGraphEdgeIndicator, sourceNode).path(pathType='coordinates',
                        #                                                             target=targetNode)
                        #max_on_path = numpy.max(distances_all[path])
                        #min_on_path = numpy.min(distances_all[path])
                        # print max_on_path, min_on_path
                        # print path.shape
                        #print "distance b/w", node, other_node, " = ", distances_all[other_node[0], other_node[1]]

                    for newcolor, newpoints in markedNodes.iteritems():
                        # go over points of other colors
                        if color == newcolor:
                            continue
                        for newi in range(len(newpoints)):
                            other_node = map(long, newpoints[newi])
                            sp_other = segm[other_node[0], other_node[1]]
                            if sp_this==sp_other:
                                ndiffsame += 1
                            else:
                                ndiffdiff += 1
                            distances_diff.append(distances_all[other_node[0], other_node[1]])

                    # highlight the source point in image
                    distances_all[node[0], node[1]] = numpy.max(distances_all)
                    outfile = ddir + "/" + str(node[0]) + "_" + str(node[1]) + "_" + str(iind) + ".tiff"
                    vigra.impex.writeImage(distances_all, outfile)

            while len(all_distances_diff)<len(edgeIndicators):
                all_distances_diff.append([])
                all_distances_same.append([])

            all_distances_diff[iind].extend(distances_diff)
            all_distances_same[iind].extend(distances_same)
            #print "summary for edge indicator:", iind
            #print "points of same color:", distances_same
            #print "points of other colors:", distances_diff
                    # print distances_same

                    # vigra.impex.writeImage(combined, f2name+"_combined.tiff")
                    # vigra.impex.writeImage(d3, f2name+"_synapse.tiff")
                    # vigra.impex.writeImage(d2, f2name+"_membrane.tiff")


    print "same color in the same superpixels:", nsamesame
    print "same color, different superpixels:", nsamediff
    print "diff color, same superpixel:", ndiffsame
    print "diff color, diff superpixels", ndiffdiff

    analyze_distances(all_distances_same, all_distances_diff)
Example #57
0
def imgStd(img,simga):

    average = vigra.filters.gaussianSmoothing(img,simga)

    return (average-img)**2


def imgStd2(img,s=5,et=2.0):

    imgd      = vigra.filters.nonlinearDiffusion(img,scale=float(s),edgeThreshold=float(et))
    return (imgd-img)**2




img       = numpy.squeeze(vigra.readImage(img))#[0:75,0:75,:]
#std       = imgStd(img,5.0)
#std2      = imgStd2(img,20.0,2.5)



def blobFinder(img):
    shape = img.shape[0:2]

    scales = numpy.arange(0.5,7.0,0.5)
    nScales = len(scales)

    bank  = numpy.zeros(shape+(nScales,))


    for si,scale in enumerate(scales):
Example #58
0
 def testComplexImage(self):
     labels = vigra.analysis.labelImageWithBackground(
         vigra.readImage("crackConvert-test1.png")[0].astype(int), 8,
         0)[0]
     cem = crackEdgeMap(labels)
     self.assertEqual(cem.faceCount, 26)