def get_areal_features(root, features_path, masks_dir, n_bins = 100):
    prep_out_path(features_path)
    files = os.listdir(root)

    df = pd.DataFrame(columns = range(n_bins * 2) + ['name', 'level'])
    names = pd.read_csv(labels_file)
    print "Starting extraction: ", time_now_str()

    for j, f in enumerate(files):
        label = names.loc[names['image'] == path.splitext(f)[0]]
        start = time.time()
        imr = ImageReader(root, f, masks_dir, gray_scale = True)

        drusen = get_predicted_region(imr.image, Labels.Drusen)
        blood = get_predicted_region(imr.image, Labels.Haemorage)

        Bc = np.ones((5, 5))
        labels_drusen, n_drusen = mh.label(drusen, Bc)
        labels_blood, n_blood = mh.label(blood, Bc)

        area = float(cv2.countNonZero(imr.mask))

        outp = np.array([], dtype = np.int)

        # sizes excluding background
        sizes_drusen = mhl.labeled_size(labels_drusen)[1:] / area
        sizes_blood = mhl.labeled_size(labels_blood)[1:] / area

        hist_druzen, _ = np.histogram(sizes_drusen, n_bins, (0, 1e-3))
        hist_blood, _ = np.histogram(sizes_blood, n_bins, (0, 1e-3))


        outp = np.r_[outp, hist_druzen]
        outp = np.r_[outp, hist_blood]
        outp = np.r_[outp, label.values[0]]
        df.loc[j] = outp
        print "Extracted: {0}, took {1:02.2f} sec ".format(f, time.time() - start)
      
    # write out the csv
    df.to_csv(path.join(features_path, prefix + ".csv"), index = False, header=True)    
    print "Extracted: ", prefix, "@", time_now_str()
Beispiel #2
0
def filter_labeled(labeled,
                   remove_bordering=False,
                   min_size=None,
                   max_size=None):
    '''Filter labeled regions based on a series of conditions

    .. versionadded :: 1.4.1

    Parameters
    ----------
    labeled : labeled array
    remove_bordering : bool, optional
        whether to remove regions that touch the border
    min_size : int, optional
        Minimum size (in pixels) of objects to keep (default is no minimum)
    max_size : int, optional
        Maximum size (in pixels) of objects to keep (default is no maximum)

    Returns
    -------
    filtered : labeled array
    nr : int
        number of new labels
    '''
    from mahotas.labeled import remove_regions, labeled_size
    import mahotas as mh
    labeled = _as_labeled(labeled, labeled, 'filter_labeled')
    if remove_bordering:
        labeled = mh.labeled.remove_bordering(labeled)
        labeled, nr = mh.labeled.relabel(labeled)
    else:
        nr = labeled.max()

    to_keep = np.ones(nr + 1, bool)

    if min_size is not None or max_size is not None:
        sizes = labeled_size(labeled)
        if min_size:
            to_keep &= (sizes >= min_size)
        if max_size:
            to_keep &= (sizes <= max_size)
    to_keep[0] = True
    to_remove = np.where(~to_keep)
    labeled = remove_regions(labeled, to_remove)
    labeled, nr = mh.labeled.relabel(labeled, inplace=True)
    return labeled, nr
Beispiel #3
0
def filter_labeled(labeled, remove_bordering=False, min_size=None, max_size=None):
    """Filter labeled regions based on a series of conditions

    .. versionadded :: 1.4.1

    Parameters
    ----------
    labeled : labeled array
    remove_bordering : bool, optional
        whether to remove regions that touch the border
    min_size : int, optional
        Minimum size (in pixels) of objects to keep (default is no minimum)
    max_size : int, optional
        Maximum size (in pixels) of objects to keep (default is no maximum)

    Returns
    -------
    filtered : labeled array
    nr : int
        number of new labels
    """
    from mahotas.labeled import remove_regions, labeled_size
    import mahotas as mh

    labeled = _as_labeled(labeled, labeled, "filter_labeled")
    if remove_bordering:
        labeled = mh.labeled.remove_bordering(labeled)
        labeled, nr = mh.labeled.relabel(labeled)
    else:
        nr = labeled.max()

    to_keep = np.ones(nr + 1, bool)

    if min_size is not None or max_size is not None:
        sizes = labeled_size(labeled)
        if min_size:
            to_keep &= sizes >= min_size
        if max_size:
            to_keep &= sizes <= max_size
    to_keep[0] = True
    to_remove = np.where(~to_keep)
    labeled = remove_regions(labeled, to_remove)
    labeled, nr = mh.labeled.relabel(labeled, inplace=True)
    return labeled, nr
Beispiel #4
0
    def detectMyotube(self,
                      segmentationMethod='seg1',
                      sizeThresh=0,
                      tophat=True,
                      tophatImgList=[]):
        if tophat == True and tophatImgList == []:
            tophatImgList = self.tophatAllPlanes()
        elif tophat == True and tophatImgList != []:
            #tophatImgList = tophatImgList[tophatImgList.keys()[0]]
            tophatImgList = tophatImgList[0]
        elif tophat == False:
            tophatImgList = self.images

        # median -> histeq -> otsu -> segmentation (histeq and otsu by region)
        if segmentationMethod == 'seg1':
            img_mip = self.maximumIntensityProjection(tophatImgList)
            img_median = self.smooth(img_mip)
            img_histeq, _ = imtools.histeq(img_median)
            T = otsu(np.uint(img_histeq), ignore_zeros=True)
            img_bin = (np.uint(img_histeq) > T)
            img_labeled, _ = label(img_bin)
            markers, counts = self.segmentTubes1(img_labeled, img_histeq)
            # segmentation by watershed
            img_labeled = img_bin * cwatershed(-distance(img_bin), markers)
            result = {
                'MIP': img_mip,
                'median': img_median,
                'histEq': img_histeq,
                'otsu': T,
                'bin': img_bin
            }

        # median -> otsu -> segmentation (histeq and otsu by region)
        elif segmentationMethod == 'seg2':
            img_mip = self.maximumIntensityProjection(tophatImgList)
            img_median = self.smooth(img_mip)
            T = otsu(np.uint(img_median), ignore_zeros=True)
            img_bin = (np.uint(img_median) > T)
            img_labeled, _ = label(img_bin)
            markers, counts = self.segmentTubes2(img_labeled, img_median)
            # segmentation by watershed
            img_labeled = img_bin * cwatershed(-distance(img_bin), markers)
            result = {
                'MIP': img_mip,
                'median': img_median,
                'otsu': T,
                'bin': img_bin
            }

        # median -> histeq -> otsu -> segmentation (histeq and cut regions less than mean-intensity by region)
        elif segmentationMethod == 'seg3':
            img_mip = self.maximumIntensityProjection(tophatImgList)
            img_median = self.smooth(img_mip)
            img_histeq, _ = imtools.histeq(img_median)
            T = otsu(np.uint(img_histeq), ignore_zeros=True)
            img_bin = (np.uint(img_histeq) > T)
            img_labeled, _ = label(img_bin)
            markers, counts = self.segmentTubes3(img_labeled, img_histeq)
            # segmentation by watershed
            img_labeled = img_bin * cwatershed(-distance(img_bin), markers)
            result = {
                'MIP': img_mip,
                'median': img_median,
                'histEq': img_histeq,
                'otsu': T,
                'bin': img_bin
            }

        # median -> histeq -> otsu -> segmentation (cut regions less than mean-intensity by region)
        elif segmentationMethod == 'seg4':
            img_mip = self.maximumIntensityProjection(tophatImgList)
            img_median = self.smooth(img_mip)
            img_histeq, _ = imtools.histeq(img_median)
            T = otsu(np.uint(img_histeq), ignore_zeros=True)
            img_bin = (np.uint(img_histeq) > T)
            img_labeled, _ = label(img_bin)
            markers, counts = self.segmentTubes4(img_labeled, img_histeq)
            # segmentation by watershed
            img_labeled = img_bin * cwatershed(-distance(img_bin), markers)
            result = {
                'MIP': img_mip,
                'median': img_median,
                'histEq': img_histeq,
                'otsu': T,
                'bin': img_bin
            }

        # median -> otsu -> segmentation (cut regions less than mean-intensity by region)
        elif segmentationMethod == 'seg5':
            img_mip = self.maximumIntensityProjection(tophatImgList)
            img_median = self.smooth(img_mip)
            T = otsu(np.uint(img_median), ignore_zeros=True)
            img_bin = (np.uint(img_median) > T)
            img_labeled, _ = label(img_bin)
            markers, counts = self.segmentTubes4(img_labeled, img_median)
            # segmentation by watershed
            img_labeled = img_bin * cwatershed(-distance(img_bin), markers)
            result = {
                'MIP': img_mip,
                'median': img_median,
                'otsu': T,
                'bin': img_bin
            }

        # non-segmentation
        else:
            img_mip = self.maximumIntensityProjection(tophatImgList)
            img_median = self.smooth(img_mip)
            img_histeq, _ = imtools.histeq(img_median)
            T = otsu(np.uint(img_histeq))
            img_bin = (np.uint(img_histeq) > T)
            img_labeled, counts = label(img_bin)
            result = {
                'MIP': img_mip,
                'median': img_median,
                'histEq': img_histeq,
                'otsu': T,
                'bin': img_bin
            }
            print('non-segmentation')
        print('Otsu\'s threshold:', T)
        print('Found {} objects.'.format(counts))
        sizes = labeled_size(img_labeled)
        img_labeled = remove_regions_where(
            img_labeled, sizes < sizeThresh)  #origin 10000, triangle 8585
        img_relabeled, counts = relabel(img_labeled)
        result['label'] = img_relabeled
        result['count'] = counts
        print('After filtering and relabeling, there are {} objects left.'.
              format(counts))
        result['labeledSkeleton'] = self.labeledSkeleton(img_relabeled)
        return ProcessImages(result)
Beispiel #5
0
        extractor = DarkBrightDetector(preprocessed, orig, im_file, masks, is_debug = False)
        labels = extractor.find_bright_regions()

        drusen = extractor.get_predicted_region(Labels.Drusen)
        blood = extractor.get_predicted_region(Labels.Haemorage)

        Bc = np.ones((5, 5))
        labels_drusen, n_drusen = mh.label(drusen, Bc)
        labels_blood, n_blood = mh.label(blood, Bc)

        area = float(cv2.countNonZero(extractor.mask))

        outp = np.array([], dtype = np.int)

        # sizes excluding background
        sizes_drusen = mhl.labeled_size(labels_drusen)[1:] / area
        sizes_blood = mhl.labeled_size(labels_blood)[1:] / area

        hist_druzen, _ = np.histogram(sizes_drusen, n_bins, (0, 1e-3))
        hist_blood, _ = np.histogram(sizes_blood, n_bins, (0, 1e-3))

        outp = np.r_[outp, hist_druzen]
        outp = np.r_[outp, hist_blood]
        outp = np.r_[outp, i]
        df.loc[j] = outp
        j += 1
        print "Extracted: {0}, took {1:02.2f} sec ".format(im_file, time.time() - start)
      
    # write out the csv
    df.to_csv(path.join(output, prefix + ".txt"), index = False, header=False)    
    print "Extracted: ", prefix, "@", time_now_str()
Beispiel #6
0
    def __init__(self,dir=None,roi=None,label_image=None,mask=True,exp_type=None,mask_type=1,split=False,back_ground_subtract=True,plot_back_hist=False,SBtype=1,backf="mean",stim = 400,frametime=25,areathreshold=5):
        """
        exp_type 0: error
        exp_type 1: 0,3,10,20,30,50,100,50,30,20,10,3,0V
        exp_type 2: 0x3,3x3,10x3,20x3,30x3,50x3,100x1,50x3,30x3,20x3,10x3,3x3,0x3Vを1往復  
        exp_type 3: 0x3,3x3,10x3,20x3,30x3,50x3,100x1   
        exp_type 4: 0,3,5,10,15,20,25,30,35,40,50,100,50,40,35,30,25,20,15,10,5,3,0Vを5往復
        exp_type 5: 0,3,5,10,15,20,25,30,35,40,50,100,50,40,35,30,25,20,15,10,5,3Vを2往復
        exp_type 6: 0,5,10,15,20,25,30,35,40,50,100,50,40,35,30,25,20,15,10,5,0Vを2往復
        exp_type 7: 0,5,10,15,20,25,30,35,40,50,100,50,40,35,30,25,20,15,10,5,0Vを1往復      
        exp_type 8: 0,5,10,15,20,25,30,30,25,20,15,10,5,0Vを4往復  
        """
        """
        SBtype 0:no subtract
        SBtype 1:every photos subtract by every photos's background
        SBtype 2:every photos subtract by 1 Stream's background
        """
        print("program has started")
        
        self.back_ground_subtract = back_ground_subtract
        self.exp_type = exp_type
        self.dir = dir.replace("\\","/")
        self.expname = self.dir.split("/")[-1]
        self.back_feature = backf
        self.frame_time = frametime
        self.area_thresh = areathreshold
        self.stim_time = stim
        if back_ground_subtract:
            if SBtype == 1:self.out_dir=self.dir + "/output_ind_subback_by_"+backf
            elif SBtype == 2:self.out_dir=self.dir + "/output_stream_subback_by_"+backf
        else:self.out_dir=self.dir + "/output"
        
        try:os.mkdir(self.out_dir)
        except:print("output has existed already")    
        print(self.out_dir)

        
        self.interval = self.get_interval()
        self.feature = ("f_basal","b_basal","peak","auc","modified_auc","auc_divided_by_peak")
                
        if split:
            stream_dir = glob.glob(dir+"/split/*V*")
            stream_dir = [i.replace("\\","/") for i in stream_dir]
        else:
            stream_fname = glob.glob(dir+"/Stream/*.tif")
            stream_fname = [i.replace("\\","/") for i in stream_fname]
            
        streamname = [i.split("/")[-1] for i in stream_fname]
        self.Vol_list = [self.get_voltage(i) for i in streamname]                         
        self.Vol_str_list = [str(self.get_voltage(i))+"V" for i in streamname]
        all_vol = list(set(self.Vol_list))
        repeat_num_each_vol = [self.Vol_list.index(i) for i in all_vol]
        self.repeat_num = min(repeat_num_each_vol)
        
        if mask:
            start_time = self.get_time()
            
            #read mask image
            mask_dir = glob.glob(dir+"/other/"+str(mask_type)+"*mask*.tif")
            mask_dir = mask_dir[0].replace("\\","/")
            mask_fig = io.imread(mask_dir)
            self.labelimage = measure.label(mask_fig)
            
            back_ground_label_dir = glob.glob(dir+"/other/back.tif")
            back_ground_label_dir = back_ground_label_dir[0].replace("\\","/")
            back_ground_label_image = io.imread(back_ground_label_dir)
            back_ground_label = measure.label(back_ground_label_image)
            # remove small object
            sizes = mal.labeled_size(self.labelimage)            
            self.labelimage = mal.remove_regions_where(self.labelimage,sizes<self.area_thresh)
            self.labelimage,counts = mal.relabel(self.labelimage)
            self.label_num = self.labelimage.max()
            
            # get each average of intensity and remove back ground  
            label_props = regionprops(self.labelimage)
            self.label_area = [i.area for i in label_props]
            self.back = []
            self.intensity_df_list = [self.get_stream_label_intensity(i,back_ground_label,SBtype=SBtype,plot_hist=plot_back_hist) for i in stream_fname]
            self.back= pd.DataFrame(self.back)
            self.back.columns = ["mean","minimum","median","mode"]
            self.back.to_csv(self.out_dir+"/background.csv")
            
            
            bound = np.linspace(0,self.label_num,self.label_num+1)
            norm = colors.BoundaryNorm(boundaries=bound,ncolors=256)
            plt.imshow(self.labelimage,cmap ="gist_ncar" ,norm=norm)
            cbar=plt.colorbar()
            cbar.set_label(label="cell ID")
            plt.savefig(self.out_dir+"/labeled_image.png")
            
            end_time=self.get_time()            
            print("intensity was mesured successfuly!!")
            print("times:"+str(int(end_time - start_time))+"s")
            
        elif label_image:
            self.labelimage = self.label_ndarray(dir)
            self.label_num = self.labelimage.max()
            self.intensity_df_list = [self.get_stream_label_intensity(i) for i in stream_dir]
            print("intensity was mesured successfuly!!")
            label_props = regionprops(self.labelimage)
            self.label_area = [i.area for i in label_props]        
        elif roi :
            csv_dir = glob.glob(dir+"/csv/*.csv")
            csv_dir = [i.replace("\\","/") for i in csv_dir]
            self.intensity_df_list = [pd.read_csv(i) for i in csv_dir ]
            time=list(range(len(self.intensity_df_list[0])))
            time = [i*self.frame_time for i in time ]
            for i in self.intensity_df_list:
                i.index=time
            self.intensity_df_list = [i.iloc[:,1:] for i in self.intensity_df_list]
            print("intensity was mesured successfuly!!")
            
        self.label_num = len(self.intensity_df_list[0].columns)        
        #self.x_label,self.legend_label = dd.get_x_label(self.exp_type)
        #self.get_propaty()
        self.save_csv()
Beispiel #7
0
                                       is_debug=False)
        labels = extractor.find_bright_regions()

        drusen = extractor.get_predicted_region(Labels.Drusen)
        blood = extractor.get_predicted_region(Labels.Haemorage)

        Bc = np.ones((5, 5))
        labels_drusen, n_drusen = mh.label(drusen, Bc)
        labels_blood, n_blood = mh.label(blood, Bc)

        area = float(cv2.countNonZero(extractor.mask))

        outp = np.array([], dtype=np.int)

        # sizes excluding background
        sizes_drusen = mhl.labeled_size(labels_drusen)[1:] / area
        sizes_blood = mhl.labeled_size(labels_blood)[1:] / area

        hist_druzen, _ = np.histogram(sizes_drusen, n_bins, (0, 1e-3))
        hist_blood, _ = np.histogram(sizes_blood, n_bins, (0, 1e-3))

        outp = np.r_[outp, hist_druzen]
        outp = np.r_[outp, hist_blood]
        outp = np.r_[outp, i]
        df.loc[j] = outp
        j += 1
        print "Extracted: {0}, took {1:02.2f} sec ".format(
            im_file,
            time.time() - start)

    # write out the csv