Esempio n. 1
0
def getPixelCoordinatesOfBuildings(urlBuildings, midX, midY):
    images['source']['value'] = io.imread(urlBuildings)
    images['grayscale']['value'] = color.rgb2gray(images['source']['value'])
    # Will create inverted binary image.
    images['binary']['value'] = np.where(
        images['grayscale']['value'] > np.mean(images['grayscale']['value']),
        0.0, 1.0)
    contours = find_contours(images['binary']['value'], 0.1)

    for n, contourBuilding in enumerate(contours):
        if (contourBuilding[0, 1]
                == contourBuilding[-1, 1]) and (contourBuilding[0, 0]
                                                == contourBuilding[-1, 0]):
            # Check if it is inside any other polygon, so this will remove any additional elements.
            isInside = False
            skipPoly = False
            for othersPolygon in contours:
                isInside = points_in_poly(contourBuilding, othersPolygon)
                if all(isInside):
                    skipPoly = True
                    break

            if skipPoly == False:
                center_inside = points_in_poly(np.array([[midX, midY]]),
                                               contourBuilding)
                if center_inside:
                    # Approximate will generalize the polygon.
                    mainBuilding = approximate_polygon(contourBuilding,
                                                       tolerance=2)
                    print('Main building: ' + str(mainBuilding))  # DEBUG
Esempio n. 2
0
 def test_square(self):
     v = np.array([[0, 0],
                   [0, 1],
                   [1, 1],
                   [1, 0]])
     assert(points_in_poly([[0.5, 0.5]], v)[0])
     assert(not points_in_poly([[-0.1, 0.1]], v)[0])
Esempio n. 3
0
 def test_triangle(self):
     v = np.array([[0, 0],
                   [1, 0],
                   [0.5, 0.75]])
     assert(points_in_poly([[0.5, 0.7]], v)[0])
     assert(not points_in_poly([[0.5, 0.76]], v)[0])
     assert(not points_in_poly([[0.7, 0.5]], v)[0])
Esempio n. 4
0
def get_polygon_inner_point(polygon):
    """
    Algorithm:
        1) Take a point on the exterior boundary
        2) Find an adjacent point (with digitized coordinates) that lies in the polygon
        3) Return the coordinates of this point
    Parameters
    ----------
    polygon: Polygon
        The polygon
    Returns
    -------
    point: tuple
        (x, y) coordinates for the found points. x and y are integers.
    """
    if isinstance(polygon, Point):
        return int(polygon.x), int(polygon.y)
    if isinstance(polygon, LineString):
        return [int(c) for c in polygon.coords[0]]
    # this function works whether or not the boundary is inside or outside (one pixel around) the
    # object boundary in the mask
    exterior = polygon.exterior.coords
    for x, y in exterior:  # usually this function will return in one iteration
        neighbours = np.array(neighbour_pixels(int(x), int(y)))
        in_poly = np.array(points_in_poly(list(neighbours), exterior))
        if np.count_nonzero(
                in_poly) > 0:  # make sure at least one point is in the polygon
            return neighbours[in_poly][0]
    if len(exterior) == 4:  # fallback for three pixel polygons
        return [int(v) for v in exterior[0]]
    raise ValueError(
        "No points could be found inside the polygon ({}) !".format(
            polygon.wkt))
Esempio n. 5
0
    def _exclude_bad_XYpos(self, GDF):
        """Exclude all points outside of the image overlap area and where the bad data mask is True (if given).

        :param GDF:     <geopandas.GeoDataFrame> must include the columns 'X_UTM' and 'Y_UTM'
        :return:
        """
        from skimage.measure import points_in_poly  # import here to avoid static TLS ImportError

        # exclude all points outside of overlap area
        inliers = points_in_poly(self.XY_mapPoints,
                                 np.swapaxes(np.array(self.COREG_obj.overlap_poly.exterior.coords.xy), 0, 1))
        GDF = GDF[inliers].copy()
        # GDF = GDF[GDF['geometry'].within(self.COREG_obj.overlap_poly.simplify(tolerance=15))] # works but much slower

        assert not GDF.empty, 'No coregistration point could be placed within the overlap area. Check your input data!'

        # exclude all points where bad data mask is True (e.g. points on clouds etc.)
        orig_len_GDF = len(GDF)  # length of GDF after dropping all points outside the overlap polygon
        mapXY = np.array(GDF.loc[:, ['X_UTM', 'Y_UTM']])
        GDF['REF_BADDATA'] = self.COREG_obj.ref.mask_baddata.read_pointData(mapXY) \
            if self.COREG_obj.ref.mask_baddata is not None else False
        GDF['TGT_BADDATA'] = self.COREG_obj.shift.mask_baddata.read_pointData(mapXY) \
            if self.COREG_obj.shift.mask_baddata is not None else False
        GDF = GDF[(~GDF['REF_BADDATA']) & (~GDF['TGT_BADDATA'])]
        if self.COREG_obj.ref.mask_baddata is not None or self.COREG_obj.shift.mask_baddata is not None:
            if not self.q:
                if not GDF.empty:
                    print('With respect to the provided bad data mask(s) %s points of initially %s have been excluded.'
                          % (orig_len_GDF - len(GDF), orig_len_GDF))
                else:
                    warnings.warn('With respect to the provided bad data mask(s) no coregistration point could be '
                                  'placed within an image area usable for coregistration.')

        return GDF
Esempio n. 6
0
def ExtractImage(tc,index =1, offsetx=128,offsety =128,  level = 0):
    offsetx = 128
    offsety = 128
    name = tc.Get_Aname(index)
    pl_arr = tc.AnnotationDots(index)
    pl_arr = np.vstack((pl_arr,pl_arr[0]))
    xi = pl_arr[:,0].min()
    xa = pl_arr[:,0].max()
    yi = pl_arr[:,1].min()
    ya = pl_arr[:,1].max()

    tmpim = tc.img.read_region((xi-offsetx,yi-offsety),level,((xa-xi+2*offsetx)/2**level,(ya-yi+2*offsety)/2**level))
    
    #fig,ax = plt.subplots()
    #ax.imshow(tmpim)    
    #ax.axis('image')
    #ax.axis('off')
    #ax.plot((pl_arr[:,0]-xi+offsetx)/2**level,(pl_arr[:,1]-yi+offsety)/2**level,'y-',linewidth =3)
    
    pl_arr2 = pl_arr
    pl_arr2[:,0] = pl_arr[:,0]-xi+offsetx
    pl_arr2[:,1] = pl_arr[:,1]-yi+offsety
    
    maskx = pl_arr2[:,0].max() + offsetx
    masky = pl_arr2[:,1].max() + offsety
    maskp = [(x,y) for x in range(maskx) for y in range(masky)]
    mask2 = measure.points_in_poly(maskp,pl_arr2)
    mask = np.reshape(mask2,(maskx,masky)).T
    mask = resize(mask, (np.array(tmpim).shape[0],np.array(tmpim).shape[1]))>0

    #plt.figure()
    #plt.imshow(mask)
    return name,np.array(tmpim),mask
Esempio n. 7
0
    def ExtractAnnotationImage(self,
                               index=1,
                               offsetx=128,
                               offsety=128,
                               level=0):
        """
        tc : CRLM instance
        index: index of the annotation
        offsetx,offsety:  offset for extending the anontation area
        level: magnification level for extaction
        """
        name = self.Get_Aname(index)
        pl_arr = self.AnnotationDots(index)
        pl_arr = np.vstack((pl_arr, pl_arr[0]))
        xi = pl_arr[:, 0].min()
        xa = pl_arr[:, 0].max()
        yi = pl_arr[:, 1].min()
        ya = pl_arr[:, 1].max()

        tmpim = self.img.read_region((xi-offsetx,yi-offsety),level,\
                                   (int((xa-xi+2*offsetx)/2**level),int((ya-yi+2*offsety)/2**level)))

        pl_arr2 = pl_arr
        pl_arr2[:, 0] = pl_arr[:, 0] - xi + offsetx
        pl_arr2[:, 1] = pl_arr[:, 1] - yi + offsety

        maskx = pl_arr2[:, 0].max() + offsetx
        masky = pl_arr2[:, 1].max() + offsety
        maskp = [(x, y) for x in range(maskx) for y in range(masky)]
        mask2 = measure.points_in_poly(maskp, pl_arr2)
        mask = np.reshape(mask2, (maskx, masky)).T
        mask = resize(mask,
                      (np.array(tmpim).shape[0], np.array(tmpim).shape[1])) > 0
        return name, np.array(tmpim), mask
Esempio n. 8
0
def get_sequences(tumors):
    list_of_sequence = []
    sequence = []
    j = 0
    while np.count_nonzero(tumors[..., j]) == 0:
        j += 1
    sequence.append(j)
    label_image = label(tumors[..., j])
    contour = find_contours(label_image, 0)[0]
    coords_region_previous = approximate_polygon(contour, tolerance=0)
    previous_index = j
    for i in range(j + 1, tumors.shape[2]):
        if np.count_nonzero(tumors[..., i]) == 0:
            continue
        label_image = label(tumors[..., i])
        centroid = regionprops(label_image)[0].centroid
        centroid = np.asarray([int(x) for x in centroid]).reshape(1, 2)
        if points_in_poly(centroid, coords_region_previous) and abs(i - previous_index) < 3:
            sequence.append(i)
        else:
            list_of_sequence.append(sequence)
            sequence = []
            sequence.append(i)
        contour = find_contours(label_image, 0)[0]
        coords_region_previous = approximate_polygon(contour, tolerance=0)
        previous_index = i
    list_of_sequence.append(sequence)
    return list_of_sequence
Esempio n. 9
0
def merge_sublist(list_of_sequence, tumors):
    flag = True
    while flag:
        merged_sublists = []
        for i in range(1, len(list_of_sequence) - 2):
            if (len(list_of_sequence[i]) == 1 and len(list_of_sequence[i - 1]) >= 2 and len(list_of_sequence[i + 1]) >= 2 and
            abs(list_of_sequence[i - 1][-1] - list_of_sequence[i][0]) < 3 and abs(list_of_sequence[i][-1] - list_of_sequence[i + 1][0]) < 3):

                label_image = label(tumors[..., list_of_sequence[i - 1][-1]])
                contour = find_contours(label_image, 0)[0]
                coords_region_previous = approximate_polygon(contour, tolerance=0)

                label_image = label(tumors[..., list_of_sequence[i + 1][0]])
                centroid = regionprops(label_image)[0].centroid
                centroid = np.asarray([int(x) for x in centroid]).reshape(1, 2)

                if points_in_poly(centroid, coords_region_previous):
                    new_sublist = []
                    new_sublist = list_of_sequence[i - 1] + list_of_sequence[i] + list_of_sequence[i + 1]
                    merged_sublists += (new_sublist, i - 1)
                    flag = True
                    break
            flag = False
        if flag:
            del list_of_sequence[merged_sublists[1]]
            del list_of_sequence[merged_sublists[1]]
            del list_of_sequence[merged_sublists[1]]
            list_of_sequence.append(merged_sublists[0])
            list_of_sequence.sort(key=lambda x:x[0])
    return list_of_sequence
Esempio n. 10
0
def get_polygon_inner_point(polygon):
    """
    Algorithm:
        1) Take a point on the exterior boundary
        2) Find an adjacent point (with digitized coordinates) that lies in the polygon
        3) Return the coordinates of this point
    Parameters
    ----------
    polygon: Polygon
        The polygon
    Returns
    -------
    point: tuple
        (x, y) coordinates for the found points. x and y are integers.
    """
    if isinstance(polygon, Point):
        return int(polygon.x), int(polygon.y)
    if isinstance(polygon, LineString):
        return [int(c) for c in polygon.coords[0]]
    # this function works whether or not the boundary is inside or outside (one pixel around) the
    # object boundary in the mask
    exterior = polygon.exterior.coords
    for x, y in exterior:  # usually this function will return in one iteration
        neighbours = np.array(neighbour_pixels(int(x), int(y)))
        in_poly = np.array(points_in_poly(list(neighbours), exterior))
        if np.count_nonzero(in_poly) > 0:  # make sure at least one point is in the polygon
            return neighbours[in_poly][0]
    if len(exterior) == 4:  # fallback for three pixel polygons
        return [int(v) for v in exterior[0]]
    raise ValueError("No points could be found inside the polygon ({}) !".format(polygon.wkt))
Esempio n. 11
0
def get_tumors_post(sub, coords, min_slice, max_slice):
    struct = disk(4)
    tumors = np.zeros(sub.shape)
    for i in range(min_slice - 10, max_slice + 10):
        if np.count_nonzero(sub[..., i]) == 0:
            continue

        test = binary_opening(sub[..., i], struct)
        label_image = label(test)

        list_of_data = []
        for region in regionprops(label_image):
            data = []
            centroid = tuple(int(x) for x in region.centroid)
            if (region.eccentricity < 0.95 and region.extent > 0.45 and region.area < 3600 and region.area > 50
            and np.sum(points_in_poly(region.coords, coords)) > region.area // 2):
                data.append(centroid)
                data.append(region.area)
                list_of_data.append(data)
        
        if not list_of_data:
            continue

        centroid = max(list_of_data, key=lambda item:item[1])[0]
        segmented_tumor = flood_fill(label_image, centroid, 255)
        segmented_tumor[segmented_tumor < 255] = 0
        tumors[..., i] = segmented_tumor
    tumors /= 255
    return tumors
Esempio n. 12
0
def region_encloses_grid_center(region, grid_centers):
    poly = bbox_to_poly(region.bbox)
    hits = measure.points_in_poly(grid_centers, poly)
    if np.sometrue(hits) and (np.sum(hits) == 1):
        return True, np.argwhere(hits).flatten().tolist()
    else:
        return False, None
Esempio n. 13
0
    def _assign(cells_region: regional.many,
                intensities: IntensityTable,
                use_hull: bool = True,
                verbose: bool = False) -> IntensityTable:

        x = intensities.coords[Indices.X.value].values
        y = intensities.coords[Indices.Y.value].values
        points = pd.DataFrame(dict(x=x, y=y))

        results = pd.DataFrame({'spot_id': range(0, intensities.shape[0])})
        results[Features.CELL_ID] = None

        if verbose:
            cell_iterator = tqdm(range(cells_region.count))
        else:
            cell_iterator = range(cells_region.count)

        for cell_id in cell_iterator:
            if use_hull:
                vertices = cells_region[cell_id].hull
            else:
                vertices = cells_region[cell_id].coordinates
            vertices = np.array(vertices)
            in_poly = points_in_poly(points, vertices)
            results.loc[results.spot_id[in_poly], Features.CELL_ID] = cell_id

        intensities[Features.CELL_ID] = (Features.AXIS,
                                         results[Features.CELL_ID])

        return intensities
Esempio n. 14
0
def points_inside_curve(points, contour):
    """
    Uses skimage.measure.points_in_poly function to find whether points are inside a contour (polygon)
    :param points: 2d array (N, 2) of points coordinates viz. skimage.measure.points_in_poly
    :param contour: 2d array of contour (polygon) coordinates viz. skimage.measure.points_in_poly
    :return: array of bool
    """
    return measure.points_in_poly(points, contour)
Esempio n. 15
0
 def inside(self, coord):
     """
     Determine if a given coordinate is inside the polygon or not.
     Arguments:
         coord: 2 element tuple of int, e.g. (x, y)
     Returns:
         bool, if the coord is inside the polygon.
     """
     return points_in_poly([coord], self._vertices)[0]
Esempio n. 16
0
    def does_contain(self, points: list) -> Sequence[bool]:
        '''Check if the Annotation object contains the given coordinate.

        - Args
            points: A list of points to perform the test

        - Returns
            True if the given coorindate is inside the Annotation object,
            False otherwise
        '''
        return points_in_poly(points, self.coords)
Esempio n. 17
0
def prepare_mask_on_grid(x, y, mask_coords):
    """ Converts mask coordinates of the image mask 
    to the grid of 1/0 on the x,y grid
    Inputs:
        x,y : grid of x,y points
        mask_coords : array of coordinates in pixels of the image_mask

    Outputs:
        grid of points of the mask, of the shape of x  
    """
    xymask = points_in_poly(np.c_[y.flatten(), x.flatten()], mask_coords)
    return xymask.reshape(x.shape).astype(np.int)
Esempio n. 18
0
    def contains_points(self, points: np.ndarray) -> np.ndarray:
        """Test whether points lie inside a polygon.

        Parameters
        ----------
        points : (n,2) np.ndarray
            Description

        Returns
        -------
        mask : (n,) boolean np.ndarray
            True if corresponding point is inside the p olygon
        """
        return measure.points_in_poly(points, self.points)
Esempio n. 19
0
def get_surface(equilibrium,
                psi,
                r=100,
                z=100,
                norm=True,
                closed=True,
                insidelcfs=True):
    """
    Finds points of surface with given value of psi.
    :param equilibrium: Equilibrium object
    :param psi: Value of psi to get the surface for
    :param r: If number, specifies number of points in the r dimension of the mesh. If numpy array,
    gives r grid points.
    :param z: If number, specifies number of points in the z dimension of the mesh. If numpy array,
    gives z grid points.
    :param norm: Specifies whether we are working with normalised values of psi
    :param closed: Are we looking for a closed surface?
    :param insidelcfs: Are we looking for a closed surface inside lcfs?
    :return: List of contours with surface coordinates
    """

    # if r is integer make r grid
    if not isinstance(r, np.ndarray):
        r = np.linspace(equilibrium.R_min, equilibrium.R_max, r)

    # if z is integer make z grid
    if not isinstance(z, np.ndarray):
        z = np.linspace(equilibrium.Z_min, equilibrium.Z_max, z)

    # should we work with psi or psi_n
    if norm:
        psipol = equilibrium.psi_n(R=r, Z=z)
    else:
        psipol = equilibrium.psi(R=r, Z=z)

    # find contours
    contour = find_contour(psipol, psi, r, z)

    # now we want the surfaces which enclose the magnetic acis, not some surfaces outside the vessel
    fluxsurface = []
    magaxis = np.expand_dims(equilibrium._mg_axis, axis=0)
    for i in range(len(contour)):
        # are we looking for a closed magnetic surface and is it closed?
        if closed and curve_is_closed(contour[i]):
            isinside = measure.points_in_poly(magaxis, contour[i])
            # surface inside lcfs has to be enclosing magnetic axis
            if insidelcfs and np.asscalar(isinside):
                fluxsurface.append(contour[i])
    return fluxsurface
Esempio n. 20
0
 def in_Polygon(self, x, y, scale=None):
     self.get_cor(scale)
     # print(self._regions)
     for k, reigon in self._regions.items():
         X = reigon['X']
         Y = reigon['Y']
         polygon = [[x, y] for x, y in zip(X, Y)]
         vertices = np.array(polygon)
         #print(vertices.shape)
         coord = (x, y)
         if points_in_poly([coord], vertices)[0]:
             continue
         else:
             return False
     return True
Esempio n. 21
0
    def _assign(cells_region, spots, use_hull=True, verbose=False):
        res = pd.DataFrame({'spot_id': range(0, spots.shape[0])})
        res['cell_id'] = None

        for cell_id in range(cells_region.count):
            if use_hull:
                verts = cells_region[cell_id].hull
            else:
                verts = cells_region[cell_id].coordinates
            verts = np.array(verts)
            in_poly = points_in_poly(spots, verts)
            res.loc[res.spot_id[in_poly], 'cell_id'] = cell_id
            if verbose:
                cnt = np.sum(in_poly)
                print(cell_id, cnt)

        return res
Esempio n. 22
0
def inside_object(pred, obj):
    # bounding box
    if obj.object_type == '0':
        x1, y1, x2, y2 = obj.coordinates
        x, y = pred.coordinates
        return x1 <= x <= x2 and y1 <= y <= y2
    # bounding ellipse
    if obj.object_type == '1':
        x1, y1, x2, y2 = obj.coordinates
        x, y = pred.coordinates
        x_center, y_center = (x1 + x2) / 2, (y1 + y2) / 2
        x_axis, y_axis = (x2 - x1) / 2, (y2 - y1) / 2
        return ((x - x_center) / x_axis)**2 + ((y - y_center) / y_axis)**2 <= 1
    # mask/polygon
    if obj.object_type == '2':
        num_points = len(obj.coordinates) // 2
        poly_points = obj.coordinates.reshape(num_points, 2, order='C')
        return points_in_poly(pred.coordinates.reshape(1, 2), poly_points)[0]
Esempio n. 23
0
    def find_point(self) -> np.ndarray:
        """Use rejection sampling to find point in polygon.

        Returns
        -------
        point : np.ndarray
            Coordinate of point in the polygon
        """
        # start with guess in center of polygon
        point = self.points.mean(axis=0)

        while not measure.points_in_poly([point], self.points):
            xmin, ymin = self.points.min(axis=0)
            xmax, ymax = self.points.max(axis=0)
            point = np.random.uniform(xmin,
                                      xmax), np.random.uniform(ymin, ymax)

        return point
Esempio n. 24
0
def velocalc(outpath,
             blobCoords, t, NoZeroVelocity=None,
             favoured_direction=None):
    ''' Calculates velocity over the entire blob
        video = number from zero to # of videos
    '''
    if NoZeroVelocity is None:
        NoZeroVelocity = True
    contours1 = getWaveContours(blobCoords, t)
    contours2 = getWaveContours(blobCoords, t+1)

    # only consider positive wave-veloxities (e.g. contours outside the precendent contours)
    w2_end = np.zeros((0, 2))
    len_zeroSpeed = 0
    for w2 in contours2:
        combi = np.zeros(len(w2.T), dtype=bool)
        points_check = w2.T
        for w1 in contours1:
            on_poly = points_on_poly(points_check, w1.T)
            inside = measure.points_in_poly(np.array(points_check), w1.T)
            combi += on_poly + inside
            len_zeroSpeed += len(on_poly)
        w2_end = np.concatenate((w2_end, points_check[~combi]), axis=0)
    if NoZeroVelocity:
        len_zeroSpeed = 0

    # compare the outside-contours (w2_end) with all precedent contours
    w1 = np.zeros((0, 2))
    for w in contours1:
        w1 = np.concatenate((w1, w.T))
    
    # get velocity
    if (len(w2_end) != 0 and len(w1) != 0):
        data = GetFrontDisplacementAlongSpline(w2_end, w1) 
        if favoured_direction is not None:
            data = FilterVelocitiesByDirection(data, favoured_direction)
        all_velocity = np.append(data[:,2], np.zeros(len_zeroSpeed)).flatten()
    elif len_zeroSpeed != 0:
        all_velocity = np.zeros(len_zeroSpeed)
    else:
        all_velocity = []
    
    return all_velocity
Esempio n. 25
0
def boxcheck_merge(df1, df2, pointcol, boxcol, dropcols=False):
    new_df = pd.DataFrame()
    for i, point in enumerate(df1[pointcol]):
        arr_point = np.array(point).reshape(1, 2)
        for j, bbox in enumerate(df2[boxcol]):
            boxcheck = measure.points_in_poly(arr_point, bbox)
            if boxcheck == True:
                series1 = df1.loc[i]
                series2 = df2.loc[j]
                combo_series = series1.append(series2)
                new_df = new_df.append(combo_series, ignore_index=True)
                df1.drop([i], inplace=True)
                break
    if (dropcols == True) & (not new_df.empty):
        new_df.drop(columns=[
            'centroid_skel', 'label_bin', 'median_background',
            'median_intensity', 'pc'
        ],
                    inplace=True)
    return new_df
Esempio n. 26
0
def fill(lung):
    struct = disk(5)
    test = lung.copy()
    for i in range(lung.shape[2]):
        test[..., i] = binary_fill_holes(lung[..., i])

        label_test, num = label(test[..., i], return_num=True)
        if num != 2:
            continue

        chull = convex_hull_object(test[..., i])
        coords = []
        for contour in find_contours(chull, 0):
            coords.append(approximate_polygon(contour, tolerance=0))
        
        if len(coords) < 2:
            continue

        inverted = np.invert(test[..., i])
        distance = ndi.distance_transform_edt(inverted)
        peaks = peak_local_max(distance, labels=inverted)

        left_peaks = points_in_poly(peaks, coords[0])
        right_peaks = points_in_poly(peaks, coords[1])
        peaks_mask = left_peaks | right_peaks
        peaks = peaks[peaks_mask]
        peak_image = np.zeros(lung[..., i].shape)
        peak_image[peaks[:, 0], peaks[:, 1]] = 1

        if len(peaks == 1):
            peak_image[0, 0] = 1

        markers = ndi.label(peak_image)[0]
        labels = watershed(-distance, markers, mask=inverted)
        labels[labels == 1] = 0

        for region in regionprops(labels):
            centroid = np.asarray([int(x) for x in region.centroid]).reshape(1, 2)
            if ((np.sum(points_in_poly(region.coords, coords[0])) < region.area and
            np.sum(points_in_poly(region.coords, coords[1])) < region.area) or 
            (not points_in_poly(centroid, coords[1]) and not points_in_poly(centroid, coords[1]))):
                centroid = tuple(int(x) for x in region.centroid)
                labels = flood_fill(labels, centroid, 0)

        labels[labels > 0] = 255
        test[..., i][labels > 0] = 255

    return test
Esempio n. 27
0
    def leftPressed(self, x, y, mods):
        selected_blobs = self.viewerplus.selected_blobs
        # no selected blobs: select it!
        if len(selected_blobs) == 0:
            selected_blob = self.viewerplus.annotations.clickedBlob(x, y)
            if selected_blob is None:
                self.infoMessage.emit("Click on an area to split.")
                return
            self.viewerplus.addToSelectedList(selected_blob)

        if len(selected_blobs) != 1:
            self.infoMessage.emit("A single selected area is required.")
            self.pick_points.reset()
            return

        condition = points_in_poly(np.array([[x, y]]), self.viewerplus.selected_blobs[0].contour)
        if condition[0] != True:
            self.infoMessage.emit("Click on the selected area to split.")
            return

        self.pick_points.addPoint(x, y, self.pick_style)
Esempio n. 28
0
    def snapToContour(self, points, contour):
        """
        Given a curve specified as a set of points, snap the curve on the blob mask:
          1) the initial segments of the curve are removed until they snap
          2) the end segments of the curve are removed until they snap

        """
        test = points_in_poly(points, contour)
        if test is None or test.shape[0] <= 3:
            return None
        jump = np.gradient(test.astype(int))
        ind = np.nonzero(jump)
        ind = np.asarray(ind)

        snappoints = None
        if ind.shape[1] > 2:
            first_el = ind[0, 0]
            last_el = ind[0, -1]
            snappoints = points[first_el:last_el + 1, :].copy()

        return snappoints
Esempio n. 29
0
def mom_ellipseparams_poly(poly):
    """
    Return the (estimated) moment ellipse params of a polygon.

    Parameters
    ----------
    poly : (N*2) ndarray of double
        list of corner coordinates (r,c) of the polygon.

    Returns
    ----------
    params: list
        [m0, xc, yc, a, b, theta]
        where m0 is the total area
    """
    from copy import deepcopy
    from skimage.measure import points_in_poly
    import moment
    poly = deepcopy(poly)

    xmin, xmax = np.min(poly[:, 1], axis=0.), np.max(poly[:, 1], axis=0.)
    ymin, ymax = np.min(poly[:, 0], axis=0.), np.max(poly[:, 0], axis=0.)

    poly[:, 1] = poly[:, 1]-xmin
    poly[:, 0] = poly[:, 0]-ymin

    img = np.zeros([int(ymax-ymin), int(xmax-xmin)])

    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            if points_in_poly([[i,j]],poly):
                img[i,j]=1.

    if np.sum(img)>0.:
        params=moment.mom_ellipseparams(img)
        params[1]=xmin+params[1]
        params[2]=ymin+params[2]
        return params
    else: 
        return [0., 0., 0., 0., 0., 0.]
Esempio n. 30
0
    def clickedBlob(self, x, y):
        """
        It returns the blob clicked with the smallest area (to avoid problems with overlapping blobs).
        """

        blobs_clicked = []

        for blob in self.seg_blobs:

            point = np.array([[x, y]])
            out = measure.points_in_poly(point, blob.contour)
            if out[0] == True:
                blobs_clicked.append(blob)

        area_min = 100000000.0
        selected_blob = None
        for i in range(len(blobs_clicked)):
            blob = blobs_clicked[i]
            if blob.area < area_min:
                area_min = blob.area
                selected_blob = blob

        return selected_blob
Esempio n. 31
0
    def get_mask(self, shape, region):
        """
        Parameters
        ----------
        region : {'upper left', 'lower right'}
        """
        if region == 'upper left':
            theta = np.linspace(self.THETA_END, self.THETA_START - 2 * np.pi)
        elif region == 'lower right':
            theta = np.linspace(self.THETA_END, self.THETA_START)
        else:
            msg = "Expected 'upper left' or 'lower right'; got %s" % region
            raise ValueError(msg)
        xy_circle = self.circle.point_from_angle(theta).T
        x, y = self.snake_curve()
        xy_curve = np.array((x, y)).T
        xy_poly = np.vstack((xy_curve, xy_circle))

        h, w = shape[:2]
        y_img, x_img = np.mgrid[:h, :w]
        xy_points = np.column_stack((x_img.flat, y_img.flat))

        mask = points_in_poly(xy_points, xy_poly)
        return mask.reshape((h, w))
Esempio n. 32
0
    def get_mask(self, shape, region):
        """
        Parameters
        ----------
        region : {'upper left', 'lower right'}
        """
        if region == 'upper left':
            theta = np.linspace(self.THETA_END, self.THETA_START - 2 * np.pi)
        elif region == 'lower right':
            theta = np.linspace(self.THETA_END, self.THETA_START)
        else:
            msg = "Expected 'upper left' or 'lower right'; got %s" % region
            raise ValueError(msg)
        xy_circle = self.circle.point_from_angle(theta).T
        x, y = self.snake_curve()
        xy_curve = np.array((x, y)).T
        xy_poly = np.vstack((xy_curve, xy_circle))

        h, w = shape[:2]
        y_img, x_img = np.mgrid[:h, :w]
        xy_points = np.column_stack((x_img.flat, y_img.flat))

        mask = points_in_poly(xy_points, xy_poly)
        return mask.reshape((h, w))
Esempio n. 33
0
def select_center_highcontours(contours, xc, yc, radius=3):
    """
    select all the high contours in contours that overlaps with the center region, which is defeined as a square region (xc +/- radius, yc +/- radius). 

    Find all high contours that enclose the centroid xc, yc. +/- radius
    # if skmeasure.points_in_poly([[yc, xc]], contour):
    """
    # setting
    carea = np.pi * radius**2
    ptc = np.array([xc, yc])

    highcontours = select_highcontours(contours)

    ccontours = []

    for contour in highcontours:
        if SignedPolygonArea(contour) >= carea:
            if skmeasure.points_in_poly(np.array([ptc]), contour)[0]:
                ccontours = ccontours+[contour]
        else: 
            if contour_is_close_to_point(contour, ptc, distance=radius):
                ccontours = ccontours+[contour]

    return ccontours
Esempio n. 34
0
def get_search_area(list_of_sequence_merged, tumors, area, coords_region):
    list_above_two = [x for x in list_of_sequence_merged if len(x) >= 2]
    list_of_ratio = []
    for sublist in list_above_two:
        res = np.sum(tumors[..., sublist], axis=2)
        min_value = np.min(res)
        max_value = np.max(res)

        res = (((res - min_value) / (max_value - min_value)) * 255).astype(np.uint8)

        thresh = threshold_otsu(res)
        res_otsu = res > thresh

        l, num = label(res_otsu, return_num=True)

        coords = regionprops(l)[0].coords

        good = np.sum(points_in_poly(coords, coords_region))

        ratio = good / area

        list_of_ratio.append((ratio, sublist[0], sublist[-1]))
        max_value = max(list_of_ratio, key=lambda item:item[0])
    return max_value[1], max_value[2]
Esempio n. 35
0
def get_sub(lung, segmented_lung):
    struct_closing = disk(20)
    struct_opening = disk(3)
    sub = lung.copy()
    for i in range(lung.shape[2]):

        if np.count_nonzero(lung[..., i]) == 0:
            sub[..., i] = np.zeros(lung[..., i].shape)
            continue

        test = lung[..., i]

        coords = []
        for contour in find_contours(segmented_lung[..., i], 0):
            coords.append(approximate_polygon(contour, tolerance=0))

        sub[..., i] = test.astype(np.uint8) - segmented_lung[..., i].astype(np.uint8)

        distance = ndi.distance_transform_edt(sub[..., i])
        local_maxi = peak_local_max(distance, indices=False, labels=sub[..., i])
        markers = ndi.label(local_maxi)[0]
        labels = watershed(-distance, markers, mask=sub[..., i])

        for region in regionprops(labels):
            remove  = True
            for coord in coords:
                if np.sum(points_in_poly(region.coords, coord)) > (region.area // 2):
                    remove = False
            if remove:
                centroid = tuple(int(x) for x in region.centroid)
                labels = flood_fill(labels, centroid, 0)

        labels[labels > 0] = 255
        sub[..., i] = labels

    return sub
def main2D_3D_2D_LR_MeshHair():
    '''1. read obj and img'''
    objPath = '../../github/vrn-07231340/obj/trump-12.obj'
    objLines, vLines, fLines = readObj(objPath)
    imgPath = '../../github/vrn-07231340/examples/scaled/trump-12.jpg'
    img = cv2.imread(imgPath)
    '''2. 2D face landmark'''
    oriFaceLandmark2DList = getLandmark2D(img)
    '''3. create source hair region mesh'''
    gridSize = 15
    rows, cols = img.shape[0], img.shape[1]
    src_rows = np.linspace(0, rows, gridSize)  # 10 rows
    src_cols = np.linspace(0, cols, gridSize)  # 20 columns
    src_rows, src_cols = np.meshgrid(src_rows, src_cols)
    src = np.dstack([src_cols.flat, src_rows.flat])[0]
    '''3.1 create head region ellipse'''
    '''!!!手动修改了椭圆中心点,因为川普本来就是侧脸!!!'''
    ellipseParam = {
        'ellipseCenterX': oriFaceLandmark2DList[ELLIPSE_CENTER][1],
        'ellipseCenterY': oriFaceLandmark2DList[ELLIPSE_CENTER][0] - 10,
        'ellipseSemiX': 70,
        'ellipseSemiY': 90,
        'orientation': np.pi * 1.5
    }
    rr, cc = ellipse_perimeter(ellipseParam['ellipseCenterY'],
                               ellipseParam['ellipseCenterX'],
                               ellipseParam['ellipseSemiY'],
                               ellipseParam['ellipseSemiX'],
                               orientation=ellipseParam['orientation'])
    '''3.2 mesh points in ellipse'''
    ellipseVerts = np.dstack([rr, cc])[0]
    '''3.3 add outer ellipse edge points'''
    edgePoints, outerEllipseVerts = addOuterEllipseEdgeLandmark(
        ellipseParam, img)
    # drawPointsOnImg(ellipseVerts, img, 'g', cover=True)
    # drawPointsOnImg(edgePoints, img, 'r')
    mask = points_in_poly(src, ellipseVerts)
    pointsInEllipseList = []
    indexInEllipseList = []
    for i, (s, m) in enumerate(zip(src, mask)):
        if m == True:
            pointsInEllipseList.append(s)
            indexInEllipseList.append(i)
    assert len(pointsInEllipseList) == len(indexInEllipseList)
    '''3.3 mesh points in face region'''
    faceVerts = []
    for FACE_CONTOUR_INDICE in FACE_CONTOUR_INDICES:
        faceVerts.append([
            oriFaceLandmark2DList[FACE_CONTOUR_INDICE][0],
            oriFaceLandmark2DList[FACE_CONTOUR_INDICE][1]
        ])
    mask = points_in_poly(src, faceVerts)
    pointsInFaceList = []
    indexInFaceList = []
    for i, (s, m) in enumerate(zip(src, mask)):
        if m == True:
            pointsInFaceList.append(s)
            indexInFaceList.append(i)
    assert len(pointsInFaceList) == len(indexInFaceList)
    '''3.4 mesh points between (face region) and (ellipse)'''
    for i, ((pointInEllipseX, pointInEllipseY), indexInEllise) in enumerate(
            zip(pointsInEllipseList, indexInEllipseList)):
        for (pointInFaceX, pointInFaceY) in pointsInFaceList:
            if pointInEllipseX == pointInFaceX and pointInEllipseY == pointInFaceY:
                pointsInEllipseList[i] = np.array([-1, -1])
                indexInEllipseList[i] = -1
    pointsInHairList = []
    for (pointInEllipseX, pointInEllipseY) in pointsInEllipseList:
        if pointInEllipseX != -1 and pointInEllipseY != -1:
            pointsInHairList.append([pointInEllipseX, pointInEllipseY])
    indexInHairList = [x for x in indexInEllipseList if x != -1]
    assert len(pointsInHairList) == len(indexInHairList)
    '''3.5 mesh points above minY'''
    minY = minYInLandmark(oriFaceLandmark2DList)
    hairMeshPointsInEllipseList = []
    hairMeshIndexInEllipseList = []
    for (s, i) in zip(pointsInHairList, indexInHairList):
        srcY = s[1]
        srcX = s[0]
        if srcY < minY and srcY >= 0:
            hairMeshPointsInEllipseList.append((srcX, srcY))
            hairMeshIndexInEllipseList.append(i)
    assert len(hairMeshPointsInEllipseList) == len(hairMeshIndexInEllipseList)
    # drawPointsOnImg(hairMeshPointsInEllipseList, img, 'r')
    '''4. 2D hair mesh points --> 3D hair mesh points'''
    '''4.1 3D hair landmark list: (x, y, z)'''
    # maxminDict = maxminXYZ(objLines)
    # minZ = float(maxminDict['maxZCoord'][2])
    minZ = 60.0
    hairLandmark3DList = []
    for hairLandmark2D in hairMeshPointsInEllipseList:
        hairLandmark3D = (float(hairLandmark2D[0]), float(hairLandmark2D[1]),
                          minZ)
        hairLandmark3DList.append(hairLandmark3D)
    '''4.2 hair landmark color list: (r, g, b)'''
    colorList = []
    for hairLandmark2D in hairMeshPointsInEllipseList:
        r = img[int(hairLandmark2D[0]), int(hairLandmark2D[1]), 2] / 255.0
        g = img[int(hairLandmark2D[0]), int(hairLandmark2D[1]), 1] / 255.0
        b = img[int(hairLandmark2D[0]), int(hairLandmark2D[1]), 0] / 255.0
        color = (r, g, b)
        colorList.append(color)
    '''4.3 3D vLines'''
    hairVLines = []
    for (x, y, z), (r, g, b) in zip(hairLandmark3DList, colorList):
        hairVLine = 'v {} {} {} {} {} {}'.format(x, y, z, r, g, b)
        hairVLines.append(hairVLine)
    '''4.4 write vLines txt'''
    hairVLinesTxtPath = './hairVLines.txt'
    hairVLinesTxt = open(hairVLinesTxtPath, 'w')
    for hairVLine in hairVLines:
        hairVLinesTxt.write(hairVLine + '\n')
    hairVLinesTxt.close()
    '''5. nod hair'''
    nodAngle = 15
    nodCenterMode = 'maxY'
    nodHairVLines = nodHair(objPath, nodAngle, nodCenterMode, hairVLines,
                            hairVLinesTxtPath)
    '''6. 2D nod hair landmark'''
    nodHairLandmark2DList = []
    for nodHairVLine in nodHairVLines:
        _, x, y, _, _, _, _ = nodHairVLine.split()
        nodHairLandmark2DList.append((float(x), float(y)))
    assert len(hairMeshPointsInEllipseList) == len(nodHairLandmark2DList)

    # print hairMeshPointsInEllipseList
    # print nodHairLandmark2DList
    return hairMeshPointsInEllipseList, nodHairLandmark2DList, edgePoints, ellipseVerts, outerEllipseVerts
Esempio n. 37
0
def isinside_polygon(poly1, poly2):
    """ tell if poly1 is enclosed by poly2, i.e. all the points are inside.
    Identical polys are not inside each other """
    return np.all(skmeasure.points_in_poly(poly1, poly2))
Esempio n. 38
0
 def test_type(self):
     assert(points_in_poly([[0, 0]], [[0, 0]]).dtype == np.bool)
Esempio n. 39
0
def checkContour(xRoi, yRoi):
    # check = False
    for contour in contours:
        if measure.points_in_poly([[xRoi, yRoi]], contour):
            return contour
    Im.saveImageRegions()
Esempio n. 40
0
def points_in_poly(points, verts):
    '''see skimage.measure.points_in_poly'''
    return measure.points_in_poly(points, verts)