Example #1
0
def ucm_to_raveler(ucm, sp_threshold=0.0, body_threshold=0.1, **kwargs):
    """Return Raveler map from a UCM.
    
    Parameters
    ----------
    ucm : numpy ndarray, shape (M, N, P)
        An ultrametric contour map. This is a map of scored segment boundaries
        such that if A, B, and C are segments, then 
        score(A, B) = score(B, C) >= score(A, C), for some permutation of
        A, B, and C.
        A hierarchical agglomeration process produces a UCM.
    sp_threshold : float, optional (default: 0.0)
        The value for which to threshold the UCM to obtain the superpixels.
    body_threshold : float, optional (default: 0.1)
        The value for which to threshold the UCM to obtain the segments/bodies.
        The condition `body_threshold >= sp_threshold` should hold in order
        to obtain sensible results.
    **kwargs : dict, optional
        Keyword arguments to be passed through to `segs_to_raveler`.

    Returns
    -------
    superpixels : numpy ndarray, shape (M, N, P)
        The superpixel map. Non-zero superpixels are unique to each plane.
        That is, `np.unique(superpixels[i])` and `np.unique(superpixels[j])` 
        have only 0 as their intersection.
    sp_to_segment : numpy ndarray, shape (Q, 3)
        The superpixel to segment map. Segments are unique to each plane. The
        first number on each line is the plane number.
    segment_to_body : numpy ndarray, shape (R, 2)
        The segment to body map.
    """
    sps = label(ucm < sp_threshold)[0]
    bodies = label(ucm <= body_threshold)[0]
    return segs_to_raveler(sps, bodies, **kwargs)
Example #2
0
    def label_clusters(self):
        # by default only fully connected voxels and no diagonal connections
        # if needed structure must be passed, eg structure = np.ones((3,3,3))
        self.cluster_array_labelled, self.cluster_count = label(self.cluster_array_bool)
        # filter out clusters smaller than threshold
        if self.min_cluster_size is not None:
            # loop over clusters
            for i in range(self.cluster_count):
                n_cluster = i + 1
                cluster_ids = np.where(self.cluster_array_labelled == n_cluster)
                cluster_size = cluster_ids[0].size
                # if cluster below limit set it to np.nan in cluster_array
                if cluster_size < self.min_cluster_size:
                    self.cluster_array_labelled[cluster_ids] = 0
            self.cluster_array_labelled, self.cluster_count = label(self.cluster_array_labelled)

        if self.cluster_count == 0:
            raise NoClustersError('Exiting PearsonMerger: parameters too strict, no clusters detectable!')
        # overwrite zeros with nan for plotting
        zeros_ids = np.where(self.cluster_array_labelled == 0)
        # doesnt work on int array, thus convert to float type
        self.cluster_array_labelled = self.cluster_array_labelled.astype('float')
        self.cluster_array_labelled[zeros_ids] = np.nan
        self.cluster_img = nib.Nifti1Image(self.cluster_array_labelled,
                                           self.affine)
Example #3
0
def formatTS(start, stop, r, useIntersects=True):
    aStarts = {}
    X = []
    Y = []
    coords = []
    sigNum = []
    strcArr = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
    for sig in xrange(start, stop):
        if sig % 50 == 0:
            print sig
        path = np.array(sigs[sig])
        imgO = imread(getFilePath(sig), as_grey=True)
        xs = [n for n in xrange()]
        plt.subplot(1, 2, 1)
        plt.imshow(imgO)
        plt.subplot(1, 2, 2)
        plt.imshow(guassian_filter(imgO, 1))
        plt.show()
        thresh = 0.9
        img = gaussian_filter(imgO, 1) < thresh
        imgX, imgY = np.nonzero(img)
        imgW = imgX.max() - imgX.min()
        imgH = imgY.max() - imgY.min()
        img = skeletonize(img)
        img = img[imgX.min() : imgX.max(), imgY.min() : imgY.max()]
        skltnSegs, nSkltnSegs = label(img, structure=strcArr)
        # print nSkltnSegs
        # plt.imshow(skltnSegs)
        # plt.show()
        imgO = imgO[imgX.min() : imgX.max(), imgY.min() : imgY.max()]
        sumArr = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
        summed = convolve(img, sumArr, mode="constant", cval=0)
        corners = (summed == 2) & (img == 1)
        startx = path[0][0] * img.shape[1]
        starty = path[0][1] * img.shape[0]
        aStarts[sig] = [startx, starty]
        if useIntersects:
            intersects = (summed >= 4) & (img == 1)
            labeled, nLabels = label(intersects, structure=strcArr)
            itrscts = []
            for l in xrange(1, nLabels + 1):
                intersect = np.array((labeled == l), dtype=int)
                posX, posY = np.nonzero(intersect)
                xC, yC = np.array([[np.sum(posX) / posX.size], [np.sum(posY) / posY.size]])
                itrscts.append([xC[0], yC[0]])
            itrscts = np.array(itrscts)
        corners = np.transpose(np.array(np.nonzero(corners)))
        if useIntersects:
            try:
                corners = np.vstack((itrscts, corners))
            except:
                print "something went wrong at", sig
        corners[:, [0, 1]] = corners[:, [1, 0]]
        for i, corner in enumerate(corners):
            x, y = corner[0], corner[1]
            x = getFeatures(imgO, x, y, r, imgH, imgW, skltnSegs)
            X.append(x)
            sigNum.append(sig)
            coords.append([x, y])
    return np.array(X), np.array(sigNum), np.array(coords)
def getPara(predict, true, threshold, resolution, windowsize):
    (TP, FP, TN, FN, class_lable) = perf_measure(true, predict, threshold)
    if((TP + FN) == 0):
        TPR = 0
    else:
        TPR = np.float(TP) / (TP + FN)

    class_lable = class_lable.astype(bool).reshape(250,  130)
    true = true.astype(bool).reshape((250,  130))

    num = 2
    x = np.arange( -num , num+1, 1)
    xx, yy  = np.meshgrid( x, x )
    struc = (xx * xx + yy * yy)<= num * num
    class_lable = binary_dilation(class_lable, struc)
    class_lable = binary_erosion(class_lable, struc)

    # predict2 = remove_small_objects(class_lable, windowsize * resolution, in_place=False)
    predict2 = remove_small_objects(class_lable, windowsize, in_place=False)
    labeled_array1, num_features1 = label(predict2)
    labeled_array2, num_features2 = label(true)
    FP_num = num_features1 - num_features2
    if FP_num < 0:
        FP_num = 0
    return TPR, FP_num
Example #5
0
def segs_to_raveler(sps, bodies, **kwargs):
    import morpho
    min_sp_size = kwargs.get('min_sp_size', 16)
    sps_out = []
    sps_per_plane = []
    sp_to_segment = []
    segment_to_body = [array([[0,0]])]
    total_nsegs = 0
    for i, (sp_map, body_map) in enumerate(zip(sps, bodies)):
        sp_map, nsps = label(
            morpho.remove_small_connected_components(sp_map, min_sp_size, True)
        )
        segment_map, nsegs = label(body_map)
        segment_map += total_nsegs
        segment_map *= sp_map.astype(bool)
        total_nsegs += nsegs
        sps_out.append(sp_map[newaxis,...])
        sps_per_plane.append(nsps)
        valid = (sp_map != 0) + (segment_map == 0)
        sp_to_segment.append(unique(
            zip(it.repeat(i), sp_map[valid], segment_map[valid])))
        valid = segment_map != 0
        logging.debug('plane %i done'%i)
        segment_to_body.append(unique(
                                zip(segment_map[valid], body_map[valid])))
    logging.info('total superpixels before: ' + str(len(unique(sps))) +
                    'total superpixels after: ' + str(sum(sps_per_plane)))
    sps_out = concatenate(sps_out, axis=0)
    sp_to_segment = concatenate(sp_to_segment, axis=0)
    segment_to_body = concatenate(segment_to_body, axis=0)
    return sps_out, sp_to_segment, segment_to_body
Example #6
0
def split_exclusions(image, labels, exclusions, dilation=0, connectivity=1,
    standard_seeds=False):
    """Ensure that no segment in 'labels' overlaps more than one exclusion."""
    labels = labels.copy()
    cur_label = labels.max()
    dilated_exclusions = exclusions.copy()
    foot = generate_binary_structure(exclusions.ndim, connectivity)
    for i in range(dilation):
        dilated_exclusions = grey_dilation(exclusions, footprint=foot)
    hashed = labels * (exclusions.max() + 1) + exclusions
    hashed[exclusions == 0] = 0
    violations = bincount(hashed.ravel()) > 1
    violations[0] = False
    if sum(violations) != 0:
        offending_labels = labels[violations[hashed]]
        mask = zeros(labels.shape, dtype=bool)
        for offlabel in offending_labels:
            mask += labels == offlabel
        if standard_seeds:
            seeds = label(mask * (image == 0))[0]
        else:
            seeds = label(mask * dilated_exclusions)[0]
        seeds[seeds > 0] += cur_label
        labels[mask] = watershed(image, seeds, connectivity, mask)[mask]
    return labels
Example #7
0
def __distinct_binary_object_correspondences(reference, result, connectivity=1):
    """
    Determines all distinct (where connectivity is defined by the connectivity parameter
    passed to scipy's `generate_binary_structure`) binary objects in both of the input
    parameters and returns a 1to1 mapping from the labelled objects in reference to the
    corresponding (whereas a one-voxel overlap suffices for correspondence) objects in
    result.

    All stems from the problem, that the relationship is non-surjective many-to-many.

    @return (labelmap1, labelmap2, n_lables1, n_labels2, labelmapping2to1)
    """
    result = np.atleast_1d(result.astype(np.bool))
    reference = np.atleast_1d(reference.astype(np.bool))

    # binary structure
    footprint = generate_binary_structure(result.ndim, connectivity)

    # label distinct binary objects
    labelmap1, n_obj_result = label(result, footprint)
    labelmap2, n_obj_reference = label(reference, footprint)

    # find all overlaps from labelmap2 to labelmap1; collect one-to-one relationships and store all one-two-many for later processing
    slicers = find_objects(labelmap2)  # get windows of labelled objects
    mapping = dict()  # mappings from labels in labelmap2 to corresponding object labels in labelmap1
    used_labels = set()  # set to collect all already used labels from labelmap2
    one_to_many = list()  # list to collect all one-to-many mappings
    for l1id, slicer in enumerate(slicers):  # iterate over object in labelmap2 and their windows
        l1id += 1  # labelled objects have ids sarting from 1
        bobj = (l1id) == labelmap2[slicer]  # find binary object corresponding to the label1 id in the segmentation
        l2ids = np.unique(labelmap1[slicer][
                                 bobj])  # extract all unique object identifiers at the corresponding positions in the reference (i.e. the mapping)
        l2ids = l2ids[0 != l2ids]  # remove background identifiers (=0)
        if 1 == len(
                l2ids):  # one-to-one mapping: if target label not already used, add to final list of object-to-object mappings and mark target label as used
            l2id = l2ids[0]
            if not l2id in used_labels:
                mapping[l1id] = l2id
                used_labels.add(l2id)
        elif 1 < len(l2ids):  # one-to-many mapping: store relationship for later processing
            one_to_many.append((l1id, set(l2ids)))

    # process one-to-many mappings, always choosing the one with the least labelmap2 correspondences first
    while True:
        one_to_many = [(l1id, l2ids - used_labels) for l1id, l2ids in
                       one_to_many]  # remove already used ids from all sets
        one_to_many = [x for x in one_to_many if x[1]]  # remove empty sets
        one_to_many = sorted(one_to_many, key=lambda x: len(x[1]))  # sort by set length
        if 0 == len(one_to_many):
            break
        l2id = one_to_many[0][1].pop()  # select an arbitrary target label id from the shortest set
        mapping[one_to_many[0][0]] = l2id  # add to one-to-one mappings
        used_labels.add(l2id)  # mark target label as used
        one_to_many = one_to_many[1:]  # delete the processed set from all sets

    return labelmap1, labelmap2, n_obj_result, n_obj_reference, mapping
Example #8
0
def label(image,**kw):
    """Redefine the scipy.ndimage.measurements.label function to
    work with a wider range of data types.  The default function
    is inconsistent about the data types it accepts on different
    platforms."""
    try: return measurements.label(image,**kw)
    except: pass
    types = ["int32","uint32","int64","unit64","int16","uint16"]
    for t in types:
        try: return measurements.label(array(image,dtype=t),**kw) 
        except: pass
    # let it raise the same exception as before
    return measurements.label(image,**kw)
def threshold_components(A, d1, d2, medw = (3,3), thr = 0.9999, se = np.ones((3,3),dtype=np.int), ss = np.ones((3,3),dtype=np.int)):
        
    from scipy.ndimage.filters import median_filter
    from scipy.ndimage.morphology import binary_closing    
    from scipy.ndimage.measurements import label    
    
    d, nr = np.shape(A)
    Ath = np.zeros((d,nr))
    
    for i in range(nr):
        A_temp = np.reshape(A[:,i],(d2,d1))
        A_temp = median_filter(A_temp,medw)
        Asor = np.sort(np.squeeze(np.reshape(A_temp,(d,1))))[::-1]
        temp = np.cumsum(Asor**2)
        ff = np.squeeze(np.where(temp<(1-thr)*temp[-1]))
        if ff.size > 0:
            ind = ff[-1]
            A_temp[A_temp<Asor[ind]] = 0
            BW = (A_temp>=Asor[ind])
        else:
            BW = (A_temp>=0)
            
        Ath[:,i] = np.squeeze(np.reshape(A_temp,(d,1)))
        BW = binary_closing(BW.astype(np.int),structure = se)
        labeled_array, num_features = label(BW, structure=ss)
        BW = np.reshape(BW,(d,1))
        labeled_array = np.squeeze(np.reshape(labeled_array,(d,1)))
        nrg = np.zeros((num_features,1))
        for j in range(num_features):
            nrg[j] = np.sum(Ath[labeled_array==j+1,i]**2)
            
        indm = np.argmax(nrg)
        Ath[labeled_array==indm+1,i] = A[labeled_array==indm+1,i]
        
    return Ath
Example #10
0
    def snippets_except_labels(self, exclude_labels=None):
        """
        Returns list of snippets which have labels not in the 'exclude_labels'
        list
        TODO - perhaps this should instead return the entire part?
        if exclude_labels is None, then return *all* unlabelled sections
        """

        if isinstance(exclude_labels, basestring):
            exclude_labels = [exclude_labels]

        # create vector which is one whenever one of the exclude labels occurs
        is_label = np.zeros(self.series.shape[0])

        for annotation in self.annotations:
            if annotation['Label'] in exclude_labels:
                start_point = self.time_to_position(annotation['LabelStartTime_Seconds'])
                end_point = self.time_to_position(annotation['LabelEndTime_Seconds'])
                is_label[start_point:end_point] += 1

        # now extract the snippets from which there are no labels
        labs, nlabels = measurements.label(is_label==0)

        snippets = []
        for lab in range(1, nlabels+1):
            snippet = Wave(self.series[labs==lab], self.sample_rate)
            snippets.append(snippet)

        return snippets
Example #11
0
 def set_array(self, arr, **kwds):
     kwds['structure'] = kwds.get('structure', np.ones((3,3,3)))
     self._structure = kwds['structure'] # keep record of this
     self._arr = np.asarray(arr)
     labels, num = label(self._arr, **kwds)
     self._labels = labels
     self._nlabels = num
def findLabels(heatMap):
        # Visualize the heatmap when displaying    
    thresholdMap = np.clip(heatMap, 0, 255)
    # Find final boxes from heatmap using label function
    labels = label(thresholdMap) # labels[0] is the bit map, labelss[1] is the number of maps
    print("findLabels-len(labels):", len(labels), ", labels[1]:", labels[1])
    return labels
    def mcs_find(image, thresh=None):
        if not thresh:
            print('Give threshold')
            return

        image[image > thresh] = 0
        image[image <= thresh] = 1
        image[np.isnan(image)] = 0

        if np.sum(image<10):
            return []

        labels, numL = label(image)

        ret = []

        for l in np.unique(labels):
            if l == 0:
                continue

            blob = np.sum(labels == l)

            pdb.set_trace()

            if np.sum(len(blob[0])) < 100:  # at least 1000m2
                continue

            ret.append(blob*49)

        return ret
Example #14
0
def select_downcast(pressure, for_overturn_calcs=False):
    """Find indices of the downcast part of data"""
    # Take the derivative of the pressure profile
    dp = np.diff(pressure)
    # Constants for the filter
    B, A = signal.butter(2, 0.01, output='ba')
    # Filter the pressure derivative, to smooth out the curve
    dp_smooth = signal.filtfilt(B, A, dp)
    # Make the arrays the same size
    dp_smooth = np.append(dp_smooth, [0])
    # Find the indices where the descent rate is more than 0.05
    falling_inds = dp_smooth > 0.05

    if for_overturn_calcs:
        # For overturns, we want fall to be smooth.
        # Therefore, we want to exclude portions near the surface where
        # fall rate may drop below 0.05. In such cases without the code below
        # we would end up with discontinuous pieces of the profile
        inds_label = label(falling_inds)[0]
        inds_label_mode = mode(inds_label[falling_inds])[0]
        falling_inds[inds_label != inds_label_mode] = False

    falling_inds = np.where(falling_inds)[0]

    return falling_inds
Example #15
0
def _filter_small_slopes(hgt, dx, min_slope=0):
    """Masks out slopes with NaN until the slope if all valid points is at 
    least min_slope (in degrees).
    """

    min_slope = np.deg2rad(min_slope)
    slope = np.arctan(-np.gradient(hgt, dx))  # beware the minus sign
    # slope at the end always OK
    slope[-1] = min_slope

    # Find the locs where it doesn't work and expand till we got everything
    slope_mask = np.where(slope >= min_slope, slope, np.NaN)
    r, nr = label(~np.isfinite(slope_mask))
    for objs in find_objects(r):
        obj = objs[0]
        i = 0
        while True:
            i += 1
            i0 = objs[0].start-i
            if i0 < 0:
                break
            ngap =  obj.stop - i0 - 1
            nhgt = hgt[[i0, obj.stop]]
            current_slope = np.arctan(-np.gradient(nhgt, ngap * dx))
            if i0 <= 0 or current_slope[0] >= min_slope:
                break
        slope_mask[i0:obj.stop] = np.NaN
    out = hgt.copy()
    out[~np.isfinite(slope_mask)] = np.NaN
    return out
Example #16
0
def _get_map_cluster_sizes(map_):
    labels, num = measurements.label(map_)
    area = measurements.sum(map_, labels, index=np.arange(1, num + 1))
    if not len(area):
        return [0]
    else:
        return area.astype(int)
Example #17
0
def clean_cc_mask(mask):
    """
    Cleans a segmentation of the corpus callosum so no random pixels are included.

    Parameters
    ----------
    mask : ndarray
        Binary mask of the coarse segmentation.

    Returns
    -------
    new_cc_mask : ndarray
        Binary mask of the cleaned segmentation.
    """

    from scipy.ndimage.measurements import label

    new_cc_mask = np.zeros(mask.shape)

    # Flood fill algorithm to find contiguous regions.
    labels, numL = label(mask)

    volumes = [len(labels[np.where(labels == l_idx+1)]) for l_idx in np.arange(numL)]
    biggest_vol = np.arange(numL)[np.where(volumes == np.max(volumes))] + 1
    new_cc_mask[np.where(labels == biggest_vol)] = 1

    return new_cc_mask
Example #18
0
def translate_back(outputs,threshold=0.7):
    """Translate back. Thresholds on class 0, then assigns
    the maximum class to each region."""
    labels,n = measurements.label(outputs[:,0]<threshold)
    mask = tile(labels.reshape(-1,1),(1,outputs.shape[1]))
    maxima = measurements.maximum_position(outputs,mask,arange(1,amax(mask)+1))
    return [c for (r,c) in maxima]
Example #19
0
def erase_reg(arr, p, val=0):
    from scipy.ndimage.measurements import label

    labs, num = label(arr)
    aval = labs[p]
    idxs = np.where(labs == aval)
    arr[idxs] = val
Example #20
0
def calc_modes(N2, bottom_depth, z_bins):
    """Wave velocity and structure of first three modes"""

    dz = np.mean(np.diff(z_bins))

    # Truncate N2 to appropriate length based on depth and dz
    Nz = (bottom_depth/dz).astype(int)
    N2 = N2[:Nz]

    # Find indices of start and end of finite values
    finite_vals = nan_or_masked(N2) == 0
    labels = label(finite_vals)[0]
    main_data = np.where(labels == mode(labels[finite_vals]))[1]
    start_ind, end_ind = main_data[0], main_data[-1]

    # Fill in NaN values with start or end values
    N2[:start_ind] = N2[start_ind]
    N2[end_ind + 1:] = N2[end_ind]

    # Preallocate arrays for horizontal and vertical structure
    hori = np.full((len(z_bins) - 1, 3), np.nan)
    vert = hori.copy()

    hori[:len(N2), :], vert[:len(N2), :], c, _ = vertModes(N2, dz, 3)

    return hori, vert, c[:3]
Example #21
0
def manual_split(probs, seg, body, seeds, connectivity=1, boundary_seeds=None):
    """Manually split a body from a segmentation using seeded watershed.

    Input:
        - probs: the probability of boundary in the volume given.
        - seg: the current segmentation.
        - body: the label to be split.
        - seeds: the seeds for the splitting (should be just two labels).
        [-connectivity: the connectivity to use for watershed.]
        [-boundary_seeds: if not None, these locations become inf in probs.]
    Value:
        - the segmentation with the selected body split.
    """
    struct = generate_binary_structure(seg.ndim, connectivity)
    body_pixels = seg == body
    bbox = find_objects(body_pixels)[0]
    body_pixels = body_pixels[bbox]
    body_boundary = binary_dilation(body_pixels, struct) - body_pixels
    non_body_pixels = True - body_pixels - body_boundary
    probs = probs.copy()[bbox]
    probs[non_body_pixels] = probs.min()-1
    if boundary_seeds is not None:
        probs[boundary_seeds[bbox]] = probs.max()+1
    probs[body_boundary] = probs.max()+1
    seeds = label(seeds.astype(bool)[bbox], struct)[0]
    outer_seed = seeds.max()+1 # should be 3
    seeds[non_body_pixels] = outer_seed
    seg_new = watershed(probs, seeds, 
        dams=(seg==0).any(), connectivity=connectivity, show_progress=True)
    seg = seg.copy()
    new_seeds = unique(seeds)[:-1]
    for new_seed, new_label in zip(new_seeds, [0, body, seg.max()+1]):
        seg[bbox][seg_new == new_seed] = new_label
    return seg
def fix_elevations(z, riv_i, riv_j, ch_depth, sea_level, slope, dx, max_rand, SLRR):

    test_elev = z - sea_level
    max_cell_h = slope * dx
    riv_prof = test_elev[riv_i, riv_j]
    test_elev[riv_i, riv_j] += 2*ch_depth

    # set new subaerial cells to marsh elevation
    test_elev[test_elev == 0] = max_cell_h

    # make mask for depressions
    ocean_mask = test_elev < max_cell_h
    labeled_ponds, ocean = measurements.label(ocean_mask)

    # # fill in underwater spots that are below SL (?)
    # below_SL = [z <= sea_level]
    # underwater_cells, big_ocean = measurements.label(below_SL)
    # underwater_cells[underwater_cells == big_ocean] = 0
    # test_elev[underwater_cells > 0] = max_cell_h + SLRR + (np.random.rand() * max_rand)

    # create an ocean and shoreline mask
    ocean_and_shore = np.copy(labeled_ponds)

    # create an ocean mask
    # ocean_cells = np.copy(ocean_and_shore)
    # ocean_and_shore[test_elev > 0] = 0

    # create mask for pond cells and fix them
    area = measurements.sum(ocean_mask, labeled_ponds, index=np.arange(labeled_ponds.max() + 1))
    areaPonds = area[labeled_ponds]
    labeled_ponds[areaPonds == areaPonds.max()] = 0

    #finish creating ocean and shoreline mask
    ocean_and_shore[areaPonds != areaPonds.max()] = 0

    # something here to get rid of ocean cells
    test_elev[labeled_ponds > 0] = max_cell_h + SLRR + (np.random.rand() * max_rand)

    # raise cells close to sea level above it
    test_elev[(test_elev >= max_cell_h) & (test_elev <= (max_cell_h + SLRR))] = \
        (max_cell_h + SLRR + (np.random.rand() * max_rand))

    riv_buffer = np.zeros_like(test_elev)
    riv_buffer[riv_i, riv_j] = 1
    riv_buffer[riv_i[1:]-1, riv_j[1:]] = 1

    for i in xrange(1, test_elev.shape[0]-1):
        for j in xrange(test_elev.shape[1]):
            if (not ocean_and_shore[i, j]
                and not ocean_and_shore[i-1, j]
                and not ocean_and_shore[i+1, j]
                and not riv_buffer[i, j]):
                if test_elev[i+1, j] >= test_elev[i, j]:
                    test_elev[i, j] = test_elev[i+1, j] + (np.random.rand() * slope)
    
    test_elev[riv_i, riv_j] = riv_prof

    z = test_elev + sea_level

    return z
Example #23
0
def blob_define(array, thresh, min_area=None, max_area=None, minmax_area=None):
    array[array >= thresh] = 0  # T threshold maskout
    array[np.isnan(array)] = 0  # set ocean nans to 0

    labels, numL = label(array)

    u, inv = np.unique(labels, return_inverse=True)
    n = np.bincount(inv)

    goodinds = u[u!=0]

    if min_area != None:
        goodinds = u[(n>=min_area) & (u!=0)]
        badinds = u[n<min_area]

        for b in badinds:
            pos = np.where(labels==b)
            labels[pos]=0

    if max_area != None:
        goodinds = u[(n<=max_area)  & (u!=0)]
        badinds = u[n>max_area]

    if minmax_area != None:
        goodinds = u[(n <= minmax_area[1]) & (u != 0) & (n>=minmax_area[0])]
        badinds = u[(n > minmax_area[1]) | (n < minmax_area[0])]

        for b in badinds:
            pos = np.where(labels==b)
            labels[pos]=0

    return labels, goodinds
def hist_clustersize(dic, keys, tr, clim, norm=True, bars=False, fig_name=None, higher=True):
    plt.figure(figsize=(7,5), dpi=200)
    for k in keys:
        h = np.zeros((len(dic),clim[1]-clim[0]+1))
        for j in range(len(dic)):
            x = np.array(dic[j][k])
            if higher:
	        x[x<tr[k][0]] = 0.0
                x[x>tr[k][0]] = 1.0
            else:
	        x[x<tr[k][0]] = 1.0
                x[x>tr[k][0]] = 0.0
            xs, n_clusters = measurements.label(x)
            print n_clusters, 'clusters found'
            a  = measurements.sum(x, xs, index=arange(xs.max() + 1))
            ma = np.max(a) 
            h[j,:] = np.asarray([np.sum(a==i) for i in np.arange(clim[0],clim[1]+1,1)])
        hm = np.mean(h,axis=0)
        if norm:
            hm = hm/np.sum(hm)
            plt.ylim([0.0,1.0])
        if bars:
            plt.bar(np.arange(clim[0]-0.4, clim[1], 1.0 ),hm)
        else:
            plt.plot(np.arange(clim[0], clim[1]+1, 1.0 ),hm, 'ob-', ms=10)
        plt.xlim([clim[0]-0.5,clim[1]+0.5])
        plt.xticks(range(clim[0], clim[1]+1,1))
        plt.xlabel('Cluster size')
        plt.ylabel('Number of clusters')
        #plt.title(k)
        if fig_name!=None:
            plt.savefig(fig_name, format='pdf', dpi=200)
Example #25
0
def mod_regions(composite, mod_id, algorithm):
  # paths
  template = composite.templates.get(name='region') # REGION TEMPLATE

  # get region img set that has the region template
  region_img_set = composite.gons.filter(channel__name='-regionimg', template__name='region')

  # channel
  region_channel, region_channel_created = composite.channels.get_or_create(name='-regions')

  # iterate
  for t in range(composite.series.ts):
    print(t)
    region_img = region_img_set.filter(t=t)
    if region_img.count()==0:
      region_img = region_img_set.get(t=t-1)
    else:
      region_img = region_img_set.get(t=t)

    # for each image, determine unique values of labelled array
    # make gon with label array and save

    region_gon = composite.gons.create(experiment=composite.experiment, series=composite.series, channel=region_channel, template=template)
    region_gon.set_origin(0, 0, 0, t)
    region_gon.set_extent(composite.series.rs, composite.series.cs, 1)

    # modify image
    region_array = region_img.load()
    region_array = region_array[:,:,0]
    region_array[region_array>0] = 1
    region_array, n = label(region_array)

    region_gon.array = region_array.copy()
    region_gon.save_array(composite.experiment.composite_path, template)
    region_gon.save()
Example #26
0
def find_sudoku_edges(im, axis=0):
    """ 寻找对齐后数独图像的的单元边线 """
    # threshold and sum rows and columns
    #阈值化,像素值小于128的阈值处理后为1,大于128的为0
    trim = 1*(128 > im)
    #阈值处理后对行(列)相加求和
    s = trim.sum(axis=axis)
    print s
    # find center of strongest lines
    # 寻找连通区域
    s_labels, s_nbr = measurements.label((0.5*max(s)) < s)
    print s_labels
    print s_nbr
    #计算各连通域的质心
    m = measurements.center_of_mass(s, s_labels, range(1, s_nbr+1))
    print m
    #对质心取整,质心即为粗线条所在位置
    x = [int(x[0]) for x in m]
    print x
	# if only the strong lines are detected add lines in between
    # 如果检测到了粗线条,便在粗线条间添加直线
    if 4 == len(x):
        dx = diff(x)
        x = [x[0], x[0]+dx[0]/3, x[0]+2*dx[0]/3, x[1], x[1]+dx[1]/3, x[1]+2*dx[1]/3, x[2], x[2]+dx[2]/3, x[2]+2*dx[2]/3, x[3]]
    if 10 == len(x):
        return x
    else:
        raise RuntimeError('Edges not detected.')
Example #27
0
    def _make_floor_ceil(self, cabin_voxel):
        """
        Alternate method of ceiling detection: get the label in a region containing troops (
        assumed to be the cabin interior volume), then find min and max points where that label
        is found, everywhere in the vehicle. Floor and ceiling are the endpoints of the longest
        continuous gap between floor and ceiling.

        :param cabin_voxel: 3-tuple containing the ijk indices of a voxel known to be cabin-
            determine this from the position of a troop manikin in the vehicle model
        """
        # Default value = bottom of vehicle box. Easy to spot meaningless ceiling points.

        labels = self.get_labels(mask_from_voxel=cabin_voxel)

        self.ceiling = np.zeros((labels.shape[0], labels.shape[1]), dtype=np.int16)
        self.floor = np.zeros((labels.shape[0], labels.shape[1]), dtype=np.int16)
        for i in xrange(labels.shape[0]):
            for j in xrange(labels.shape[1]):
                labs, isl = meas.label(labels[i, j, :])
                if isl == 0:
                    continue
                slices = meas.find_objects(labs)
                lrgst = np.argmax(np.array([sli[0].stop - sli[0].start for sli in slices]))
                
                self.floor[i, j] = slices[lrgst][0].start - 1
                self.ceiling[i, j] = slices[lrgst][0].stop
        # Hack: postprocess so that floor and ceiling arrays have the default values assumed
        # by rest of test bench
        self.floor[self.floor == -1] = 0
        self.ceiling[self.ceiling == labels.shape[2]] = 0
Example #28
0
def rand_by_threshold(ucm, gt, npoints=None):
    """Compute Rand and Adjusted Rand indices for each threshold of a UCM

    Parameters
    ----------
    ucm : np.ndarray, arbitrary shape
        An Ultrametric Contour Map of region boundaries having specific
        values. Higher values indicate higher boundary probabilities.
    gt : np.ndarray, int type, same shape as ucm
        The ground truth segmentation.
    npoints : int, optional
        If provided, only compute values at npoints thresholds, rather than
        all thresholds. Useful when ucm has an extremely large number of
        unique values.

    Returns
    -------
    ris : np.ndarray of float, shape (3, len(np.unique(ucm))) or (3, npoints)
        The rand indices of the segmentation induced by thresholding and
        labeling `ucm` at different values. The 3 rows of `ris` are the values
        used for thresholding, the corresponding Rand Index at that threshold,
        and the corresponding Adjusted Rand Index at that threshold.
    """
    ts = np.unique(ucm)[1:]
    if npoints is None:
        npoints = len(ts)
    if len(ts) > 2 * npoints:
        ts = ts[np.arange(1, len(ts), len(ts) / npoints)]
    result = np.zeros((2, len(ts)))
    for i, t in enumerate(ts):
        seg = label(ucm < t)[0]
        result[0, i] = rand_index(seg, gt)
        result[1, i] = adj_rand_index(seg, gt)
    return np.concatenate((ts[np.newaxis, :], result), axis=0)
  def getMapMatrix(self):
    if self.displayMode == 0: return self.breezyMap if self.INTERNAL_MAP else self.pointMap
    if self.displayMode == 1: return self.breezyMap
    if self.displayMode == 2: return self.pointMap
    fow_darkLimit = 80 # breezyMap value cutoff for points too low to be fog of war
    fow_brightLimit = 200 # breezyMap value cutoff for points too high to be fog of war
    wall_brightLimit = 240 # pointMap value cutoff for points too high to be walls
    road_darkLimit = 240 # breezyMap value cutoff for points too low to be road
    shrink_size = 200 # size of reduced-size map (used to speed up processing)
    # stime = time.clock()
    pointMapShrunk = shrinkTo(self.pointMap, shrink_size, shrink_size)
    breezyMapShrunk = shrinkTo(self.breezyMap, shrink_size, shrink_size)
    unexplored = np.logical_and(fow_darkLimit < breezyMapShrunk, breezyMapShrunk < fow_brightLimit)
    wall = pointMapShrunk < wall_brightLimit
    display = np.where(wall, 0, np.where(unexplored, 127, 255))
    if self.displayMode == 3: return display

    if self.displayMode == 6: return np.where(breezyMapShrunk > road_darkLimit, 255, 0)

    gradientx, gradienty = np.zeros((shrink_size,shrink_size), dtype=bool), np.zeros((shrink_size,shrink_size), dtype=bool)
    n = 1 # number of times to take the diff (width of diff)
    gradientx[:,:-n], gradienty[:-n,:] = np.absolute(np.diff(display, n=n, axis=1))==128, np.absolute(np.diff(display, n=n, axis=0))==128
    targets = np.any([gradientx, np.roll(gradientx, n, axis=1), gradienty, np.roll(gradienty, n, axis=0)], axis=0)
    # targets = np.logical_or(gradientx, gradienty)
    if self.displayMode == 4: return np.where(targets, 127, np.where(wall, 0, 255))

    lbl, num_lbls = label(targets, structure=[[1,1,1],[1,1,1],[1,1,1]])
    self.addFeatures(lbl, num_lbls, shrink_size)
    # etime = time.clock()
    # print etime-stime
    if self.displayMode == 5: return lbl*255/(lbl.max() if lbl.max() != 0 else 1)
Example #30
0
def character_seg_erosion(grey_scale_image, max_w_h_ratio=0.85):
    bin_img = grey_scale_image > 0
    labels, num_labels = label(binary_erosion(bin_img > 0))
    for span, mask in _create_spans_and_masks(labels, num_labels):
        char_img = grey_scale_image[:, span[0]:span[1]].copy()
        char_img[mask == False] = 0
        yield char_img
 def _label_and_slice(self, img):
     '''Label contiguous binary patches numerically and make list of smallest parallelpipeds that contain each.'''
     img = np.copy(img)
     img = scipy.ndimage.binary_fill_holes(img).astype(np.uint8)
     labeled_img, _ = measurements.label(img)
     return measurements.find_objects(labeled_img)
Example #32
0
def connected_components (array):
   structure = np.ones((3, 3), dtype=np.int)  # 8-neighboorhood
   labeled, ncomponents = label(array, structure)
   return labeled
def process_frame(img):

    rects=[]
    
    dist_pickle = pickle.load(open( "training_result.p", "rb" ))
    svc = dist_pickle["svc"]
    color_space = dist_pickle["color_space"]
    orient = dist_pickle["orient"]
    pix_per_cell = dist_pickle["pix_per_cell"]
    cell_per_block = dist_pickle["cell_per_block"]
    spatial_size = dist_pickle["spatial_size"]
    hist_bin = dist_pickle["hist_bin"]
    X_scaler = dist_pickle["X_scaler"]

    ystart = 400
    ystop = 464
    scale = 1.0
    rects.append(find_cars(img, ystart, ystop, scale, color_space, svc, X_scaler, 
                           orient, pix_per_cell, cell_per_block, spatial_size, hist_bin, show_all_rectangles=False))
    
    ystart = 416
    ystop = 480
    scale = 1.0
    rects.append(find_cars(img, ystart, ystop, scale, color_space, svc, X_scaler, 
                           orient, pix_per_cell, cell_per_block, spatial_size, hist_bin, show_all_rectangles=False))
    
    ystart = 400
    ystop = 496
    scale = 1.5
    rects.append(find_cars(img, ystart, ystop, scale, color_space, svc, X_scaler, 
                           orient, pix_per_cell, cell_per_block, spatial_size, hist_bin, show_all_rectangles=False))
    
    ystart = 432
    ystop = 528
    scale = 1.5
    rects.append(find_cars(img, ystart, ystop, scale, color_space, svc, X_scaler, 
                           orient, pix_per_cell, cell_per_block, spatial_size, hist_bin, show_all_rectangles=False))
    
    ystart = 400
    ystop = 528
    scale = 2.0
    rects.append(find_cars(img, ystart, ystop, scale, color_space, svc, X_scaler, 
                           orient, pix_per_cell, cell_per_block, spatial_size, hist_bin, show_all_rectangles=False))
    
    ystart = 400
    ystop = 596
    scale = 3.0
    rects.append(find_cars(img, ystart, ystop, scale, color_space, svc, X_scaler, 
                           orient, pix_per_cell, cell_per_block, spatial_size, hist_bin, show_all_rectangles=False))
    
    ystart = 464
    ystop = 660
    scale = 3.0
    rects.append(find_cars(img, ystart, ystop, scale, color_space, svc, X_scaler, 
                           orient, pix_per_cell, cell_per_block, spatial_size, hist_bin, show_all_rectangles=False))
    
    rectangles = [item for sublist in rects for item in sublist] 
        
    heatmap_img = np.zeros_like(img[:,:,0])
    heatmap_img = add_heat(heatmap_img, rectangles)
    heatmap_img = apply_threshold(heatmap_img, 2)
    labels = label(heatmap_img)
    draw_img = draw_labeled_bboxes(np.copy(img), labels)
    return draw_img
Example #34
0
def __distinct_binary_object_correspondences(reference,
                                             result,
                                             connectivity=1):
    """
    Determines all distinct (where connectivity is defined by the connectivity parameter
    passed to scipy's `generate_binary_structure`) binary objects in both of the input
    parameters and returns a 1to1 mapping from the labelled objects in reference to the
    corresponding (whereas a one-voxel overlap suffices for correspondence) objects in
    result.

    All stems from the problem, that the relationship is non-surjective many-to-many.

    @return (labelmap1, labelmap2, n_lables1, n_labels2, labelmapping2to1)
    """
    result = np.atleast_1d(result.astype(np.bool))
    reference = np.atleast_1d(reference.astype(np.bool))

    # binary structure
    footprint = generate_binary_structure(result.ndim, connectivity)

    # label distinct binary objects
    labelmap1, n_obj_result = label(result, footprint)
    labelmap2, n_obj_reference = label(reference, footprint)

    # find all overlaps from labelmap2 to labelmap1; collect one-to-one relationships and store all one-two-many for later processing
    slicers = find_objects(labelmap2)  # get windows of labelled objects
    mapping = dict(
    )  # mappings from labels in labelmap2 to corresponding object labels in labelmap1
    used_labels = set(
    )  # set to collect all already used labels from labelmap2
    one_to_many = list()  # list to collect all one-to-many mappings
    for l1id, slicer in enumerate(
            slicers):  # iterate over object in labelmap2 and their windows
        l1id += 1  # labelled objects have ids sarting from 1
        bobj = (l1id) == labelmap2[
            slicer]  # find binary object corresponding to the label1 id in the segmentation
        l2ids = np.unique(
            labelmap1[slicer][bobj]
        )  # extract all unique object identifiers at the corresponding positions in the reference (i.e. the mapping)
        l2ids = l2ids[0 != l2ids]  # remove background identifiers (=0)
        if 1 == len(
                l2ids
        ):  # one-to-one mapping: if target label not already used, add to final list of object-to-object mappings and mark target label as used
            l2id = l2ids[0]
            if not l2id in used_labels:
                mapping[l1id] = l2id
                used_labels.add(l2id)
        elif 1 < len(
                l2ids
        ):  # one-to-many mapping: store relationship for later processing
            one_to_many.append((l1id, set(l2ids)))

    # process one-to-many mappings, always choosing the one with the least labelmap2 correspondences first
    while True:
        one_to_many = [
            (l1id, l2ids - used_labels) for l1id, l2ids in one_to_many
        ]  # remove already used ids from all sets
        one_to_many = [x for x in one_to_many if x[1]]  # remove empty sets
        one_to_many = sorted(one_to_many,
                             key=lambda x: len(x[1]))  # sort by set length
        if 0 == len(one_to_many):
            break
        l2id = one_to_many[0][1].pop(
        )  # select an arbitrary target label id from the shortest set
        mapping[one_to_many[0][0]] = l2id  # add to one-to-one mappings
        used_labels.add(l2id)  # mark target label as used
        one_to_many = one_to_many[1:]  # delete the processed set from all sets

    return labelmap1, labelmap2, n_obj_result, n_obj_reference, mapping
Example #35
0
def process_frame(img):

    rectangles = []

    colorspace = 'YUV'  # Can be RGB, HSV, LUV, HLS, YUV, YCrCb
    orient = 11
    pix_per_cell = 16
    cell_per_block = 2
    hog_channel = 'ALL'  # Can be 0, 1, 2, or "ALL"

    ystart = 400
    ystop = 465
    scale = 1.0
    rectangles.append(
        find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc,
                  None, orient, pix_per_cell, cell_per_block, None, None))

    ystart = 415
    ystop = 480
    scale = 1.0
    rectangles.append(
        find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc,
                  None, orient, pix_per_cell, cell_per_block, None, None))

    ystart = 400
    ystop = 495
    scale = 1.5
    rectangles.append(
        find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc,
                  None, orient, pix_per_cell, cell_per_block, None, None))

    ystart = 430
    ystop = 530
    scale = 1.5
    rectangles.append(
        find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc,
                  None, orient, pix_per_cell, cell_per_block, None, None))

    ystart = 400
    ystop = 530
    scale = 2.0
    rectangles.append(
        find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc,
                  None, orient, pix_per_cell, cell_per_block, None, None))

    ystart = 430
    ystop = 560
    scale = 2.0
    rectangles.append(
        find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc,
                  None, orient, pix_per_cell, cell_per_block, None, None))

    ystart = 400
    ystop = 595
    scale = 3.5
    rectangles.append(
        find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc,
                  None, orient, pix_per_cell, cell_per_block, None, None))

    ystart = 465
    ystop = 660
    scale = 3.5
    rectangles.append(
        find_cars(img, ystart, ystop, scale, colorspace, hog_channel, svc,
                  None, orient, pix_per_cell, cell_per_block, None, None))

    rectangles = [item for sublist in rectangles for item in sublist]

    heatmap_img = np.zeros_like(img[:, :, 0])
    heatmap_img = add_heat(heatmap_img, rectangles)
    heatmap_img = apply_threshold(heatmap_img, 1)
    labels = label(heatmap_img)
    draw_img, rects = draw_labeled_bboxes(np.copy(img), labels)

    return draw_img
def process_image(img_src, vis_heat=False):

    draw_img = np.copy(img_src)
    current_draw_img = np.copy(img_src)
    scales = [1, 1.5]
    img_src = img_src.astype(np.float32) / 255
    heat_map = np.zeros_like(img_src[:, :, 0])

    img_src = cv2.cvtColor(img_src, cv2.COLOR_RGB2YCrCb)
    img_cropped = img_src[y_start_stop[0]:y_start_stop[1], :, :]

    for scale in scales:
        if scale != 1:
            imshape = img_cropped.shape
            img_cropped = cv2.resize(
                img_cropped,
                (np.int(imshape[1] / scale), np.int(imshape[0] / scale)))

        ch1 = img_cropped[:, :, 0]
        ch2 = img_cropped[:, :, 1]
        ch3 = img_cropped[:, :, 2]

        nxblocks = (ch1.shape[1] // pix_per_cell) - 1
        nyblocks = (ch1.shape[0] // pix_per_cell) - 1
        window = 64
        nblock_per_window = (window // pix_per_cell) - 1
        cells_per_step = 2
        nxsteps = (nxblocks - nblock_per_window) // cells_per_step
        nysteps = (nyblocks - nblock_per_window) // cells_per_step

        hog1 = get_hog_features(ch1,
                                orient,
                                pix_per_cell,
                                cell_per_block,
                                feature_vec=False)
        hog2 = get_hog_features(ch2,
                                orient,
                                pix_per_cell,
                                cell_per_block,
                                feature_vec=False)
        hog3 = get_hog_features(ch3,
                                orient,
                                pix_per_cell,
                                cell_per_block,
                                feature_vec=False)

        for xb in range(nxsteps):
            for yb in range(nysteps):

                test_features = []
                ypos = yb * cells_per_step
                xpos = xb * cells_per_step
                xleft = xpos * pix_per_cell
                ytop = ypos * pix_per_cell

                subimg = cv2.resize(
                    img_cropped[ytop:ytop + window, xleft:xleft + window],
                    (64, 64))
                spatial_features = bin_spatial(subimg, size=spatial_size)
                test_features.append(spatial_features)
                hist_features = color_hist(subimg, nbins=hist_bins)
                test_features.append(hist_features)

                hog_feat1 = hog1[ypos:ypos + nblock_per_window,
                                 xpos:xpos + nblock_per_window].ravel()
                hog_feat2 = hog2[ypos:ypos + nblock_per_window,
                                 xpos:xpos + nblock_per_window].ravel()
                hog_feat3 = hog3[ypos:ypos + nblock_per_window,
                                 xpos:xpos + nblock_per_window].ravel()
                hog_features = np.hstack((hog_feat1, hog_feat2, hog_feat3))
                test_features.append(hog_features)

                test_features = np.concatenate(test_features)
                test_features = X_scaler.transform(test_features)

                test_prediction = svc.decision_function(test_features)

                if test_prediction > 0.75:
                    xbox_left = np.int(xleft * scale)
                    ytop_draw = np.int(ytop * scale)
                    win_draw = np.int(window * scale)
                    cv2.rectangle(current_draw_img,
                                  (xbox_left, ytop_draw + y_start_stop[0]),
                                  (xbox_left + win_draw,
                                   ytop_draw + y_start_stop[0] + win_draw),
                                  color=(0, 0, 255),
                                  thickness=6)
                    heat_map[ytop_draw + y_start_stop[0]:ytop_draw +
                             y_start_stop[0] + win_draw,
                             xbox_left:xbox_left + win_draw] += 1

    heat_map[heat_map <= 1] = 0
    heat_maps.append(heat_map)
    summed_heat_maps = np.sum(heat_maps, axis=0)
    #summed_heat_maps[summed_heat_maps <= 5] = 0
    labels = label(summed_heat_maps)
    draw_img = draw_labeled_bboxes(draw_img, labels)

    if vis_heat == True:
        diagScreen = np.zeros((720, 2560, 3), dtype=np.uint8)
        diagScreen[0:720, 0:1280] = draw_img
        diagScreen[0:720,
                   1280:2560] = stack_arr(heat_map * 255 // heat_map.max())
        return diagScreen
    else:
        return draw_img
Example #37
0
def eval_on_images(shading_image_arr, pixel_labels_dir, thres_list, photo_id,
                   bl_filter_size, img_dir):
    """
    This method generates a list of precision-recall pairs and confusion
    matrices for each threshold provided in ``thres_list`` for a specific
    photo.

    :param shading_image_arr: predicted shading images

    :param pixel_labels_dir: Directory which contains the SAW pixel labels for each photo.

    :param thres_list: List of shading gradient magnitude thresholds we use to
    generate points on the precision-recall curve.

    :param photo_id: ID of the photo we want to evaluate on.

    :param bl_filter_size: The size of the maximum filter used on the shading
    gradient magnitude image. We used 10 in the paper. If 0, we do not filter.
    """

    shading_image_linear_grayscale = shading_image_arr
    shading_image_linear_grayscale[
        shading_image_linear_grayscale < 1e-4] = 1e-4
    shading_image_linear_grayscale = np.log(shading_image_linear_grayscale)

    shading_gradmag = saw_utils.compute_gradmag(shading_image_linear_grayscale)
    shading_gradmag = np.abs(shading_gradmag)

    if bl_filter_size:
        shading_gradmag_max = maximum_filter(shading_gradmag,
                                             size=bl_filter_size)

    # We have the following ground truth labels:
    # (0) normal/depth discontinuity non-smooth shading (NS-ND)
    # (1) shadow boundary non-smooth shading (NS-SB)
    # (2) smooth shading (S)
    # (100) no data, ignored
    y_true = saw_utils.load_pixel_labels(pixel_labels_dir=pixel_labels_dir,
                                         photo_id=photo_id)

    img_path = img_dir + str(photo_id) + ".png"

    # diffuclut and harder dataset
    srgb_img = saw_utils.load_img_arr(img_path)
    srgb_img = np.mean(srgb_img, axis=2)
    img_gradmag = saw_utils.compute_gradmag(srgb_img)

    smooth_mask = (y_true == 2)
    average_gradient = np.zeros_like(img_gradmag)
    # find every connected component
    labeled_array, num_features = label(smooth_mask)
    for j in range(1, num_features + 1):
        # for each connected component, compute the average image graident for the region
        avg = np.mean(img_gradmag[labeled_array == j])
        average_gradient[labeled_array == j] = avg

    average_gradient = np.ravel(average_gradient)

    y_true = np.ravel(y_true)
    ignored_mask = y_true > 99

    # If we don't have labels for this photo (so everything is ignored), return
    # None
    if np.all(ignored_mask):
        return [None] * len(thres_list)

    ret = []
    for thres in thres_list:
        y_pred = (shading_gradmag < thres).astype(int)
        y_pred_max = (shading_gradmag_max < thres).astype(int)
        y_pred = np.ravel(y_pred)
        y_pred_max = np.ravel(y_pred_max)
        # Note: y_pred should have the same image resolution as y_true
        assert y_pred.shape == y_true.shape

        # confusion_matrix = saw_utils.grouped_confusion_matrix(y_true[~ignored_mask], y_pred[~ignored_mask], y_pred_max[~ignored_mask])
        confusion_matrix = saw_utils.grouped_weighted_confusion_matrix(
            y_true[~ignored_mask], y_pred[~ignored_mask],
            y_pred_max[~ignored_mask], average_gradient[~ignored_mask])
        ret.append(confusion_matrix)

    return ret
Example #38
0
data = npz['data']
times = npz['times']

## Load trial info.
info = read_csv(os.path.join(root_dir, 'afMSIT_%s_info.csv' % space))

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
### Perform correlations.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

## Compute true correlations.
r_vals, p_vals = np.apply_along_axis(spearmanr, axis=0, arr=data, b=info.RT)

## Find real clusters.
masked = p_vals < alpha
clusters, n_clusters = measurements.label(masked)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
### Perform permutation testing (if possible).
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
np.random.seed(seed)

if n_clusters > 0:

    ## Compute cluster sums.
    cluster_sums = measurements.sum(r_vals,
                                    clusters,
                                    index=np.arange(n_clusters) + 1)

    ## Compute cluster bounds.
    tmin = np.array(
Example #39
0
def extract_baselines(image_map: np.array,
                      base_line_index=1,
                      base_line_border_index=2,
                      original=None,
                      processes=1):
    from scipy.ndimage.measurements import label

    base_ind = np.where(image_map == base_line_index)
    base_border_ind = np.where(image_map == base_line_border_index)

    baseline = np.zeros(image_map.shape)
    baseline_border = np.zeros(image_map.shape)
    baseline[base_ind] = 1
    baseline_border[base_border_ind] = 1
    baseline_ccs, n_baseline_ccs = label(baseline,
                                         structure=[[1, 1, 1], [1, 1, 1],
                                                    [1, 1, 1]])

    baseline_ccs = [
        np.where(baseline_ccs == x) for x in range(1, n_baseline_ccs + 1)
    ]
    baseline_ccs = [
        BaseLineCCs(x, 'baseline') for x in baseline_ccs if len(x[0]) > 10
    ]

    all_ccs = baseline_ccs  # + baseline_border_ccs
    logger.info("Extracted {} CCs from probability map \n".format(
        len(all_ccs)))

    def calculate_distance_matrix(ccs, maximum_angle=5, processes=8):
        distance_matrix = np.zeros((len(ccs), len(ccs)))

        from functools import partial
        distance_func = partial(calculate_distance,
                                ccs=ccs,
                                maximum_angle=maximum_angle,
                                baseline_border_image=baseline_border)
        indexes_ccs = list(range(len(ccs)))
        with multiprocessing.Pool(processes=processes,
                                  maxtasksperchild=100) as p:
            out = list(p.map(distance_func, indexes_ccs))
        for x in out:
            indexes, values = x
            distance_matrix[indexes] = values
        return distance_matrix

    with PerformanceCounter(function_name="calculate_distance_matrix"):
        matrix = calculate_distance_matrix(all_ccs, processes=processes)

    from sklearn.cluster import DBSCAN
    if np.sum(matrix) == 0:
        print("Empty Image")
        return
    t = DBSCAN(eps=100, min_samples=1, metric="precomputed").fit(matrix)

    ccs = []
    for x in np.unique(t.labels_):
        ind = np.where(t.labels_ == x)
        line = []
        for d in ind[0]:
            if all_ccs[d].type == 'baseline':
                line.append(all_ccs[d])
        if len(line) > 0:
            ccs.append((np.concatenate([x.cc[0] for x in line]),
                        np.concatenate([x.cc[1] for x in line])))

    ccs = [list(zip(x[0], x[1])) for x in ccs]

    from itertools import chain
    from typing import List, Tuple
    from collections import defaultdict

    def normalize_connected_components(cc_list: List[List[Tuple[int, int]]]):
        # Normalize the CCs (line segments), so that the height of each cc is normalized to one pixel
        def normalize(point_list):
            normalized_cc_list = []
            for cc in point_list:
                cc_dict = defaultdict(list)
                for y, x in cc:
                    cc_dict[x].append(y)
                normalized_cc = []
                for key in sorted(cc_dict.keys()):
                    value = cc_dict[key]
                    normalized_cc.append(
                        [int(np.floor(np.mean(value) + 0.5)), key])
                normalized_cc_list.append(normalized_cc)
            return normalized_cc_list

        return normalize(cc_list)

    ccs = normalize_connected_components(ccs)
    new_ccs = []
    for baseline in ccs:
        new_ccs.append([coord_tup[::-1] for coord_tup in baseline])

    return new_ccs
Example #40
0
# %%
'''Report Area and Intensity in Segmentation Mask'''
nPixels = len(I[idx])
PercentArea = 100.0 * float(nPixels) / float(I.size)
print("Number of Pixels in Mask: ", nPixels)  #131296
print("Percent Area: ", PercentArea)  #25.88

print("Mean Intensity in Mask: ", I[idx].mean())  #50.782
print("Max Intensity in Mask: ", I[idx].max())  #137
print("Min Intensity in Mask: ", I[idx].min())  #21

# %%

from scipy.ndimage import measurements
ILabel, nFeatures = measurements.label(idx)
print("Number of elements: ", nFeatures)
plt.imshow(ILabel)
plt.show()

# %%
'''Report Intensity Per Object'''
for k in range(nFeatures):
    idxCell = ILabel == k
    print("Cell: ", k, " Mean: ", I[idxCell].mean())

# %%
# Exercises:
# 1. What may be the result of thresholding after np.log2 ?
# 2. Better way to visualize the images and thresholding results?
# 3. What can you do to separate the clustered cells?
Example #41
0
    def lung_segmentation(self, sc):
        """Method for lungs segmentation."""
        """
            Arguments:
                sc - LiTSscan object containing normalized volume
        """
        h, w, d = [sc.get_height(), sc.get_width(), sc.get_depth()]
        # .....................................................................
        #  1. Detecting air regions around and in body
        # .....................................................................
        air_r = (sc.get_volume() < self.air_th)
        # .....................................................................
        #  2. Detecting body bounds
        # .....................................................................
        b_bounds = np.zeros((sc.get_depth(), 4), dtype='int16')
        b_bounds[:, 1], b_bounds[:, 3] = [w - 1, h - 1]

        bb_v, bb_h = [h - np.sum(air_r, axis=0), w - np.sum(air_r, axis=1)]

        bb_v = bb_v.astype('float32') / np.sum(bb_v, axis=0)
        bb_h = bb_h.astype('float32') / np.sum(bb_h, axis=0)

        xc = np.dot(np.arange(w).astype('float32'), bb_v)
        yc = np.dot(np.arange(h).astype('float32'), bb_h)

        for i in range(d):
            for j in range(int(xc[i]), 0, -1):
                if bb_v[j, i] < self.body_bounds_th[0]:
                    b_bounds[i, 0] = j
                    break
            for j in range(int(xc[i]), w):
                if bb_v[j, i] < self.body_bounds_th[0]:
                    b_bounds[i, 1] = j
                    break
            for j in range(int(yc[i]), 0, -1):
                if bb_h[j, i] < self.body_bounds_th[1]:
                    b_bounds[i, 2] = j
                    break
            for j in range(int(yc[i]), h):
                if bb_h[j, i] < self.body_bounds_th[2]:
                    b_bounds[i, 3] = j
                    break
        # .....................................................................
        #  3. Down-sample air mask for further processing
        # .....................................................................
        air_r_ds = (air_r[::self.ds_f[0], ::self.ds_f[1], ::self.ds_f[2]] == 0)
        # .....................................................................
        #  4. Remove outside body air
        # .....................................................................
        l_cs = np.zeros((h / self.ds_f[0], w / self.ds_f[1], d / self.ds_f[2]),
                        dtype='bool')
        b_bounds_ds = b_bounds
        b_bounds_ds[:, 0:2] = b_bounds_ds[:, 0:2] / self.ds_f[0]
        b_bounds_ds[:, 2:] = b_bounds_ds[:, 2:] / self.ds_f[1]

        for i in range(sc.get_depth()):
            img_patch = air_r_ds[b_bounds_ds[i, 2]:b_bounds_ds[i, 3],
                                 b_bounds_ds[i, 0]:b_bounds_ds[i, 1], i]
            img_patch = self._remove_outside_body(img_patch)
            l_cs[b_bounds_ds[i, 2]:b_bounds_ds[i, 3],
                 b_bounds_ds[i, 0]:b_bounds_ds[i, 1], i] = (img_patch == 0)
        # .....................................................................
        #  5. Determine center of the largest air object along vertical axis
        # .....................................................................
        self._largest_air_object_center(l_cs)
        # .....................................................................
        #  6. Labeling in body air
        # .....................................................................
        masks, label = measurements.label(l_cs)
        labels_list = np.arange(label) + 1
        object_sizes = np.zeros(label)
        count_sgnf = 0
        lv_th_ds = self.lv_th / (self.ds_f[0] * self.ds_f[1] * self.ds_f[2])
        for l in range(1, label + 1):
            object_sizes[l - 1] = np.sum(masks == l)
            if object_sizes[l - 1] > lv_th_ds:
                count_sgnf += 1
        # .....................................................................
        #  7. Extracting lung candidates from labeled data according to the
        #     size and/or position
        # .....................................................................
        lungs = self._extract_lung_candidates(count_sgnf, labels_list, masks,
                                              object_sizes)
        # .....................................................................
        #  8. Up-sample detected mask corresponding to the lungs
        # .....................................................................
        lungs_up = np.zeros((h, w, d), dtype='bool')
        for s in range(d):
            for r in range(h / self.ds_f[0]):
                for c in range(w / self.ds_f[1]):
                    if lungs[r, c, s]:
                        r1, r2 = [r * self.ds_f[0], (r + 1) * self.ds_f[0]]
                        c1, c2 = [c * self.ds_f[1], (c + 1) * self.ds_f[1]]
                        lungs_up[r1:r2, c1:c2, s] = air_r[r1:r2, c1:c2, s]

        # .....................................................................
        #  9. Re-labeling in body air
        # .....................................................................
        masks, label = measurements.label(lungs_up)
        labels_list = np.arange(label) + 1
        object_sizes = np.zeros(label)
        count_sgnf = 0
        for l in range(1, label + 1):
            object_sizes[l - 1] = np.sum(masks == l)
            if object_sizes[l - 1] > self.lv_th:
                count_sgnf += 1
        lungs = self._extract_lung_candidates(count_sgnf, labels_list, masks,
                                              object_sizes)
        # .....................................................................
        #  10. Set meta segmentation
        # .....................................................................
        sc.set_meta_segmentation(lungs.astype('uint8') * 3)
def drift_adjustment(image_file,
                     anchor_centers,
                     output_filename,
                     drift_trajectory_file,
                     tracking=True,
                     region_radius=50,
                     ref_tstep=0):

    ###This function tracks an *actually stationary* object to account for drift in the x-y plane. This is only designed to work in the simplest case, where there is an isolated stationary object to use as an anchor (for example a pigment spot)

    ###Parameters:
    ###input_array: a numpy array containing input images, assumed to be (Txmxn)
    ###anchor_center: the center of the stationary object in the reference timestep, which is either the first or last frame. Can be approximate; the function will recalculate the centroid.
    ###region_radius: the radius of the region over which to calculate the centroid in the next frame.
    ###ref_tstep: drift displacement vectors will be calculated relative to the location of the object at this timestep. Default is 0.

    ###Calculate trajectory of stationary object by finding the brightness centroid within the circle with r=region_radius, around the center from the previous frame.

    import numpy
    from scipy.ndimage.measurements import center_of_mass, label
    import matplotlib.pylab as pt
    from skimage import io, filters, morphology

    image_data = io.imread(image_file)

    ntsteps = image_data.shape[0]

    if not tracking_flag:

        output_filename = io.imsave(output_filename, image_data)

        file = open(drift_trajectory_file, 'w')

        for t in range(ntsteps):
            file.write(str(t) + '\t' + '0,0' + '\n')
        file.close()

    else:
        coms = anchor_centers

        circle_mask = numpy.zeros(
            (2 * region_radius + 1, 2 * region_radius + 1), dtype='int')

        for j in range(region_radius):

            x = numpy.sqrt((region_radius)**2 - j**2)
            circle_start = region_radius - int(round(x))
            circle_end = region_radius + int(round(x))

            for i in range(circle_start, circle_end + 1):

                circle_mask[i, region_radius - j] = 1
                circle_mask[i, region_radius + j] = 1

        com_trajectories = {}
        for com in coms:
            com_trajectories[tuple(com)] = []
        com_trajectories['avg_disp'] = []
        #print(coms)
        #ntsteps = 10
        for i in range(ntsteps):

            if ref_tstep == -1:  ###Trajectory will be calculated from the final tstep backwards

                t = ntsteps - i - 1
            else:
                t = i

            disps = numpy.array([0., 0.])

            for com in coms:

                com_list = com_trajectories[tuple(com)]
                if len(com_list) < 1:
                    prev_com = com
                else:
                    prev_com = com_list[i - 1]

                region = image_data[t,
                                    int(round(prev_com[0])) -
                                    region_radius:int(round(prev_com[0])) +
                                    region_radius + 1,
                                    int(round(prev_com[1])) -
                                    region_radius:int(round(prev_com[1])) +
                                    region_radius + 1]  #*circle_mask

                ###Find the spot in the region

                otsu_thresh = filters.threshold_otsu(region)
                mask = numpy.zeros_like(region)
                mask[region > otsu_thresh] = 1
                labels, num_features = label(mask)

                large_label = morphology.remove_small_objects(labels,
                                                              min_size=4)

                ###Find the com of the labeled region

                local_com = center_of_mass(region, labels=large_label)

                #pt.pcolor(region)
                #pt.axis('equal')
                #pt.plot([local_com[1]],[local_com[0]], marker='o',color='k')

                #print(local_com)
                new_com = [
                    int(round(prev_com[0])) - region_radius + local_com[0],
                    int(round(prev_com[1])) - region_radius + local_com[1]
                ]

                #pt.figure(figsize=(8,20))
                #pt.pcolor(image_data[t,:,:])
                #pt.plot([new_com[1]], [new_com[0]], marker='o',color='k')
                #pt.show()

                if t == 0:
                    disp = numpy.array([0., 0.])
                else:
                    disp = numpy.array(new_com) - numpy.array(prev_com)

                com_trajectories[tuple(com)].append(new_com)

                disps += disp

                #print(local_com[0])
                #print(prev_com[0])
                #print(int(round(prev_com[0])) - region_radius)
                #print(new_com[0])
                #print(disps)

            com_trajectories['avg_disp'].append(disps / 2.)
            #print(disp)

        drift_corrected_array = numpy.zeros_like(image_data)

        trajectories = numpy.array(com_trajectories['avg_disp'])
        for i in range(ntsteps):

            driftx = numpy.int(
                numpy.round(numpy.sum(trajectories[0:i + 1, :], axis=0)[0]))
            drifty = numpy.int(
                numpy.round(numpy.sum(trajectories[0:i + 1, :], axis=0)[1]))

            print(driftx, drifty)
            if ref_tstep == -1:  ###Trajectory will be calculated from the final tstep backwards

                t = ntsteps - i - 1
            else:
                t = i

            if (driftx >= 0 and drifty >= 0):
                if (driftx == 0 and drifty == 0):
                    drift_corrected_array[t, :, :] = image_data[t, :, :]
                elif driftx == 0:
                    drift_corrected_array[t, :, :-drifty] = image_data[t, :,
                                                                       drifty:]
                elif drifty == 0:
                    drift_corrected_array[t, :-driftx, :] = image_data[t,
                                                                       driftx:,
                                                                       drifty:]
                else:
                    drift_corrected_array[t, :-driftx, :-drifty] = image_data[
                        t, driftx:, drifty:]

            elif (driftx >= 0 and drifty < 0):
                if driftx == 0:
                    drift_corrected_array[t, :, -drifty:] = image_data[
                        t, driftx:, :drifty]
                else:
                    drift_corrected_array[t, :-driftx, -drifty:] = image_data[
                        t, driftx:, :drifty]

            elif (driftx < 0 and drifty >= 0):
                if drifty == 0:
                    drift_corrected_array[t,
                                          -driftx:, :] = image_data[t, :driftx,
                                                                    drifty:]
                else:
                    drift_corrected_array[t, -driftx:, :-drifty] = image_data[
                        t, :driftx, drifty:]

            elif (driftx < 0 and drifty < 0):

                drift_corrected_array[t, -driftx:, -drifty:] = image_data[
                    t, :driftx, :drifty]

        io.imsave(output_filename, drift_corrected_array)

        file = open(drift_trajectory_file, 'w')

        for t in range(ntsteps):
            file.write(
                str(t) + '\t' +
                (',').join([str(d)
                            for d in com_trajectories['avg_disp'][t]]) + '\n')
        file.close()
Example #43
0
def detector(image):

    image_backup = np.copy(image)

    windows = []

    search_config = [(100, 180, 1.5), (96, 192, 2), (80, 200, 2, 5),
                     (72, 232, 3), (64, 232, 3.5), (64, 232, 4), (64, 232, 5),
                     (40, 300, 6), (40, 300, 8)]
    # search_config = [(190, 280, 1), (200, 300, 1.5), (150, 480, 2.5)]

    for config in search_config:
        out_img, out_windows = find_signs(image, config[0], config[1],
                                          config[2], svc, X_scaler, orient,
                                          pix_per_cell, cell_per_block,
                                          spatial_size, hist_bins, hog_channel)
        windows.append(out_windows)

    windows = [item for sublist in windows for item in sublist]

    # Initialize the heatmap
    heat = np.zeros_like(image[:, :, 0].astype(np.float))

    # Add heat to each windows in windows list
    heat = add_heat(heat, windows)

    # Apply threshold to help remove false positives
    heat = apply_threshold(heat, 6)

    # Visualize the heatmap when displaying
    heatmap = np.clip(heat, 0, 255)

    # Find final windows from heatmap using lable function
    labels = label(heatmap)
    num_signs = labels[1]
    draw_img, sign_label, sign_position = draw_labeled_bboxes(
        np.copy(image), labels)

    # judge the types of traffic sign
    sign_types = []
    for i in range(len(sign_label)):
        sign = image_backup[sign_position[i][1]:sign_position[i][3],
                            sign_position[i][0]:sign_position[i][2]]
        sign = np.uint8(sign * 255)
        sign = cv2.resize(sign, (32, 32))
        sign = cv2.cvtColor(sign, cv2.COLOR_RGB2BGR)
        sign = np.expand_dims(sign, axis=0)
        sign_normalized = normalized(sign)
        sign_type = sess.run(tf.argmax(logits, 1),
                             feed_dict={x: sign_normalized})
        sign_types.append(sign_type)

        # anotate the traffic sign name in image
        cv2.putText(draw_img, '%s' % label_name[int('%d' % sign_type)],
                    (sign_position[i][0], sign_position[i][1] - 10),
                    cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 1, 0), 2)


#         cv2.putText(draw_img, '%d: %s' %(sign_type, label_name[sign_type]), (sign_position[i][1]-20,
#                     sign_position[i][0]), cv2.FONT_HERSHEY_COMPLEX, 6, (0,0,255), 25)

    return np.uint8(draw_img * 255), sign_types
Example #44
0
# set threshold
gradthreshold = 16

# calculate gradients and threshold
#GradX = roll(U,-1,axis=1)-U   # x component of U gradient
#GradY = roll(U,-1,axis=0)-U   # y component
#tGradX = 1*(GradX > gradthreshold)
#tGradY = 1*(GradY > gradthreshold)
imx = zeros(im.shape)
filters.sobel(im, 1, imx)
imy = zeros(im.shape)
filters.sobel(im, 0, imy)
gradmag = sqrt(imx**2 + imy**2)
gradmag = 1 * (gradmag > gradthreshold)

# find objects from gradient
labels, nbr_objects = measurements.label(gradmag)
print "Number of objects:", nbr_objects

#display original, gradient
figure()
gray()
subplot(1, 2, 1)
axis('off')
imshow(im)
subplot(1, 2, 2)
imshow(gradmag)
axis('off')
subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)
show()
Example #45
0
def batch_compute_invalid_moves(batch_state, batch_player, batch_ko_protect):
    """
    Updates invalid moves in the OPPONENT's perspective
    1.) Opponent cannot move at a location
        i.) If it's occupied
        i.) If it's protected by ko
    2.) Opponent can move at a location
        i.) If it can kill
    3.) Opponent cannot move at a location
        i.) If it's adjacent to one of their groups with only one liberty and
            not adjacent to other groups with more than one liberty and is completely surrounded
        ii.) If it's surrounded by our pieces and all of those corresponding groups
            move more than one liberty
    """
    batch_idcs = np.arange(len(batch_state))

    # All pieces and empty spaces
    batch_all_pieces = np.sum(batch_state[:, [govars.BLACK, govars.WHITE]],
                              axis=1)
    batch_empties = 1 - batch_all_pieces

    # Setup invalid and valid arrays
    batch_possible_invalid_array = np.zeros(batch_state.shape[:1] +
                                            batch_state.shape[2:])
    batch_definite_valids_array = np.zeros(batch_state.shape[:1] +
                                           batch_state.shape[2:])

    # Get all groups
    batch_all_own_groups, _ = measurements.label(
        batch_state[batch_idcs, batch_player], group_struct)
    batch_all_opp_groups, _ = measurements.label(
        batch_state[batch_idcs, 1 - batch_player], group_struct)

    batch_data = enumerate(
        zip(batch_all_own_groups, batch_all_opp_groups, batch_empties))
    for i, (all_own_groups, all_opp_groups, empties) in batch_data:
        own_labels = np.unique(all_own_groups)
        opp_labels = np.unique(all_opp_groups)
        own_labels = own_labels[np.nonzero(own_labels)]
        opp_labels = opp_labels[np.nonzero(opp_labels)]
        expanded_own_groups = np.zeros(
            (len(own_labels), *all_own_groups.shape))
        expanded_opp_groups = np.zeros(
            (len(opp_labels), *all_opp_groups.shape))

        # Expand the groups such that each group is in its own channel
        for j, label in enumerate(own_labels):
            expanded_own_groups[j] = all_own_groups == label

        for j, label in enumerate(opp_labels):
            expanded_opp_groups[j] = all_opp_groups == label

        # Get all liberties in the expanded form
        all_own_liberties = empties[np.newaxis] * ndimage.binary_dilation(
            expanded_own_groups, surround_struct[np.newaxis])
        all_opp_liberties = empties[np.newaxis] * ndimage.binary_dilation(
            expanded_opp_groups, surround_struct[np.newaxis])

        own_liberty_counts = np.sum(all_own_liberties, axis=(1, 2))
        opp_liberty_counts = np.sum(all_opp_liberties, axis=(1, 2))

        # Possible invalids are on single liberties of opponent groups and on multi-liberties of own groups
        # Definite valids are on single liberties of own groups, multi-liberties of opponent groups
        # or you are not surrounded
        batch_possible_invalid_array[i] += np.sum(
            all_own_liberties[own_liberty_counts > 1], axis=0)
        batch_possible_invalid_array[i] += np.sum(
            all_opp_liberties[opp_liberty_counts == 1], axis=0)

        batch_definite_valids_array[i] += np.sum(
            all_own_liberties[own_liberty_counts == 1], axis=0)
        batch_definite_valids_array[i] += np.sum(
            all_opp_liberties[opp_liberty_counts > 1], axis=0)

    # All invalid moves are occupied spaces + (possible invalids minus the definite valids and it's surrounded)
    surrounded = ndimage.convolve(batch_all_pieces,
                                  surround_struct[np.newaxis],
                                  mode='constant',
                                  cval=1) == 4
    invalid_moves = batch_all_pieces + batch_possible_invalid_array * \
        (batch_definite_valids_array == 0) * surrounded

    # Ko-protection
    for i, ko_protect in enumerate(batch_ko_protect):
        if ko_protect is not None:
            invalid_moves[i, ko_protect[0], ko_protect[1]] = 1
    return invalid_moves > 0
Example #46
0
def execute_test_pipeline(calibration_components,
                          perspective_transform_components, src_vertices):

    #############################
    ## TEST CAMERA CALIBRATION ##
    #############################

    #test camera calibration by undistorting a test road image
    #load image
    bgr_test_road_image = cv2.imread("test_images/test6.jpg")
    #convert from bgr to rgb
    test_road_image = cv2.cvtColor(bgr_test_road_image, cv2.COLOR_BGR2RGB)

    #undistort image - this undistorted image will be used to demonstrate the production_pipeline along the way (all outputs will be placed in 'output_images' folder)
    undistorted_test_road_image = perform_undistort(test_road_image,
                                                    calibration_components)

    ################################
    ## TEST PERSPECTIVE TRANSFORM ##
    ################################

    #transform perspective (warp) - this will squish the depth of field in the source mapping into the height of the image,
    #which will make the upper 3/4ths blurry, need to adjust dest_upper* y-values to negative to stretch it out and clear the transformed image up
    #we won't do that as we'll lose right dashes in the 720 pix height of the image frame
    warped_undistorted_test_road_image = perform_perspective_transform(
        undistorted_test_road_image, perspective_transform_components[0])

    ####################################
    ## TEST COLOR/GRADIENT THRESHOLD  ##
    ####################################

    #apply thresholding to warped image and produce a binary result
    thresholded_warped_undistorted_test_road_image = perform_thresholding(
        warped_undistorted_test_road_image)

    #########################
    ## TEST LANE DETECTION ##
    #########################

    ## BLIND SEARCH ##

    #map out the left and right lane line pixel coordinates via windowed search
    left_lane_pixel_coordinates, right_lane_pixel_coordinates, _ = perform_blind_lane_line_pixel_search(
        thresholded_warped_undistorted_test_road_image,
        return_debug_image=False)

    #compute the polynomial coefficients for each lane line using the x and y pixel locations from the mapping function
    #we're fitting (computing coefficients of) a second order polynomial: f(y) = A(y^2) + By + C
    #we're fitting for f(y) rather than f(x), as the lane lines in the warped image are near vertical and may have the same x value for more than one y value
    left_lane_line_coeff, right_lane_line_coeff = compute_lane_line_coefficients(
        left_lane_pixel_coordinates, right_lane_pixel_coordinates)

    #generate range of evenly spaced numbers over y interval (0 - 719) matching image height
    y_linespace = np.linspace(
        0, (thresholded_warped_undistorted_test_road_image.shape[0] - 1),
        thresholded_warped_undistorted_test_road_image.shape[0])

    #left lane fitted polynomial (f(y) = A(y^2) + By + C)
    left_lane_line_fitted_poly = (left_lane_line_coeff[0] *
                                  (y_linespace**2)) + (
                                      left_lane_line_coeff[1] *
                                      y_linespace) + left_lane_line_coeff[2]
    #right lane fitted polynomial (f(y) = A(y^2) + By + C)
    right_lane_line_fitted_poly = (right_lane_line_coeff[0] *
                                   (y_linespace**2)) + (
                                       right_lane_line_coeff[1] *
                                       y_linespace) + right_lane_line_coeff[2]

    #draw the fitted polynomials on the debug_image and export
    #recast the x and y points into usable format for polylines and fillPoly
    pts_left = np.array(
        [np.transpose(np.vstack([left_lane_line_fitted_poly, y_linespace]))])
    pts_right = np.array([
        np.flipud(
            np.transpose(np.vstack([right_lane_line_fitted_poly,
                                    y_linespace])))
    ])

    ## compute left and right lane curvature ##
    left_curvature, right_curvature = compute_curvature_of_lane_lines(
        thresholded_warped_undistorted_test_road_image.shape,
        left_lane_line_fitted_poly, right_lane_line_fitted_poly)

    ## compute vehicle offset from center ##
    vehicle_offset = compute_vehicle_offset(
        thresholded_warped_undistorted_test_road_image.shape,
        left_lane_line_coeff, right_lane_line_coeff)

    #####################################
    ## TEST PROJECTION BACK ONTO ROAD  ##
    #####################################

    #create an image to draw the lines on
    warped_lane = np.zeros_like(warped_undistorted_test_road_image).astype(
        np.uint8)

    #draw the lane onto the warped blank image
    pts = np.hstack((pts_left, pts_right))
    cv2.fillPoly(warped_lane, np.int_([pts]), (152, 251, 152))

    #draw fitted lines on image
    cv2.polylines(warped_lane,
                  np.int_([pts_left]),
                  False,
                  color=(189, 183, 107),
                  thickness=20,
                  lineType=cv2.LINE_AA)
    cv2.polylines(warped_lane,
                  np.int_([pts_right]),
                  False,
                  color=(189, 183, 107),
                  thickness=20,
                  lineType=cv2.LINE_AA)

    #transform perspective back to original (unwarp)
    warped_to_original_perspective = perform_perspective_transform(
        warped_lane, perspective_transform_components[1])

    #combine (weight) the result with the original image
    projected_lane = cv2.addWeighted(undistorted_test_road_image, 1,
                                     warped_to_original_perspective, 0.3, 0)

    #add tracking text
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(
        projected_lane, 'Lane curvature: {0:.2f} meters'.format(
            np.mean([left_curvature, right_curvature])), (20, 50), font, 1,
        (255, 255, 255), 2, cv2.LINE_AA)
    cv2.putText(projected_lane,
                'Vehicle offset: {0:.2f} meters'.format(vehicle_offset),
                (20, 100), font, 1, (255, 255, 255), 2, cv2.LINE_AA)

    ######################################
    ## TEST VEHICLE DETECTION/TRACKING  ##
    ######################################

    #hyperparameters
    spatial_reduction_size = 32  #reduce the training images from 64x64 to 32x32 resolution (smaller feature vector but still retains useful shape and color information)
    pixel_intensity_fd_bins = 64  #number of bins to use to compute raw pixel intensity frequency distribution
    hog_orientation_bins = 9  #number of orientation bins to use in hog feature extraction
    hog_pixels_per_cell = 8  #number of pixels per cell to use in hog feature extraction
    hog_cells_per_block = 2  #number of cells per block to use in hog feature extraction
    scale_factor_list = [2.0, 1.5, 1.2, 1]  #window scales
    y_axis_start = 400  #start y-axis crop
    y_axis_stop = 656  #end y-axis crop

    dist_pickle = pickle.load(
        open("model_training/pickled_objects/trained_model.p", "rb"))
    support_vector_classifier = dist_pickle["model"]
    X_feature_scaler = dist_pickle["scaler"]

    #for the current frame, detect vehicles at each scale,
    #returning the list of coordinates (p1 and p2) of the window that signaled a positive prediction
    positive_detection_window_coordinates = perform_vehicle_search(
        undistorted_test_road_image, y_axis_start, y_axis_stop,
        scale_factor_list, support_vector_classifier, X_feature_scaler,
        spatial_reduction_size, pixel_intensity_fd_bins, hog_orientation_bins,
        hog_pixels_per_cell, hog_cells_per_block)

    #create a heat map
    heatmap = np.zeros_like(undistorted_test_road_image[:, :,
                                                        0]).astype(np.float)

    #apply heat to all pixels within the set of detected windows
    heatmap = apply_heat_to_heatmap(heatmap,
                                    positive_detection_window_coordinates)

    #apply threshold to heatmap to help remove false positives
    heatmap = apply_threshold_to_heatmap(heatmap, 3)

    #compute final bounding boxes from heatmap
    labeled_objects = label(heatmap)

    #draw final bounding boxes on projected lane image
    projected_lane = draw_bounding_boxes_for_labeled_objects(
        projected_lane, labeled_objects)

    #save image
    #convert from rgb to bgr (the format opencv likes)
    projected_lane = cv2.cvtColor(projected_lane, cv2.COLOR_RGB2BGR)
    cv2.imwrite("output_images/projected_lane_and_detected_vehices_test6.jpg",
                projected_lane)
Example #47
0
    def from_mask(cls,
                  array,
                  geometry=None,
                  chunk_size=None,
                  threshold=None,
                  overlap=1,
                  pbar=False,
                  cube_shape=None,
                  fmt='mask'):
        """ Label faults in an array.

        Parameters
        ----------
        array : numpy.ndarray or SeismicGeometry
            binary mask of faults or array of coordinates.
        geometry : SeismicGeometry or None
            geometry instance to create Fault-instance.
        chunk_size : int
            size of chunks to apply `measurements.label`.
        threshold : float or None
            threshold to drop small faults.
        overlap : int
            size of overlap to join faults from different chunks.
        pbar : bool
            progress bar
        cube_shape : tuple
            shape of cube. If fmt='mask', can be infered from array.
        fmt : str
            if 'mask', array is a binary mask of faults. If 'points', array consists of coordinates of fault points.

        Returns
        -------
        numpy.ndarray
            array of shape (n_faults, ) where each item is array of fault points of shape (N_i, 3).
        """
        # TODO: make chunks along xlines
        if isinstance(array, SeismicGeometry):
            array = array.file_hdf5
        chunk_size = chunk_size or len(array)
        if chunk_size == len(array):
            overlap = 0

        if cube_shape is None and fmt == 'points':
            raise ValueError("If fmt='points', cube_shape must be specified")

        cube_shape = cube_shape or array.shape

        if fmt == 'mask':
            chunks = [
                (start, array[start:start + chunk_size])
                for start in range(0, cube_shape[0], chunk_size - overlap)
            ]
            total = len(chunks)
        else:

            def _chunks():
                for start in range(0, cube_shape[0], chunk_size - overlap):
                    chunk = np.zeros((chunk_size, *cube_shape[1:]))
                    points = array[array[:, 0] < start + chunk_size]
                    points = points[points[:, 0] >= start]
                    chunk[points[:, 0] - start, points[:, 1], points[:, 2]] = 1
                    yield (start, chunk)

            chunks = _chunks()
            total = len(range(0, cube_shape[0], chunk_size - overlap))

        prev_overlap = np.zeros((0, *cube_shape[1:]))
        labels = np.zeros((0, 4), dtype='int32')
        n_objects = 0
        chunks = tqdm(chunks, total=total) if pbar else chunks
        for start, item in chunks:
            chunk_labels, new_objects = measurements.label(
                item, structure=np.ones((3, 3, 3)))  # labels for new chunk
            chunk_labels[
                chunk_labels >
                0] += n_objects  # shift all values to avoid intersecting with previous labels
            new_overlap = chunk_labels[:overlap]

            if len(prev_overlap) > 0:
                coords = np.where(prev_overlap > 0)
                if len(coords[0]) > 0:
                    # while there are the same objects with different labels repeat procedure
                    while (new_overlap != prev_overlap).any():
                        # find overlapping objects and change labels in chunk
                        chunk_transform = {
                            k: v
                            for k, v in zip(new_overlap[coords],
                                            prev_overlap[coords]) if k != v
                        }
                        for k, v in chunk_transform.items():
                            chunk_labels[chunk_labels == k] = v
                        new_overlap = chunk_labels[:overlap]

                        # find overlapping objects and change labels in processed part of cube
                        labels_transform = {
                            k: v
                            for k, v in zip(prev_overlap[coords],
                                            new_overlap[coords]) if k != v
                        }
                        for k, v in labels_transform.items():
                            labels[labels[:, 3] == k, 3] = v
                            prev_overlap[prev_overlap == k] = v

            prev_overlap = chunk_labels[-overlap:]
            chunk_labels = chunk_labels[overlap:]

            nonzero_coord = np.where(chunk_labels)
            chunk_labels = np.stack(
                [*nonzero_coord, chunk_labels[nonzero_coord]], axis=-1)
            chunk_labels[:, 0] += start
            labels = np.concatenate([labels, chunk_labels])
            n_objects += new_objects

        labels = labels[np.argsort(labels[:, 3])]
        labels = np.array(np.split(
            labels[:, :-1],
            np.unique(labels[:, 3], return_index=True)[1][1:]),
                          dtype=object)
        sizes = faults_sizes(labels)
        if threshold:
            labels = labels[sizes >= threshold]
        if geometry is not None:
            labels = [
                Fault(points.astype('int32'), geometry=geometry)
                for points in labels
            ]
        return labels
def DHRegions(DH, DH_threshold):
    '''
    This code uses the concept of connected components from image processing
    library of Scipy in order to detect the potential district heating areas.
    '''
    # "struct" defines how the connected components can be considered.
    struct = np.ones((3, 3)).astype(int)
    # expansion and erosion of the connected components in order to connect
    # different components which are in close vicinity of each other
    # struct(3,3): 200 meter distance between the two connected components
    DH_expanded = binary_dilation(DH, structure=struct)
    DH_connected = binary_erosion(DH_expanded, structure=struct)
    # fills the holes within the connected components
    #DH_noHole = binary_fill_holes(DH_connected)
    DH_noHole = DH_connected
    # label the connected components
    struct = np.ones((3, 3)).astype(int)
    labels, numLabels = measurements.label(DH_noHole, structure=struct)
    # the conditional statement prevents from error in the following code.
    # This can also be incorporated in order to filter areas smaller than a
    # specific size e.g. 1km2 ~ 100.
    if labels.size > 0:
        # labels start from 1. Therefore, PotDH should have numLabels+1
        # elements
        PotDH = np.zeros((numLabels + 1)).astype(bool)
        # using sparse matrix indices to swift the calculation. This helps to
        # implement "np.unique" much faster
        sparseRow, sparseCol = np.nonzero(labels)
        sparseLabels = labels[sparseRow, sparseCol]
        sparseDH = DH[sparseRow, sparseCol]
        # sort sparse values based on sparseLabels. This helps to implement
        # summation process much faster.
        sortedSparseData = np.asarray(
            sorted(zip(sparseRow, sparseCol, sparseLabels, sparseDH),
                   key=lambda x: x[2]))
        # find unique values and their counts within the sparseLabels
        unique, counts = np.unique(sparseLabels, return_counts=True)
        '''
        calculate starting and ending indices of each unique value in order
        to swift the summation operation. calculate starting and ending
        indices of each unique value in order to swift the summation
        operation. Note that a[st:end] refers to elements of a including "st"
        and excluding end.
        Note: To get the last element of the same type, however, cumsum shoud
        be subtracted by 1:
        (e.g. [1,1,1,1,2,2,2]: hear st for 1 is 0; end for 1 is 4; the last
        element which is one is 3)
        '''
        end = np.cumsum(counts)
        st = np.concatenate((np.zeros((1)), end[0:numLabels - 1]))

        for i in range(numLabels):
            # sum over sparseDH
            # input: [MWh/ha] for each ha --> summation returns MWh for the
            # coherent area
            pot = np.sum(sortedSparseData[int(st[i]):int(end[i]), 3])
            if pot >= DH_threshold:
                # here should be i+1 because labeling starts from one and not
                # from zero
                PotDH[i + 1] = True
        DH_regions = PotDH[labels]
        return DH_regions
Example #49
0
# show()

# 对红章图进行连通域标记,选择面积第二大的区域为红章区域
bn_stamp = np.zeros((thirdw, h))
maskstamp_third = maskstamp[0:thirdw, :]
bn_stamp[maskstamp_third] = 1

# bn_stamp = np.ones((w,h))
# bn_stamp[maskstamp] = 0
# gray()
# figure()
# imshow(bn_stamp)
# show()
# stamp_open = morphology.binary_erosion(bn_stamp,ones((2,2)),iterations = 2)
# stamp_open = morphology.binary_dilation(stamp_open,ones((2,2)),iterations = 1)
labels_open, nbr = measurements.label(bn_stamp)

# imsave("label.png",labels_open)
# gray()
# figure()
# imshow(stamp_open)
# show()
count = zeros(nbr)
for i in range(nbr):
    count[i] = np.sum(labels_open == i)
    # print(count[i])
index = np.argsort(-count)[1]
# print(index)

maskstamponly = (labels_open == index)
# print(a.shape)
Example #50
0
def compute_invalid_moves(state, player, ko_protect=None):
    """
    Updates invalid moves in the OPPONENT's perspective
    1.) Opponent cannot move at a location
        i.) If it's occupied
        i.) If it's protected by ko
    2.) Opponent can move at a location
        i.) If it can kill
    3.) Opponent cannot move at a location
        i.) If it's adjacent to one of their groups with only one liberty and
            not adjacent to other groups with more than one liberty and is completely surrounded
        ii.) If it's surrounded by our pieces and all of those corresponding groups
            move more than one liberty
    """

    # All pieces and empty spaces
    all_pieces = np.sum(state[[govars.BLACK, govars.WHITE]], axis=0)
    empties = 1 - all_pieces

    # Setup invalid and valid arrays
    possible_invalid_array = np.zeros(state.shape[1:])
    definite_valids_array = np.zeros(state.shape[1:])

    # Get all groups
    all_own_groups, num_own_groups = measurements.label(state[player])
    all_opp_groups, num_opp_groups = measurements.label(state[1 - player])
    expanded_own_groups = np.zeros((num_own_groups, *state.shape[1:]))
    expanded_opp_groups = np.zeros((num_opp_groups, *state.shape[1:]))

    # Expand the groups such that each group is in its own channel
    for i in range(num_own_groups):
        expanded_own_groups[i] = all_own_groups == (i + 1)

    for i in range(num_opp_groups):
        expanded_opp_groups[i] = all_opp_groups == (i + 1)

    # Get all liberties in the expanded form
    all_own_liberties = empties[np.newaxis] * ndimage.binary_dilation(
        expanded_own_groups, surround_struct[np.newaxis])
    all_opp_liberties = empties[np.newaxis] * ndimage.binary_dilation(
        expanded_opp_groups, surround_struct[np.newaxis])

    own_liberty_counts = np.sum(all_own_liberties, axis=(1, 2))
    opp_liberty_counts = np.sum(all_opp_liberties, axis=(1, 2))

    # Possible invalids are on single liberties of opponent groups and on multi-liberties of own groups
    # Definite valids are on single liberties of own groups, multi-liberties of opponent groups
    # or you are not surrounded
    possible_invalid_array += np.sum(all_own_liberties[own_liberty_counts > 1],
                                     axis=0)
    possible_invalid_array += np.sum(
        all_opp_liberties[opp_liberty_counts == 1], axis=0)

    definite_valids_array += np.sum(all_own_liberties[own_liberty_counts == 1],
                                    axis=0)
    definite_valids_array += np.sum(all_opp_liberties[opp_liberty_counts > 1],
                                    axis=0)

    # All invalid moves are occupied spaces + (possible invalids minus the definite valids and it's surrounded)
    surrounded = ndimage.convolve(all_pieces,
                                  surround_struct,
                                  mode='constant',
                                  cval=1) == 4
    invalid_moves = all_pieces + possible_invalid_array * \
        (definite_valids_array == 0) * surrounded

    # Ko-protection
    if ko_protect is not None:
        invalid_moves[ko_protect[0], ko_protect[1]] = 1
    return invalid_moves > 0
Example #51
0
def file_loop(passit):

    gridd = passit[0]
    inds = gridd[0]
    weights = gridd[1]
    shape = gridd[2]
    grid = gridd[3]

    m = passit[1]
    files = passit[2]

    min = '00'

    strr = files.split(os.sep)[-1]

    if ((np.int(strr[4:6]) > 9) | (np.int(strr[4:6]) < 6)):
        print('Skip month')
        return

    if not (
        (np.int(strr[8:10]) >= 17) | (np.int(strr[8:10]) <= 3)
    ):  #& (np.int(strr[8:10]) <= 19) ): #((np.int(strr[8:10]) > 3)): #not ((np.int(strr[8:10]) >= 16) & (np.int(strr[8:10]) <= 19) ): #& (np.int(strr[8:10]) < 18): #(np.int(strr[4:6]) != 6) & #(np.int(strr[8:10]) != 3) , (np.int(strr[8:10]) > 3)
        print('Skip hour')
        return

    lon, lat = grid.ll_coordinates

    file = files + min + '.gra'

    print('Doing file: ' + file)
    try:
        mdic = m.read_data(file, llbox=[-11, 11, 9, 20])
    except FileNotFoundError:
        print('File not found')
        return

    if not mdic:
        print('File missing')
        return
    hour = mdic['time.hour']
    minute = mdic['time.minute']
    day = mdic['time.day']
    month = mdic['time.month']
    year = mdic['time.year']

    date = dt.datetime(year, month, day, hour, minute)

    outt = u_int.interpolate_data(mdic['t'].values, inds, weights, shape)

    figure = np.zeros_like(outt)

    outt[outt > -70] = 0
    outt[np.isnan(outt)] = 0

    labels, numL = label(outt)

    u, inv = np.unique(labels, return_inverse=True)
    n = np.bincount(inv)

    badinds = u[(
        n < 200
    )]  # 40 / 200 pixels for 1000-5000k, 600 for 15k, all blobs with more than 36 pixels = 18 km x*y = 324 km2 (meteosat ca. 3km)
    goodinds = u[(n > 200)]

    for bi in badinds:
        inds = np.where(labels == bi)
        outt[inds] = 0

    for gi in goodinds:

        if gi == 0:
            continue

        dummy = np.zeros_like(outt)

        pos = np.where(labels == gi)

        y_middle = np.int(
            (np.min(pos[0]) + (np.max(pos[0]) - np.min(pos[0])) / 2))
        x_front = np.int(np.min(
            pos[1]))  #+ (np.max(pos[1]) - np.min(pos[1])) / 2))
        x_middle = np.int(
            np.min(pos[1]) + (np.max(pos[1]) - np.min(pos[1])) / 2)
        x_back = np.int(np.max(pos[1]))
        y_bottom = np.int(np.min(pos[0]))
        y_top = np.int(np.max(pos[0]))

        random1 = np.int(np.min(pos[1]) - 40)
        random2 = np.int(np.max(pos[1]) + 40)
        random11 = np.int(np.min(pos[1]) - 80)
        random22 = np.int(np.max(pos[1]) + 80)
        random3 = np.int(np.min(pos[0]) - 40)
        random4 = np.int(np.max(pos[0]) + 40)

        rlist1 = [(y_middle, random1),
                  (y_middle, random2)]  # west - east shift
        rlist11 = [(y_middle, random11)]  # west - east shift
        rlist12 = [(y_middle, random22)]
        rlist2 = [(random3, x_middle),
                  (random4, x_middle)]  # south-north shift

        dummy[y_middle, x_middle] = 2
        dummy[y_middle, x_front] = 1
        dummy[y_middle, x_back] = 3
        dummy[y_bottom, x_middle] = 4
        dummy[y_top, x_middle] = 5

        for r in rlist1:
            try:
                dummy[r[0], r[1]] = 6  # west east
            except IndexError:
                continue

        for r in rlist11:
            try:
                dummy[r[0], r[1]] = 8  # west far front
            except IndexError:
                continue

        for r in rlist12:
            try:
                dummy[r[0], r[1]] = 9  #  east far back
            except IndexError:
                continue

        for r in rlist2:
            try:
                dummy[r[0], r[1]] = 7  # north south
            except IndexError:
                continue

        figure[dummy > 0] = dummy[dummy > 0]

    da = xr.DataArray(figure,
                      coords={
                          'time': date,
                          'lat': lat[:, 0],
                          'lon': lon[0, :]
                      },
                      dims=['lat', 'lon'])  #[np.newaxis, :]

    print('Did ', file)

    return (da)
                                          thresh=90,
                                          thresh_op=0)
#plt.figure()
#plt.imshow(grad_img_bin_open)

#Deleting black noise
grad_img_bin_open_1 = select_special_object(abs(1 - grad_img_bin_open),
                                            img_op=0,
                                            thresh=70,
                                            thresh_op=0)
#plt.figure()
#plt.imshow(grad_img_bin_open_1)
'''add a condition about abnormal graduations'''

# check for abnormal graduations
labs, _ = measurements.label(grad_img_bin_open_1)
labs = np.array(labs)
lab = labs.flatten()
count_grad = Counter(lab)
del count_grad[0]
if bin_thresh == 100:
    grad_sizes = [x for x in count_grad.values() if 120 < x < 500]
else:
    grad_sizes = [x for x in count_grad.values() if 120 < x < 700]
grad_size = np.mean(grad_sizes) + 0.75 * (np.max(grad_sizes) -
                                          np.min(grad_sizes))
for _, size in count_grad.items():
    if size > grad_size:
        abnormal = 1
        break
    else:
Example #53
0
    def fit(self,
            target_mask,
            method='min_distance',
            r=5,
            n_exps=50,
            n_parcels=2,
            meta_estimator=SCALE,
            **kwargs):
        """
        Run CBP parcellation.

        Parameters
        ----------
        target_mask : img_like
            Image with binary mask for region of interest to be parcellated.
        n_parcels : :obj:`int` or array_like of :obj:`int`, optional
            Number of parcels to generate for ROI. If array_like, each parcel
            number will be evaluated and results for all will be returned.
            Default is 2.
        n_iters : :obj:`int`, optional
            Number of iterations to run for each parcel number.
            Default is 10000.
        n_cores : :obj:`int`, optional
            Number of cores to use for model fitting.

        Returns
        -------
        results
        """
        assert np.array_equal(self.mask.affine, target_mask.affine)
        kernel_args = {
            k: v
            for k, v in kwargs.items() if k.startswith('kernel__')
        }
        meta_args = {
            k.split('meta__')[1]: v
            for k, v in kwargs.items() if k.startswith('meta__')
        }

        if not isinstance(n_parcels, list):
            n_parcels = [n_parcels]

        # Step 1: Build correlation matrix
        target_data = apply_mask(target_mask, self.mask)
        target_map = unmask(target_data, self.mask)
        target_data = target_map.get_data()
        mask_idx = np.vstack(np.where(target_data))
        n_voxels = mask_idx.shape[1]
        voxel_arr = np.zeros((n_voxels, np.sum(self.mask)))

        ijk = self.coordinates[['i', 'j', 'k']].values
        temp_df = self.coordinates.copy()
        for i_voxel in range(n_voxels):
            voxel = mask_idx[:, i_voxel]
            temp_df['distance'] = cdist(ijk, voxel)

            if method == 'min_studies':
                # number of studies
                temp_df2 = temp_df.groupby('id')[['distance']].min()
                temp_df2 = temp_df2.sort_values(by='distance')
                sel_ids = temp_df2.iloc[:n_exps].index.values
            elif method == 'min_distance':
                # minimum distance
                temp_df2 = temp_df.groupby('id')[['distance']].min()
                sel_ids = temp_df2.loc[temp_df2['distance'] < r].index.values

            # Run MACM
            voxel_meta = meta_estimator(self.dataset,
                                        ids=sel_ids,
                                        **kernel_args)
            voxel_meta.fit(**meta_args)
            voxel_arr[i_voxel, :] = apply_mask(voxel_meta.results['ale'],
                                               self.mask)

        # Correlate voxel-specific MACMs across voxels in ROI
        voxel_corr = np.corrcoef(voxel_arr)
        corr_dist = 1 - voxel_corr

        # Step 2: Clustering
        labels = np.zeros((n_voxels, len(n_parcels)))
        metric_types = ['contiguous']
        metrics = pd.DataFrame(index=n_parcels,
                               columns=metric_types,
                               data=np.zeros(
                                   (len(n_parcels), len(metric_types))))
        for i_parc, n_clusters in enumerate(n_parcels):
            # K-Means clustering
            _, labeled, _ = k_means(corr_dist,
                                    n_clusters,
                                    init='k-means++',
                                    precompute_distances='auto',
                                    n_init=1000,
                                    max_iter=1023,
                                    verbose=False,
                                    tol=0.0001,
                                    random_state=1,
                                    copy_x=True,
                                    n_jobs=1,
                                    algorithm='auto',
                                    return_n_iter=False)
            labels[:, i_parc] = labeled

            # Check contiguity of clusters
            # Can nilearn do this?
            temp_mask = np.zeros(target_data.shape)
            for j_voxel in range(n_voxels):
                i, j, k = mask_idx[:, j_voxel]
                temp_mask[i, j, k] = labeled[j_voxel]
            labeled = meas.label(temp_mask, np.ones((3, 3, 3)))[0]
            n_contig = len(np.unique(labeled))
            metrics.loc[n_clusters,
                        'contiguous'] = int(n_contig > (n_clusters + 1))

        self.solutions = labels
        self.metrics = metrics
    def _call(self, ds):
        if len(ds) > 1:
            # average all samples into one, assuming we got something like one
            # sample per subject as input
            avgr = mean_sample()
            ds = avgr(ds)
        # threshold input; at this point we only have one sample left
        thrd = ds.samples[0] > self._thrmap
        # mapper default
        mapper = IdentityMapper()
        # overwrite if possible
        if hasattr(ds, 'a') and 'mapper' in ds.a:
            mapper = ds.a.mapper
        # reverse-map input
        othrd = _verified_reverse1(mapper, thrd)
        # TODO: what is your purpose in life osamp? ;-)
        osamp = _verified_reverse1(mapper, ds.samples[0])
        # prep output dataset
        outds = ds.copy(deep=False)
        outds.fa['featurewise_thresh'] = self._thrmap
        # determine clusters
        labels, num = measurements.label(othrd)
        area = measurements.sum(othrd, labels,
                                index=np.arange(1, num + 1)).astype(int)
        com = measurements.center_of_mass(osamp,
                                          labels=labels,
                                          index=np.arange(1, num + 1))
        maxpos = measurements.maximum_position(osamp,
                                               labels=labels,
                                               index=np.arange(1, num + 1))
        # for the rest we need the labels flattened
        labels = mapper.forward1(labels)
        # relabel clusters starting with the biggest and increase index with
        # decreasing size
        ordered_labels = np.zeros(labels.shape, dtype=int)
        ordered_area = np.zeros(area.shape, dtype=int)
        ordered_com = np.zeros((num, len(osamp.shape)), dtype=float)
        ordered_maxpos = np.zeros((num, len(osamp.shape)), dtype=float)
        for i, idx in enumerate(np.argsort(area)):
            ordered_labels[labels == idx + 1] = num - i
            # kinda ugly, but we are looping anyway
            ordered_area[i] = area[idx]
            ordered_com[i] = com[idx]
            ordered_maxpos[i] = maxpos[idx]
        labels = ordered_labels
        area = ordered_area[::-1]
        com = ordered_com[::-1]
        maxpos = ordered_maxpos[::-1]
        del ordered_labels  # this one can be big
        # store cluster labels after forward-mapping
        outds.fa['clusters_featurewise_thresh'] = labels.copy()
        # location info
        outds.a['clusterlocations'] = \
            np.rec.fromarrays(
                [com, maxpos], names=('center_of_mass', 'max'))

        # update cluster size histogram with the actual result to get a
        # proper lower bound for p-values
        # this will make a copy, because the original matrix is int
        cluster_probs_raw = _transform_to_pvals(
            area, self._null_cluster_sizes.astype('float'))

        clusterstats = ([area, cluster_probs_raw], ['size', 'prob_raw'])
        # evaluate a bunch of stats for all clusters
        morestats = {}
        for cid in xrange(len(area)):
            # keep clusters on outer loop, because selection is more expensive
            clvals = ds.samples[0, labels == cid + 1]
            for id_, fx in (('mean', np.mean), ('median', np.median),
                            ('min', np.min), ('max', np.max), ('std', np.std)):
                stats = morestats.get(id_, [])
                stats.append(fx(clvals))
                morestats[id_] = stats

        for k, v in morestats.items():
            clusterstats[0].append(v)
            clusterstats[1].append(k)

        if self.params.multicomp_correction is not None:
            # do a local import as only this tiny portion needs statsmodels
            import statsmodels.stats.multitest as smm
            rej, probs_corr = smm.multipletests(
                cluster_probs_raw,
                alpha=self.params.fwe_rate,
                method=self.params.multicomp_correction)[:2]
            # store corrected per-cluster probabilities
            clusterstats[0].append(probs_corr)
            clusterstats[1].append('prob_corrected')
            # remove cluster labels that did not pass the FWE threshold
            for i, r in enumerate(rej):
                if not r:
                    labels[labels == i + 1] = 0
            outds.fa['clusters_fwe_thresh'] = labels
        outds.a['clusterstats'] = \
            np.rec.fromarrays(clusterstats[0], names=clusterstats[1])
        return outds
Example #55
0
def process_image(image, do_output=False, image_name="", image_was_jpg=True, return_original=True):
    """
    Given an image (loaded from a file or a frame of a video), 
    process it to find the vehicles and draw bounding boxes around them.

    image:      the full-color image (eg: from cv2.imread()).
    do_output:  whether to output images of the various steps. Intended to be done doing
                for the static images but not for the videos (since there are a ton of frames).
    image_name: optional. Recommended when do_output is true. This will be used for debug
                and output-filenames related to this image.
    image_was_jpg: If true, then we assume the input image was a jpg (video frames are not jpgs) which
                   means that we need to scale the color to be 0-1 instead of 0-255 to match the
                   training-images that were loaded as pngs.
    """
    global recent_hot_windows

    image_name, image_extension = os.path.splitext(image_name)
    image_copy = image.copy()
    
    # Scale the colors from the range 0-255 to be 0-1 which matches training images.
    if image_was_jpg:
        image = image.astype(np.float32)/255
    #box_color = (0,0,1.0) if image_was_jpg else (0,0,255) # since we draw onto the pre-scaled version
    box_color = (0,0,255)


    # Get the sliding-window boundaries that we'll search for cars.
    # We use just the bottom area of the images since we don't expect flying-cars to interfere with our driving ;)
    y_start = int(image.shape[0] * 0.55)
    x_start = int(image.shape[1] * 0.35) # ignore the far-left.. it's the shoulder of the road
    windows = slide_window(image, x_start_stop=[x_start, None], y_start_stop=[y_start, None], 
                           xy_window=(128, 128), xy_overlap=(0.5, 0.5))

    # Use multiple scales of windows... here we add a second scale which has much smaller windows, only processes
    # the top part of our area of interest (because that's the only place that cars are that small) and adds these
    # windows to our list
    y_start -= 16 # just to stagger it a bit from the bigger windows
    x_start = int(image.shape[1] * 0.45) # ignore the far-left.. it's the shoulder of the road
    y_stop = int(image.shape[0] * 0.80)
    windows.extend(slide_window(image, x_start_stop=[x_start, None], y_start_stop=[y_start, y_stop],
                           xy_window=(64, 64), xy_overlap=(0.5, 0.5)))
    #smaller_windows = slide_window(image, x_start_stop=[x_start, None], y_start_stop=[y_start, y_stop],
    #                       xy_window=(64, 64), xy_overlap=(0.5, 0.5))
    if do_output:
        window_image = draw_boxes(image_copy, windows, color=box_color, thick=6)
        #window_image = draw_boxes(window_image, smaller_windows, color=(1.0,0,0), thick=4) # DEBUG: This was just used to render the smaller scaled windows
        plt.imsave(os.path.join(OUT_DIR, "010-all-windows-"+image_name+".png"), window_image)
        plt.close()
    #else:
        # if(randint(0, 30) == 1):
            # window_image = draw_boxes(image_copy, windows, color=box_color, thick=6)
            # plt.imsave(os.path.join(OUT_DIR, "010-all-windows-VIDEO.png"), window_image)
            # plt.close()
        
    # Extract the HOG features for the whole image here, then we will pass this into search_windows
    # which will sub-sample from this array to get the HOG features for each desired window.
    color_spaces = {
        'HSV': cv2.COLOR_RGB2HSV,
        'LUV': cv2.COLOR_RGB2LUV,
        'HLS': cv2.COLOR_RGB2HLS,
        'YUV': cv2.COLOR_RGB2YUV,
        'YCrCb': cv2.COLOR_RGB2YCrCb
    }
    if colorspace in color_spaces:
        converted_image = cv2.cvtColor(image, color_spaces[colorspace])
    else:
        converted_image = np.copy(image)

    # if do_output:
        # plt.imsave(os.path.join(OUT_DIR, "011-color-converted-"+image_name+".png"), converted_image)
        # plt.close()
    #else:
        #if(randint(0, 30) == 1):
        #    plt.imsave(os.path.join(OUT_DIR, "011-color-converted-VIDEO.png"), converted_image)
        #    plt.close()

    ch1 = converted_image[:,:,0]
    ch2 = converted_image[:,:,1]
    ch3 = converted_image[:,:,2]
    hog1 = get_hog_features(ch1, orient, pix_per_cell, cell_per_block, vis=False, feature_vec=False)
    hog2 = get_hog_features(ch2, orient, pix_per_cell, cell_per_block, vis=False, feature_vec=False)
    hog3 = get_hog_features(ch3, orient, pix_per_cell, cell_per_block, vis=False, feature_vec=False)
    
    # Do the sliding-window search across the image to find "hot" windows where it appears
    # that there is a car.
    hot_windows = search_windows(converted_image, windows, svc, X_scaler,
                        spatial_size=(spatial, spatial), hist_bins=histbin, 
                        orient=orient, pix_per_cell=pix_per_cell, 
                        cell_per_block=cell_per_block, 
                        hog_channel=hog_channel, use_spatial_feat=use_spatial_feat, 
                        use_hist_feat=use_hist_feat, use_hog_feat=use_hog_feat,
                        hog_channels=(hog1, hog2, hog3),
                        do_output=do_output, image_name=image_name)

    # Instead of drawing the bounding-boxes directly, we'll use a heatmap to find the best fits.
    #hot_windows_instantaneous = draw_boxes(image_copy, hot_windows, color=box_color, thick=6)
    if do_output:
        plt.imsave(os.path.join(OUT_DIR, "011-hot-windows-"+image_name+".png"), hot_windows_instantaneous)
        plt.close()
        
        plt.imsave(os.path.join(OUT_DIR, "010-boxes-"+image_name+".png"), window_image)
        plt.close()

    # RUBRIC POINT:
    # Combine overlapping-detections and remove false-positives.
    # == HEATMAPPING THE RECENT X FRAMES ==
    NUM_FRAMES_TO_REMEMBER = 10
    MIN_BOXES_NEEDED = 3 # remember: there are multiple (often overlapping) boxes per video-frame
    while( len(recent_hot_windows) >= NUM_FRAMES_TO_REMEMBER ):
        # Deletes the oldest set of hot windows
        del recent_hot_windows[0]
    recent_hot_windows.append( hot_windows ) # adds the new frame's hot windows
    heat = np.zeros_like(image[:,:,0]).astype(np.float)
    
    # Add heat to each box in box list, for each of the last NUM_FRAMES_TO_REMEMBER frames.
    for hot_wins in recent_hot_windows:
        heat = add_heat(heat, hot_wins)
    
    # Apply threshold to help remove false positives
    heat = apply_threshold(heat, MIN_BOXES_NEEDED)
    
    # Visualize the heatmap when displaying
    heatmap = np.clip(heat, 0, 255)
    labels = label(heatmap)
    hot_window_image = draw_labeled_bboxes(np.copy(image_copy), labels, color=box_color)

    if do_output:
        fig = plt.figure()
        plt.subplot(121)
        plt.imshow(hot_windows_instantaneous) # instantaneous is the raw boxes
        plt.title('Car Positions')
        plt.subplot(122)
        # Render an individual heatmap rather than an averaged one
        heat = np.zeros_like(image[:,:,0]).astype(np.float)
        heat = add_heat(heat, hot_windows)
        plt.imshow(heat, cmap='hot')
        plt.title('Heat Map')
        fig.tight_layout()
        plt.savefig(os.path.join(OUT_DIR, "015-heatmap-"+image_name+".png"))
        plt.close()    

    #return hot_windows_instantaneous # only use this if you want to debug the hot-windows instead of the heatmapped/thresholded bounding boxes.
    return hot_window_image
def tracking_pipeline(image,
                      svc,
                      X_scaler,
                      y_start_stop=[350, 700],
                      color_space='LUV',
                      hog_channel='ALL',
                      orient=9,
                      pix_per_cell=8,
                      cell_per_block=2,
                      spatial_feat=True,
                      hist_feat=True,
                      hog_feat=True,
                      spatial_size=(32, 32),
                      hist_bins=32,
                      stage='final',
                      smooth=False):

    draw_image = np.copy(image)

    # Uncomment the following line if you extracted training
    # data from .png images (scaled 0 to 1 by mpimg) and the
    # image you are searching is a .jpg (scaled 0 to 255)
    # image = image.astype(np.float32)/255

    # small_windows = slide_window(image, x_start_stop=[None, None], y_start_stop=y_start_stop, xy_window=(48, 48), xy_overlap=(0., 0.75))

    # print("small_windows size", len(small_windows))

    small_windows = slide_window(image,
                                 x_start_stop=[None, None],
                                 y_start_stop=[350, 414],
                                 xy_window=(64, 64),
                                 xy_overlap=(0.8, 0.8))
    # small_windows = []

    # print("medium_windows size", len(medium_windows))

    medium_windows = slide_window(image,
                                  x_start_stop=[None, None],
                                  y_start_stop=[350, 520],
                                  xy_window=(96, 96),
                                  xy_overlap=(0.8, 0.8))
    # medium_windows = []

    medium_windows2 = slide_window(image,
                                   x_start_stop=[None, None],
                                   y_start_stop=[350, 540],
                                   xy_window=(128, 128),
                                   xy_overlap=(0.8, 0.8))
    # medium_windows2 = []

    # print("medium_windows3 size", len(medium_windows3))

    large_windows = slide_window(image,
                                 x_start_stop=[None, None],
                                 y_start_stop=[350, 700],
                                 xy_window=(192, 192),
                                 xy_overlap=(0.75, 0.75))
    # large_windows = []

    # print("large_windows size", len(large_windows))

    # windows = small_windows + medium_windows + medium_windows2 + medium_windows3 + large_windows

    # windows = small_windows + medium_windows + medium_windows2 + medium_windows3 + large_windows

    windows = small_windows + medium_windows + medium_windows2 + large_windows

    # print("total windows size", len(windows))

    hot_windows = search_windows(image,
                                 windows,
                                 svc,
                                 X_scaler,
                                 color_space=color_space,
                                 spatial_size=spatial_size,
                                 hist_bins=hist_bins,
                                 orient=orient,
                                 pix_per_cell=pix_per_cell,
                                 cell_per_block=cell_per_block,
                                 hog_channel=hog_channel,
                                 spatial_feat=spatial_feat,
                                 hist_feat=hist_feat,
                                 hog_feat=hog_feat)

    # print(hot_windows)
    if stage == 'hot':
        window_img = draw_boxes(draw_image,
                                hot_windows,
                                color=(0, 0, 255),
                                thick=6)
        return window_img

    heat = np.zeros_like(image[:, :, 0]).astype(np.float)

    # Add heat to each box in box list
    heat = add_heat(heat, hot_windows)

    # Apply threshold to help remove false positives
    heat = apply_heat_threshold(heat, 1)

    # Visualize the heatmap when displaying
    heatmap = np.clip(heat, 0, 255)

    # TODO: get smoothing working properly
    # if smooth:
    #     # print("smoothing")
    #     # Average the heatmap over past 4 frames
    #     if len(globals.heatmaps) == 4:
    #         globals.heatmaps.append(heatmap)
    #         globals.heatmaps.pop()
    #         heatmap = np.mean(globals.heatmaps, axis=0)
    #     else:
    #         globals.heatmaps.append(heatmap)

    if stage == 'heat':
        return heatmap

    # Find final boxes from heatmap using label function
    labels = label(heatmap)
    draw_img = draw_labeled_bboxes(np.copy(image), labels)

    return draw_img
Example #57
0
File: P.py Project: mathhat/fys4660
from skimage.viewer import ImageViewer
Lx = 100
Ly = 100
N = 1000  #number of porous systems generated
p_ = np.linspace(0.5, 1, 51)
with open("P.txt", "w") as File:
    File.write("    p        P \n")
    for p in p_:
        average = []
        for i in range(0, N):
            c**t = 0
            seed = i
            np.random.seed(seed)
            r = np.random.rand(Lx, Ly)
            z = r < p
            # This generates the binary array
            z_labeled, num_clusters = label(z)
            regionp = regionprops(z_labeled)
            for prop in regionp:
                bbox = prop.bbox
                if (bbox[3] - bbox[1] == Lx) or (bbox[2] - bbox[0] == Ly):
                    c**t += prop.area

            prob = c**t / float(Lx * Ly)
            average.append(prob)
        P = np.mean(average)
        print P
        File.write("   %1.3f    %1.4f \n" % (p, P))

#view = ImageViewer(image)
#view.show()
Example #58
0
from PIL import Image
from numpy import *
from scipy.ndimage import measurements, morphology
from pylab import *
"""   This is the morphology counting objects example in Section 1.4.  """

# load image and threshold to make sure it is binary
figure()
gray()
im = array(Image.open('../data/houses.png').convert('L'))
subplot(221)
imshow(im)
axis('off')
im = (im < 128)

labels, nbr_objects = measurements.label(im)
print("Number of objects:", nbr_objects)
subplot(222)
imshow(labels)
axis('off')

# morphology - opening to separate objects better
im_open = morphology.binary_opening(im, ones((9, 5)), iterations=2)
subplot(223)
imshow(im_open)
axis('off')

labels_open, nbr_objects_open = measurements.label(im_open)
print("Number of objects:", nbr_objects_open)
subplot(224)
imshow(labels_open)
Example #59
0
# Loop through training images
for filename in os.listdir(label_path):
    file = label_path + filename
    print(file)

    # Read images
    #image = Image.open(rgb_path)
    labeled_image = Image.open(file)

    # Binarize labeled image
    labeled_image = np.array(labeled_image)
    red_labeled_image = labeled_image[:, :, 0]
    vehicle_out = np.where(red_labeled_image == 10, 255, 0)
    road_out = np.where((red_labeled_image == 6) | (red_labeled_image == 7),
                        255, 0)

    # Remove front part of ego car
    structure = np.ones((3, 3), dtype=np.int)
    labeled, ncomponents = label(vehicle_out, structure)
    post_vehicle_out = np.where(labeled == ncomponents, 0, vehicle_out)

    # Merge both images
    red_channel = np.where(post_vehicle_out == 255, 0, 255)
    labeled_image[:, :, 0] = red_channel
    labeled_image[:, :, 2] = road_out
    final_image = Image.fromarray(np.uint8(labeled_image))

    # Save images
    final_label_path = path + 'LabeledSeg/' + filename
    final_image.save(final_label_path)
    ystop = 660
    scale = 3.0
    rects.append(find_cars(img, ystart, ystop, scale, color_space, svc, X_scaler, 
                           orient, pix_per_cell, cell_per_block, spatial_size, hist_bin, show_all_rectangles=False))
    
    rectangles = [item for sublist in rects for item in sublist] 
    
    # add detections to the history
    if len(rectangles) > 0:
        vehicles_rec.add_pos(rectangles)
    
    heatmap_img = np.zeros_like(img[:,:,0])
    for rect_set in vehicles_rec.prepos:
        heatmap_img = add_heat(heatmap_img, rect_set)
    heatmap_img = apply_threshold(heatmap_img, 2 + len(vehicles_rec.prepos)//2)
     
    labels = label(heatmap_img)
    draw_img = draw_labeled_bboxes(np.copy(img), labels)
    return draw_img



vehicles_rec = Vehicles()

test_out_file = 'project_video_out.mp4'
clip_test = VideoFileClip('project_video.mp4')
clip_test_out = clip_test.fl_image(process_frame_for_video)
clip_test_out.write_videofile(test_out_file, audio=False)