def returnProcessedImage(que,folder,img_flist):
	X = []
	for fname in img_flist:
		cur_img = imread(folder+'/'+fname , as_grey=True)
		cur_img = 1 - cur_img

		######## randomly add samples

		# random add contrast
		r_for_eq = random()
		cur_img = equalize_adapthist(cur_img,ntiles_x=8,ntiles_y=8,clip_limit=(r_for_eq+0.5)/3)

		
		#random morphological operation
		r_for_mf_1 = random()
		if 0.05 < r_for_mf_1 < 0.25: # small vessel
			selem1 = disk(0.5+r_for_mf_1)
			cur_img = dilation(cur_img,selem1)
			cur_img = erosion(cur_img,selem1)
		elif 0.25 < r_for_mf_1 < 0.5: # large vessel
			selem2 = disk(2.5+r_for_mf_1*3)
			cur_img = dilation(cur_img,selem2)
			cur_img = erosion(cur_img,selem2)
		elif 0.5 < r_for_mf_1 < 0.75: # exudate
			selem1 = disk(9.21)
			selem2 = disk(7.21)
			dilated1 = dilation(cur_img, selem1)
			dilated2 = dilation(cur_img, selem2)
			cur_img = np.subtract(dilated1, dilated2)
		
		cur_img = img_as_float(cur_img)
		X.append([cur_img.tolist()])
	# X = np.array(X , dtype = theano.config.floatX)
	que.put(X)
	return X
Пример #2
0
def removeNoise(img, r=7):

    # Creating a circle inside an array
    c = r # Center
    d = 2 * r + 1 # Diameter
    y, x = np.ogrid[-c:d-c, -c:d-c] # Create a True/False grid in numpy
    mask = x * x + y * y <= r * r # Circular shape
    structuringElement = np.zeros((d, d))
    structuringElement[mask] = 1 # Fill ones at the places with True

    # Applying erosion at the binary image
    eroded = erosion(img, structuringElement)

    # Dilate the remaining pixels from the eroded image
    dilated = dilation(eroded, structuringElement)

    # We have now opened the image. Now we need to close it:

    # We could have done this in the same step as the last, but we want to show all steps
    dilated2 = dilation(dilated, structuringElement)

    # Then we close by eroding back to normal
    eroded2 = erosion(dilated2, structuringElement)

    return eroded, dilated, dilated2, eroded2
Пример #3
0
def get_segmentation_features(im):
    dilwindow = [4, 4]
    imthr = np.where(im > np.mean(im), 0.0, 1.0)
    imdil = morphology.dilation(imthr, np.ones(dilwindow))
    labels = measure.label(imdil)
    labels = imthr * labels
    labels = labels.astype(int)
    regions = measure.regionprops(labels)
    numregions = len(regions)
    while len(regions) < 1:
        dilwindow[0] = dilwindow[0] - 1
        dilwindow[1] = dilwindow[1] - 1
        if dilwindow == [0, 0]:
            regions = None
            break
        imthr = np.where(im > np.mean(im), 0.0, 1.0)
        imdil = morphology.dilation(imthr, np.ones(dilwindow))
        labels = measure.label(imdil)
        labels = imthr * labels
        labels = labels.astype(int)
        regions = measure.regionprops(labels)
    regionmax = get_largest_region(regions, labels, imthr)

    if regionmax is None:
        return (np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan)
    eccentricity = regionmax.eccentricity
    convex_area = regionmax.convex_area
    convex_to_total_area = regionmax.convex_area / regionmax.area
    extent = regionmax.extent
    filled_area = regionmax.filled_area
    return (eccentricity, convex_area, convex_to_total_area, extent,
            filled_area, numregions)
Пример #4
0
def main():
    _bpcl = buffer_mask(bpcl)
    selem = morphology.disk(2)
    _bpcl = morphology.dilation(_bpcl, selem)
    pcs = make_potential_cloud_shadow_mask(nir, water, _bpcl, 0.02, 0.0005)
    pcs_buff = buffer_mask(pcs)
    # pcs_buffer = buffer_mask(pcs_buff)
    pcs_buffer = morphology.dilation(pcs_buff, selem)
    return _bpcl, pcs_buffer
Пример #5
0
def extract_binary_masks_from_structural_channel(Y, min_area_size=30, min_hole_size=15, gSig=5, expand_method='closing', selem=np.ones((3, 3))):
    """Extract binary masks by using adaptive thresholding on a structural channel

    Inputs:
    ------
    Y:                  caiman movie object
                        movie of the structural channel (assumed motion corrected)

    min_area_size:      int
                        ignore components with smaller size

    min_hole_size:      int
                        fill in holes up to that size (donuts)

    gSig:               int
                        average radius of cell

    expand_method:      string
                        method to expand binary masks (morphological closing or dilation)

    selem:              np.array
                        morphological element with which to expand binary masks

    Output:
    -------
    A:                  sparse column format matrix
                        matrix of binary masks to be used for CNMF seeding

    mR:                 np.array
                        mean image used to detect cell boundaries
    """

    mR = Y.mean(axis=0)
    img = cv2.blur(mR, (gSig, gSig))
    img = (img - np.min(img)) / (np.max(img) - np.min(img)) * 255.
    img = img.astype(np.uint8)

    th = cv2.adaptiveThreshold(img, np.max(
        img), cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, gSig, 0)
    th = remove_small_holes(th > 0, min_size=min_hole_size)
    th = remove_small_objects(th, min_size=min_area_size)
    areas = label(th)

    A = np.zeros((np.prod(th.shape), areas[1]), dtype=bool)

    for i in range(areas[1]):
        temp = (areas[0] == i + 1)
        if expand_method == 'dilation':
            temp = dilation(temp, selem=selem)
        elif expand_method == 'closing':
            temp = dilation(temp, selem=selem)

        A[:, i] = temp.flatten('F')

    return A, mR
def extract_region_opening(img, is_demo=False):
    """
    Extracts fingerprint region of image via mophological opening
    """

    after_median = skimage.filter.rank.median(img, skmorph.disk(9))
    after_erode = skmorph.erosion(after_median, skmorph.disk(11))
    after_dil = skmorph.dilation(after_erode, skmorph.disk(5))
    _, t_dil_img = cv2.threshold(after_dil, 240, 40, cv2.THRESH_BINARY)

    if is_demo:
        _, t_med_img = cv2.threshold(after_median, 240, 255, cv2.THRESH_BINARY)
        _, t_erd_img = cv2.threshold(after_erode, 240, 40, cv2.THRESH_BINARY)
        erd_gry = t_erd_img.astype(np.uint8) * 255
        rgb_erd = np.dstack((erd_gry, img, img))
        dil_gry = t_dil_img.astype(np.uint8) * 255
        rgb_dil = np.dstack((dil_gry, img, img))

        plt.subplot(2,2,1)
        plt.imshow(after_erode, cmap="gray", interpolation="nearest")

        plt.subplot(2,2,2)
        plt.imshow(rgb_erd, interpolation="nearest")

        plt.subplot(2,2,3)
        plt.imshow(after_dil, cmap="gray", interpolation="nearest")

        plt.subplot(2,2,4)
        plt.imshow(rgb_dil, interpolation="nearest")
        plt.show()

    return t_dil_img
Пример #7
0
def pestFeatureExtraction(filename):
	selem = disk(8)
	image = data.imread(filename,as_grey=True)
	thresh = threshold_otsu(image)
	elevation_map = sobel(image)
	markers = np.zeros_like(image)

	if ((image<thresh).sum() > (image>thresh).sum()):
		markers[image < thresh] = 1
		markers[image > thresh] = 2
	else:
		markers[image < thresh] = 2
		markers[image > thresh] = 1

	segmentation = morphology.watershed(elevation_map, markers)
	segmentation = dilation(segmentation-1, selem)
	segmentation = ndimage.binary_fill_holes(segmentation)

	segmentation = np.logical_not(segmentation)
	image[segmentation]=0;

	hist = np.histogram(image.ravel(),256,[0,1])

	hist = list(hist[0])
	hist[:] = [float(x) / (sum(hist) - hist[0]) for x in hist]
	hist.pop(0)

	features = np.empty( (1, len(hist)), 'float' )
	
	a = np.array(list(hist))
	f = a.astype('float')
	features[0,:]=f[:]

	return features
Пример #8
0
def getMinorMajorRatio(image):
# First we threshold the image by only taking values greater than the mean to reduce noise in the image
# to use later as a mask

    image = image.copy()
    # Create the thresholded image to eliminate some of the background
    imagethr = np.where(image > np.mean(image),0.,1.0)

    #Dilate the image
    imdilated = morphology.dilation(imagethr, np.ones((4,4)))

    # Create the label list
    label_list = measure.label(imdilated)
    label_list = imagethr*label_list
    label_list = label_list.astype(int)
    # calculate common region properties for each region within the segmentation

    region_list = measure.regionprops(label_list)
    maxregion = getLargestRegion(region_list, label_list, imagethr)
    
    # guard against cases where the segmentation fails by providing zeros
    ratio = 0.0
    if ((not maxregion is None) and  (maxregion.major_axis_length != 0.0)):
        ratio = 0.0 if maxregion is None else  maxregion.minor_axis_length*1.0 / maxregion.major_axis_length
    return ratio
Пример #9
0
def getMinorMajorRatio(image):
    image = image.copy()
    # Create the thresholded image to eliminate some of the background
    imagethr = np.where(image > np.mean(image),0.,1.0)

    #Dilate the image
    imdilated = morphology.dilation(imagethr, np.ones((4,4)))

    # Create the label list
    label_list = measure.label(imdilated)
    label_list = imagethr*label_list
    label_list = label_list.astype(int)

    region_list = measure.regionprops(label_list)
    maxregion = getLargestRegion(region_list, label_list, imagethr)

    # guard against cases where the segmentation fails by providing zeros
    ratio = 0.0
    filled_area = 0.0
    perimeter = 0.0
    solidity = 0.0
    if ((not maxregion is None) and  (maxregion.major_axis_length != 0.0)):
        ratio = 0.0 if maxregion is None else  maxregion.minor_axis_length*1.0 / maxregion.major_axis_length
    filled_area = 0.0 if maxregion is None else maxregion.filled_area
    perimeter = 0.0 if maxregion is None else maxregion.perimeter
    solidity = 0.0 if maxregion is None else maxregion.solidity

    return ratio, filled_area, perimeter, solidity
Пример #10
0
def plot_tbss(img, mean_FA_skeleton, start, end, row_l=6, step=1, title='',
    axis='z', pngfile=None):
    ''' Inspired from plot_two_maps. Plots a TBSS contrast map over the
    skeleton of a mean FA map'''

    # Dilate tbss map
    import numpy as np
    from skimage.morphology import cube, dilation
    from nilearn import image
    d = np.array(image.load_img(img).dataobj)
    dil_tbss = dilation(d, cube(2))
    dil_tbss_img = image.new_img_like(img, dil_tbss)

    slice_nb = int(abs(((end - start) / float(step))))
    images = []

    for line in range(int(slice_nb/float(row_l) + 1)):
        opt = {'title':{True:title,
                        False:None}[line==0],
               'colorbar':False,
               'black_bg':True,
               'display_mode':axis,
               'threshold':0.2,
               'cmap': cm.Greens,
               'cut_coords':range(start + line * row_l * step,
                                       start + (line+1) * row_l * step,
                                       step)}
        method = 'plot_stat_map'
        opt.update({'stat_map_img': mean_FA_skeleton})

        t = getattr(plotting, method).__call__(**opt)


        try:
            # Add overlay
            t.add_overlay(dil_tbss_img, cmap=cm.hot, threshold=0.95, colorbar=True)
        except TypeError:
            print img, 'probably empty tbss map'
            pass

        # Converting to PIL and appending it to the list
        buf = io.BytesIO()
        t.savefig(buf)
        buf.seek(0)
        im = Image.open(buf)
        images.append(im)

    # Joining the images
    imsize = images[0].size
    out = Image.new('RGBA', size=(imsize[0], len(images)*imsize[1]))
    for i, im in enumerate(images):
        box = (0, i * imsize[1], imsize[0], (i+1) * imsize[1])
        out.paste(im, box)

    if pngfile is None:
        import tempfile
        pngfile = tempfile.mkstemp(suffix='.png')[1]
    print 'Saving to...', pngfile, '(%s)'%title

    out.save(pngfile)
Пример #11
0
def mask_to_objects_2d(mask, background=0, offset=None):
    """Convert 2D (binary or label) mask to polygons. Generates borders fitting in the objects.
    Parameters
    ----------
    mask: ndarray
        2D mask array. Expected shape: (height, width).
    background: int
        Value used for encoding background pixels.
    offset: tuple (optional, default: None)
        (x, y) coordinate offset to apply to all the extracted polygons.
    Returns
    -------
    extracted: list of AnnotationSlice
        Each object slice represent an object from the image. Fields time and depth of AnnotationSlice are set to None.
    """
    if mask.ndim != 2:
        raise ValueError("Cannot handle image with ndim different from 2 ({} dim. given).".format(mask.ndim))
    if offset is None:
        offset = (0, 0)
    # opencv only supports contour extraction for binary masks: clean mask and binarize
    mask_cpy = np.zeros(mask.shape, dtype=np.uint8)
    mask_cpy[mask != background] = 255
    # create artificial separation between adjacent touching each other + clean
    contours = dilation(mask, square(3)) - mask
    mask_cpy[np.logical_and(contours > 0, mask > 0)] = background
    mask_cpy = clean_mask(mask_cpy, background=background)
    # extract polygons and labels
    polygons = _locate(mask_cpy, offset=offset)
    objects = list()
    for polygon in polygons:
        # loop for handling multipart geometries
        for curr in flatten_geoms(polygon.geoms) if hasattr(polygon, "geoms") else [polygon]:
            x, y = get_polygon_inner_point(curr)
            objects.append((polygon, mask[y - offset[1], x - offset[0]]))
    return objects
Пример #12
0
def load_scenes(filename):
    zipped_scenes = []
    print 'Working on: ' + filename
    img = data.imread('scenes/' + filename, as_grey=True)
    tmp = img
    tmp = filter.canny(tmp, sigma=2.0)
    tmp = ndimage.binary_fill_holes(tmp)
    #tmp = morphology.dilation(tmp, morphology.disk(2))
    tmp = morphology.remove_small_objects(tmp, 2000)
    contours = measure.find_contours(tmp, 0.8)
    ymin, xmin = contours[0].min(axis=0)
    ymax, xmax = contours[0].max(axis=0)
    if xmax - xmin > ymax - ymin:
        xdest = 1000
        ydest = 670
    else:
        xdest = 670
        ydest = 1000
    src = np.array(((0, 0), (0, ydest), (xdest, ydest), (xdest, 0)))
    dst = np.array(((xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)))
    tform3 = tf.ProjectiveTransform()
    tform3.estimate(src, dst)
    warped = tf.warp(img, tform3, output_shape=(ydest, xdest))
    tmp = filter.canny(warped, sigma=2.0)
    tmp = morphology.dilation(tmp, morphology.disk(2))
    descriptor_extractor.detect_and_extract(tmp)
    obj_key = descriptor_extractor.keypoints
    scen_desc = descriptor_extractor.descriptors
    zipped_scenes.append([warped, scen_desc, obj_key, filename])
    return zipped_scenes
Пример #13
0
def get_segmented_lungs(im):

    binary = im < -320
    cleared = clear_border(binary) 
    cleared=morph(cleared,5)
    label_image = label(cleared)
  
    areas = [r.area for r in regionprops(label_image)]
    areas.sort()
    if len(areas) > 2:
        for region in regionprops(label_image):
            if region.area < areas[-2]:
                for coordinates in region.coords:
                       label_image[coordinates[0], coordinates[1]] = 0
    binary = label_image > 0  
    selem = disk(2)
    binary = binary_erosion(binary, selem)
 
    selem = disk(10)
    binary = binary_closing(binary, selem)
    edges = roberts(binary)
    binary = ndi.binary_fill_holes(edges)
 
    get_high_vals = binary == 0
    im[get_high_vals] = 0
  
    binary = morphology.dilation(binary,np.ones([5,5]))
    return binary
Пример #14
0
def make_thicker(img, filter_size):
    # Make greyscale image thicker
    # Parameter
    
    img = dilation(img, disk(filter_size))

    return img
Пример #15
0
def blobs(image, remove_mb = None, val = 160, size = 100):
    """ Convolve a kernel on the image and a gaussian filter to highligh blobs. Find blobs using the
    Difference of Gaussian. Remove from the list of blobs the blobs that are at the membrane.
    return 3 different list
    """

    thresh = threshold_otsu(image)

    #Find all the blobs in the image using Difference of Gaussian
    blobs_in_image = feature.blob_dog(image, min_sigma=0.01,
                        max_sigma=3, threshold=thresh)
    blob_list = []
    for blob in blobs_in_image:
        y, x, r = blob
        blob_list.append((y, x))



    if remove_mb == None:
        blob_in_image_after_binary = set(blob_list)

    else:
        #Create a mask to remove blobs that are at the membrane and surrounded
        #by bright big object
        binary = image >= val*thresh/100
        binary = dilation(binary, square(3))
        binary = remove_small_objects(binary, min_size=size)
        # Create a list of coordinate with the binary image
        coor_binary = np.nonzero(binary)
        list_blob_masked = zip(*coor_binary)
        #Substract the list of coordinate from the binary image to the list of blobs
        blob_in_image_after_binary = (set(blob_list) - set (list_blob_masked))

    return blob_in_image_after_binary
 def morpho_rec2(self, img, size=10):
     # internal gradient of the cells: 
     se = morphology.diamond(size)
     dil = morphology.dilation(img, se)
     rec = morphology.reconstruction(dil, img, method='erosion').astype(np.dtype('uint8'))
             
     return rec
Пример #17
0
def cluster_process(labels, original, activations):
    rbase = np.zeros(labels.shape)
    rubase = np.zeros(labels.shape)
    rubase[range(0,20),:] = 1
    rubase[:,range(0,20)] = 1
    rubase[range(-20,-1),:] = 1
    rubase[:,range(-20,-1)] = 1
    for i in range(1, int(np.max(labels))+1):
        base = np.zeros(labels.shape)
        base[labels==i] = 1
        li = len(base.nonzero()[0])
        if li>0:
            hull = convex_hull_image(base)
            lh =len(hull.nonzero()[0])
            sel_org = base*original
            sel_act = base*activations
            cond = (li > 4000 and float(lh) / float(li) < 1.07 and perimeter(base)**2.0 / li < 30) or np.max(base * rubase) > 0.5
            # print li>4000 and float(lh)/float(li)<1.07, perimeter(base)**2.0/li<30, np.max(base*rubase)>0.5, np.min(original[base>0])
            hard_array =[li > 4000, float(lh) / float(li) < 1.07]
            optional_array = [perimeter(base)**2.0/li < 25,
                              np.percentile(sel_org[sel_org>0], 5) > 0.2,
                              np.percentile(sel_act, 90) - np.percentile(sel_act, 90)]
            print hard_array, optional_array
            if debug and li>1000:
                rs(base,'subspread cluster')
            if cond:
                rbase = rbase + base
    rbase[rubase.astype(np.bool)] = 1
    return dilation(rbase, selem)
Пример #18
0
def test_accuracy():
    ''' Verify that our implementation returns exactly the same as scikit
    '''
    base_dir = '/home/omar/data/DATA_NeoBrainS12/'
    neo_subject = '30wCoronal/example2/'

    # Read subject files
    t2CurrentSubjectName  = base_dir + 'trainingDataNeoBrainS12/'+neo_subject+'T2_1-1.nii.gz'
    t2CurrentSubject_data = nib.load(t2CurrentSubjectName).get_data()
    affineT2CS            = nib.load(t2CurrentSubjectName).get_affine()
    zoomsT2CS             = nib.load(t2CurrentSubjectName).get_header().get_zooms()[:3]

    n_zooms = (zoomsT2CS[0],zoomsT2CS[0],zoomsT2CS[0])
    t2CurrentSubject_data,affineT2CS = reslice(t2CurrentSubject_data,affineT2CS,zoomsT2CS,n_zooms)

    S = t2CurrentSubject_data.astype(np.float64)

    max_radius = 4
    D = SequencialSphereDilation(S)
    for r in range(1, 1+max_radius):
        D.expand(S)
        expected = dilation(S, ball(r))
        actual = D.get_current_dilation()
        assert_array_equal(expected, actual)
        expected = closing(S, ball(r))
        actual = D.get_current_closing()
        assert_array_equal(expected, actual)
Пример #19
0
def get_mask(img_org):
    noise_ratio = 0.3
    img = img_org > noise_ratio * img_org.max()
    skeleton = skeletonize(img)
    
    mask = dilation(skeleton > 0, disk(2))
    return mask
Пример #20
0
def get_stomata(max_proj_image, min_obj_size=200, max_obj_size=1000):
    """Performs image segmentation from a max_proj_image.
     Disposes of objects in range min_obj_size to
    max_obj_size

    :param max_proj_image: the maximum projection image
    :type max_proj_image: numpy.ndarray, uint16
    :param min_obj_size: minimum size of object to keep
    :type min_obj_size: int
    :param max_obj_size: maximum size of object to keep
    :type max_obj_size: int
    :returns: list of [ [coordinates of kept objects - list of slice objects],
                        binary object image - numpy.ndarray,
                        labelled object image - numpy.ndarray
                     ]

    """

    # pore_margin = 10
    # max_obj_size = 1000
    # min_obj_size = 200
    # for prop, value in segment_options:
    #     if prop == 'pore_margin':
    #         pore_margin = value
    #     if prop == 'max_obj_size':
    #         max_obj_size = value
    #     if prop == 'min_obj_size':
    #         min_obj_size = value
    #
    # print(pore_margin)
    # print(max_obj_size)
    # print(min_obj_size)

    #rescale_min = 50
    #rescale_max= 100
    #rescaled = exposure.rescale_intensity(max_proj_image, in_range=(rescale_min,rescale_max))
    rescaled = max_proj_image
    seed = np.copy(rescaled)
    seed[1:-1, 1:-1] = rescaled.max()
    #mask = rescaled
    #if gamma != None:
    #    rescaled = exposure.adjust_gamma(max_proj_image, gamma)
    #filled = reconstruction(seed, mask, method='erosion')
    closed = dilation(rescaled)
    seed = np.copy(closed)
    seed[1:-1, 1:-1] = closed.max()
    mask = closed


    filled = reconstruction(seed, mask, method='erosion')
    label_objects, nb_labels = ndimage.label(filled)
    sizes = np.bincount(label_objects.ravel())
    mask_sizes = sizes
    mask_sizes = (sizes > min_obj_size) & (sizes < max_obj_size)
    #mask_sizes = (sizes > 200) & (sizes < 1000)
    mask_sizes[0] = 0
    big_objs = mask_sizes[label_objects]
    stomata, _ = ndimage.label(big_objs)
    obj_slices = ndimage.find_objects(stomata)
    return [obj_slices, big_objs, stomata]
Пример #21
0
def largest_region(imData):

    belowMeanFilter = np.where(imData > np.mean(imData), 0., 1.0)
    dialated = morphology.dilation(belowMeanFilter, np.ones((3, 3)))
    regionLabels = (belowMeanFilter * measure.label(dialated)).astype(int)

    # calculate common region properties for each region within the segmentation
    regions = measure.regionprops(regionLabels)
    areas = [(None
              if sum(belowMeanFilter[regionLabels == region.label]) * 1.0 / region.area < 0.50
              else region.filled_area)
             for region in regions]

    if len(areas) > 0:

        regionMax = regions[np.argmax(areas)]

        # trim image to the max region
        regionMaxImg = trim_image(
            np.minimum(
                imData*np.where(regionLabels == regionMax.label, 1, 255),
                255))

        # rotate
        angle = intertial_axis(regionMaxImg)[2]
        rotatedRegionMaxImg = ndimage.rotate(regionMaxImg, np.degrees(angle))
        rotatedRegionMaxImg = trim_image(trim_image(rotatedRegionMaxImg, 0), 255)

    else:
        regionMax = None
        rotatedRegionMaxImg = None
        angle = 0

    return regionMax, rotatedRegionMaxImg, angle, regionLabels, regions, areas, belowMeanFilter, dialated
Пример #22
0
def get_symbols(image):
  dil_eros = bin_search(dilatation_cross_numb, [image], (1, 16), 1.0, "dec")
  block_size = 50
  binary_adaptive_image = erosion(dilation(threshold_adaptive(
    array(image.convert("L")), block_size, offset=10),
      square(dil_eros)), square(dil_eros))

  all_labels = label(binary_adaptive_image, background = True)
  objects = find_objects(all_labels)

  av_width = av_height = 0
  symbols = []

  for obj in objects:
    symb = (binary_adaptive_image[obj], (obj[0].start, obj[1].start))
    symbols.append(symb)
    av_height += symb[0].shape[0]
    av_width += symb[0].shape[1]

  av_width /= float(len(objects))
  av_height /= float(len(objects))

  symbols = [symb for symb in symbols
    if symb[0].shape[0] >= av_height and symb[0].shape[1] >= av_width]

  return symbols
def find_edges(img, sigma = 4):
    img = feature.canny(img, sigma)

    selem = disk(10)
    img = dilation(img, selem)

    return img
Пример #24
0
def get_distorted(image, params, orient = "horizont"):
  shifts = []
  np_image = array(image.convert("L"))
  for el in params:
    if el[0] == "sin":
      shifts.append(lambda x: np_image.shape[0] / el[1] * \
        np.sin(x * el[2] / np_image.shape[1]))
    if el[0] == "cos":
      shifts.append(lambda x: np_image.shape[0] / el[1] * \
        np.cos(x * el[2] / np_image.shape[1]))
    if el[0] == "triang":
      lambda x: np_image.shape[0] / el[1] * \
        (x / el[2] / np_image.shape[1] - math.floor(x / (el[2] / np_image.shape[1])))
    if el[0] == "erosion":
      np_image = erosion(np_image, square(el[1]))
    if el[0] == "dilation":
      np_image = dilation(np_image, square(el[1]))

  if orient == "horizont":
    for idx in xrange(np_image.shape[0]):
      for shift in shifts:
        np_image[idx,:] = np.roll(np_image[idx,:], int(shift(idx)))
  if orient == "vert":
    for idx in xrange(np_image.shape[1]):
      for shift in shifts:
        np_image[:, idx] = np.roll(np_image[:, idx], int(shift(idx)))

  return Image.fromarray(np_image)
Пример #25
0
def buffer_pcl(pcl):

    from skimage.morphology import dilation, disk
    selem = disk(3)
    dilated = dilation(pcl, selem)

    return dilated
Пример #26
0
def main():
    plt.figure(figsize=(25, 24))
    planes = ['samolot00.jpg', 'samolot01.jpg', 'samolot03.jpg', 'samolot04.jpg', 'samolot05.jpg','samolot07.jpg',
              'samolot08.jpg', 'samolot09.jpg', 'samolot10.jpg', 'samolot11.jpg', 'samolot12.jpg', 'samolot13.jpg',
              'samolot14.jpg', 'samolot15.jpg', 'samolot16.jpg', 'samolot17.jpg', 'samolot18.jpg', 'samolot20.jpg']
    i = 1
    for file in planes:
        img = data.imread(file, as_grey=True)
        img2 = data.imread(file)
        ax = plt.subplot(6, 3, i)
        ax.axis('off')
        img **= 0.4
        img = filter.canny(img, sigma=3.0)
        img = morphology.dilation(img, morphology.disk(4))
        img = ndimage.binary_fill_holes(img)
        img = morphology.remove_small_objects(img, 1000)
        contours = measure.find_contours(img, 0.8)
        ax.imshow(img2, aspect='auto')
        for n, contour in enumerate(contours):
            ax.plot(contour[:, 1], contour[:, 0], linewidth=1.5)
            center = (sum(contour[:, 1])/len(contour[:, 1]), sum(contour[:, 0])/len(contour[:, 0]))
            ax.scatter(center[0], center[1], color='white')
        i += 1

    plt.savefig('zad2.pdf')
Пример #27
0
def segment_lowest_cluster(img_k):
    """Returns a binarized image with only the smallest 
    cluster of the kmeans."""

    mini_img_k = np.amin(img_k)
    darkest_cluster = dilation(img_k < mini_img_k + 1, disk(3))
    return darkest_cluster
Пример #28
0
 def get_large_wsl(self, labelimage):
     se = morphology.diamond(1)
     dil = morphology.dilation(labelimage, se)
     ero = morphology.erosion(labelimage, se)
     grad = dil - ero
     res = np.zeros(labelimage.shape)
     res[grad>0] = 255
     return res
Пример #29
0
def getContourImage(imageFile):
    planeImage = data.imread(imageFile,True)
    imageArray = np.asarray(planeImage)
    averageColor = np.mean(imageArray)
    imageArray = getBlackAndWhiteImage(imageArray,averageColor*0.85)
    imageArray = filters.sobel(imageArray)
    imageArray = morphology.dilation(imageArray,morphology.disk(3))
    return imageArray
Пример #30
0
 def get_external_wsl(self, labelimage):
     #se = morphology.square(3)
     se = morphology.diamond(1)
     dil = morphology.dilation(labelimage, se)
     grad = dil - labelimage
     res = np.zeros(labelimage.shape)
     res[grad>0] = 255
     return res
Пример #31
0
def Bio_edgeview(B, E, cc=None, g=1, show_now=True):
    """ Bio_edgeview(B, E, c, g)

     Toolbox: Balu
       Display gray or color image I overimposed by color pixels determined
       by binary image E. Useful to display the edges of an image.
       Variable c is the color vector [r g b] indicating the color to be displayed
      (default: c = [1 0 0], i.e., red)
       Variable g is the number of pixels of the edge lines, default g = 1

     Example to display a red edge of a food:
        from balu.ImagesAndData import balu_imageload
        from balu.ImageProcessing import Bim_segbalu
        from balu.InputOutput import Bio_edgeview

        I = balu_imageload('testimg2.jpg')             # Input image
        R, E, J = Bim_segbalu(I)                       # Segmentation
        Bio_edgeview(I, E)

     D.Mery, PUC-DCC, Apr. 2008
     http://dmery.ing.puc.cl

     With collaboration from:
     Diego Patiño ([email protected]) -> Translated implementation into python (2016)
    %"""

    if cc is None:
        cc = np.array([1, 0, 0])

    B = B.astype(float)

    if B.max() > 1:
        B /= 256.0

    if len(B.shape) == 2:
        N, M = B.shape
        J = np.zeros((N, M, 3))
        J[:, :, 0] = B
        J[:, :, 1] = B
        J[:, :, 2] = B
        B = J

    B1 = B[:, :, 0]
    B2 = B[:, :, 1]
    B3 = B[:, :, 2]

    Z = B1 == 0
    Z = np.logical_and(Z, B2 == 0)
    Z = np.logical_and(Z, B3 == 0)
    ii, jj = np.where(Z == 1)
    if ii.size > 0:
        B1[ii, jj] = 1 / 256.0
        B2[ii, jj] = 1 / 256.0
        B3[ii, jj] = 1 / 256.0

    filterwarnings('ignore')
    E = dilation(E, square(g))
    ii, jj = np.where(E == 1)
    B1[ii, jj] = cc[0]
    B2[ii, jj] = cc[1]
    B3[ii, jj] = cc[2]
    Y = B.astype(float)
    Y[:, :, 0] = B1
    Y[:, :, 1] = B2
    Y[:, :, 2] = B3
    imshow((255 * Y).astype('uint8'))
    if show_now:
        show()
Пример #32
0
 def cornerDetect(self, img):
     self.img = copy.deepcopy(img)
     height, width = self.img.shape[0:2]
     gray = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
     # edges = filters.sobel(gray)
     # edges = img_as_ubyte(edges)
     # ret, binary = cv2.threshold(edges, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
     ret, binary = cv2.threshold(gray, 10, 255, 1)
     binary = sm.dilation(binary, sm.disk(3))
     contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE,
                                            cv2.CHAIN_APPROX_SIMPLE)
     box = None
     area_max = 0
     for i in range(len(contours)):
         cnt = contours[i]
         area = cv2.contourArea(cnt)
         if area < area_max:
             continue
         area_max = area
         epsilon = 0.001 * cv2.arcLength(cnt, True)
         approx = cv2.approxPolyDP(cnt, epsilon, True)
         box = minAreaRect(cnt)
         box = np.int0(box)
     ex = 0
     if area_max < 3 / 4 * width * height:
         ex = 350
         box = self.cornerDetect_bk()
     cv2.drawContours(img, [box], 0, (0, 255, 0), 10)
     # cv2.imwrite('test.png', img)
     # quit()
     self.areas = []
     self.bases = []
     self.bases.append((max(0, box[0][1] - SHORT_EXTENSION),
                        max(0, box[0][0] - SHORT_EXTENSION)))
     self.areas.append(
         self.img[max(0, box[0][1] -
                      SHORT_EXTENSION):min(box[0][1] + LONG_EXTENSION +
                                           ex, height),
                  max(0, box[0][0] -
                      SHORT_EXTENSION):min(box[0][0] + LONG_EXTENSION +
                                           ex, width)])
     self.bases.append((max(0, box[1][1] - SHORT_EXTENSION),
                        max(0, box[1][0] - LONG_EXTENSION - ex)))
     self.areas.append(
         self.img[max(0, box[1][1] -
                      SHORT_EXTENSION):min(box[1][1] + LONG_EXTENSION +
                                           ex, height),
                  max(0, box[1][0] - LONG_EXTENSION -
                      ex):min(box[1][0] + SHORT_EXTENSION, width)])
     self.bases.append((max(0, box[2][1] - LONG_EXTENSION - ex),
                        max(0, box[2][0] - LONG_EXTENSION - ex)))
     self.areas.append(
         self.img[max(0, box[2][1] - LONG_EXTENSION -
                      ex):min(box[2][1] + SHORT_EXTENSION, height),
                  max(0, box[2][0] - LONG_EXTENSION -
                      ex):min(box[2][0] + SHORT_EXTENSION, width)])
     self.bases.append((max(0, box[3][1] - LONG_EXTENSION - ex),
                        max(0, box[3][0] - SHORT_EXTENSION)))
     self.areas.append(
         self.img[max(0, box[3][1] - LONG_EXTENSION -
                      ex):min(box[3][1] + SHORT_EXTENSION, height),
                  max(0, box[3][0] -
                      SHORT_EXTENSION):min(box[3][0] + LONG_EXTENSION +
                                           ex, width)])
     return [self.bases, self.areas]
Пример #33
0
print("imagen recortada binarizada")

borde2 = sobel(F, mask=None)
Z = (borde2 > 0.1).astype('uint32')
imshow(Z, cmap='gray')
show()

print("histograma de imagen binarizada")
plt.hist(borde2.ravel(), 256, [0, 1])
plt.show()

selSquare = square(14)
selDisk = disk(12)
selRectangle = rectangle(6, 20)

IDilationSquare = dilation(Z, selem=selSquare)
#imshow(IDilationSquare, cmap='gray')
#show()
IDilationDisk = dilation(Z, selem=selDisk)
#imshow(IDilationDisk, cmap='gray')
#show()
print("imagen recortada y aplicando dilatacion para segmentar")
IDilationRectangle = dilation(Z, selem=selRectangle)
imshow(IDilationRectangle, cmap='gray')
show()

IClosingSquare = closing(IDilationDisk, selem=selSquare)
#imshow(IClosingSquare, cmap='gray')
#show()
IClosingDisk = closing(IDilationDisk, selem=selDisk)
#imshow(IClosingDisk, cmap='gray')
Пример #34
0
norm0 = feature.canny(normal_r[:, :, 0],
                      sigma=sigma,
                      low_threshold=low_thresh,
                      use_quantiles=quant)
norm1 = feature.canny(normal_r[:, :, 1],
                      sigma=sigma,
                      low_threshold=low_thresh,
                      use_quantiles=quant)
norm2 = feature.canny(normal_r[:, :, 2],
                      sigma=sigma,
                      low_threshold=low_thresh,
                      use_quantiles=quant)
norm_cont1 = norm0 + norm1 + norm2

thresh_depth = depth_cont  #> 0.05*np.max(depth_cont)
depth_cont2 = morphology.dilation(thresh_depth.astype(int))
depth_cont3 = morphology.dilation(morphology.dilation(depth_cont2))
diff = norm_cont1 - depth_cont3
cont = (diff > 0.1) + thresh_depth
cont = 1 - morphology.dilation(cont.astype(float))

gauss = filters.gaussian(normal_r, sigma=0, multichannel=True)
norm_cont2 = filters.sobel(gauss[:, :, 1]) + filters.sobel(
    gauss[:, :, 2]) + filters.sobel(gauss[:, :, 0])
thresh_cont2 = norm_cont2 > 0.3 * np.max(norm_cont2)

#plt.subplot(2,2,1)
#plt.imshow(normal_r)
#plt.subplot(2,2,2)
#plt.imshow(depth_r)
#plt.subplot(2,2,3)
Пример #35
0
def extractFeatV1(image_file):
    image = imread(image_file, as_grey=True)
    image = image.copy()
        
    # Create the thresholded image to eliminate some of the background
    imagethr = np.where(image > np.mean(image),0.,1.0)

    #Dilate the image
    imdilated = morphology.dilation(imagethr, np.ones((4,4)))

    # Create the label list
    label_list = measure.label(imdilated)
    label_list = imagethr*label_list
    label_list = label_list.astype(int)
    
    region_list = measure.regionprops(label_list, image)
    maxregion = getLargestRegion(region_list, label_list, imagethr)
    
    # guard against cases where the segmentation fails by providing zeros
    #angle = 0.0
    minor_axis = 0.0
    major_axis = 0.0
    area = 0.0
    convex_area = 0.0
    perimeter = 0.0
    equivalent_diameter = 0.0
    solidity = 0.0
    eccentricity = 0.0
    extent = 0.0
    mean_intensity = 0.0
    weighted_moments_normalized = [0.0] * 13
    exclude = [0, 1, 4] # this indices contain NaN
    if not maxregion is None:
        #angle = maxregion.orientation*180.0/pi
        minor_axis = maxregion.minor_axis_length
        major_axis = maxregion.major_axis_length
        area = maxregion.area
        convex_area = maxregion.convex_area
        extent = maxregion.extent
        perimeter = maxregion.perimeter
        equivalent_diameter = maxregion.perimeter
        solidity = maxregion.solidity
        eccentricity = maxregion.eccentricity
        mean_intensity = maxregion.mean_intensity
        #print maxregion.weighted_moments_normalized.shape
        tmp = maxregion.weighted_moments_normalized.reshape((16))
        tmp = np.nan_to_num(tmp)
        weighted_moments_normalized = [tmp[i] for i in range(16) if i not in exclude]

    axis_ratio = tryDivide(minor_axis, major_axis)

    region_feat = [
        axis_ratio,
        minor_axis,
        major_axis,
        area,
        convex_area,
        extent,
        perimeter,
        equivalent_diameter,
        solidity,
        eccentricity,
        mean_intensity,
    ]
    region_feat += weighted_moments_normalized

    # concat all the features
    feat = region_feat
    feat = np.asarray(feat, dtype="float32")
    
    return feat
Пример #36
0
    def dilation(self):
        image = self.image
        se = star(5)
        dil = morphology.dilation(image, se)

        self.plotResult(dil, 'Dilation')
Пример #37
0
from skimage import io, morphology, filters
from matplotlib import pyplot as plt

image = io.imread("../result/lena_SLIC_boundary.png", as_gray=True)
image = morphology.dilation(image)
io.imshow(image)
plt.show()

image = morphology.erosion(image)
io.imshow(image)
plt.show()
Пример #38
0
def extract_binary_masks_from_structural_channel(Y,
                                                 min_area_size=30,
                                                 min_hole_size=15,
                                                 gSig=5,
                                                 expand_method='closing',
                                                 selem=np.ones((3, 3))):
    """Extract binary masks by using adaptive thresholding on a structural channel

    Inputs:
    ------
    Y:                  caiman movie object
                        movie of the structural channel (assumed motion corrected)

    min_area_size:      int
                        ignore components with smaller size

    min_hole_size:      int
                        fill in holes up to that size (donuts)

    gSig:               int
                        average radius of cell

    expand_method:      string
                        method to expand binary masks (morphological closing or dilation)

    selem:              np.array
                        morphological element with which to expand binary masks

    Output:
    -------
    A:                  sparse column format matrix
                        matrix of binary masks to be used for CNMF seeding

    mR:                 np.array
                        mean image used to detect cell boundaries
    """

    mR = Y.mean(axis=0)
    img = cv2.blur(mR, (gSig, gSig))
    img = (img - np.min(img)) / (np.max(img) - np.min(img)) * 255.
    img = img.astype(np.uint8)

    th = cv2.adaptiveThreshold(img, np.max(img),
                               cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                               cv2.THRESH_BINARY, gSig, 0)
    th = remove_small_holes(th > 0, min_size=min_hole_size)
    th = remove_small_objects(th, min_size=min_area_size)
    areas = label(th)

    A = np.zeros((np.prod(th.shape), areas[1]), dtype=bool)

    for i in range(areas[1]):
        temp = (areas[0] == i + 1)
        if expand_method == 'dilation':
            temp = dilation(temp, selem=selem)
        elif expand_method == 'closing':
            temp = dilation(temp, selem=selem)

        A[:, i] = temp.flatten('F')

    return A, mR
Пример #39
0
def imgpros(URL):
			import numpy as np
			
			from skimage.segmentation import slic
			from skimage import filters,io,morphology,color,data,exposure,img_as_float,feature
			from scipy import ndimage as ndi
			from skimage import feature
			from skimage import io
			from skimage.morphology import watershed
			from skimage.feature import peak_local_max
			from skimage.morphology import closing,dilation,opening,white_tophat,erosion,skeletonize
			from skimage.morphology import disk
			from skimage.filters.rank import median,mean
			from skimage.measure import regionprops
			from skimage.morphology import remove_small_objects

			d1  = disk(0)     #(EROSION)Thickness improvement
			d2 = disk(10)      #opening radius 
			d3 = disk(0)      #Noise reduction                    
			d4 = disk(0)      #smoothing
			d5 = disk(1)      #Dilation
			bimg = io.imread(URL)
			cimg = bimg
			image = img_as_float(cimg)
			gamma_corrected = exposure.adjust_gamma(image, 1)
			gamma_corrected = exposure.equalize_hist(gamma_corrected)
			gamma_corrected = exposure.equalize_adapthist(gamma_corrected)
			gamma_corrected = exposure.rescale_intensity(gamma_corrected)
			img  =gamma_corrected 
			imgg = color.rgb2gray(img)
			imgg1 = mean(imgg, d4)
			imgg2 = median(imgg1, d3) 
			dilated =dilation(imgg2,d5)
			eroded = erosion(dilated, d1) 
			thresh = filters.threshold_adaptive(eroded,250,'gaussian')
			BW = imgg>=thresh
			BW_smt = remove_small_objects(BW,500) 
			opened = opening(BW_smt ,d2)
			distance = ndi.distance_transform_edt(~BW_smt)
			labels = morphology.label(~opened, background=-1)
			labels = morphology.remove_small_objects(labels,500) 
			########################################################
			props = regionprops(labels)
			END = []
			for i in range(len(np.unique(labels))-1):
			    if 10 in props[i].coords:
			        END.append(i)
			for i in range(len(np.unique(labels))-1):
			    if 899 in props[i].coords:
			        END.append(i)
			#########################################################
			from scipy import ndimage
			X = []
			Y = []
			for i in np.unique(labels)[1:]:
			    X.append(ndimage.measurements.center_of_mass(labels == i)[1])
			    Y.append(ndimage.measurements.center_of_mass(labels == i)[0])

			from skimage.color import label2rgb
			import matplotlib.patches as mpatches

			label_overlay = label2rgb(labels, image=image)
			global fig
			global ax
			fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
			#ax.imshow(label_overlay)

			props = regionprops(labels)
			for  i in range(len(regionprops(labels))):

			    
			    if props[i].area < 100:
			        continue 
			    if i in END:
			        minr, minc, maxr, maxc = props[i].bbox
			        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
			                               fill=False, edgecolor='white', linewidth=1)
			    else:
			        minr, minc, maxr, maxc = props[i].bbox
			        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
			                               fill=False, edgecolor='red', linewidth=1)
			        
			    
			ax.add_patch(rect)
			global ans
			ans = len(np.unique(labels))
Пример #40
0
def merge_overlapping_images(metadata, inputs):
    """
    Merge simultaneous overlapping images that cover the area of interest.
    When the area of interest is located at the boundary between 2 images, there
    will be overlap between the 2 images and both will be downloaded from Google
    Earth Engine. This function merges the 2 images, so that the area of interest
    is covered by only 1 image.

    KV WRL 2018

    Arguments:
    -----------
    metadata: dict
        contains all the information about the satellite images that were downloaded
    inputs: dict with the following keys
        'sitename': str
            name of the site
        'polygon': list
            polygon containing the lon/lat coordinates to be extracted,
            longitudes in the first column and latitudes in the second column,
            there are 5 pairs of lat/lon with the fifth point equal to the first point:
            ```
            polygon = [[[151.3, -33.7],[151.4, -33.7],[151.4, -33.8],[151.3, -33.8],
            [151.3, -33.7]]]
            ```
        'dates': list of str
            list that contains 2 strings with the initial and final dates in
            format 'yyyy-mm-dd':
            ```
            dates = ['1987-01-01', '2018-01-01']
            ```
        'sat_list': list of str
            list that contains the names of the satellite missions to include:
            ```
            sat_list = ['L5', 'L7', 'L8', 'S2']
            ```
        'filepath_data': str
            filepath to the directory where the images are downloaded

    Returns:
    -----------
    metadata_updated: dict
        updated metadata

    """

    # only for Sentinel-2 at this stage (not sure if this is needed for Landsat images)
    sat = 'S2'
    filepath = os.path.join(inputs['filepath'], inputs['sitename'])
    filenames = metadata[sat]['filenames']

    # find the pairs of images that are within 5 minutes of each other
    time_delta = 5 * 60  # 5 minutes in seconds
    dates = metadata[sat]['dates'].copy()
    pairs = []
    for i, date in enumerate(metadata[sat]['dates']):
        # dummy value so it does not match it again
        dates[i] = pytz.utc.localize(datetime(1, 1, 1) + timedelta(days=i + 1))
        # calculate time difference
        time_diff = np.array(
            [np.abs((date - _).total_seconds()) for _ in dates])
        # find the matching times and add to pairs list
        boolvec = time_diff <= time_delta
        if np.sum(boolvec) == 0:
            continue
        else:
            idx_dup = np.where(boolvec)[0][0]
            pairs.append([i, idx_dup])

    # because they could be triplicates in S2 images, adjust the pairs for consecutive merges
    for i in range(1, len(pairs)):
        if pairs[i - 1][1] == pairs[i][0]:
            pairs[i][0] = pairs[i - 1][0]

    # check also for quadruplicates and remove them
    pair_first = [_[0] for _ in pairs]
    idx_remove_pair = []
    for idx in np.unique(pair_first):
        # quadruplicate if trying to merge 3 times the same image with a successive image
        if sum(pair_first == idx) == 3:
            # remove the last image: 3 .tif files + the .txt file
            idx_last = [pairs[_]
                        for _ in np.where(pair_first == idx)[0]][-1][-1]
            fn_im = [
                os.path.join(filepath, 'S2', '10m', filenames[idx_last]),
                os.path.join(filepath, 'S2', '20m',
                             filenames[idx_last].replace('10m', '20m')),
                os.path.join(filepath, 'S2', '60m',
                             filenames[idx_last].replace('10m', '60m')),
                os.path.join(
                    filepath, 'S2', 'meta',
                    filenames[idx_last].replace('_10m',
                                                '').replace('.tif', '.txt'))
            ]
            for k in range(4):
                os.chmod(fn_im[k], 0o777)
                os.remove(fn_im[k])
            # store the index of the pair to remove it outside the loop
            idx_remove_pair.append(np.where(pair_first == idx)[0][-1])
    # remove quadruplicates from list of pairs
    pairs = [i for j, i in enumerate(pairs) if j not in idx_remove_pair]

    # for each pair of image, first check if one image completely contains the other
    # in that case keep the larger image. Otherwise merge the two images.
    for i, pair in enumerate(pairs):
        # get filenames of all the files corresponding to the each image in the pair
        fn_im = []
        for index in range(len(pair)):
            fn_im.append([
                os.path.join(filepath, 'S2', '10m', filenames[pair[index]]),
                os.path.join(filepath, 'S2', '20m',
                             filenames[pair[index]].replace('10m', '20m')),
                os.path.join(filepath, 'S2', '60m',
                             filenames[pair[index]].replace('10m', '60m')),
                os.path.join(
                    filepath, 'S2', 'meta',
                    filenames[pair[index]].replace('_10m',
                                                   '').replace('.tif', '.txt'))
            ])
        # get polygon for first image
        polygon0 = SDS_tools.get_image_bounds(fn_im[0][0])
        im_epsg0 = metadata[sat]['epsg'][pair[0]]
        # get polygon for second image
        polygon1 = SDS_tools.get_image_bounds(fn_im[1][0])
        im_epsg1 = metadata[sat]['epsg'][pair[1]]
        # check if epsg are the same
        if not im_epsg0 == im_epsg1:
            print(
                'WARNING: there was an error as two S2 images do not have the same epsg,'
                +
                ' please open an issue on Github at https://github.com/kvos/CoastSat/issues'
                + ' and include your script so we can find out what happened.')
            break
        # check if one image contains the other one
        if polygon0.contains(polygon1):
            # if polygon0 contains polygon1, remove files for polygon1
            for k in range(4):  # remove the 3 .tif files + the .txt file
                os.chmod(fn_im[1][k], 0o777)
                os.remove(fn_im[1][k])
            # print('removed 1')
            continue
        elif polygon1.contains(polygon0):
            # if polygon1 contains polygon0, remove image0
            for k in range(4):  # remove the 3 .tif files + the .txt file
                os.chmod(fn_im[0][k], 0o777)
                os.remove(fn_im[0][k])
            # print('removed 0')
            # adjust the order in case of triplicates
            if i + 1 < len(pairs):
                if pairs[i + 1][0] == pair[0]: pairs[i + 1][0] = pairs[i][1]
            continue
        # otherwise merge the two images after masking the nodata values
        else:
            for index in range(len(pair)):
                # read image
                im_ms, georef, cloud_mask, im_extra, im_QA, im_nodata = SDS_preprocess.preprocess_single(
                    fn_im[index], sat, False)
                # in Sentinel2 images close to the edge of the image there are some artefacts,
                # that are squares with constant pixel intensities. They need to be masked in the
                # raster (GEOTIFF). It can be done using the image standard deviation, which
                # indicates values close to 0 for the artefacts.
                if len(im_ms) > 0:
                    # calculate image std for the first 10m band
                    im_std = SDS_tools.image_std(im_ms[:, :, 0], 1)
                    # convert to binary
                    im_binary = np.logical_or(im_std < 1e-6, np.isnan(im_std))
                    # dilate to fill the edges (which have high std)
                    mask10 = morphology.dilation(im_binary,
                                                 morphology.square(3))
                    # mask the 10m .tif file (add no_data where mask is True)
                    SDS_tools.mask_raster(fn_im[index][0], mask10)
                    # now calculate the mask for the 20m band (SWIR1)
                    # for the older version of the ee api calculate the image std again
                    if int(ee.__version__[-3:]) <= 201:
                        # calculate std to create another mask for the 20m band (SWIR1)
                        im_std = SDS_tools.image_std(im_extra, 1)
                        im_binary = np.logical_or(im_std < 1e-6,
                                                  np.isnan(im_std))
                        mask20 = morphology.dilation(im_binary,
                                                     morphology.square(3))
                    # for the newer versions just resample the mask for the 10m bands
                    else:
                        # create mask for the 20m band (SWIR1) by resampling the 10m one
                        mask20 = ndimage.zoom(mask10, zoom=1 / 2, order=0)
                        mask20 = transform.resize(mask20,
                                                  im_extra.shape,
                                                  mode='constant',
                                                  order=0,
                                                  preserve_range=True)
                        mask20 = mask20.astype(bool)
                    # mask the 20m .tif file (im_extra)
                    SDS_tools.mask_raster(fn_im[index][1], mask20)
                    # create a mask for the 60m QA band by resampling the 20m one
                    mask60 = ndimage.zoom(mask20, zoom=1 / 3, order=0)
                    mask60 = transform.resize(mask60,
                                              im_QA.shape,
                                              mode='constant',
                                              order=0,
                                              preserve_range=True)
                    mask60 = mask60.astype(bool)
                    # mask the 60m .tif file (im_QA)
                    SDS_tools.mask_raster(fn_im[index][2], mask60)
                    # make a figure for quality control/debugging
                    # im_RGB = SDS_preprocess.rescale_image_intensity(im_ms[:,:,[2,1,0]], cloud_mask, 99.9)
                    # fig,ax= plt.subplots(2,3,tight_layout=True)
                    # ax[0,0].imshow(im_RGB)
                    # ax[0,0].set_title('RGB original')
                    # ax[1,0].imshow(mask10)
                    # ax[1,0].set_title('Mask 10m')
                    # ax[0,1].imshow(mask20)
                    # ax[0,1].set_title('Mask 20m')
                    # ax[1,1].imshow(mask60)
                    # ax[1,1].set_title('Mask 60 m')
                    # ax[0,2].imshow(im_QA)
                    # ax[0,2].set_title('Im QA')
                    # ax[1,2].imshow(im_nodata)
                    # ax[1,2].set_title('Im nodata')
                else:
                    continue

            # once all the pairs of .tif files have been masked with no_data, merge the using gdal_merge
            fn_merged = os.path.join(filepath, 'merged.tif')
            for k in range(3):
                # merge masked bands
                gdal_merge.main(
                    ['', '-o', fn_merged, '-n', '0', fn_im[0][k], fn_im[1][k]])
                # remove old files
                os.chmod(fn_im[0][k], 0o777)
                os.remove(fn_im[0][k])
                os.chmod(fn_im[1][k], 0o777)
                os.remove(fn_im[1][k])
                # rename new file
                fn_new = fn_im[0][k].split('.')[0] + '_merged.tif'
                os.chmod(fn_merged, 0o777)
                os.rename(fn_merged, fn_new)

            # open both metadata files
            metadict0 = dict([])
            with open(fn_im[0][3], 'r') as f:
                metadict0['filename'] = f.readline().split('\t')[1].replace(
                    '\n', '')
                metadict0['acc_georef'] = float(
                    f.readline().split('\t')[1].replace('\n', ''))
                metadict0['epsg'] = int(f.readline().split('\t')[1].replace(
                    '\n', ''))
            metadict1 = dict([])
            with open(fn_im[1][3], 'r') as f:
                metadict1['filename'] = f.readline().split('\t')[1].replace(
                    '\n', '')
                metadict1['acc_georef'] = float(
                    f.readline().split('\t')[1].replace('\n', ''))
                metadict1['epsg'] = int(f.readline().split('\t')[1].replace(
                    '\n', ''))
            # check if both images have the same georef accuracy
            if np.any(
                    np.array([
                        metadict0['acc_georef'], metadict1['acc_georef']
                    ]) == -1):
                metadict0['georef'] = -1
            # add new name
            metadict0['filename'] = metadict0['filename'].split(
                '.')[0] + '_merged.tif'
            # remove the old metadata.txt files
            os.chmod(fn_im[0][3], 0o777)
            os.remove(fn_im[0][3])
            os.chmod(fn_im[1][3], 0o777)
            os.remove(fn_im[1][3])
            # rewrite the .txt file with a new metadata file
            fn_new = fn_im[0][3].split('.')[0] + '_merged.txt'
            with open(fn_new, 'w') as f:
                for key in metadict0.keys():
                    f.write('%s\t%s\n' % (key, metadict0[key]))

            # update filenames list (in case there are triplicates)
            filenames[pair[0]] = metadict0['filename']

    print(
        '%d out of %d Sentinel-2 images were merged (overlapping or duplicate)'
        % (len(pairs), len(filenames)))

    # update the metadata dict
    metadata_updated = get_metadata(inputs)

    return metadata_updated
Пример #41
0
image = cv2.imread('pears.png', cv2.IMREAD_GRAYSCALE)
sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
# Compute gradient magnitude
grad_magn = np.sqrt(sobelx**2 + sobely**2)
# Put it in [0, 255] value range
grad_magn = 255 * (grad_magn - np.min(grad_magn)) / (np.max(grad_magn) -
                                                     np.min(grad_magn))
selem = morphology.disk(20)
opened = morphology.opening(image, selem)
eroded = morphology.erosion(image, selem)
opening_recon = morphology.reconstruction(seed=eroded,
                                          mask=image,
                                          method='dilation')
closed_opening = morphology.closing(opened, selem)
dilated_recon_dilation = morphology.dilation(opening_recon, selem)
recon_erosion_recon_dilation = morphology.reconstruction(
    dilated_recon_dilation, opening_recon, method='erosion').astype(np.uint8)


def imcomplement(image):
    """Equivalent to matlabs imcomplement function"""
    min_type_val = np.iinfo(image.dtype).min
    max_type_val = np.iinfo(image.dtype).max
    return min_type_val + max_type_val - image


recon_dilation_recon_dilation = morphology.reconstruction(
    imcomplement(dilated_recon_dilation),
    imcomplement(opening_recon),
    method='dilation').astype(np.uint8)
Пример #42
0
def runnoword(filename = "8.jpg"):
	matplotlib.rcParams['font.size'] = 9
	image_ori = data.imread(filename)
	image = rgb2gray(image_ori)

	window_size = 15
	thresh_sauvola = threshold_sauvola(image, window_size=window_size)

	binary_sauvola = image < thresh_sauvola
	binary_sauvola = median(binary_sauvola, square(3))

	binary_sauvola_rec = morphology.dilation(binary_sauvola, rectangle(5,50))
	binary_sauvola_diamond = morphology.dilation(binary_sauvola, rectangle(5,11))
	coded_paws, num_paws = sp.ndimage.label(binary_sauvola_rec)
	data_slices = sp.ndimage.find_objects(coded_paws)
	count = 0
	count2 = 0;
	object = open("result\groundTruth.txt", "w")

	toReturn = []

	for slice in data_slices:
		dx, dy = slice
		
		result = binary_sauvola_diamond[dx.start:dx.start + dx.stop-dx.start+1, dy.start:dy.start + dy.stop-dy.start+1]
		
		coded_paws_2, num_paws_2 = sp.ndimage.label(result)
		data_slices_2 = sp.ndimage.find_objects(coded_paws_2)
		
		bB = []
		
		for slice_2 in data_slices_2:
			dx_2, dy_2 = slice_2
			x = dx.start + dx_2.start
			y = dy.start + dy_2.start
			width = dx_2.stop-dx_2.start+1
			height = dy_2.stop-dy_2.start+1
			bB.append(boundingB(x, y, width, height))
		
		bbB = sorted(bB, key=attrgetter('dy'))
		
		for i in bbB:
			result_2 = image_ori[i.dx : i.dx+i.width, i.dy : i.dy+i.height]
			result3 = binary_sauvola[i.dx : i.dx+i.width, i.dy : i.dy+i.height]

			if (i.width) * (i.height) > 900:
				boundTop = result3[0].sum()
				boundBot = result3[i.width-2].sum()
			
				if boundTop < 7 and boundBot < 8:
					toReturn.append(i)
					os.makedirs("result\\" + str(count))
					tmpStr = "result\\" + str(count) + "\\filename_" + str(count) + ".png"
					plt.imsave(tmpStr, result_2)
					object.write('.\\result\\' + str(count) + '\\filename_' + str(count) + '.png large\n');
					count = count + 1

	object.close()
	inverse = ~binary_sauvola_diamond
	idbinary_sauvola = np.dstack((inverse, inverse, inverse))
	defect = image_ori.copy()

	for i in range(idbinary_sauvola.shape[0]):
		for j in range(idbinary_sauvola.shape[1]):
			if idbinary_sauvola[i,j,0] == 0:
				defect[i,j,:] = 0
			if binary_sauvola_diamond[i,j] > 0:
				binary_sauvola_diamond[i,j] = 1

	dst_TELEA = cv2.inpaint(defect, binary_sauvola_diamond, 3, cv2.INPAINT_TELEA)
	plt.imsave("nowordpage.jpg", dst_TELEA)
	return toReturn
Пример #43
0
def byExampleWithFeatures(s, params):
    name = params.get("name", "classTask")
    logging.info(f"{s['filename']} - \tClassificationModule.byExample:\t{name}")

    thresh = float(params.get("threshold", .5))

    examples = params.get("examples", "")
    if examples == "":
        logging.error(f"{s['filename']} - No examples provided in ClassificationModule.byExample for {name} !!")
        sys.exit(1)
        return

    if params.get("features", "") == "":
        logging.error(f"{s['filename']} - No features provided in ClassificationModule.byExample for {name} !!")
        sys.exit(1)
        return

    with params["lock"]:  # this lock is shared across all threads such that only one thread needs to train the model
        # then it is shared with all other modules
        if not params["shared_dict"].get("model_" + name, False):
            logging.info(f"{s['filename']} - Training model ClassificationModule.byExample:{name}")

            model_vals = []
            model_labels = np.empty([0, 1])

            for ex in params["examples"].splitlines():
                ex = ex.split(":")
                img = io.imread(ex[0])
                eximg = compute_features(img, params)
                eximg = eximg.reshape(-1, eximg.shape[2])
                model_vals.append(eximg)

                mask = io.imread(ex[1]).reshape(-1, 1)
                model_labels = np.vstack((model_labels, mask))

            # do stuff here with model_vals
            model_vals = np.vstack(model_vals)
            clf = RandomForestClassifier(n_jobs=-1)
            clf.fit(model_vals, model_labels.ravel())
            params["shared_dict"]["model_" + name] = clf
            logging.info(f"{s['filename']} - Training model ClassificationModule.byExample:{name}....done")

    clf = params["shared_dict"]["model_" + name]
    img = s.getImgThumb(s["image_work_size"])
    feats = compute_features(img, params)
    cal = clf.predict_proba(feats.reshape(-1, feats.shape[2]))
    cal = cal.reshape(img.shape[0], img.shape[1], 2)

    mask = cal[:, :, 1] > thresh

    area_thresh = int(params.get("area_thresh", "5"))
    if area_thresh > 0:
        mask = remove_small_objects(mask, min_size=area_thresh, in_place=True)

    dilate_kernel_size = int(params.get("dilate_kernel_size", "0"))
    if dilate_kernel_size > 0:
        mask = dilation(mask, selem=np.ones((dilate_kernel_size, dilate_kernel_size)))

    mask = s["img_mask_use"] & (mask > 0)

    io.imsave(s["outdir"] + os.sep + s["filename"] + "_" + name + ".png", img_as_ubyte(mask))
    s["img_mask_" + name] = (mask * 255) > 0
    prev_mask = s["img_mask_use"]
    s["img_mask_use"] = s["img_mask_use"] & ~s["img_mask_" + name]

    s.addToPrintList(name,
                     printMaskHelper(params.get("mask_statistics", s["mask_statistics"]), prev_mask, s["img_mask_use"]))

    if len(s["img_mask_use"].nonzero()[0]) == 0:  # add warning in case the final tissue is empty
        logging.warning(f"{s['filename']} - After ClassificationModule.byExampleWithFeatures:{name} NO tissue "
                        f"remains detectable! Downstream modules likely to be incorrect/fail")
        s["warnings"].append(f"After ClassificationModule.byExampleWithFeatures:{name} NO tissue remains "
                             f"detectable! Downstream modules likely to be incorrect/fail")

    s["img_mask_force"].append("img_mask_" + name)
    s["completed"].append(f"byExampleWithFeatures:{name}")

    return
Пример #44
0
def test_seg(save_dir,data_path,model_def,model_weight):
    is_save=True
    prob_threshould=0.4#二值化阈值
    save_dir=opt.save_dir#切块保存路径
    data_list=indices = open(data_path, 'r').read().splitlines()
    start=time.time()
    net=caffe.Net(model_def,model_weight,caffe.TEST)
    for file_name in tqdm(data_list[:2]):
        img_arr,origin,spacing=load_ct(file_name)
        img_new=normalize(img_arr)
        seg_size = [64,64,64] 
        crop_z=64
        dim_z=img_arr.shape[0]
        if dim_z<seg_size[0]:
            crop_z=img_arr.shape[0]
            img_pad=np.zeros(img_arr.shape,dtype=np.float32)
            img_pad[32-dim_z/2:32+dim_z-dim_z/2,:,:]=img_new
            probs1=seg(net2,file_name,seg_size,img_pad)
            probs1=probs1[32-dim_z/2:32+dim_z-dim_z/2]
            del img_pad
        else:
            probs1=seg(net2,file_name,seg_size,img_new)
        
        
        seg_size = [80,80,80] 
        dim_z=img_arr.shape[0]
        if dim_z<seg_size[0]:
            img_pad=np.zeros(img_arr.shape,dtype=np.float32)
            img_pad[40-dim_z/2:40+dim_z-dim_z/2,:,:]=img_new
            probs2=seg(net1,file_name,seg_size,img_pad)
            probs2=probs2[40-dim_z/2:40+dim_z-dim_z/2]
            del img_pad
        else:
            probs2=seg(net1,file_name,seg_size,img_new)
            #seg_size=[img_arr.shape[0],80,80]
        #net.blobs['data'].reshape(opt.batch_size,1,seg_size[0],seg_size[1],seg_size[2])
        #import ipdb; ipdb.set_trace()
        probs=(probs1+probs2)/2.0
        np.save(mhd_name+"_probs.npy",probs)
        crop_size=[crop_z,64,64]
        mhd_name=file_name.split('/')[-1][:-4]
        probs=probs>prob_threshould 
        probs=morphology.dilation(probs,np.ones([3,3,3]))
        probs=morphology.dilation(probs,np.ones([3,3,3]))
        probs=morphology.erosion(probs,np.ones([3,3,3]))
        np.save(file_name+"_probs.npy",probs)
        labels = measure.label(probs,connectivity=2)
        #label_vals = np.unique(labels)
        regions = measure.regionprops(labels)
        centers = []
        crops = []
        bboxes = []
        spans=[]
        for prop in regions:
            B = prop.bbox
            if B[3]-B[0]>2 and B[4]-B[1]>4 and B[5]-B[2]>4:
                z=int((B[3]+B[0])/2.0)
                y=int((B[4]+B[1])/2.0)
                x=int((B[5]+B[2])/2.0)
                span=np.array([int(B[3]-B[0]),int(B[4]-B[1]),int(B[5]-B[2])])
                spans.append(span)
                centers.append(np.array([z,y,x]))
                bboxes.append(B)
        for idx,bbox in enumerate(bboxes):
            crop=np.zeros(crop_size,dtype=np.float32)
            crop_center=centers[idx]
            half=np.array(crop_size)/2
            crop_center=check_center(crop_size,crop_center,img_arr.shape)
            crop=img_arr[int(crop_center[0]-half[0]):int(crop_center[0]+half[0]),\
                         int(crop_center[1]-half[1]):int(crop_center[1]+half[1]),\
                         int(crop_center[2]-half[1]):int(crop_center[2]+half[1])]
            crops.append(crop)
        if is_save:
            np.save(save_dir+mhd_name+"_nodule.npy",np.array(crops))
            np.save(save_dir+mhd_name+"_center.npy",np.array(centers))
            np.save(save_dir+mhd_name+"_size.npy",np.array(spans))
Пример #45
0
def extract_roi(img):
    rows, cols = img.shape
    mean = np.mean(img)
    max = np.max(img)
    min = np.min(img)

    img[img == max] = mean
    img[img == min] = mean

    kmeans = KMeans(n_clusters=2).fit(np.reshape(img, [np.prod(img.shape), 1]))
    centers = sorted(kmeans.cluster_centers_.flatten())

    threshold = np.mean(centers)
    thresh_img = np.where(img < threshold, 1.0, 0.0)  # threshold the image

    eroded = morphology.erosion(thresh_img, np.ones([3, 3]))
    dilation = morphology.dilation(eroded, np.ones([10, 10]))

    labels = measure.label(dilation)
    label_vals = np.unique(labels)

    regions = measure.regionprops(labels)
    good_labels = []

    for prop in regions:
        B = prop.bbox
        if B[2] - B[0] < 475 and B[3] - B[1] < 475 and B[0] > 40 and B[2] < 472:
            good_labels.append(prop.label)

    mask = np.ndarray([rows, cols], dtype=np.int8)
    mask[:] = 0

    for N in good_labels:
        mask = mask + np.where(labels == N, 1, 0)

    if (mask == 1).sum() < 26214:
        return None

    mask = morphology.dilation(mask, np.ones([10, 10]))  # one last dilation

    img_masked = img * mask
    new_mean = np.mean(img_masked[mask > 0])
    new_std = np.std(img_masked[mask > 0])
    old_min = np.min(img_masked)
    img_masked[img_masked ==
               old_min] = new_mean - 1.2 * new_std  # resetting backgound color
    img_masked = img_masked - new_mean
    img_masked = img_masked / new_std

    labels = measure.label(mask)
    regions = measure.regionprops(labels)

    min_row = 512
    max_row = 0
    min_col = 512
    max_col = 0
    for prop in regions:
        B = prop.bbox
        if min_row > B[0]:
            min_row = B[0]
        if min_col > B[1]:
            min_col = B[1]
        if max_row < B[2]:
            max_row = B[2]
        if max_col < B[3]:
            max_col = B[3]
    width = max_col - min_col
    height = max_row - min_row
    if width > height:
        max_row = min_row + width
    else:
        max_col = min_col + height

    imgc = img_masked[min_row:max_row, min_col:max_col]
    maskc = mask[min_row:max_row, min_col:max_col]

    if max_row - min_row < 5 or max_col - min_col < 5:  # skipping all images with no god regions
        return None
    else:
        mean = np.mean(imgc)
        imgc = imgc - mean
        min = np.min(imgc)
        max = np.max(imgc)
        imgc = imgc / (max - min)
        new_img = resize(imgc, [512, 512], mode='constant')

        return new_img
Пример #46
0
 # Doing this only on the center of the image to avoid
 # the non-tissue parts of the image as much as possible
 #
 kmeans = KMeans(n_clusters=2).fit(
     np.reshape(middle, [np.prod(middle.shape), 1]))
 centers = sorted(kmeans.cluster_centers_.flatten())
 threshold = np.mean(centers)
 thresh_img = np.where(img < threshold, 1.0, 0.0)  # threshold the image
 #
 # I found an initial erosion helful for removing graininess from some of the regions
 # and then large dialation is used to make the lung region
 # engulf the vessels and incursions into the lung cavity by
 # radio opaque tissue
 #
 eroded = morphology.erosion(thresh_img, np.ones([4, 4]))
 dilation = morphology.dilation(eroded, np.ones([10, 10]))
 #
 #  Label each region and obtain the region properties
 #  The background region is removed by removing regions
 #  with a bbox that is to large in either dimnsion
 #  Also, the lungs are generally far away from the top
 #  and bottom of the image, so any regions that are too
 #  close to the top and bottom are removed
 #  This does not produce a perfect segmentation of the lungs
 #  from the image, but it is surprisingly good considering its
 #  simplicity.
 #
 labels = measure.label(dilation)
 label_vals = np.unique(labels)
 regions = measure.regionprops(labels)
 good_labels = []
Пример #47
0
def volspike(pars):
    """ Function for finding spikes of single neuron with given ROI in
        voltage imaging. Use function denoise_spikes to find spikes
        from one dimensional signal, and use ridge regression to find the
        best weight. Do these two steps iteratively to find
        best spike times.

        Args:
            pars: list
                fnames: str
                    name of the memory mapping file in C order

                fr: int
                    frame rate of the movie

                cell_n: int
                    index of the cell processing

                ROIs: 3-d array
                    all regions of interests

                weights: 3-d array
                    spatial weights of different cells generated by previous data blocks as initialization

                args: dictionary
                    context_size: int
                        number of pixels surrounding the ROI to use as context

                    censor_size: int
                        number of pixels surrounding the ROI to censor from the background PCA; roughly
                        the spatial scale of scattered/dendritic neural signals, in pixels
                        
                    flip_signal: boolean
                        whether to flip signal upside down for spike detection 
                        True for voltron, False for others

                    hp_freq_pb: float
                        high-pass frequency for removing photobleaching    
                    
                    nPC_bg: int
                        number of principle components used for background subtraction
                        
                    ridge_bg: float
                        regularization strength for ridge regression in background removal 

                    hp_freq: float
                        high-pass cutoff frequency to filter the signal after computing the trace
                        
                    clip: int
                        maximum number of spikes for producing templates

                    threshold_method: str
                        'simple' or 'adaptive_threshold' method for thresholding signals
                        'simple' method threshold based on estimated noise level 
                        'adaptive_threshold' method threshold based on estimated peak distribution
                        
                    min_spikes: int
                        minimal number of spikes to be detected

                    threshold: float
                        threshold for spike detection in 'simple' threshold method 
                        The real threshold is the value multiplied by the estimated noise level

                    sigmas: 1-d array
                        spatial smoothing radius imposed on high-pass filtered 
                        movie only for finding weights

                    n_iter: int
                        number of iterations alternating between estimating spike times
                        and spatial filters
                        
                    weight_update: str
                        'ridge' or 'NMF' for weight update
                        
                    do_plot: boolean
                        if Ture, plot trace of signals and spiketimes, 
                        peak triggered average, histogram of heights in the last iteration

                    do_cross_val: boolean
                        whether to use cross validation to optimize regression regularization parameters
                        
                    sub_freq: float
                        frequency for subthreshold extraction


        Returns:
            output: dictionary
                cell_n: int
                    index of cell        
            
                t: 1-d array
                    trace without applying whitened matched filter
                    
                ts: 1-d array
                    trace after applying whitened matched filter

                t_rec: 1-d array
                    reconstructed signal of the neuron
                
                t_sub: 1-d array
                    subthreshold signal of the neuron
                    
                spikes: 1-d array
                    spike time of the neuron

                num_spikes: list
                    number of spikes detected in each iteration 
                    
                low_spikes: boolean
                    True if detected number spikes is less than min_spikes 
                         
                template: 1-d array
                    temporal template of the neuron
                
                snr: float
                    signal to noise ratio of the processed signal
                    
                thresh: float
                    threshold of the signal

                spatial_filter: 2-d array
                    spatial filter of the neuron in the whole FOV
                    
                weights: 2-d array
                    ridge regression coefficients for fitting reconstructed signal
                
                locality: boolean
                    False if the maximum of spatial filter is not in the initial ROI
                
                context_coord: 2-d array
                    boundary of context region in x,y coordinates
                
                mean_im: 1-d array
                    mean of the signal in ROI after removing photobleaching, used for 
                    producing F0
                    
                F0: 1-d array
                    baseline signal
                    
                dFF: 1-d array
                    scaled signal
                    
                rawROI: dictionary
                    including the result after the first spike extraction
    """
    # load parameters
    fnames = pars[0]
    fr = pars[1]
    cell_n = pars[2]
    bw = pars[3]    
    weights_init = pars[4]    
    args = pars[5]
    window_length = fr * 0.02 # window length for temporal templates
    output = {}
    output['rawROI'] = {}
    print(f'Now processing cell number {cell_n}')
    
    # load the movie in C order mmap file
    Yr, dims, T = cm.load_memmap(fnames)
    if bw.shape == dims:
        images = np.reshape(Yr.T, [T] + list(dims), order='F')
    else:
        raise Exception('Dimensions of movie and ROIs do not accord')
        
    # extract relevant region and align
    bwexp = dilation(bw, np.ones([args['context_size'], args['context_size']]), shift_x=True, shift_y=True)
    Xinds = np.where(np.any(bwexp > 0, axis=1) > 0)[0]
    Yinds = np.where(np.any(bwexp > 0, axis=0) > 0)[0]
    bw = bw[Xinds[0]:Xinds[-1] + 1, Yinds[0]:Yinds[-1] + 1]
    notbw = 1 - dilation(bw, disk(args['censor_size']))
    data = np.array(images[:, Xinds[0]:Xinds[-1] + 1, Yinds[0]:Yinds[-1] + 1])
    bw = (bw > 0)
    notbw = (notbw > 0)
    ref = np.median(data[:500, :, :], axis=0)
    bwexp[Xinds[0]:Xinds[-1] + 1, Yinds[0]:Yinds[-1] + 1] = True

    # visualize ROI
    visualize_ROI = False
    if visualize_ROI:
        fig = plt.figure()
        plt.subplot(131);plt.imshow(ref);plt.axis('image');plt.xlabel('mean Intensity')
        plt.subplot(132);plt.imshow(bw);plt.axis('image');plt.xlabel('initial ROI')
        plt.subplot(133);plt.imshow(notbw);plt.axis('image');plt.xlabel('background')
        fig.suptitle('ROI selection')
        plt.show()
    
    # flip the signal if necessary
    if args['flip_signal']==True:
        data = -data
    else:
        pass
    
    # remove photobleaching effect by high pass filtering signal
    output['mean_im'] = np.mean(data, axis=0)
    data = np.reshape(data, (data.shape[0], -1))
    data = data - np.mean(data, 0)
    data = data - np.mean(data, 0)   #do again because of numeric issues
    data_hp = signal_filter(data.T, args['hp_freq_pb'], fr).T  
    data_lp = data - data_hp

    # initial trace
    if weights_init is None:
        t0 = np.nanmean(data_hp[:, bw.ravel()], 1)
    else:
        t0 = np.matmul(data_hp, weights_init[1:])  
    t0 = t0 - np.mean(t0)

    # remove any variance in trace that can be predicted from the background principal components
    Ub, Sb, Vb = svds(data_hp[:, notbw.ravel()], args['nPC_bg'])
    alpha = args['nPC_bg'] * args['ridge_bg']    # square of F-norm of Ub is equal to number of principal components
    reg = Ridge(alpha=alpha, fit_intercept=False, solver='lsqr').fit(Ub, t0)
    t0 = np.double(t0 - np.matmul(Ub, reg.coef_))
    
    # find out spikes of initial trace
    ts, spikes, t_rec, templates, low_spikes, thresh = denoise_spikes(t0, 
                                          window_length, fr, hp_freq=args['hp_freq'], clip=args['clip'],
                                          threshold_method=args['threshold_method'], threshold=args['threshold'], 
                                          min_spikes=args['min_spikes'], do_plot=False)

    output['rawROI']['t'] = t0.copy()
    output['rawROI']['ts'] = ts.copy()
    output['rawROI']['spikes'] = spikes.copy()
    output['rawROI']['spatial_filter'] = bw.copy()
    output['rawROI']['t'] = output['rawROI']['t'] * np.mean(t0[output['rawROI']['spikes']]) / np.mean(
        output['rawROI']['t'][output['rawROI']['spikes']])  # correct shrinkage
    output['rawROI']['templates'] = templates
    num_spikes = [spikes.shape[0]]

    # prebuild the regression matrix generate a predictor for ridge regression
    pred = np.empty_like(data_hp)
    pred[:] = data_hp
    pred = np.hstack((np.ones((data_hp.shape[0], 1), dtype=np.single), np.reshape
    (movie.gaussian_blur_2D(np.reshape(pred,
                                       (data_hp.shape[0], ref.shape[0], ref.shape[1])),
                            kernel_size_x=7, kernel_size_y=7, kernel_std_x=1.5,
                            kernel_std_y=1.5, borderType=cv2.BORDER_REPLICATE), data_hp.shape)))

    # cross-validation of regularized regression parameters
    lambdamax = np.single(np.linalg.norm(pred[:, 1:], ord='fro') ** 2)
    lambdas = lambdamax * np.logspace(-4, -2, 3)
    
    if args['do_cross_val']:
        # need to add
        logging.warning('doing cross validation')
        raise Exception('cross validation option is not availble yet')
    else:
        s_max = 1
        l_max = 2
        sigma = args['sigmas'][s_max]
    
    recon = np.empty_like(data_hp)
    recon[:] = data_hp
    recon = np.hstack((np.ones((data_hp.shape[0], 1), dtype=np.single), np.reshape
    (movie.gaussian_blur_2D(np.reshape(recon,
                                       (data_hp.shape[0], ref.shape[0], ref.shape[1])),
                            kernel_size_x=np.int(2 * np.ceil(2 * sigma) + 1),
                            kernel_size_y=np.int(2 * np.ceil(2 * sigma) + 1),
                            kernel_std_x=sigma, kernel_std_y=sigma,
                            borderType=cv2.BORDER_REPLICATE), data_hp.shape)))

    # do the following two steps for several iterations: update spatial filter, detect 
    # best spike times
    for iteration in range(args['n_iter']):
        if iteration == args['n_iter'] - 1:
            do_plot = args['do_plot']
        else:
            do_plot = False
            
        # update spatial weights
        tr = np.single(t_rec.copy())
        if args['weight_update'] == 'NMF':
            C = np.array([tr, np.ones_like(tr)])  # constant baselines as 2nd component
            CCt = C.dot(C.T)
            CY = C.dot(recon[:, 1:])
            A = np.maximum(np.linalg.inv(CCt).dot(CY), 0)
            for _ in range(5):
                for m in range(2):
                    A[m] += (CY[m] - CCt[m].dot(A)) / CCt[m, m]
                    if m == 0:
                        A[m] = np.maximum(A[m], 0)
            weights = np.concatenate([[0], A[0]])
        elif args['weight_update'] == 'ridge':
            Ri = Ridge(alpha=lambdas[l_max], fit_intercept=True, solver='lsqr')
            Ri.fit(recon, tr)
            weights = Ri.coef_
            weights[0] = Ri.intercept_

        # compute spatial filter
        spatial_filter = np.empty_like(weights)
        spatial_filter[:] = weights
        spatial_filter = movie.gaussian_blur_2D(np.reshape(spatial_filter[1:],
                                                          ref.shape, order='C')[np.newaxis, :, :],
                                               kernel_size_x=np.int(2 * np.ceil(2 * sigma) + 1),
                                               kernel_size_y=np.int(2 * np.ceil(2 * sigma) + 1),
                                               kernel_std_x=sigma, kernel_std_y=sigma,
                                               borderType=cv2.BORDER_REPLICATE)[0]

        # compute new signal            
        t = np.matmul(recon, weights)
        t = t - np.mean(t)

        # ridge regression to remove background components
        b = Ridge(alpha=alpha, fit_intercept=False, solver='lsqr').fit(Ub, t).coef_
        t = t - np.matmul(Ub, b)

        # correct shrinkage
        t = np.double(t * np.mean(t0[spikes]) / np.mean(t[spikes]))

        # generate the new trace and the new denoised trace
        ts, spikes, t_rec, templates, low_spikes, thresh = denoise_spikes(t, 
                    window_length, fr,  hp_freq=args['hp_freq'], clip=args['clip'],
                    threshold_method=args['threshold_method'], threshold=args['threshold'], 
                    min_spikes=args['min_spikes'], do_plot=do_plot)
    
        num_spikes.append(spikes.shape[0])

    # compute SNR
    if len(spikes)>0:
        t = t - np.median(t)
        selectSpikes = np.zeros(t.shape)
        selectSpikes[spikes] = 1
        sgn = np.mean(t[selectSpikes > 0])
        ff1 = -t * (t < 0)
        Ns = np.sum(ff1 > 0)
        noise = np.sqrt(np.divide(np.sum(ff1**2), Ns)) 
        snr = sgn / noise
    else:
        snr = 0

    # locality test       
    matrix = np.matmul(np.transpose(pred[:, 1:]), t_rec)
    sigmax = np.sqrt(np.sum(np.multiply(pred[:, 1:], pred[:, 1:]), axis=0))
    sigmay = np.sqrt(np.dot(t_rec, t_rec))
    IMcorr = matrix / sigmax / sigmay
    maxCorrInROI = np.max(IMcorr[bw.ravel()])
    if np.any(IMcorr[notbw.ravel()] > maxCorrInROI):
        locality = False
    else:
        locality = True
    
    # spatial filter in FOV
    weights = np.reshape(weights[1:],ref.shape, order='C')
    weights_FOV = np.zeros(images.shape[1:])
    weights_FOV[Xinds[0]:Xinds[-1] + 1, Yinds[0]:Yinds[-1] + 1] = weights

    spatial = np.zeros(images.shape[1:])
    spatial[Xinds[0]:Xinds[-1] + 1, Yinds[0]:Yinds[-1] + 1] = spatial_filter

    # subthreshold activity extraction    
    t_sub = t.copy() - t_rec
    t_sub = signal_filter(t_sub, args['sub_freq'], fr, order=5, mode='low') 

    # output
    output['cell_n'] = cell_n
    output['t'] = t
    output['ts'] = ts
    output['t_rec'] = t_rec        
    output['t_sub'] = t_sub
    output['spikes'] = spikes
    output['low_spikes'] = low_spikes
    output['num_spikes'] = num_spikes
    output['templates'] = templates
    output['snr'] = snr
    output['thresh'] = thresh
    output['spatial_filter'] = spatial    
    output['weights'] = weights_FOV
    output['locality'] = locality    
    output['context_coord'] = np.transpose(np.vstack((Xinds[[0, -1]], Yinds[[0, -1]])))
    output['F0'] = np.abs(np.nanmean(data_lp[:, bw.flatten()] + output['mean_im'][bw][np.newaxis, :], 1))
    output['dFF'] = t / output['F0']
    output['rawROI']['dFF'] = output['rawROI']['t'] / output['F0']
    
    return output
Пример #48
0
def polarity2instance(volume, thres=0.5, thres_small=128, 
                      scale_factors=(1.0, 1.0, 1.0), semantic=False):
    """From synaptic polarity prediction to instance masks via connected-component 
    labeling. The input volume should be a 3-channel probability map of shape :math:`(C, Z, Y, X)`
    where :math:`C=3`, representing pre-synaptic region, post-synaptic region and their
    union, respectively.

    Note:
        For each pair of pre- and post-synaptic segmentation, the decoding function will
        annotate pre-synaptic region as :math:`2n-1` and post-synaptic region as :math:`2n`,
        for :math:`n>0`. If :attr:`semantic=True`, all pre-synaptic pixels are labeled with
        while all post-synaptic pixels are labeled with 2. Both kinds of annotation are compatible
        with the ``TARGET_OPT: ['1']`` configuration in training. 

    Note:
        The number of pre- and post-synaptic segments will be reported when setting :attr:`semantic=False`.
        Note that the numbers can be different due to either incomplete syanpses touching the volume borders,
        or errors in the prediction. We thus make a conservative estimate of the total number of synapses
        by using the relatively small number among the two.

    Args: 
        volume (numpy.ndarray): 3-channel probability map of shape :math:`(3, Z, Y, X)`.
        thres (float): probability threshold of foreground. Default: 0.5
        thres_small (int): size threshold of small objects to remove. Default: 128
        scale_factors (tuple): scale factors for resizing the output volume in :math:`(Z, Y, X)` order. Default: :math:`(1.0, 1.0, 1.0)`
        semantic (bool): return only the semantic mask of pre- and post-synaptic regions. Default: False

    Examples::
        >>> from connectomics.data.utils import readvol, savevol
        >>> from connectomics.utils.processing import polarity2instance
        >>> volume = readvol(input_name)
        >>> instances = polarity2instance(volume)
        >>> savevol(output_name, instances)
    """
    thres = int(255.0 * thres)
    temp = (volume > thres).astype(np.uint8)

    syn_pre = temp[0] * temp[2]
    syn_pre = remove_small_objects(syn_pre, 
                min_size=thres_small, connectivity=1)
    syn_post = temp[1] * temp[2]
    syn_post = remove_small_objects(syn_post, 
                min_size=thres_small, connectivity=1)

    if semantic: 
        # Generate only the semantic mask. The pre-synaptic region is labeled
        # with 1, while the post-synaptic region is labeled with 2.
        segm = np.maximum(syn_pre.astype(np.uint8), 
                          syn_post.astype(np.uint8) * 2)

    else:
        # Generate the instance mask.
        foreground = dilation(temp[2].copy(), np.ones((1,5,5)))
        foreground = label(foreground)

        # Since non-zero pixels in seg_pos and seg_neg are subsets of temp[2], 
        # they are naturally subsets of non-zero pixels in foreground.
        seg_pre = (foreground*2 - 1) * syn_pre.astype(foreground.dtype)
        seg_post = (foreground*2) * syn_post.astype(foreground.dtype)
        segm = np.maximum(seg_pre, seg_post)

        # Report the number of synapses
        num_syn_pre = len(np.unique(seg_pre))-1
        num_syn_post = len(np.unique(seg_post))-1
        num_syn = min(num_syn_pre, num_syn_post) # a conservative estimate
        print("Stats: found %d pre- and %d post-" % (num_syn_pre, num_syn_post) + 
              "synaptic segments in the volume")
        print("There are %d synapses under a conservative estimate." % num_syn)

    # resize the segmentation based on specified scale factors
    if not all(x==1.0 for x in scale_factors):
        target_size = (int(segm.shape[0]*scale_factors[0]), 
                       int(segm.shape[1]*scale_factors[1]), 
                       int(segm.shape[2]*scale_factors[2]))
        segm = resize(segm, target_size, order=0, anti_aliasing=False, preserve_range=True)
    
    # Cast the segmentation to the best dtype to save memory.
    max_id = np.amax(np.unique(segm))
    m_type = getSegType(int(max_id))
    segm = segm.astype(m_type)
    return segm
Пример #49
0
            Y_cont_test = unpad_test(Y_cont_test, pads_h[i], pads_w[i])
        else:
            im = X_test[i]
            temp_lab, temp_cont = model.predict(np.expand_dims(X_test[i],
                                                               axis=0),
                                                verbose=0)
            temp_lab = temp_lab[:, :, :, 0]
            temp_cont = temp_cont[:, :, :, 0]
            Y_lab_test = unpad_test(temp_lab[0], pads_h[i], pads_w[i])
            Y_cont_test = unpad_test(temp_cont[0], pads_h[i], pads_w[i])

        Y[i] = (Y_lab_test >= thresh_lab) & (Y_cont_test <= thresh_cont)
        Y[i] = Y[i].astype(np.int16)
        Y[i] = ndimage.binary_fill_holes(Y[i])
        Y[i] = label(Y[i])
        Y[i] = dilation(Y[i])
        if len(np.unique(Y[i])) == 1:
            Y[i] = (Y_lab_test >= thresh_lab - 0.1) & (Y_cont_test <=
                                                       thresh_cont)
            Y[i] = Y[i].astype(np.int16)
            Y[i] = ndimage.binary_fill_holes(Y[i])
            Y[i] = label(Y[i])
            Y[i] = dilation(Y[i])
        if len(np.unique(Y[i])) == 1:
            Y[i] = (Y_lab_test >= thresh_lab - 0.2) & (Y_cont_test <=
                                                       thresh_cont)
            Y[i] = Y[i].astype(np.int16)
            Y[i] = ndimage.binary_fill_holes(Y[i])
            Y[i] = label(Y[i])
            Y[i] = dilation(Y[i])
Пример #50
0
def export_segment(fpath,
                   outfpath,
                   dim,
                   pooling="none",
                   mask_type="none",
                   fmt="npy",
                   debug=True):
    """
    Given an MRI numpy image of dim: frames X height X width,
    generate a segmentation mask for valve candidates.

    Segmentation code based on sample from
    http://douglasduhaime.com/posts/simple-image-segmentation-with-scikit-image.html

    :param fpath:
    :param outfpath:
    :param dim:     crop dimensions
    :param fmt:     (frames|max_pool|std_pool|video) image format options
    :param mask_type: (None|hard|soft) DEFAULT: None
    :param debug:
    :return:
    """
    # 1: LOAD/PREPROCESS IMAGE
    img = np.load(fpath)
    if len(img.shape) != 3:
        raise ValueError('DICOM / numpy array is empty')

    # compute pixel intensity SD percentiles
    X = np.std(img, axis=0)

    # 2: SEGMENTATION
    labeled = segment(X, upscale=1.0, denoise=False)

    # rank segment candidates (most likely atrial valve)
    segments = score_segmentations(X, labeled)
    target = segments[0]
    cx, cy = target[-1]

    # debug: save segmentations as a PNG
    if debug:
        plt.figure(figsize=(6, 6))
        plt.imshow(labeled, cmap='tab10')
        plt.scatter(x=cx, y=cy, c='r', s=20)
        plt.savefig(outfpath)
        plt.close()

    # save all valve masks (index 0 is the most likely atrial valve)
    masks = get_segmentation_masks(labeled, segments)

    # debug: dump each image mask as a PNG
    if debug:
        for m in range(masks.shape[0]):
            plt.figure(figsize=(6, 6))
            plt.imshow(masks[m], cmap='tab10')
            plt.savefig(outfpath + "_{}".format(m))
            plt.close()

    # get segment mask points, compute bounding box, and crop original image
    px, py = np.where(masks[0] == 1)
    bbox = get_crop_region(px, py, dim)
    c_img = crop(img, bbox)

    # mask data: by default, don't mask anything
    mask = np.ones((bbox[1] - bbox[0], bbox[3] - bbox[2]), dtype=np.float32)
    if mask_type in ["soft", "hard"]:
        msk = np.copy(masks[0])
        exp_msk = dilation(msk)
        exp_msk = crop(exp_msk, bbox)
        mask = filters.gaussian(exp_msk,
                                sigma=1.01) if mask_type == "soft" else exp_msk

    # 3: EXPORT IMAGE DATA
    img_path = "{}_{}x{}".format(outfpath, dim, dim)
    img_path = "{}_{}pool".format(img_path,
                                  pooling) if pooling != "none" else img_path
    img_path = "{}_{}".format(img_path,
                              mask_type) if mask_type != "none" else img_path

    # pool data
    if pooling in ["max", "std", "z_add"]:
        if pooling == "max":
            c_img = np.max(c_img, axis=0)
        elif pooling == "std":
            c_img = np.std(c_img, axis=0)
        elif pooling == "z_add":
            c_img = z_score_normalize(c_img)
            c_img = np.sum(c_img, axis=0)
            c_img = (c_img - np.min(c_img)) / (np.max(c_img) - np.min(c_img))

    c_img = (mask * c_img)

    # export format
    if fmt == "png":
        plt.figure(figsize=(4, 4))
        plt.imshow(c_img, cmap='gray')
        plt.savefig(outfpath)

    elif fmt == "mp4":
        seq_to_video(c_img, img_path, width=4, height=4)

    else:
        np.save(img_path, c_img)

    # save segmentation masks
    np.save("{}_masks".format(outfpath), masks.astype(np.int8))
Пример #51
0
    lines = data.split('\n')

calculated = []
for id, line in enumerate(lines):
    cols = line.split('\t')
    if cols[0] == '':
        continue
    elif id > 1:
        img = imread(cols[0])
        (height, width, c) = img.shape
        img_gray = rgb2gray(img)

        # the numbers are white, the background is black
        img_bin = img_gray > 0.5
        # make sure that every number is classified as a single region
        img_bin_dilation = dilation(img_bin, selem=str_elem1)
        labeled_img = label(img_bin_dilation)
        regions = regionprops(labeled_img)

        # eliminate white noise
        true_regions = []
        for region in regions:
            bbox = region.bbox
            block_height = bbox[2] - bbox[0]
            # during the testing, none of the regions that didn't represent a number were higher than 12
            if block_height > 12:
                true_regions.append(region)

        recognized_numbers = []

        for region in true_regions:
Пример #52
0
def nuclear_expansion_pixel(label_map, expansion_radius):
    expanded_map = morph.dilation(label_map,
                                  selem=morph.disk(expansion_radius))
    return expanded_map
Пример #53
0
def poisson_disc(img, n, k=30):
    h, w = img.shape[:2]

    nimg = denoise_bilateral(img, sigma_color=0.1, sigma_spatial=15)
    img_gray = rgb2gray(nimg)
    img_lab = rgb2lab(nimg)

    entropy_weight = 2**(entropy(img_as_ubyte(img_gray), disk(15)))
    entropy_weight /= np.amax(entropy_weight)
    entropy_weight = gaussian(dilation(entropy_weight, disk(15)), 5)

    color = [sobel(img_lab[:, :, channel])**2 for channel in range(1, 3)]
    edge_weight = functools.reduce(op.add, color)**(1 / 2) / 75
    edge_weight = dilation(edge_weight, disk(5))

    weight = (0.3 * entropy_weight + 0.7 * edge_weight)
    weight /= np.mean(weight)
    weight = weight

    max_dist = min(h, w) / 4
    avg_dist = math.sqrt(w * h / (n * math.pi * 0.5)**(1.05))
    min_dist = avg_dist / 4

    dists = np.clip(avg_dist / weight, min_dist, max_dist)

    def gen_rand_point_around(point):
        radius = random.uniform(dists[point], max_dist)
        angle = rand(2 * math.pi)
        offset = np.array([radius * math.sin(angle), radius * math.cos(angle)])
        return tuple(point + offset)

    def has_neighbours(point):
        point_dist = dists[point]
        distances, idxs = tree.query(point,
                                     len(sample_points) + 1,
                                     distance_upper_bound=max_dist)

        if len(distances) == 0:
            return True

        for dist, idx in zip(distances, idxs):
            if np.isinf(dist):
                break

            if dist < point_dist and dist < dists[tuple(
                    map(int, tuple(tree.data[idx])))]:
                return True

        return False

    # Generate first point randomly.
    first_point = (rand(h), rand(w))
    to_process = [first_point]
    sample_points = [first_point]
    tree = KDTree(sample_points)

    while to_process:
        # Pop a random point.
        point = to_process.pop(random.randrange(len(to_process)))
        point = tuple(map(int, point))
        for _ in range(k):
            new_point = gen_rand_point_around(point)
            new_point = tuple(map(int, new_point))
            if (0 <= new_point[0] < h and 0 <= new_point[1] < w
                    and not has_neighbours(new_point)):
                to_process.append(new_point)
                sample_points.append(new_point)
                tree = KDTree(sample_points)
                if len(sample_points) % 1000 == 0:
                    print("Generated {} points.".format(len(sample_points)))

    print("Generated {} points.".format(len(sample_points)))

    return sample_points
Пример #54
0
    def __getitem__(self, idx):
        _idx = self.train_idxs[idx]

        fn = all_files[_idx]

        img = cv2.imread(fn, cv2.IMREAD_COLOR)
        img2 = cv2.imread(fn.replace('_pre_', '_post_'), cv2.IMREAD_COLOR)

        msk0 = cv2.imread(fn.replace('/images/', '/masks/'), cv2.IMREAD_UNCHANGED)
        lbl_msk1 = cv2.imread(fn.replace('/images/', '/masks/').replace('_pre_disaster', '_post_disaster'), cv2.IMREAD_UNCHANGED)

        msk1 = np.zeros_like(lbl_msk1)
        msk2 = np.zeros_like(lbl_msk1)
        msk3 = np.zeros_like(lbl_msk1)
        msk4 = np.zeros_like(lbl_msk1)
        msk2[lbl_msk1 == 2] = 255
        msk3[lbl_msk1 == 3] = 255
        msk4[lbl_msk1 == 4] = 255
        msk1[lbl_msk1 == 1] = 255

        if random.random() > 0.7:
            img = img[::-1, ...]
            img2 = img2[::-1, ...]
            msk0 = msk0[::-1, ...]
            msk1 = msk1[::-1, ...]
            msk2 = msk2[::-1, ...]
            msk3 = msk3[::-1, ...]
            msk4 = msk4[::-1, ...]

        if random.random() > 0.3:
            rot = random.randrange(4)
            if rot > 0:
                img = np.rot90(img, k=rot)
                img2 = np.rot90(img2, k=rot)
                msk0 = np.rot90(msk0, k=rot)
                msk1 = np.rot90(msk1, k=rot)
                msk2 = np.rot90(msk2, k=rot)
                msk3 = np.rot90(msk3, k=rot)
                msk4 = np.rot90(msk4, k=rot)
                    
        if random.random() > 0.99:
            shift_pnt = (random.randint(-320, 320), random.randint(-320, 320))
            img = shift_image(img, shift_pnt)
            img2 = shift_image(img2, shift_pnt)
            msk0 = shift_image(msk0, shift_pnt)
            msk1 = shift_image(msk1, shift_pnt)
            msk2 = shift_image(msk2, shift_pnt)
            msk3 = shift_image(msk3, shift_pnt)
            msk4 = shift_image(msk4, shift_pnt)
            
        if random.random() > 0.5:
            rot_pnt =  (img.shape[0] // 2 + random.randint(-320, 320), img.shape[1] // 2 + random.randint(-320, 320))
            scale = 0.9 + random.random() * 0.2
            angle = random.randint(0, 20) - 10
            if (angle != 0) or (scale != 1):
                img = rotate_image(img, angle, scale, rot_pnt)
                img2 = rotate_image(img2, angle, scale, rot_pnt)
                msk0 = rotate_image(msk0, angle, scale, rot_pnt)
                msk1 = rotate_image(msk1, angle, scale, rot_pnt)
                msk2 = rotate_image(msk2, angle, scale, rot_pnt)
                msk3 = rotate_image(msk3, angle, scale, rot_pnt)
                msk4 = rotate_image(msk4, angle, scale, rot_pnt)

        crop_size = input_shape[0]
        if random.random() > 0.5:
            crop_size = random.randint(int(input_shape[0] / 1.1), int(input_shape[0] / 0.9))

        bst_x0 = random.randint(0, img.shape[1] - crop_size)
        bst_y0 = random.randint(0, img.shape[0] - crop_size)
        bst_sc = -1
        try_cnt = random.randint(1, 10)
        for i in range(try_cnt):
            x0 = random.randint(0, img.shape[1] - crop_size)
            y0 = random.randint(0, img.shape[0] - crop_size)
            _sc = msk2[y0:y0+crop_size, x0:x0+crop_size].sum() * 5 + msk3[y0:y0+crop_size, x0:x0+crop_size].sum() * 5 + msk4[y0:y0+crop_size, x0:x0+crop_size].sum() * 2 + msk1[y0:y0+crop_size, x0:x0+crop_size].sum()
            if _sc > bst_sc:
                bst_sc = _sc
                bst_x0 = x0
                bst_y0 = y0
        x0 = bst_x0
        y0 = bst_y0
        img = img[y0:y0+crop_size, x0:x0+crop_size, :]
        img2 = img2[y0:y0+crop_size, x0:x0+crop_size, :]
        msk0 = msk0[y0:y0+crop_size, x0:x0+crop_size]
        msk1 = msk1[y0:y0+crop_size, x0:x0+crop_size]
        msk2 = msk2[y0:y0+crop_size, x0:x0+crop_size]
        msk3 = msk3[y0:y0+crop_size, x0:x0+crop_size]
        msk4 = msk4[y0:y0+crop_size, x0:x0+crop_size]
        
        if crop_size != input_shape[0]:
            img = cv2.resize(img, input_shape, interpolation=cv2.INTER_LINEAR)
            img2 = cv2.resize(img2, input_shape, interpolation=cv2.INTER_LINEAR)
            msk0 = cv2.resize(msk0, input_shape, interpolation=cv2.INTER_LINEAR)
            msk1 = cv2.resize(msk1, input_shape, interpolation=cv2.INTER_LINEAR)
            msk2 = cv2.resize(msk2, input_shape, interpolation=cv2.INTER_LINEAR)
            msk3 = cv2.resize(msk3, input_shape, interpolation=cv2.INTER_LINEAR)
            msk4 = cv2.resize(msk4, input_shape, interpolation=cv2.INTER_LINEAR)
            

        if random.random() > 0.99:
            img = shift_channels(img, random.randint(-5, 5), random.randint(-5, 5), random.randint(-5, 5))
        elif random.random() > 0.99:
            img2 = shift_channels(img2, random.randint(-5, 5), random.randint(-5, 5), random.randint(-5, 5))

        if random.random() > 0.99:
            img = change_hsv(img, random.randint(-5, 5), random.randint(-5, 5), random.randint(-5, 5))
        elif random.random() > 0.99:
            img2 = change_hsv(img2, random.randint(-5, 5), random.randint(-5, 5), random.randint(-5, 5))

        if random.random() > 0.99:
            if random.random() > 0.99:
                img = clahe(img)
            elif random.random() > 0.99:
                img = gauss_noise(img)
            elif random.random() > 0.99:
                img = cv2.blur(img, (3, 3))
        elif random.random() > 0.99:
            if random.random() > 0.99:
                img = saturation(img, 0.9 + random.random() * 0.2)
            elif random.random() > 0.99:
                img = brightness(img, 0.9 + random.random() * 0.2)
            elif random.random() > 0.99:
                img = contrast(img, 0.9 + random.random() * 0.2)

        if random.random() > 0.99:
            if random.random() > 0.99:
                img2 = clahe(img2)
            elif random.random() > 0.99:
                img2 = gauss_noise(img2)
            elif random.random() > 0.99:
                img2 = cv2.blur(img2, (3, 3))
        elif random.random() > 0.99:
            if random.random() > 0.99:
                img2 = saturation(img2, 0.9 + random.random() * 0.2)
            elif random.random() > 0.99:
                img2 = brightness(img2, 0.9 + random.random() * 0.2)
            elif random.random() > 0.99:
                img2 = contrast(img2, 0.9 + random.random() * 0.2)

                
        if random.random() > 0.99:
            el_det = self.elastic.to_deterministic()
            img = el_det.augment_image(img)

        if random.random() > 0.99:
            el_det = self.elastic.to_deterministic()
            img2 = el_det.augment_image(img2)

        msk0 = msk0[..., np.newaxis]
        msk1 = msk1[..., np.newaxis]
        msk2 = msk2[..., np.newaxis]
        msk3 = msk3[..., np.newaxis]
        msk4 = msk4[..., np.newaxis]

        msk = np.concatenate([msk0, msk1, msk2, msk3, msk4], axis=2)
        msk = (msk > 127)

        msk[..., 0] = True
        msk[..., 1] = dilation(msk[..., 1], square(5))
        msk[..., 2] = dilation(msk[..., 2], square(5))
        msk[..., 3] = dilation(msk[..., 3], square(5))
        msk[..., 4] = dilation(msk[..., 4], square(5))
        msk[..., 1][msk[..., 2:].max(axis=2)] = False
        msk[..., 3][msk[..., 2]] = False
        msk[..., 4][msk[..., 2]] = False
        msk[..., 4][msk[..., 3]] = False
        msk[..., 0][msk[..., 1:].max(axis=2)] = False
        msk = msk * 1

        lbl_msk = msk.argmax(axis=2)

        img = np.concatenate([img, img2], axis=2)
        img = preprocess_inputs(img)

        img = torch.from_numpy(img.transpose((2, 0, 1))).float()
        msk = torch.from_numpy(msk.transpose((2, 0, 1))).long()

        sample = {'img': img, 'msk': msk, 'lbl_msk': lbl_msk, 'fn': fn}
        return sample
Пример #55
0
def make_lungmask(img, display=False):
    row_size = img.shape[0]
    col_size = img.shape[1]

    mean = np.mean(img)
    std = np.std(img)
    img = img - mean
    img = img / std
    # Find the average pixel value near the lungs
    # to renormalize washed out images
    middle = img[int(col_size / 5):int(col_size / 5 * 4),
                 int(row_size / 5):int(row_size / 5 * 4)]
    mean = np.mean(middle)
    max = np.max(img)
    min = np.min(img)
    # To improve threshold finding, I'm moving the
    # underflow and overflow on the pixel spectrum
    img[img == max] = mean
    img[img == min] = mean
    #
    # Using Kmeans to separate foreground (soft tissue / bone) and background (lung/air)
    #
    kmeans = KMeans(n_clusters=1).fit(
        np.reshape(middle, [np.prod(middle.shape), 1]))
    centers = sorted(kmeans.cluster_centers_.flatten())
    threshold = np.mean(centers)
    thresh_img = np.where(img < threshold, 1.0,
                          0.0)  # threshold the image 满足输出1,不满足输出0

    # First erode away the finer elements, then dilate to include some of the pixels surrounding the lung.
    # We don't want to accidentally clip the lung.

    eroded = morphology.erosion(thresh_img, np.ones([3, 3]))
    dilation = morphology.dilation(eroded, np.ones([5, 5]))

    labels = measure.label(
        dilation)  # Different labels are displayed in different colors
    label_vals = np.unique(labels)
    regions = measure.regionprops(labels)
    good_labels = []
    good_area = []
    numsR = len(regions)
    print('regions数量为:', numsR)
    for prop in regions:
        B = prop.bbox
        A = prop.area
        print(A)
        print(B)
        #if A > 3000 and A < 150000:
        if A > 1000 and A < 50000:
            if B[2] - B[0] < row_size / 10 * 10 and B[3] - B[
                    1] < col_size / 10 * 10 and B[0] > row_size / 12 and B[
                        2] < col_size:

                #if A == 19749 or A==18319:
                good_labels.append(prop.label)

    mask = np.ndarray([row_size, col_size], dtype=np.int8)
    mask[:] = 0

    # print(good_labels)
    # print(row_size)
    # print(col_size / 5 *4)
    # print(col_size / 10 * 9)
    # print(col_size / 12 )

    #
    #  After just the lungs are left, we do another large dilation
    #  in order to fill in and out the lung mask
    #
    for N in good_labels:
        mask = mask + np.where(labels == N, 1, 0)
    mask = morphology.dilation(mask, np.ones([10, 10]))  # one last dilation

    if (display):
        fig, ax = plt.subplots(3, 2, figsize=[12, 12])
        ax[0, 0].set_title("Original")
        ax[0, 0].imshow(img, cmap='gray')
        ax[0, 0].axis('off')
        ax[0, 1].set_title("Threshold")
        ax[0, 1].imshow(thresh_img, cmap='gray')
        ax[0, 1].axis('off')
        ax[1, 0].set_title("After Erosion and Dilation")
        ax[1, 0].imshow(dilation, cmap='gray')
        ax[1, 0].axis('off')
        ax[1, 1].set_title("Color Labels")
        ax[1, 1].imshow(labels)
        ax[1, 1].axis('off')
        ax[2, 0].set_title("Final Mask")
        ax[2, 0].imshow(mask, cmap='gray')
        ax[2, 0].axis('off')
        ax[2, 1].set_title("Apply Mask on Original")
        ax[2, 1].imshow(mask * img, cmap='gray')
        ax[2, 1].axis('off')

        plt.show()
    return mask
Пример #56
0
        i += 1
    return i


plt.close('all')
plt.rcParams['image.cmap'] = 'gray'

ticket = skio.imread(image_path, as_gray=True)
if args.downscale:
    ticket = skt.rescale(ticket, 1024 / ticket.shape[1], preserve_range=True)

## Canny
ticket_contours = canny(ticket,
                        sigma=2 * (ticket.shape[0] * ticket.shape[1])**.5 /
                        1500)
ticket_contours = morpho.dilation(ticket_contours, morpho.disk(3))

## Transfo de Hough
hspace, angles, distances = skt.hough_line(ticket_contours)
hspacep, angles, distances = skt.hough_line_peaks(hspace, angles, distances)
normalized_hspacep = (hspacep - np.min(hspacep)) / (np.max(hspacep) -
                                                    np.min(hspacep))

## Rotation
angles_candidats = []
distances_candidats = []
for i, angle in enumerate(angles):  # filtrage des angles
    if abs(angle) < np.deg2rad(30):
        angles_candidats.append(angle)  # ils sont dans l'ordre
        distances_candidats.append(distances[i])
Пример #57
0
def regiongrow(crop, xs, ys):
    
        crop = numpy.int32(crop)
        # compute edge intensity
        eimg = sobel(crop)
        # define kernel for different orientation of edges
        k1 = numpy.asarray([[0, 0, 0], [1, 1, 1], [0, 0, 0]])
        k2 = numpy.asarray([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
        k3 = numpy.asarray([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        k4 = numpy.asarray([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
        # edge at different orientations
        c1 = conv2(eimg, k1, 'same')
        c2 = conv2(eimg, k2, 'same')
        c3 = conv2(eimg, k3, 'same')
        c4 = conv2(eimg, k4, 'same')
        # combine them
        combimg = numpy.maximum(c1, numpy.maximum(c2, numpy.maximum(c3, c4)))

        # color difference in difference directions
        d3 = [[-1, -1], [1, 1]]
        dis3 = numpy.absolute(conv2(crop, d3, 'same'))
        d4 = [[1, 1], [-1, -1]]
        dis4 = numpy.absolute(conv2(crop, d4, 'same'))
        d1 = [[-1, 1], [-1, 1]]
        dis1 = numpy.absolute(conv2(crop, d1, 'same'))
        d2 = [[1, -1], [1, -1]]
        dis2 = numpy.absolute(conv2(crop, d2, 'same'))

        # initialize label image
        label = numpy.zeros_like(crop)
        label[0, :] = 1
        label[-1, :] = 1
        label[:, 0] = 1
        label[:, -1] = 1
        label[xs, ys] = 2

        # dilation kernel for different directions    
        selem1 = [[0, 1]]
        selem2 = [[1, 1, 0]]
        selem3 = [[0], [1]]
        selem4 = [[1], [1], [0]]
        maxe = numpy.percentile(numpy.abs(eimg), 80)

        i = 0
        while 1:
                i += 1 
                # dilation image: new regions
                dulllabel = numpy.uint8(label>0)
                nimg1 = dilation(dulllabel, selem1)  
                nimg2 = dilation(dulllabel, selem2) 
                nimg3 = dilation(dulllabel, selem3) 
                nimg4 = dilation(dulllabel, selem4) 

                # color differences at new regions
                diffimg1 = numpy.multiply(dis1, nimg1)
                diffimg2 = numpy.multiply(dis2, nimg2)
                diffimg3 = numpy.multiply(dis3, nimg3)
                diffimg4 = numpy.multiply(dis4, nimg4)

                # total dilation image
                nimg = dilation(dulllabel) - dulllabel 
                # cost function
                tcost = eimg + diffimg1 + diffimg2 + diffimg3 + diffimg4 + (nimg == 0)*maxint

                gp = numpy.argmin(tcost)
                xp, yp = numpy.unravel_index(gp, tcost.shape)

                # grows
                seedlabel = max([label[xp-1, yp], label[xp+1, yp], label[xp, yp-1], label[xp, yp+1]])
                label[xp, yp] = seedlabel
                if not i%100:
                        if not numpy.sum(label == 2):
                                return numpy.zeros_like(label)                                

                        if numpy.sum(numpy.multiply(numpy.uint8(label==2), numpy.abs(eimg)))/numpy.sum(label==2) > maxe :
                                return label == 2
                        if numpy.sum(label == 0) == 0:
                                if numpy.sum(numpy.multiply(numpy.uint8(label==2), numpy.abs(eimg)))/numpy.sum(label==2) > maxe * 0.5:
                                        return label == 2
                                else:
                                        return numpy.zeros_like(label)                                
Пример #58
0
#lab5 zad1
import skimage as sk
from skimage import feature
import scipy as sc
import numpy as np
import os
import matplotlib.pyplot as plt
import cv2
from skimage.morphology import disk, star, diamond, octagon, rectangle, square, erosion, dilation
img = 255 - sk.io.imread("bw1.bmp", True)
img_e = img.copy()
img_d = img.copy()
for i in range(0, 3):
    img_e = erosion(img_e.copy(), rectangle(6, 4))
    img_d = dilation(img_d.copy(), rectangle(6, 4))
    plt.imshow(np.hstack([255 - img, 255 - img_e, 255 - img_d]), cmap='gray')
    plt.title(
        "rectangle(6,4), iteration: " + str(i + 1), {
            'fontsize': 28,
            'fontweight': 'bold',
            'verticalalignment': 'baseline',
            'horizontalalignment': 'center'
        })
    plt.axis('off')
    plt.show()

img_e = img.copy()
img_d = img.copy()
for i in range(0, 3):
    img_e = erosion(img_e.copy(), disk(5))
    img_d = dilation(img_d.copy(), disk(5))
Пример #59
0
def dil_skel(BW, radius=3):

    selem_disk = disk(radius)
    BW = dilation(BW, selem_disk)
    BW = skeletonize(BW)
    return BW
Пример #60
0
        origin = np.array(itk_img.GetOrigin())

        new_image, new_spacing = resample(img_array,
                                          old_spacing=spacing,
                                          new_spacing=new_spacing)

        segmented_lungs = segment_lung_mask(new_image, False)
        segmented_lungs_filled = segment_lung_mask(new_image, True)
        nodule_mask0 = segmented_lungs_filled - segmented_lungs

        img_gradient = ndimage.gaussian_gradient_magnitude(new_image, sigma=1)
        lung_gradient = img_gradient * segmented_lungs_filled
        nodule_mask1 = (lung_gradient > g_threshold) * segmented_lungs_filled
        for j in range(len(nodule_mask1)):
            mask_slice = nodule_mask1[j].copy()
            mask_dilation = dilation(mask_slice, disk(tissue_dilation_radius))
            mask_erosion = erosion(mask_dilation, disk(tissue_erosion_radius))
            label = measure.label(mask_erosion, background=0)
            vals, counts = np.unique(label, return_counts=True)
            counts = counts[vals != 0]
            vals = vals[vals != 0]
            for k in range(len(counts)):  # remove too small region
                count = counts[k]
                val = vals[k]
                if count < min_nodule_area:
                    mask_erosion[label == val] = 0
            nodule_mask1[j] = mask_erosion

        nodule_mask2 = np.zeros_like(segmented_lungs)  # lung borders
        for j in range(len(segmented_lungs)):
            lung_slice = segmented_lungs[j]