def test_ignore_zeros(): np.seterr(all='raise') np.random.seed(22) A = np.zeros((1024,24), np.uint8) A[:24,:24] = np.random.randint(100, 200, size=(24,24)) assert rc(A) < 100 assert otsu(A) < 100 assert rc(A, ignore_zeros=1) > 100 assert otsu(A, ignore_zeros=1) > 100
def test_ignore_zeros(): np.seterr(all='raise') np.random.seed(22) A = np.zeros((1024, 24), np.uint8) A[:24, :24] = np.random.random_integers(100, 200, size=(24, 24)) assert rc(A) < 100 assert otsu(A) < 100 assert rc(A, ignore_zeros=1) > 100 assert otsu(A, ignore_zeros=1) > 100
def segmentTubes2(img_labeled, img_intensity): """ Parameters ========== :img_labeled: labeled image before segmentation img_intensity: intensity image Returns ======= labeld image segmented by Otsu's binarization by each region. * This function do not perform histogram equalization by each region. If you want to do that before the segmentation, use 'segementation1' function. """ img_seg = np.zeros_like(img_intensity) labels = np.unique(img_labeled.flatten()) for cell_id in labels[1:]: bin_region = (img_labeled == cell_id) region = bin_region * img_intensity region, _ = imtools.histeq(region) # segmentation T = otsu(np.uint(region), ignore_zeros=True) img_seg += (np.uint(region) > T) labeled, counts = label(img_seg) return labeled, counts
def pftas(img, T=None): """ values = pftas(img, T={mahotas.threshold.otsu(img)}) Compute parameter free Threshold Adjacency Statistics TAS were presented by Hamilton et al. in "Fast automated cell phenotype image classification" (http://www.biomedcentral.com/1471-2105/8/110) The current version is an adapted version which is free of parameters. The thresholding is done by using Otsu's algorithm (or can be pre-computed and passed in by setting `T`), the margin around the mean of pixels to be included is the standard deviation. Also returns a version computed on the negative of the binarisation defined by Hamilton et al. Use tas() to get the original version of the features. Parameters ---------- img : A 2-D image T : Threshold to use (default: compute with otsu) Returns ------- values : A 1-D ndarray of feature values """ if T is None: T = otsu(img) pixels = img[img > T].ravel() std = pixels.std() return _tas(img, T, std)
def threshold(img,thresh): ''' T = threshold(img, thresh) thresh can be: * None: returns -1 * a number: returns thresh * a function: returns thresh(img) * a string: one of ('otsu','rc','murphy_rc','mean') ''' if thresh is None: return -1 if type(thresh) is str: if thresh == 'otsu': return otsu(img) if thresh == 'rc': return rc(img) if thresh == 'murphy_rc': return murphy_rc(img) if thresh == 'mean': return img.mean() raise ValueError("pyslic.threshold: Cannot handle argument '%s'" % thresh) if callable(thresh): return thresh(img) return thresh
def test_otsu_fast(): np.random.seed(120) for i in range(12): A = 32*np.random.rand(128,128) A = A.astype(np.uint8) fast = otsu(A) slow = slow_otsu(A) assert fast == slow
def test_otsu_fast(): np.random.seed(120) for i in range(12): A = 32 * np.random.rand(128, 128) A = A.astype(np.uint8) fast = otsu(A) slow = slow_otsu(A) assert fast == slow
def extract1(img, solution): labeled, _ = solution binary = (labeled > 0) bg = img[~binary].ravel() objects = img[binary].ravel() bstd = bg.std() if bstd == 0: bstd = 1 separation = (bg.mean()-objects.mean())/bstd corrcoefs = [] for T in (img.mean(), img.mean() + img.std(), otsu(img), rc(img)): corrcoefs.append(_corrcoef(binary, img > T)) return np.concatenate( ([separation], corrcoefs) )
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)
def oversegment(img): overseg = sobel_segment(img) for t in (img.mean(), otsu(img), rc(img)): overseg = intersec(overseg, segment(img, t)) return overseg
def test_nozeros(): np.seterr(all='raise') np.random.seed(22) A = (np.random.rand(100,100)*50).astype(np.uint8)+201 assert rc(A) > 200 assert otsu(A) > 200
def masked_pftas(intensity_image): T = otsu(intensity_image, ignore_zeros=True) return pftas(intensity_image, T=T)