def main():
    args = getArguments(getParser())

    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)
    
    # loading input images
    b0img, b0hdr = load(args.b0image)
    bximg, bxhdr = load(args.bximage)
    
    # convert to float
    b0img = b0img.astype(numpy.float)
    bximg = bximg.astype(numpy.float)

    # check if image are compatible
    if not b0img.shape == bximg.shape:
        raise ArgumentError('The input images shapes differ i.e. {} != {}.'.format(b0img.shape, bximg.shape))
    if not header.get_pixel_spacing(b0hdr) == header.get_pixel_spacing(bxhdr):
        raise ArgumentError('The input images voxel spacing differs i.e. {} != {}.'.format(header.get_pixel_spacing(b0hdr), header.get_pixel_spacing(bxhdr)))
    
    # check if supplied threshold value as well as the b value is above 0
    if args.threshold is not None and not args.threshold >= 0:
        raise ArgumentError('The supplied threshold value must be greater than 0, otherwise a division through 0 might occur.')
    if not args.b > 0:
        raise ArgumentError('The supplied b-value must be greater than 0.')
    
    # compute threshold value if not supplied
    if args.threshold is None:
        b0thr = otsu(b0img, 32) / 4. # divide by 4 to decrease impact
        bxthr = otsu(bximg, 32) / 4.
        if 0 >= b0thr:
            raise ArgumentError('The supplied b0image seems to contain negative values.')
        if 0 >= bxthr:
            raise ArgumentError('The supplied bximage seems to contain negative values.')
    else:
        b0thr = bxthr = args.threshold
    
    logger.debug('thresholds={}/{}, b-value={}'.format(b0thr, bxthr, args.b))
    
    # threshold b0 + bx DW image to obtain a mask
    # b0 mask avoid division through 0, bx mask avoids a zero in the ln(x) computation
    mask = binary_fill_holes(b0img > b0thr) & binary_fill_holes(bximg > bxthr)
    
    # perform a number of binary morphology steps to select the brain only
    mask = binary_erosion(mask, iterations=1)
    mask = largest_connected_component(mask)
    mask = binary_dilation(mask, iterations=1)
    
    logger.debug('excluding {} of {} voxels from the computation and setting them to zero'.format(numpy.count_nonzero(mask), numpy.prod(mask.shape)))
    
    # compute the ADC
    adc = numpy.zeros(b0img.shape, b0img.dtype)
    adc[mask] = -1. * args.b * numpy.log(bximg[mask] / b0img[mask])
    adc[adc < 0] = 0
            
    # saving the resulting image
    save(adc, args.output, b0hdr, args.force)
        def proc_roi_region(self,add_region=True):

            mpossx = self.roi_item.getArrayRegion(self.possx,self.img).astype(int)
            mpossx = mpossx[np.nonzero(binary_fill_holes(mpossx))]#get the x pos from ROI
            mpossy = self.roi_item.getArrayRegion(self.possy,self.img).astype(int)



            mpossy = mpossy[np.nonzero(binary_fill_holes(mpossy))]# get the y pos from ROI
            xLims = [np.min(mpossx)-10,np.max(mpossx)+10]
            yLims = [np.min(mpossy)-10,np.max(mpossy)+10]
            #xLims = [np.mean(mpossx)-20,np.mean(mpossx)+20]; yLims = [np.mean(mpossy)-20,np.mean(mpossy)+20]
            xIn = np.all(np.logical_and(xLims[0]>1,xLims[1]<510))
            yIn = np.all(np.logical_and(yLims[0]>1,yLims[1]<510))
            if np.logical_and(xIn,yIn):

                self.temp_mask[mpossx,mpossy] = 1
                self.temp_mask = binary_fill_holes(self.temp_mask).T
                if add_region:
                    self.vidTimer.stop()
                    self.ROI_attrs['centres'].append([np.mean(mpossx),np.mean(mpossy)])
                    self.ROI_attrs['patches'].append(self.mean_image[yLims[0]:yLims[1],xLims[0]:xLims[1]])
                    self.ROI_attrs['idxs'].append([mpossx,mpossy])
                    self.ROI_attrs['masks'].append(self.temp_mask[yLims[0]:yLims[1],xLims[0]:xLims[1]])
                    if online_trace_extract:
                        temp = areaFile[:,yLims[0]:yLims[1],xLims[0]:xLims[1]] *self.ROI_attrs['masks'][-1]
                        temp = temp.astype('float64')
                        temp[temp==0] = np.nan
                        self.ROI_attrs['traces'].append(np.nanmean(temp,axis=(1,2)))
                        #self.ROI_attrs['mask_arr'].append(temp_mask)
                        self.Gplt.clear()
                        for i in self.stimattrs['clicks']:
                            clt_L = pg.InfiniteLine(pos=i,angle=90,movable=False)
                            self.Gplt.addItem(clt_L)
                        self.Gplt.addItem(self.timeLine)
                        self.Gplt.plot(self.ROI_attrs['traces'][-1])
                    else:
                        self.ROI_attrs['traces'].append([0])

                    self.vidTimer.start(self.IFI)

                    self.mask[:,:,0] += self.mask[:,:,1]
                    self.mask[:,:,1] = 0
                    self.mask[:,:,1] = self.temp_mask.T
                    #self.mask[:,:,0] += self.temp_mask.T
                    self.mask[:,:,3] += self.temp_mask.T
                    self.nROIs += 1
                    self.roi_idx = self.nROIs - 1

                else:
                    self.mask[mpossx,mpossy,0] = 0
                    self.mask[mpossx,mpossy,3] = 0
            else:
                print 'Cannot draw ROI, out of bounds.'
            self.temp_mask = np.zeros(self.temp_mask.shape) 
Пример #3
0
 def get_mask_boundary(masks, radius_dil=2, radius_ero=2):
     mask_boundaries = np.zeros(masks.shape)
     for i, mask in enumerate(masks):
         mask_img = sitk.GetImageFromArray(mask, isVector=False)
         mask_dilated = sitk.GrayscaleDilate(mask_img, radius_dil)
         mask_dilated = binary_fill_holes(
             sitk.GetArrayFromImage(mask_dilated)).astype(mask.dtype)
         mask_eroded = sitk.GrayscaleErode(mask_img, radius_ero)
         mask_eroded = binary_fill_holes(
             sitk.GetArrayFromImage(mask_eroded)).astype(mask.dtype)
         mask_boundaries[i] = mask_dilated - mask_eroded
     return mask_boundaries
Пример #4
0
def load_data(patient_path, mask_path):
    ct_scan = read_ct_scan(patient_path)
    mask = load(mask_path)
    patient = get_pixels_hu(ct_scan)
    patient, spacing = resample(patient, ct_scan, SPACING)
    mask, spacing = resample(mask, ct_scan, SPACING)

    mask = morphology.binary_fill_holes(
        morphology.binary_dilation(morphology.binary_fill_holes(mask > 0),
                                   iterations=4))

    return patient, mask
Пример #5
0
def norm_spray(test_path, flat_path, dark_path, save_fld, mask_fld):
    if 'SC' in test_path:
        rot = -1.5
        otsu_scale = 0.1
        erode_iter = 7
        dilat_iter = 10
    else:
        rot = -1
        otsu_scale = 0.01

    data = np.array(Image.open(test_path))
    flat = np.array(Image.open(flat_path))
    dark = np.array(Image.open(dark_path))
    data_norm = (data - dark) / (flat - dark)

    # Calculate and apply offset
    ofst = np.nanmedian(data_norm[52:52 + 52, 52:52 + 52])
    data_norm /= ofst
    data_norm = rotate(data_norm, rot, preserve_range=True, cval=np.nan)

    # Apply median filter to remove spurious pixels
    data_norm = median_filter(data_norm, 3)

    mask = np.zeros(data_norm.shape, dtype='single')
    mask[20:, :] = 1

    data_filt = median_filter(data_norm, 13)
    data_atten = 1 - data_filt
    data_atten[data_atten < 0] = 0
    float2int = 10000
    int2float = 1 / float2int
    data_atten16 = np.rint(data_atten * float2int).astype(np.uint16)
    data_thresh = data_atten16 * mask.astype(np.uint16)
    thresh_otsu = threshold_otsu(data_thresh[~np.isnan(data_thresh)])
    spray_mask = data_thresh > (otsu_scale * thresh_otsu)

    if 'SC' in test_path:
        spray_mask = binary_erosion(spray_mask, iterations=erode_iter)
        spray_mask = binary_erosion(spray_mask, iterations=dilat_iter)
    else:
        spray_mask = binary_erosion(spray_mask, iterations=5)
        spray_mask = binary_dilation(spray_mask, iterations=11)
        spray_mask = binary_fill_holes(spray_mask)
        spray_mask = binary_dilation(spray_mask, iterations=11)
        spray_mask = binary_fill_holes(spray_mask)

    # Save Transmission images
    im = Image.fromarray(data_norm)
    im.save(save_fld + '/' + split(test_path)[1])

    # Save mask images
    im = Image.fromarray(spray_mask)
    im.save(mask_fld + '/' + split(test_path)[1])
Пример #6
0
def compute_volume_pixels(lung):
    col = lung.shape[1]
    left_volume = 0
    right_volume = 0
    struct_opening_post = disk(2)
    struct_closing = disk(10)
    filled = np.copy(lung)
    label_image = np.zeros(lung[..., 0].shape)
    max_area = 0
    for i in range(lung.shape[2]):

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

        label_image_first, num = label(lung[..., i], return_num=True)
        coords = regionprops(label_image_first)[0].coords
        threshold = coords[0, 0] + ((coords[-1, 0] - coords[0, 0]) // 8)

        filled_test = (binary_fill_holes(lung[..., i])).astype(np.uint8) * 255
        closed_test = filled_test.copy()
        closed_test = (binary_closing(filled_test, struct_closing)).astype(np.uint8) * 255
        closed_test = (binary_opening(closed_test, struct_opening_post)).astype(np.uint8) * 255

        label_image, num = label(closed_test, return_num=True)

        size = 5
        opened_high = closed_test.copy()
        while size <= 40 and num == 1 and regionprops(label_image)[0].area > 10000:
            struct_opening = disk(size)
            opened_high = (binary_opening(opened_high, struct_opening)).astype(np.uint8) * 255
            label_image, num = label(opened_high, return_num=True)
            size += 5
        if size <= 40:
            closed_test = opened_high

        filled_test_second = (binary_fill_holes(closed_test)).astype(np.uint8) * 255
        #filled_test_second = (binary_opening(filled_test_second, struct_opening_post)).astype(np.uint8) * 255

        label_image, num = label(filled_test_second, return_num=True)
    
        filled[..., i] = filled_test_second
        
        area = 0
        for region in regionprops(label_image):
            area += region.area
            if np.sum(region.coords[:, 1] > col // 2) <= np.sum(region.coords[:, 1] <= col // 2):
                left_volume += region.area
            else:
                right_volume += region.area

    return left_volume, right_volume, filled
Пример #7
0
def makeCervixAndChannelMask(pmapCervixU8, pmapChannelU8):
    try:
        # (1) load images
        mskChn = pmapChannelU8.astype(np.float) / 255.
        mskCrv = pmapCervixU8.astype(np.float) / 255.
        # (2) preprocess Cervix Mask
        thresholdCrv = 0.5
        if np.sum(mskCrv>0.5)<50:
            thresholdCrv = 0.75 * np.max(mskCrv)
        _, R_crv = get_max_blob_mask(mskCrv > thresholdCrv)
        msizCRV = math.ceil(R_crv * 0.04)
        if msizCRV < 3:
            msizCRV = 3
        mskCrv_Blob2 = skmorph.closing(mskCrv > thresholdCrv, skmorph.disk(msizCRV))
        mskCrv_Blob3, _ = get_max_blob_mask(mskCrv_Blob2 > 0)
        mskCrv_Blob4 = binary_fill_holes(mskCrv_Blob3)
        # (3) preprocess Channel mask
        thresholdChn = 0.5
        if np.sum(mskChn>0.5)<50:
            thresholdChn = 0.75 * np.max(mskChn)
        mskChn_Blob1 = mskChn.copy()
        mskChn_Blob1[~mskCrv_Blob4] = 0
        # (3.1) check zero-channel-mask
        if np.sum(mskChn_Blob1 > thresholdChn) < 1:
            mskChn_Blob1 = skmorph.skeletonize(mskCrv_Blob4)
            mskChn_Blob1 = skmorph.closing(mskChn_Blob1, skmorph.disk(5))
            R_chn = 5
        else:
            _, R_chn = get_max_blob_mask(mskChn_Blob1 > 0.5)
        msizChn = math.ceil(R_chn * 0.1)
        if msizChn < 3:
            msizChn = 3
        mskChn_Blob2 = skmorph.closing(mskChn_Blob1 > 0.5, skmorph.disk(msizChn))
        mskChn_Blob2[~mskCrv_Blob4] = 0
        # (3.1) check zero-channel-mask
        if np.sum(mskChn_Blob2 > 0) < 1:
            mskChn_Blob2 = skmorph.skeletonize(mskCrv_Blob4)
            mskChn_Blob2 = skmorph.closing(mskChn_Blob2, skmorph.disk(5))
            #
        mskChn_Blob3, _ = get_max_blob_mask(mskChn_Blob2 > 0)
        mskChn_Blob4 = binary_fill_holes(mskChn_Blob3)
        # (4) Composing output mask
        mskShape = pmapCervixU8.shape[:2]
        mskOut = 64 * np.ones(mskShape, dtype=np.uint8)
        mskOut[mskCrv_Blob4] = 255
        mskOut[mskChn_Blob4] = 128
    except Exception as err:
        print ('\t!!! ERROR !!! [{0}]'.format(err))
        mskOut = np.zeros(pmapCervixU8.shape[:2], dtype=np.uint8)
    return mskOut
Пример #8
0
def segment_lung(imgs_with_hu):
    masks = create_mask(imgs_with_hu)

    h_masks = morphology.binary_fill_holes(masks > 0)

    # dilation = extract pixels, smoothes the boundaires
    d1_masks = morphology.binary_dilation(h_masks, iterations=1)

    # fill_holes = fill in holes
    h1_masks = morphology.binary_fill_holes(d1_masks)

    imgs_segmented_lung = imgs_with_hu * h1_masks

    return imgs_segmented_lung
def FillHole(bin_image, invert=False):
    # files holes
    values = np.unique(bin_image)
    if len(values) > 2:
        print "Not binary image"
        return []
    background = min(values)
    bin_image -= background
    bin_image[bin_image > 0] = 1
    if invert:
        bin_image -= 1
        bin_image[bin_image < 0] = 1
    result = np.copy(bin_image)
    binary_fill_holes(bin_image, output=result)
    return result
Пример #10
0
def load_data(in_folder, pixel_size):
    name_add = "" # "_large

    mask_traction = np.load(os.path.join(in_folder, "traction_mask.npy"))
    mask_traction = binary_fill_holes(mask_traction)
    mask_cell_border = np.load(os.path.join(in_folder, "cell_border_mask.npy"))
    mask_cell_border = binary_fill_holes(mask_cell_border)
    fx = np.load(os.path.join(in_folder, "fx%s.npy"%name_add))
    fy = np.load(os.path.join(in_folder, "fy%s.npy"%name_add))
    tx = fx / ((pixel_size * 10 ** -6) ** 2)
    ty = fy / ((pixel_size * 10 ** -6) ** 2)
    u = np.load(os.path.join(in_folder, "u%s.npy"%name_add))
    v = np.load(os.path.join(in_folder, "v%s.npy"%name_add))

    return mask_traction, mask_cell_border, fx, fy, tx, ty, u, v
Пример #11
0
def fill_hole(bin_image, invert=False):
    # files holes
    values = np.unique(bin_image)
    if len(values) > 2:
        print("Not binary image")
        return []
    background = min(values)
    bin_image -= background
    bin_image[bin_image > 0] = 1
    if invert:
        bin_image -= 1
        bin_image[bin_image < 0] = 1
    result = np.copy(bin_image)
    binary_fill_holes(bin_image, output=result)
    return result
Пример #12
0
def Fg_extract(frame,type = 1): #extract foreground    

    if type ==1:
        mu[:]       = alpha*frame + (1.0-alpha)*mu_old
        mu_old[:]   = mu
        sig2[:]     = alpha*(1.0*frame-mu)**2 + (1.0-alpha)*sig2_old
        sig2_old[:] = sig2
        
        sig = sig2**0.5
        
        lmcs = lmc*sig
        bmcs = bmc*sig
        
        fg= np.abs(1.0*frame-mu)[:,:,0]-1*sig[:,:,0]>0.0
    elif type == 2:
        try:
            fg = np.abs(1.0*frame.mean(2)-BG)>50.0
        except:
            BG = pickle.load(open("bg13-19.pkl","rb"))
            BG = cv2.resize(BG,(0,0),fx = 0.5,fy=0.5)
            fg = np.abs(1.0*frame.mean(2)-BG)>50.0
            

    fgo = ndm.binary_opening(fg)
    fgf = ndm.binary_fill_holes(fgo)
    right.set_data(fgf)
    plt.draw()
       
    return fgf
Пример #13
0
def post_processing(image):
    image_post = image.copy()

     # get texte pixel
    illu = 255*(np.sum((image -[0,0,255])**2,axis=2)<10).astype(np.uint8)
    # fill holes
    illu_out = binary_fill_holes(illu)
    image_post[illu_out>0,:] = [0,0,255]

   # get illustration pixel
    illu = 255*(np.sum((image -[255,0,0])**2,axis=2)<10).astype(np.uint8)
    # get bounding-box of connected components
    bbox = pymorph.blob(measure.label(illu),'boundingbox','data')
    illu_out = illu.copy()
    # transform connected components into englobing rectangles
    for l in bbox:
	x1 = l[0]
	y1 = l[1]
	x2 = l[2]
	y2 = l[3]
        if ((y2-y1)<image.shape[0]/2) | ((x2-x1)<image.shape[1]/2):
            illu_out[y1:y2,x1:x2]=255
    image_post[illu_out>0,:] = [255,0,0]

    return image_post
Пример #14
0
def refine_aseg(aseg, ball_size=4):
    """
    First step to reconcile ANTs' and FreeSurfer's brain masks.

    Here, the ``aseg.mgz`` mask from FreeSurfer is refined in two
    steps, using binary morphological operations:

      1. With a binary closing operation the sulci are included
         into the mask. This results in a smoother brain mask
         that does not exclude deep, wide sulci.

      2. Fill any holes (typically, there could be a hole next to
         the pineal gland and the corpora quadrigemina if the great
         cerebral brain is segmented out).


    """
    # Read aseg data
    bmask = aseg.copy()
    bmask[bmask > 0] = 1
    bmask = bmask.astype(np.uint8)

    # Morphological operations
    selem = sim.ball(ball_size)
    newmask = sim.binary_closing(bmask, selem)
    newmask = binary_fill_holes(newmask.astype(np.uint8), selem).astype(np.uint8)

    return newmask.astype(np.uint8)
Пример #15
0
def find_ellipse(img, background=None, threshrange=[1,254], sizerange=[10,400], dist_thresh=10, erode=False, check_centers=False, autothreshpercentage=None, show=False):
    
    #print '**img shape** ', img.shape
    body = find_object(img, background=background, threshrange=threshrange, sizerange=sizerange, dist_thresh=dist_thresh, erode=erode, check_centers=check_centers, autothreshpercentage=autothreshpercentage)

    if body.sum() < 1 and check_centers==True:
        body = find_object(img, background=background, threshrange=threshrange, sizerange=sizerange, dist_thresh=dist_thresh, erode=erode, check_centers=check_centers, autothreshpercentage=autothreshpercentage)
        
    body = binary_fill_holes(body)
    
    if body.sum() < 1:
        body[body.shape[0] / 2, body.shape[1] / 2] = 1
    
    center, longaxis, shortaxis, ratio = get_ellipse_cov(body, erode=False, recenter=True)
    
    
    if show:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.imshow(img)
        
        circle = patches.Circle((center[1], center[0]), 2, facecolor='white', edgecolor='none')
        ax.add_artist(circle)
        ax.plot([center[1]-longaxis[1]*ratio[0], center[1]+longaxis[1]*ratio[0]], [center[0]-longaxis[0]*ratio[0], center[0]+longaxis[0]*ratio[0]], zorder=10, color='white')
    
    
    return center, longaxis, shortaxis, body, ratio
Пример #16
0
def detect_sources(snmap, threshold):
    hot = (snmap > threshold)
    hot = binary_dilation(hot, iterations=2)
    hot = binary_fill_holes(hot)
    blobs,nblobs = label(hot)
    print(nblobs, 'blobs')
    #print('blobs min', blobs.min(), 'max', blobs.max())
    slices = find_objects(blobs)
    px,py = [],[]
    for i,slc in enumerate(slices):
        blob_loc = blobs[slc]
        sn_loc = snmap[slc]
        imax = np.argmax((blob_loc == (i+1)) * sn_loc)
        y,x = np.unravel_index(imax, blob_loc.shape)
        y0,x0 = slc[0].start, slc[1].start
        px.append(x0+x)
        py.append(y0+y)
        #if i == 0:
        #    plt.subplot(2,2,1)
        #    plt.imshow(blob_loc, interpolation='nearest', origin='lower')
        #    plt.colorbar()
        #    plt.subplot(2,2,2)
        #    plt.imshow((blob_loc==(i+1))*sn_loc, interpolation='nearest', origin='lower')
        #    plt.subplot(2,2,3)
        #    plt.plot(x, y, 'ro')
    return np.array(px),np.array(py)
def crop_face(image):
    """takes as input an rgb image of a centered face. crops the skin part out using the HSV distribution of the center
    part. Fills in the holes and returns the cropped image as a grey-value matrix"""
    assert shape(image)==(300,300,3)
    subimage = image[110:190,110:190]

    subimage_hsv=cv2.cvtColor(subimage, cv2.COLOR_RGB2HSV)
    image_hsv=cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
    h,w,colors=shape(subimage)
    HSV=zeros([h*w,3])

    for i in range(3):
        HSV[:,i]=ravel(subimage_hsv[:,:,i])

    Mean_hsv=mean(HSV,axis=0)
    STD_hsv=std(HSV,axis=0)

    notface=sum(((image_hsv-Mean_hsv)/STD_hsv)**2,axis=2)
    mask=(1*(notface<10)).astype(int)

    label_im, nb_labels = ndimage.label(mask)
    sizes = ndimage.sum(mask, label_im, range(nb_labels + 1))
    mask=label_im==argmax(sizes)

    mask2=binary_fill_holes(mask)

    image2=copy(image)
    for i in range(3):
        image2[:,:,i] *= mask2   #2.astype(int64)

    grey_image=sum(image2,axis=2)
    return grey_image, image2
Пример #18
0
def remove_background (image):

  # remove frame if any
  image = image[5:-5, 5:-5, :]

  thresh = np.array([10,10,10]).astype(int)
  c = get_dominant_colors(image, bin_size=thresh/4)
  if c is None: return None

  b, g, r = np.rollaxis(image, axis=-1)
  c = c.astype(int)
  mask = np.ones(r.shape, dtype=bool)
  mask = np.bitwise_and(mask, b.astype(int) >= c[0]-thresh[0])
  mask = np.bitwise_and(mask, b.astype(int) <= c[0]+thresh[0])
  mask = np.bitwise_and(mask, g.astype(int) >= c[1]-thresh[1])
  mask = np.bitwise_and(mask, g.astype(int) <= c[1]+thresh[1])
  mask = np.bitwise_and(mask, r.astype(int) >= c[2]-thresh[2])
  mask = np.bitwise_and(mask, r.astype(int) <= c[2]+thresh[2])

  mask = np.invert(binary_fill_holes(np.invert(mask)))
  for i in range(DILATE_ITER):
    mask = binary_dilation(mask)

  r[mask] = 0
  g[mask] = 0
  b[mask] = 0
  image = np.dstack((b, g, r, np.invert(mask.astype(np.uint8)*255)))
  return image
Пример #19
0
def postproc_masks(im, thrs=[], fill_holes=True, output='', report={}):
    """Apply slicewise thresholds to data and fill holes.

    NOTE: zyx assumed
    """

    ods = 'mask_thr{:05d}'.format(0)

    im.load(load_data=False)
    if thrs:
        data = im.slice_dataset()
        mask = np.zeros(im.dims[:3], dtype='bool')
        for slc in range(0, mask.shape[0]):
            mask[slc, :, :] = data[slc, :, :] > thrs[slc]
    else:
        mask = im.slice_dataset()
    im.close()

    if fill_holes:
        for slc in range(0, mask.shape[0]):
            mask[slc, :, :] = binary_fill_holes(mask[slc, :, :])

    props = im.get_props()
    mo = write_data(mask, props, output, ods)

    c_slcs = {dim: get_centreslice(mo, '', dim) for dim in 'zyx'}
    report['centreslices'][ods] = c_slcs

    return mo, report
Пример #20
0
def main():
	# catch parameters
	forest_file = sys.argv[1]
	case_folder = sys.argv[2]
	mask_file = sys.argv[3]
	segmentation_file = sys.argv[4]

        # loading case features
	feature_vector = []
	for _file in os.listdir(case_folder):
		if _file.endswith('.npy') and _file.startswith('feature.'):
			with open(os.path.join(case_folder, _file), 'r') as f:
				feature_vector.append(numpy.load(f))
	feature_vector = join(*feature_vector)
	if 1 == feature_vector.ndim:
		feature_vector = numpy.expand_dims(feature_vector, -1)

	# load and apply the decision forest
	with open(forest_file, 'r') as f:
		forest = pickle.load(f)
	classification_results = forest.predict(feature_vector)

	# preparing  image
	m, h = load(mask_file)
    	m = m.astype(numpy.bool)
    	o = numpy.zeros(m.shape, numpy.uint8)
    	o[m] = numpy.squeeze(classification_results).ravel()

	# applying the post-processing morphology
	#o = binary_dilation(o, iterations=2)
	#o = keep_largest_connected_component(o)
	o = binary_fill_holes(o)

	# savin the results
    	save(o, segmentation_file, h, True)
Пример #21
0
def get_body_mask(ct_data, win_min=1200, morphology_radius=2, connectivity=3):
    """
    Get body mask from CT image.
    Argument `win_min` should approx to air intensity value.
    """
    body_mask = (ct_data>=win_min)# & (ct_data<=win_max)
    #print(' {} of {} voxels masked.'.format(np.sum(body_mask),np.size(body_mask)))
    if np.sum(body_mask)==0:
        raise ValueError('BODY could not be extracted!')
    
    # Find largest connected component in 3D
    struct = generate_binary_structure(3,connectivity)
    body_mask = binary_erosion(body_mask,structure=struct,iterations=morphology_radius)
    if np.sum(body_mask)==0:
        raise ValueError('BODY mask disappeared after erosion!')

    # Get the largest connected component
    labeled_array, num_features = label(body_mask, structure=struct)
    component_sizes = np.bincount(labeled_array.ravel())
    max_label = np.argmax(component_sizes[1:-1])+1

    # only keep largest, dilate again and fill holes                
    body_mask = binary_dilation(labeled_array==max_label,structure=struct,iterations=morphology_radius)
    # Fill holes slice-wise
    for z in range(0,body_mask.shape[2]):    
        body_mask[:,:,z] = binary_fill_holes(body_mask[:,:,z])
    return body_mask
Пример #22
0
def Fg_extract(frame): #extract foreground    

    mu[:]       = alpha*frame + (1.0-alpha)*mu_old
    mu_old[:]   = mu
    sig2[:]     = alpha*(1.0*frame-mu)**2 + (1.0-alpha)*sig2_old
    sig2_old[:] = sig2

    sig = sig2**0.5

    lmcs = lmc*sig
    bmcs = bmc*sig

    fg= np.abs(1.0*frame-mu)[:,:,0]-2*sig[:,:,0]>0.0
    fgo = ndm.binary_opening(fg)
    fgf = ndm.binary_fill_holes(fgo)
    right.set_data(fgf)
    plt.draw()
    '''
    if imsave:
        im = zeros(frame.shape)
        a = fgf.astype(np.uint8)*255
        im[:,:,0]=a
        im[:,:,1]=a
        im[:,:,2]=a
        
        im = Image.fromarray(im.astype(np.uint8))
        im.save('/home/andyc/image/tracking VIDEO0004/binary/b%.3d.bmp'%vid_idx)
    '''
    return fgf
Пример #23
0
def segment_roi(roi):
    # step 1. phase congruency (edge detection)
    Mm = phasecong_Mm(roi)
    # step 2. hysteresis thresholding (of edges)
    B = hysthresh(Mm,HT_T1,HT_T2)
    # step 3. trim pixels off border
    B[B[:,1]==0,0]=0
    B[B[:,-2]==0,-1]=0
    B[0,B[1,:]==0]=0
    B[-1,B[-2,:]==0]=0
    # step 4. threshold to find dark areas
    dark = dark_threshold(roi, DARK_THRESHOLD_ADJUSTMENT)
    # step 5. add dark areas back to blob
    B = B | dark
    # step 6. binary closing
    B = binary_closing(B,SE3)
    # step 7. binary dilation
    B = binary_dilation(B,SE2)
    # step 8. thinning
    B = bwmorph_thin(B,3)
    # step 9. fill holes
    B = binary_fill_holes(B)
    # step 10. remove blobs smaller than BLOB_MIN
    B = remove_small_objects(B,BLOB_MIN,connectivity=2)
    # done.
    return B
Пример #24
0
def get_segmented_lungs(im, plot=False, THRESHOLD=-320):

    binary = im < THRESHOLD

    cleared = clear_border(binary)

    label_image = measure.label(cleared)

    areas = [r.area for r in measure.regionprops(label_image)]
    areas.sort()
    #print areas
    if len(areas) > 2:
        for region in measure.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 = morphology.disk(2)
    binary = morphology.binary_erosion(binary, selem)

    selem = morphology.disk(15)
    binary = morphology.binary_closing(binary, selem)

    edges = filters.roberts(binary)
    binary = binary_fill_holes(edges)

    get_high_vals = binary == 0
    im[get_high_vals] = 0

    return im
Пример #25
0
def _label_fill_holes(im_label):
    im_label_out = np.copy(im_label)
    for j in regionprops(im_label):
        mask = (im_label == j.label)
        mask = binary_fill_holes(mask)
        im_label_out[mask] = j.label
    return im_label_out
Пример #26
0
def clean_seg(seg):
    labels = label(seg)
    labels = remove_small_objects(labels)
    seg = labels > 0.5
    seg = binary_closing(seg, structure=np.ones((10, 10)))
    seg = binary_fill_holes(seg)
    return seg
Пример #27
0
def mask_polar_to_cart(mask, center, min_radius, max_radius, output_shape, zoom_factor=1):
    '''Converts a polar binary mask to Cartesian and places in an image of zeros'''

    # Account for upsampling
    if zoom_factor != 1:
        center = (center[0]*zoom_factor + zoom_factor/2, center[1]*zoom_factor + zoom_factor/2)
        min_radius = min_radius * zoom_factor
        max_radius = max_radius * zoom_factor
        output_shape = map(lambda a: a * zoom_factor, output_shape)

    # new image
    image = np.zeros(output_shape)

    # coordinate conversion
    theta, r = np.meshgrid(np.linspace(0, 2*np.pi, mask.shape[1]),
                           np.arange(0, max_radius))
    x, y = coord_polar_to_cart(r, theta, center)
    x, y = np.round(x), np.round(y)
    x, y = x.astype(int), y.astype(int)

    x = np.clip(x, 0, image.shape[0]-1)
    y = np.clip(y, 0, image.shape[1]-1)
    ix,iy = np.meshgrid(np.arange(0,mask.shape[1]), np.arange(0,mask.shape[0]))
    image[x,y] = mask

    # downsample image
    if zoom_factor != 1:
        zf = 1/float(zoom_factor)
        image = zoom(image, (zf, zf), order=4)

    # ensure image remains a filled binary mask
    image = (image > 0.5).astype(int)
    image = binary_fill_holes(image)
    return image
Пример #28
0
def get_FOV(around_lung, lung):
    FOV = np.where((around_lung + lung) > 0, 1, 0)
    for idx in range(FOV.shape[0]):
        FOV[idx, :, :] = binary_fill_holes(FOV[idx, :, :],
                                           structure=np.ones(
                                               (5, 5))).astype(FOV.dtype)
    return FOV
Пример #29
0
 def binary(self, img="", tol=15):
     if isinstance(img, str): img = self.data
     gray = self.grayscale(img, type='max')
     gray[gray < tol] = 0
     gray[gray > tol] = 255
     gray = binary_fill_holes(gray).astype(np.uint8)*255
     return gray
Пример #30
0
def _fill_label_holes(lbl_img, **kwargs):
    lbl_img_filled = np.zeros_like(lbl_img)
    for l in (set(np.unique(lbl_img)) - set([0])):
        mask = lbl_img == l
        mask_filled = binary_fill_holes(mask, **kwargs)
        lbl_img_filled[mask_filled] = l
    return lbl_img_filled
Пример #31
0
def proc_np_dst(pred):
    """
    Process Nuclei Prediction with Distance Map

    Args:
        pred: prediction output, assuming 
                channel 0 contain probability map of nuclei
                channel 1 containing the regressed distance map
    """
    blb_raw = pred[...,0]
    dst_raw = pred[...,1]

    blb = np.copy(blb_raw)
    blb[blb >  0.5] = 1
    blb[blb <= 0.5] = 0
    blb = measurements.label(blb)[0]
    blb = remove_small_objects(blb, min_size=30)
    blb[blb > 0] = 1   

    dst_raw[dst_raw < 0] = 0
    dst = np.copy(dst_raw)
    dst = dst * blb
    dst[dst  > 0.5] = 1
    dst[dst <= 0.5] = 0

    marker = dst.copy()
    marker = binary_fill_holes(marker) 
    marker = measurements.label(marker)[0]
    marker = remove_small_objects(marker, min_size=30)
    proced_pred = watershed(-dst_raw, marker, mask=blb)    
    return proced_pred
Пример #32
0
def refine_aseg(aseg, ball_size=4):
    """
    Refine the ``aseg.mgz`` mask of Freesurfer.

    First step to reconcile ANTs' and FreeSurfer's brain masks.
    Here, the ``aseg.mgz`` mask from FreeSurfer is refined in two
    steps, using binary morphological operations:

      1. With a binary closing operation the sulci are included
         into the mask. This results in a smoother brain mask
         that does not exclude deep, wide sulci.

      2. Fill any holes (typically, there could be a hole next to
         the pineal gland and the corpora quadrigemina if the great
         cerebral brain is segmented out).

    """
    from skimage import morphology as sim
    from scipy.ndimage.morphology import binary_fill_holes

    # Read aseg data
    bmask = aseg.copy()
    bmask[bmask > 0] = 1
    bmask = bmask.astype(np.uint8)

    # Morphological operations
    selem = sim.ball(ball_size)
    newmask = sim.binary_closing(bmask, selem)
    newmask = binary_fill_holes(newmask.astype(np.uint8), selem).astype(np.uint8)

    return newmask.astype(np.uint8)
Пример #33
0
def hide_image_elements(img, bin_mask):
    """
    where mask, change pixels' brightness min(img) value
    """
    bin_mask = bin_mask.astype(bool)
    bin_mask = binary_fill_holes(bin_mask)
    return bin_mask * np.min(img) + img * (~bin_mask.astype(bool))
Пример #34
0
def get_chest_boundary(im, plot=False):

    size = im.shape[1]
    if plot == True:
        f, plots = plt.subplots(6, 1, figsize=(5, 30))
    binary = im < -320

    if plot == True:
        plots[0].axis('off')
        plots[0].imshow(binary, cmap=plt.cm.bone)

    cleared = clear_border(binary)
    temp_label = label(cleared)
    for region in regionprops(temp_label):
        if region.area < 300:
            for coordinates in region.coords:
                temp_label[coordinates[0], coordinates[1]] = 0
    cleared = temp_label > 0

    label_img = label(cleared)
    for region in regionprops(label_img):
        if region.eccentricity > 0.99 \
                or region.centroid[0] > 0.90 * size \
                or region.centroid[0] < 0.12 * size \
                or region.centroid[1] > 0.88 * size \
                or region.centroid[1] < 0.10 * size \
                or (region.centroid[1] > 0.46 * size and region.centroid[1] < 0.54 * size and region.centroid[
                    0] > 0.75 * size) \
                or (region.centroid[0] < 0.2 * size and region.centroid[1] < 0.2 * size) \
                or (region.centroid[0] < 0.2 * size and region.centroid[1] > 0.8 * size) \
                or (region.centroid[0] > 0.8 * size and region.centroid[1] < 0.2 * size) \
                or (region.centroid[0] > 0.8 * size and region.centroid[1] > 0.8 * size):
            for coordinates in region.coords:
                label_img[coordinates[0], coordinates[1]] = 0

    if plot == True:
        plots[1].axis('off')
        plots[1].imshow(label_img, cmap=plt.cm.bone)

    region_n = np.max(label_img)
    selem = disk(10)
    filled = np.zeros(cleared.shape, np.uint8)
    for i in range(1, region_n + 1):
        curr_region = np.zeros(cleared.shape, np.uint8)
        curr_region[label_img == i] = 1
        curr_region = binary_closing(curr_region, selem)
        curr_region = binary_fill_holes(curr_region)
        filled[curr_region == 1] = 1

    if plot == True:
        plots[2].axis('off')
        plots[2].imshow(filled, cmap=plt.cm.bone)

    filled_edge = misc.imfilter(filled.astype(np.float64), 'find_edges') / 255

    if plot == True:
        plots[3].axis('off')
        plots[3].imshow(filled_edge, cmap=plt.cm.bone)

    return filled_edge
def extract_foreground(img):
    """
    Extracts the single largest object from grayscale image img.
    Returns a boolean mask and a skimage RegionProperty for that object.
    """
    thresholds = threshold_multiotsu(img, classes=3)
    true_fg = np.digitize(img, [thresholds[0]])

    mask_edge = get_edge_mask(true_fg, 100, 20)
    masked_img = extract_mask(img, mask_edge)
    new_true_fg = new_edge_threshold(masked_img, mask_edge, true_fg)

    new_mask_edge = get_edge_mask(new_true_fg, 50, 10)
    new_masked_img = extract_mask(img, new_mask_edge)
    last_true_fg = new_edge_threshold(
        new_masked_img,
        new_mask_edge,
        new_true_fg,
    )

    labels = label(last_true_fg)
    props = regionprops(labels, img)

    areas = np.asarray([prop.area for prop in props])
    ind = areas.argmax()

    prop = props[ind]
    mask = binary_fill_holes(labels == prop.label)

    return mask, prop
Пример #36
0
def postprocess(X, min_area=0):
    """
    performs non_max_suppression within connected components

    parameters
    __________
    X : np.array
        the image you are trying to suppress
    percentile : int
        the fraction of images you are accepting
    min_area : int
        the minimum area of a component you are accepting

    return
    __________
    predictions : np.array
        the thresholded image
    """
    otsu_predictions = otsu(X)
    labeled, num_labels = label(otsu_predictions)
    flattened_labels = labeled.reshape((256 * 256, 1))
    flattened_otsu_predictions = otsu_predictions.reshape((256 * 256, 1))
    for label_num in range(0, num_labels + 4):
        indices = np.where(flattened_labels == label_num)[0]
        component = flattened_otsu_predictions[indices]
        if (len(indices) > min_area):
            flattened_otsu_predictions[indices] = component
        else:
            flattened_otsu_predictions[indices] = 0
    return binary_fill_holes(flattened_otsu_predictions.reshape((256, 256)))
def extract_foreground_biofilms(img, area_threshold=9000):
    """
    Extracts the single largest object from grayscale image img.
    Returns a boolean mask and a skimage RegionProperty for that object.
    """
    thresholds = threshold_multiotsu(img, classes=3)
    true_fg = np.digitize(img, [thresholds[0]])

    mask_edge = get_edge_mask(true_fg, 100, 20)
    masked_img = extract_mask(img, mask_edge)
    new_true_fg = new_edge_threshold(masked_img, mask_edge, true_fg)

    new_mask_edge = get_edge_mask(new_true_fg, 50, 10)
    new_masked_img = extract_mask(img, new_mask_edge)
    last_true_fg = new_edge_threshold(
        new_masked_img,
        new_mask_edge,
        new_true_fg,
    )

    labels = label(last_true_fg)
    props = regionprops(labels, img)

    areas = np.asarray([prop.area for prop in props])
    mask = np.zeros_like(labels)
    inds = np.arange(len(areas))[areas > area_threshold]
    for i, ind in enumerate(inds):
        prop = props[ind]
        mask[binary_fill_holes(labels == prop.label)] = i + 1

    return mask
Пример #38
0
def find_all_children(labels):
    mask = binary_fill_holes(labels < 0)
    mask[labels < 0] = False
    clabelnums = np.unique(labels[mask]).tolist()
    if 0 in clabelnums:
        clabelnums.remove(0)
    return clabelnums
Пример #39
0
def find_ellipse(img,
                 background=None,
                 threshrange=[1, 254],
                 sizerange=[10, 400],
                 dist_thresh=10,
                 erode=False,
                 check_centers=False,
                 autothreshpercentage=None,
                 show=False):

    #print '**img shape** ', img.shape
    body = find_object(img,
                       background=background,
                       threshrange=threshrange,
                       sizerange=sizerange,
                       dist_thresh=dist_thresh,
                       erode=erode,
                       check_centers=check_centers,
                       autothreshpercentage=autothreshpercentage)

    if body.sum() < 1 and check_centers == True:
        body = find_object(img,
                           background=background,
                           threshrange=threshrange,
                           sizerange=sizerange,
                           dist_thresh=dist_thresh,
                           erode=erode,
                           check_centers=check_centers,
                           autothreshpercentage=autothreshpercentage)

    body = binary_fill_holes(body)

    if body.sum() < 1:
        body[body.shape[0] / 2, body.shape[1] / 2] = 1

    center, longaxis, shortaxis, ratio = get_ellipse_cov(body,
                                                         erode=False,
                                                         recenter=True)

    if show:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.imshow(img)

        circle = patches.Circle((center[1], center[0]),
                                2,
                                facecolor='white',
                                edgecolor='none')
        ax.add_artist(circle)
        ax.plot([
            center[1] - longaxis[1] * ratio[0],
            center[1] + longaxis[1] * ratio[0]
        ], [
            center[0] - longaxis[0] * ratio[0],
            center[0] + longaxis[0] * ratio[0]
        ],
                zorder=10,
                color='white')

    return center, longaxis, shortaxis, body, ratio
Пример #40
0
            def _compute_sample_params(data, label, rim, rate_mg, logits_g):
                nonmissing = label != 0
                zero_count_spots = 1 + torch.where(data.sum(1) == 0)[0]
                nonpartial = binary_fill_holes(
                    np.isin(label.cpu(), [0, *zero_count_spots.cpu()]))
                nonpartial = torch.as_tensor(nonpartial).to(nonmissing)
                mask = nonpartial & nonmissing

                if not mask.any():
                    return (
                        data[[]],
                        torch.zeros(0, num_genes).to(rim),
                        logits_g.expand(0, -1),
                    )

                label = label[mask] - 1
                idxs, label = torch.unique(label, return_inverse=True)
                data = data[idxs]

                rim = rim[:, mask]
                labelonehot = sparseonehot(label)
                rim = torch.sparse.mm(labelonehot.t().float(), rim.t())

                rgs = rim @ rate_mg.exp()

                return data, rgs, logits_g.expand(len(rgs), -1)
def postprocess_img(img, close_ksize=5, size_factor=0.05):
    """ Postprocess the binary mask from kmeans clustering in order to fill holes and remove small elements to grab the main epithelial sheet.
    
    Parameters
    -----------
    img : numpy array
        (n_rows x n_cols) binary image
    close_ksize : int
        kernel size for binary_closing with a disk kernel.
    size_factor : float
        0-1, the proportion of image area below which are regarded as small objects and removed from the binary mask
    
    Returns
    -------
    filt : numpy array
        (n_rows x n_cols), post-processed binary image.
    """

    from scipy.ndimage.morphology import binary_fill_holes
    from skimage.morphology import disk, binary_closing, remove_small_objects

    img_rows, img_cols = img.shape

    filt = binary_closing(img, disk(close_ksize))
    filt = remove_small_objects(filt, size_factor * img_rows * img_cols)
    filt = binary_fill_holes(filt)

    return filt
Пример #42
0
def get_breast_mask(shape, breast, debug_verbose=True):
    # Fill all contour points
    mask = np.zeros(shape)
    x, y = dists.spline(breast, n_points=8000)
    x = np.clip(np.round(x).astype(int), 0, mask.shape[0] - 1)
    y = np.clip(np.round(y).astype(int), 0, mask.shape[1] - 1)
    mask[x, y] = 1

    # Fill all points at the top
    x = np.clip(np.linspace(breast[0, 0], breast[-1, 0], 2000), 0,
                mask.shape[0] - 1)
    y = np.clip(np.linspace(breast[0, 1], breast[-1, 1], 2000), 0,
                mask.shape[1] - 1)
    x = np.round(x).astype(int)
    y = np.round(y).astype(int)
    mask[x, y] = 1

    # Fill black holes of the mask
    mask_full = morpho.binary_fill_holes(mask)

    if debug_verbose:
        plt.clf()
        plt.imshow(mask_full)
        plt.savefig(config.debug_path + "mask.png")

    return mask_full
Пример #43
0
    def match_contour_frames(self, desired_frames):
        """
        returns a 3D np array of filled in contours of desired frames
        """
        wdf = self.df.copy()
        wdf = wdf.reset_index()
        wdf = wdf.set_index('frame')
        wdf = wdf.loc[desired_frames]
        wdf[wdf['index'] >= len(self.contours)]
        wdf[wdf['index'] < 0]

        desired_contours = np.array(wdf.dropna('index')['index'])
        print(np.min(wdf['index']), 'min index')
        print(np.min(desired_contours))
        shape = list(self.contours.shape)
        shape[0] = len(desired_contours)

        # xshape = list(self.x_mid.shape)
        # xshape[0] = len(desired_contours)
        # x_mid2 = np.zeros(xshape)

        # yshape = list(self.y_mid.shape)
        # yshape[0] = len(desired_contours)
        # y_mid2 = np.zeros(yshape)

        contours2 = np.zeros(shape)
        for i, j in enumerate(desired_contours):
            if j > len(self.contours):
                print(j, 'wtf')
            if j < 0:
                print(j, 'wtf')
            contours2[i] = morph.binary_fill_holes(self.contours[j])
            # x_mid2[i] = self.x_mid[j]
            # y_mid2[i] = self.y_mid[j]
        return contours2
Пример #44
0
def postprocess_prediction(pred, gaussian_std=1, threshold=0.5, fill_holes=True, connected_component=True):
    pred = gaussian_filter(pred, gaussian_std) > threshold
    if fill_holes:
        pred = binary_fill_holes(pred)
    if connected_component:
        pred = get_main_connected_component(pred)
    return pred
def draw_segm_mask(mask, vertices, triangles, color):
    m = np.zeros_like(mask)
    for t in triangles:
        coord = np.array([vertices[t[0]][:2], vertices[t[1]][:2], vertices[t[2]][:2]], dtype=np.int32)
        cv2.fillConvexPoly(m, coord, 1)
    m = binary_fill_holes(m).astype(np.uint8)
    mask[m.astype(bool)] = color
def remove_abundant(image,mask):
	edges = cv2.bitwise_and(image,image,mask=mask);
	red_mean = np.average(edges[:,:,0], weights=edges[:,:,0].astype(bool));
	green_mean = np.average(edges[:,:,1], weights=edges[:,:,1].astype(bool));
	blue_mean = np.average(edges[:,:,2], weights=edges[:,:,2].astype(bool));
	im_temp = copy.copy(image);
	im = np.zeros(im_temp.shape,dtype='int');	
	im[:,:,0] = abs(im_temp[:,:,0] - red_mean);
	im[:,:,1] = abs(im_temp[:,:,1] - green_mean);
	im[:,:,2] = abs(im_temp[:,:,2] - blue_mean);
	im = im.astype('uint8');
	im = cv2.bitwise_and(im,im,mask=mask)	
	red_mean = np.average(im[:,:,0], weights=im[:,:,0].astype(bool));
	green_mean = np.average(im[:,:,1], weights=im[:,:,1].astype(bool));
	blue_mean = np.average(im[:,:,2], weights=im[:,:,2].astype(bool));
	ret1, grad0 = cv2.threshold (im[:,:,0],red_mean, 255, cv2.THRESH_BINARY);
	ret1, grad1 = cv2.threshold (im[:,:,1],green_mean, 255, cv2.THRESH_BINARY);
	ret1, grad2 = cv2.threshold (im[:,:,2],blue_mean, 255, cv2.THRESH_BINARY);
	grad = cv2.bitwise_and(grad0,grad1);
	grad = cv2.bitwise_and(grad,grad2);
	grad = morphology.binary_closing(grad,iterations=5);
	grad = morphology.binary_fill_holes(grad);
	grad = grad.astype('uint8');
	im = cv2.bitwise_and(image,image,mask=grad);	
	return (im,grad);
Пример #47
0
def _fill2d(arr, structure = None, dimension = 2):
    r"""
    Fill holes along a certain dimension only.
    """
    res = numpy.zeros(arr.shape, numpy.bool)
    for sl in range(arr.shape[dimension]):    
        res[:,:,sl] = binary_fill_holes(arr[:,:,sl], structure)
    return res
Пример #48
0
	def __morph_ops__(self,mask):
		mask = cv2.medianBlur(np.uint8(mask),3)
		mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, KERNEL)
		mask = cv2.medianBlur(mask,3)
		mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE,KERNEL)
		mask = binary_fill_holes(mask)
		mask = remove_small_objects(mask,min_size=128,connectivity=2)
		return np.uint8(mask);
Пример #49
0
def find_circle(img, npts=25, nstart=0, navg=20, plot=False):

    filled_img = binary_fill_holes(img)
    dil_img = binary_dilation(filled_img)
    edges = dil_img-filled_img
    pts = np.transpose(np.nonzero(edges))
    
    # select an evenly spaced subset of points (to speed up computation):
    if len(pts) > npts:
        indices = np.linspace(nstart, len(pts)-1, npts)
        indices = [int(indices[i]) for i in range(len(indices))]        
    else:
        indices = np.arange(0, len(pts), 1).tolist()
    pts_subset = pts[indices,:]
    
    len_pts_diff = np.arange(1,len(pts_subset), 1)
    
    pts_diff = np.zeros(np.sum(len_pts_diff))
    pts_diff_arr = np.zeros([np.sum(len_pts_diff), 2])
    
    iarr = 0
    for i in range(len(pts_subset)):
        indices = np.arange(i+1, len(pts_subset), 1)
        pts_diff_arr[iarr:len(indices)+iarr, 1] = indices
        pts_diff_arr[iarr:len(indices)+iarr, 0] = np.ones_like(indices)*i
        
        d_arr = pts_subset[indices.tolist(), :] - pts_subset[i,:]
        d = np.array( [np.linalg.norm(d_arr[n]) for n in range(len(d_arr))] )
        pts_diff[iarr:len(indices)+iarr] = d
        
        iarr += len(indices)
        
    ordered_pairs = np.argsort(pts_diff)[::-1]
    best_pairs = pts_diff_arr[(ordered_pairs[0:navg]).tolist()]
    
    center_arr = np.zeros([len(best_pairs), 2])
    radius_arr = np.zeros([len(best_pairs), 1])
    
    #centers = np.zeros([len(best_pairs)])
    for i, pair in enumerate(best_pairs):
        pt1 = np.array(pts_subset[ pair[0] ], dtype=float)
        pt2 = np.array(pts_subset[ pair[1] ], dtype=float)
    
        pt_diff = pt2 - pt1
        radius_arr[i] = np.linalg.norm( pt_diff ) / 2.
        center_arr[i] = pt1 + pt_diff/2
        
    center = np.mean(center_arr, axis=0)
    radius = np.mean(radius_arr)
    
    if plot:
        fig = plt.figure(None)
        ax = fig.add_axes([.1,.1,.8,.8])
        circle = patches.Circle( center, radius=radius, facecolor='none', edgecolor='green')
        ax.add_artist(circle)
        ax.imshow(edges)
        
    return center, radius
        def proc_roi_region(self,add_region=True):
            mpossx = self.roi_item.getArrayRegion(self.possx,self.img).astype(int)
            mpossx = mpossx[np.nonzero(binary_fill_holes(mpossx))]#get the x pos from ROI
            mpossy = self.roi_item.getArrayRegion(self.possy,self.img).astype(int)
            mpossy = mpossy[np.nonzero(binary_fill_holes(mpossy))]# get the y pos from ROI
            xLims = [np.min(mpossx)-10,np.max(mpossx)+10]
            yLims = [np.min(mpossy)-10,np.max(mpossy)+10]
            #xLims = [np.mean(mpossx)-20,np.mean(mpossx)+20]; yLims = [np.mean(mpossy)-20,np.mean(mpossy)+20]


            self.temp_mask[mpossx,mpossy] = 1
            self.temp_mask = binary_fill_holes(self.temp_mask).T

            if add_region:
                self.vidTimer.stop()
                self.ROI_attrs['centres'].append([np.mean(mpossx),np.mean(mpossy)])
                self.ROI_attrs['patches'].append(self.mean_image[yLims[0]:yLims[1],xLims[0]:xLims[1]])
                self.ROI_attrs['idxs'].append([mpossx,mpossy])
                self.ROI_attrs['masks'].append(self.temp_mask[yLims[0]:yLims[1],xLims[0]:xLims[1]])
                if online_trace_extract:
                    temp = areaFile[:,yLims[0]:yLims[1],xLims[0]:xLims[1]] *self.ROI_attrs['masks'][-1]
                    temp = temp.astype('float64')
                    temp[temp==0] = np.nan
                    self.ROI_attrs['traces'].append(np.nanmean(temp,axis=(1,2)))
                    #self.ROI_attrs['mask_arr'].append(temp_mask)
                    self.Gplt.clear()
                    self.Gplt.addItem(self.timeLine)
                    self.Gplt.plot(self.ROI_attrs['traces'][-1])
                else:
                    self.ROI_attrs['traces'].append([0])

                self.vidTimer.start(self.IFI)

                self.mask[:,:,0] += self.mask[:,:,1]
                self.mask[:,:,1] = 0
                self.mask[:,:,1] = self.temp_mask.T
                #self.mask[:,:,0] += self.temp_mask.T
                self.mask[:,:,3] += self.temp_mask.T
                self.nROIs += 1
                self.roi_idx = self.nROIs - 1

            else:
                self.mask[mpossx,mpossy,0] = 0
                self.mask[mpossx,mpossy,3] = 0
            self.temp_mask = np.zeros(self.temp_mask.shape) 
Пример #51
0
def get_ellipse_cov(img, erode=False, recenter=True):
    # Pattern. Recogn. 20, Sept. 1998, pp. 31-40
    # J. Prakash, and K. Rajesh
    # Human Face Detection and Segmentation using Eigenvalues of Covariance Matrix, Hough Transform and Raster Scan Algorithms

    #eroded_img = binary_erosion(img)
    #boundary = img-eroded_img
    
    if img is not None:
    
        if erode is not False:
            try:
                e = 0
                while e < erode:
                    e += 1
                    img = binary_erosion(img)
            except:
                pass
                
        img = binary_fill_holes(img)
                
        if recenter:
            center = center_of_blob(img)
        else:
            center = np.array([0,0])

        if 1:
            ptsT = np.transpose(np.nonzero(img))
            for pt in ptsT:
                pt -= center
            pts = (ptsT).T
            cov = np.cov(pts)
            cov = np.nan_to_num(cov)
            
            e,v = np.linalg.eig(cov)
            
            longaxis = v[:,np.argmax(e)]
            shortaxis = v[:,np.argmin(e)]
            
            
            if len(ptsT) > 2:
                dl = [np.dot(longaxis, ptsT[i]) for i in range(len(ptsT))]
                longaxis_radius = np.max( np.abs(dl) )
                
                ds = [np.dot(shortaxis, ptsT[i]) for i in range(len(ptsT))]
                shortaxis_radius = np.max( np.abs(ds) )
            else:
                longaxis_radius = None
                shortaxis_radius = None
                
        if recenter is False:
            return longaxis, shortaxis, [longaxis_radius, shortaxis_radius]
        else:
            return center, longaxis, shortaxis, [longaxis_radius, shortaxis_radius]
            
    else:
        return [0,0],0
Пример #52
0
def __morphologicalOps__(mask):
	#_mask = binary_fill_holes(mask)
	_mask = cv2.medianBlur(np.uint8(mask),3)
	_mask = cv2.morphologyEx(_mask, cv2.MORPH_CLOSE,KERNEL)
	_mask = binary_fill_holes(_mask)
	_mask = remove_small_objects(_mask,min_size=128,connectivity=2)
	kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
	_mask = cv2.dilate(np.uint8(_mask),kernel,iterations = 1)
	return _mask;
Пример #53
0
 def seg_sect(self, img):
     img_canny = canny(img, sigma=self.sigma,
                       low_threshold=self.low_threshold)
     
     img_dilate = binary_dilation(img_canny, square(3))
     img_erode = binary_erosion(img_dilate, square(3))
     img_fill = binary_fill_holes(img_erode)
     
     return img_fill
Пример #54
0
def compute_skeleton_from_outline(outline, return_intermediate_steps=False, verbose=False):
    '''
    accepts a list of points in the outline of a shape and returns the winding centerline 1 pixel wide of that shape

    :param outline: a list of x,y tuples containing the points around the shape's outline
    :param return_intermediate_steps: a toggle to show internal guts of the process.
    '''
    # occasional outlines do not form a fully closed shape. this closes it.
    xs, ys = zip(*close_outline_border(outline))
    # make a box that is big enough to hold the outline + borders + a safety margin
    border_size, safety_margin = 1, 1
    matrix_x = max(xs) - min(xs) + 2 * border_size + safety_margin
    matrix_y = max(ys) - min(ys) + 2 * border_size + safety_margin
    outline_matrix = np.zeros([matrix_x, matrix_y], dtype=int)

    # add outline to the matrix by shifting the xy coordinates accordingly
    xy_shift = [min(xs) - border_size, min(ys) - border_size]
    for x, y in zip(xs, ys):
        outline_matrix[x - xy_shift[0]][y - xy_shift[1]] = 1

    # fill in the inside of the outline to make a solid shape
    filled_matrix = binary_fill_holes(outline_matrix)
    # if something is wrong, save the offending outline for later
    if sum(sum(outline_matrix)) == sum(sum(filled_matrix)):
        write_pathological_input(outline.tolist(), input_type='outline', note='outline filling problem',
                                 savename='%sfill_outline_%s.json' %(exception_directory, str(time.time())))
        assert False, 'you have an outline filling problem'

    # thin the solid shape until it is 1px thick, then remove all shortest branches.
    spine_matrix_branched = skeletonize(filled_matrix)
    if return_intermediate_steps:
        spine_matrix_branched_copy = spine_matrix_branched.copy()
    spine_matrix, endpoints = cut_branchpoints_from_spine_matrix(spine_matrix_branched)

    # the option to return matrices from intermediate steps is for plotting
    if return_intermediate_steps:
        return outline_matrix, filled_matrix, spine_matrix_branched_copy, spine_matrix, xy_shift, endpoints

    if len(endpoints) < 2:
        if sum(sum(spine_matrix)) > 2:
            # if spine is long enough to have endpoints, something is wrong.
            # this saves the offending outline for later tests
            write_pathological_input(outline.tolist(),
                                     input_type='outline', note='less than 2 endpoints. long enough spine.',
                                     savename='%sendpoints_%s.json' %(exception_directory, str(time.time())))
            assert False, 'spine is long enough, but does not have endpoints' + str(endpoints)
        else:
            # short spines can't be helped.
            if verbose:
                print('warning: spine too short to find endpoints!')
            return []

    # change spine matrix back to a list of points and reverse the previous coordinate shift
    shifted_spine = line_matrix_to_ordered_points(spine_matrix, endpoints)
    real_spine = [(pt[0] + xy_shift[0], pt[1] + xy_shift[1]) for pt in shifted_spine]
    return real_spine
def neuropil_correct(areaF,roi_attrs):
    


    nROIs = len(roiattrs['idxs'])
    len_trace = areaFile.shape[0]


    roiattrs['traces'] = np.zeros([nROIs,len_trace])
    roiattrs['neuropil_traces'] = np.zeros([nROIs,len_trace])
    roiattrs['corr_traces'] = np.zeros([nROIs,len_trace])
    for idx in range(nROIs):

        sys.stdout.write('\r Extracting_Trace_from roi: %s' %idx)
        sys.stdout.flush()

        mpossx= roi_attrs['idxs'][idx][0]
        mpossy = roi_attrs['idxs'][idx][1]



        xLims = [np.clip(np.min(mpossx)-10,0,510),np.clip(np.max(mpossx)+10,0,510)]
        yLims = [np.clip(np.min(mpossy)-10,0,510),np.clip(np.max(mpossy)+10,0,510)]

        temp_mask = np.zeros(areaFile.shape[1:])
        temp_mask[mpossx,mpossy] = 1
        temp_mask = binary_fill_holes(temp_mask).T
        mask = temp_mask[yLims[0]:yLims[1],xLims[0]:xLims[1]]
        print '___',np.sum(mask),

        im_mask = np.dstack([mask,mask*0,mask*0,mask*.2])
        #plt.imshow(np.mean(areaF[:,yLims[0]:yLims[1],xLims[0]:xLims[1]],axis=0),cmap='binary_r')
        #plt.imshow(im_mask)
        #plt.show()



        temp = areaF[:,yLims[0]:yLims[1],xLims[0]:xLims[1]] *np.abs(mask-1)
        temp = temp.astype('float64')
        temp[temp==0] = np.nan
        neuropil_trace = np.nanmean(temp,axis=(1,2))



        temp = areaF[:,yLims[0]:yLims[1],xLims[0]:xLims[1]] *mask
        temp = temp.astype('float64')
        temp[temp==0] = np.nan
        trace = np.nanmean(temp,axis=(1,2))
        corrected_trace = trace - .4*neuropil_trace

        roiattrs['traces'][idx] = trace
        roiattrs['neuropil_traces'][idx] = neuropil_trace
        roiattrs['corr_traces'][idx] = corrected_trace


    return roiattrs
Пример #56
0
def gen_tmp_extent(f_wi,f_temp):
	ref_wi = GR.geo_raster.open(f_wi)
	bnd_wi = ref_wi.get_band()
	m_wi = bnd_wi.read()

	m_out = np.zeros_like(m_wi)
	m_out[m_wi > -9999] = 1
	m_fill = binary_fill_holes(m_out)
	m_out = lib_amerl_c.remove_small_objects(m_fill.astype(np.int16),100,0,4)
	GR.write_raster(f_temp, ref_wi.geo_transform, ref_wi.projection, m_out.astype(np.int8),1)
	return ref_wi.geo_transform,ref_wi.projection
Пример #57
0
    def _is_converged(result, result_previous):
        """Check convergence.

        Criterion: exclusion masks unchanged in subsequent iterations.
        """
        mask = result["exclusion"].data == result_previous["exclusion"].data

        # Because of pixel to pixel noise, the masks can still differ.
        # This is handled by removing structures of the scale of one pixel
        mask = binary_fill_holes(mask)
        return np.all(mask)
def nuclei_detection(img, MinPixel, MaxPixel):
    img_f = ski.img_as_float(img)
    adjustRed = rescale_intensity(img_f[:,:,0])
    roiGamma = rescale_intensity(adjustRed, in_range=(0, 0.5));
    roiMaskThresh = roiGamma < (250 / 255.0) ;

    roiMaskFill = morphology.remove_small_objects(~roiMaskThresh, MinPixel);
    roiMaskNoiseRem = morphology.remove_small_objects(~roiMaskFill,150);
    roiMaskDilat = morphology.dilation(roiMaskNoiseRem, morphology.disk(3));
    roiMask = smorphology.binary_fill_holes(roiMaskDilat)

    hsv = ski.color.rgb2hsv(img);
    hsv[:,:,2] = 0.8;
    img2 = ski.color.hsv2rgb(hsv)
    diffRGB = img2-img_f
    adjRGB = np.zeros(diffRGB.shape)
    adjRGB[:,:,0] = rescale_intensity(diffRGB[:,:,0],in_range=(0, 0.4))
    adjRGB[:,:,1] = rescale_intensity(diffRGB[:,:,1],in_range=(0, 0.4))
    adjRGB[:,:,2] = rescale_intensity(diffRGB[:,:,2],in_range=(0, 0.4))

    gauss = gaussian_filter(adjRGB[:,:,2], sigma=3, truncate=5.0);

    bw1 = gauss>(100/255.0);
    bw1 = bw1 * roiMask;
    bw1_bwareaopen = morphology.remove_small_objects(bw1, MinPixel)
    bw2 = smorphology.binary_fill_holes(bw1_bwareaopen);

    bwDist = nd.distance_transform_edt(bw2);
    filtDist = gaussian_filter(bwDist,sigma=5, truncate=5.0);

    L = label(bw2)
    R = regionprops(L)
    coutn = 0
    for idx, R_i in enumerate(R):
        if R_i.area < MaxPixel and R_i.area > MinPixel:
            r, l = R_i.centroid
            #print(idx, filtDist[r,l])
        else:
            L[L==(idx+1)] = 0
    BW = L > 0
    return BW
Пример #59
0
    def _is_converged(self, result, result_previous):
        """
        Check convercence by comparing the exclusion masks between two
        subsequent iterations.
        """
        from scipy.ndimage.morphology import binary_fill_holes
        mask = result['exclusion'].data == result_previous['exclusion'].data

        # Because of pixel to pixel noise, the masks can still differ.
        # This is handled by removing structures of the scale of one pixel
        mask = binary_fill_holes(mask)
        return np.all(mask)
Пример #60
0
 def _init_data(self, img, mask):
     # get image data
     data = img.get_data()
     self.affine = img.get_affine()
     self.shape = data.shape
     # masking -- by default, mask out zero intensities
     if mask == None:
         mask = binary_fill_holes(data > 0)
     self.data = data[mask]
     X, Y, Z = np.where(mask)
     XYZ = np.zeros((X.shape[0], 3), dtype='intp')
     XYZ[:, 0], XYZ[:, 1], XYZ[:, 2] = X, Y, Z
     self.mask = mask
     self.XYZ = XYZ