Esempio n. 1
0
def test_percentile_median():
    # check that percentile p0 = 0.5 is identical to local median
    img = data.camera()
    img16 = img.astype(np.uint16)
    selem = disk(15)
    # check for 8bit
    img_p0 = rank.percentile(img, selem=selem, p0=.5)
    img_max = rank.median(img, selem=selem)
    assert_array_equal(img_p0, img_max)
    # check for 16bit
    img_p0 = rank.percentile(img16, selem=selem, p0=.5)
    img_max = rank.median(img16, selem=selem)
    assert_array_equal(img_p0, img_max)
Esempio n. 2
0
def test_percentile_median():
    # check that percentile p0 = 0.5 is identical to local median
    img = data.camera()
    img16 = img.astype(np.uint16)
    selem = disk(15)
    # check for 8bit
    img_p0 = rank.percentile(img, selem=selem, p0=.5)
    img_max = rank.median(img, selem=selem)
    assert_array_equal(img_p0, img_max)
    # check for 16bit
    img_p0 = rank.percentile(img16, selem=selem, p0=.5)
    img_max = rank.median(img16, selem=selem)
    assert_array_equal(img_p0, img_max)
Esempio n. 3
0
def Image_ws_tranche(image):
    
    laser = Detect_laser(image)
    laser_tranche = tranche_image(laser,60)
    
    image_g = skimage.color.rgb2gray(image)
    image_g = image_g * laser_tranche
    
    image_med = rank2.median((image_g*255).astype('uint8'),disk(8))
    
    image_clahe = exposure.equalize_adapthist(image_med, clip_limit=0.03)
    image_clahe_stretch = exposure.rescale_intensity(image_clahe, out_range=(0, 256))

    image_grad = rank2.gradient(image_clahe_stretch,disk(3))
    
    image_grad_mark = image_grad<20
    image_grad_forws = rank2.gradient(image_clahe_stretch,disk(1))
    
    image_grad_mark_closed = closing(image_grad_mark,disk(1))
    
    Labelised = (skimage.measure.label(image_grad_mark_closed,8,0))+1
    Watersheded  = watershed(image_grad_forws,Labelised)
    
    cooc = coocurence_liste(Watersheded,laser,3)
    
    x,y = compte_occurences(cooc)
    
    return x,y
Esempio n. 4
0
def _find_bad_pixel_candidates_generic(raw, isCandidateFn):
    color_masks = _colormasks(raw)   
    rawimg = raw.raw_image_visible    
    coords = []
    
    # median filtering for each channel    
    r = 5
    kernel = np.ones((r,r))
    for mask in color_masks:
        t1 = time.time()
        # skimage's median is quite slow, it uses an O(r) filtering algorithm.
        # There exist O(log(r)) and O(1) algorithms, see https://nomis80.org/ctmf.pdf.
        # Also, we only need the median values for the masked pixels.
        # Currently, they are calculated for all pixels for each color.
        med = median(rawimg, kernel, mask=mask)
        print('median:', time.time()-t1, 's')
        
        # detect possible bad pixels
        candidates = isCandidateFn(rawimg, med)
        candidates &= mask
        
        y,x = np.nonzero(candidates)
        # note: the following is much faster than np.transpose((y,x))
        candidates = np.empty((len(y),2), dtype=y.dtype)
        candidates[:,0] = y
        candidates[:,1] = x
        
        coords.append(candidates)
        
    return coords
Esempio n. 5
0
def _repair_bad_pixels_generic(raw, coords, method='median'):
    color_masks = _colormasks(raw)
        
    rawimg = raw.raw_image_visible
    r = 5
    kernel = np.ones((r,r))
    for color_mask in color_masks:       
        mask = np.zeros_like(color_mask)
        mask[coords[:,0],coords[:,1]] = True
        mask &= color_mask
        
        # interpolate all bad pixels belonging to this color
        if method == 'mean':
            # With mean filtering we have to ignore the bad pixels as they
            # would influence the mean too much.
            # FIXME could lead to invalid values if bad pixels are clustered
            #       such that no valid pixels are left in a block
            raise NotImplementedError
        elif method == 'median':
            # bad pixels won't influence the median in most cases and just using
            # the color mask prevents bad pixel clusters from producing
            # bad interpolated values (NaNs)
            smooth = median(rawimg, kernel, mask=color_mask)
        else:
            raise ValueError
        
        rawimg[mask] = smooth[mask]   
Esempio n. 6
0
def substract_background(data, radius, background='mean', show=0):
    dmin, dmax = data.min(), data.max()
    tmp = ((data - dmin) / (dmax - dmin) * 255).astype(np.uint8)
    lforeground = rank.minimum(tmp, disk(radius // 2))

    if background == 'mean':
        lbackground = rank.mean(lforeground, disk(radius * 4)).astype(int)
    elif background == 'median':
        lbackground = rank.median(lforeground, disk(radius * 4)).astype(int)
    elif background == 'maximum':
        lbackground = rank.maximum(lforeground, disk(radius * 4)).astype(int)
    else:
        lbackground = rank.modal(lforeground, disk(radius * 4)).astype(int)

    rdata = (lforeground - lbackground)

    if show > 2:
        fig, axes = plt.subplots(ncols=3, nrows=1, figsize=(30, 10))
        for ax, im, title in zip(axes, [lforeground, lbackground, rdata],
                                 ['Foreground', 'Background', 'Substraction']):
            ax.imshow(im, 'optiona')
            ax.set_xticks([])
            ax.set_yticks([])
            ax.set_title(title)
        plt.show()

    return rdata
    def split_object(self, labeled_image):
        """ split object when it's necessary
        """
        
        labeled_image = labeled_image.astype(np.uint16)

        labeled_mask = np.zeros_like(labeled_image, dtype=np.uint16)
        labeled_mask[labeled_image != 0] = 1

        #ift structuring element about center point. This only affects eccentric structuring elements (i.e. selem with even num===============================
       
        labeled_image = skr.median(labeled_image, skm.disk(4))
        labeled_mask = np.zeros_like(labeled_image, dtype=np.uint16)
        labeled_mask[labeled_image != 0] = 1
        distance = scipym.distance_transform_edt(labeled_image).astype(np.uint16)
        #=======================================================================
        # binary = np.zeros(np.shape(labeled_image))
        # binary[labeled_image > 0] = 1
        #=======================================================================
        distance = skr.mean(distance, skm.disk(15))
         
        l_max = skr.maximum(distance, skm.disk(5))
        #l_max = skf.peak_local_max(distance, indices=False,labels=labeled_image, footprint=np.ones((3,3)))
        l_max = l_max - distance <= 0
        
        l_max = skr.maximum(l_max.astype(np.uint8), skm.disk(6))
        
       
        
        marker = ndimage.label(l_max)[0]
        split_image = skm.watershed(-distance, marker)
        
        split_image[split_image[0,0] == split_image] = 0
        
        return split_image
Esempio n. 8
0
def _repair_bad_pixels_generic(raw, coords, method='median'):
    color_masks = _colormasks(raw)

    rawimg = raw.raw_image_visible
    r = 5
    kernel = np.ones((r, r))
    for color_mask in color_masks:
        mask = np.zeros_like(color_mask)
        mask[coords[:, 0], coords[:, 1]] = True
        mask &= color_mask

        # interpolate all bad pixels belonging to this color
        if method == 'mean':
            # With mean filtering we have to ignore the bad pixels as they
            # would influence the mean too much.
            # FIXME could lead to invalid values if bad pixels are clustered
            #       such that no valid pixels are left in a block
            raise NotImplementedError
        elif method == 'median':
            # bad pixels won't influence the median in most cases and just using
            # the color mask prevents bad pixel clusters from producing
            # bad interpolated values (NaNs)
            smooth = median(rawimg, kernel, mask=color_mask)
        else:
            raise ValueError

        rawimg[mask] = smooth[mask]
Esempio n. 9
0
def _find_bad_pixel_candidates_generic(raw, isCandidateFn):
    color_masks = _colormasks(raw)
    rawimg = raw.raw_image_visible
    coords = []

    # median filtering for each channel
    r = 5
    kernel = np.ones((r, r))
    for mask in color_masks:
        t1 = time.time()
        # skimage's median is quite slow, it uses an O(r) filtering algorithm.
        # There exist O(log(r)) and O(1) algorithms, see https://nomis80.org/ctmf.pdf.
        # Also, we only need the median values for the masked pixels.
        # Currently, they are calculated for all pixels for each color.
        med = median(rawimg, kernel, mask=mask)
        print('median:', time.time() - t1, 's')

        # detect possible bad pixels
        candidates = isCandidateFn(rawimg, med)
        candidates &= mask

        y, x = np.nonzero(candidates)
        # note: the following is much faster than np.transpose((y,x))
        candidates = np.empty((len(y), 2), dtype=y.dtype)
        candidates[:, 0] = y
        candidates[:, 1] = x

        coords.append(candidates)

    return coords
Esempio n. 10
0
def median_filter(image, selem=None):
    if selem is None:
        # default mask is 5x5 square
        selem = square(5)
    depth = image.shape[2]
    return np.dstack(median(channel[...,0], selem)
                     for channel in np.dsplit(image, depth)) / 255.
Esempio n. 11
0
def segm(img):
    den = rank.median(img, morph.disk(3))
    # continuous regions (low gradient)
    markers = rank.gradient(den, morph.disk(5)) < 10
    mrk, tot = ndimage.label(markers)
    grad = rank.gradient(den, morph.disk(2))
    labels = morph.watershed(grad, mrk)
    return seg_regions(img, labels, tot)
Esempio n. 12
0
File: toolbox.py Progetto: rhoef/afw
def outlineSmoothing(image, radius=1):
    """Smooth outlines of foreground object using morphological operations."""

    struct = median(disk(radius), disk(1))
    label_image = binary_closing(image, struct)
    label_image = binary_opening(label_image, struct)

    return label_image.astype(image.dtype)
Esempio n. 13
0
def processing_ndarray(data, median_disks, closing_disks,
                       histogram_clip=[0.0, 99.], func=np.sqrt):
    """

    :param data: 3d ndarray of the form (ny,nx,nt)
    :param median_disks: disks used to do median noise cleaning
    :param closing_disks: disks used to morphological closing
    :param histogram_clip: clip the data
    :param func: function used to scale the data nicely
    :return: an AWARE processed
    """
    nt = data.shape[2]

    rdpi = datacube_tools.running_difference(datacube_tools.persistence(data))

    mc_data = func(rdpi)
    mc_data[mc_data < 0] = 0.0
    clip_limit = np.nanpercentile(mc_data, histogram_clip)

    # Rescale the data using the input function, and subtract the lower
    # clip limit so it begins at zero.
    f_data = mc_data - clip_limit[0] / (clip_limit[1]-clip_limit[0])

    # Replace the nans with zeros - the reason for doing this rather than
    # something more sophisticated is that nans will not contribute
    # greatly to the final answer.  The nans are put back in at the end
    # and get carried through in the maps.
    nans_here = np.logical_not(np.isfinite(f_data))
    nans_replaced = deepcopy(f_data)
    nans_replaced[nans_here] = 0.0

    # Final datacube
    processed_datacube = np.zeros_like(data)

    for t in range(0, nt):
        this = f_data[:, :, t]
        for d in range(0, len(median_disks)):

            # Get rid of noise by applying the median filter.  Although the
            # input is a byte array make sure that the output is a floating
            # point array for use with the morphological closing operation.
            new_d = 1.0*median(this, median_disks[d])

            # Apply the morphological closing operation to rejoin separated
            # parts of the wave front.
            new_d = closing(new_d, closing_disks[d])

            # Sum all the final results
            processed_datacube[:, :, t] += new_d*1.0

    return processed_datacube, nans_here
Esempio n. 14
0
    def segmente(self):
        
        #lecture de l'image vers un ndarray. toutes les images fournies ont une taille de 12051000 pixels.
        self.im = imread(self.name)
        
        if self.equalizeHist == 1:
            self.im = equalize_hist(self.im)
        
        #Filtrage median pour enlever le bruit et eviter le repliement spectral en cas d'echantillonnage. Point positif : c'est un filtre qui conserve les bords !!!
        #plus le rayon du disque est important plus le lissage est fort et plus il y a risque de perte d'information, le filtre supprimant les details.
        self.im = rank.median(self.im,disk(8))
        
        #on reduit l'image pour diminuer le temps de computation. On ne s'interesse qu'a certaines parties de l'image plus echantillonnage. C'est l'image grayscale. On diminue par deux le nombre de pixels contenu dans l'image.
        self.im_red = self.im[::2,::2] 

        
        # slic attend une image rgb il faut donc faire la conversion
        self.img_temp= gray2rgb(self.im_red)
        
        # cette fonction attend egalement des valeurs d'intensite en float
        self.img = img_as_float(self.img_temp)
        
        #segmentation en superpixel, fonction slic de skimage.segmentation, le parametre sigma prend la std dev d'un filtre gaussien applique a l'image avant de la segmenter
        self.segments_slic = slic(self.img,n_segments = self.segments,compactness = self.compacite, sigma =1)
        

        #on doit avoir tous les indices sup a 0 pour region props donc on les augmente tous de 1
        self.segments_slic = self.segments_slic + np.ones(self.segments_slic.shape,dtype = np.int64)
        
        # liste de proprietes par superpixel
        self.props = regionprops(self.segments_slic, intensity_image = self.im_red)
        
        # on cree la matrice d'adjacence
        self.create_neighbour_matrix()


        #liste qui permettra de stocker les labels des superpixel qui ont ete colore
        self.colored_pixel_label = list()


        # appliquons maintenant un deuxieme clustering sur ces superpixels base sur leurs proprietes
        #self.clustering()

        # Liaison de click avec la fonction onclick et des evenements clavier
        self.fig = plt.figure('segmentation')
        self.cid1 = self.fig.canvas.mpl_connect('button_press_event', self.onclick)


        #Affichage
        self.affichage(mark_boundaries(self.img,self.segments_slic))
Esempio n. 15
0
    def __call__(self, image, range_, shrink_per_label=False):

        radius = np.abs(range_[0])
        struct = median(disk(radius), disk(1))

        if shrink_per_label:
            out = np.zeros(image.shape, dtype=np.int16)
            for label in np.unique(image)[1:]:
                bin_ = (image == label).astype(np.uint8) # ev. as bool
                bin_ = binary_erosion(bin_, struct)
                out += (bin_*label).astype(out.dtype)
        else:
            out = binary_erosion(image.astype(np.uint8), struct)

        return out*image.astype(np.int16)
def gen_mask(slide_ind=None, in_dir=None, stack=None, use_hsv=True, out_dir=None, num_section_per_slide=5, mirror=None, rotate=None):

	imgcolor = Image.open(os.path.join(in_dir, '%s_%02d_x0.3125_z0.tif'%(stack, slide_ind)))
	imgcolor = imgcolor.convert('RGB')
	imgcolor = np.array(imgcolor)
	img_gray = rgb2gray(imgcolor)
	# slide_height, slide_width = img_gray.shape[:2]

	if use_hsv:
		imghsv = rgb2hsv(imgcolor)
		img = imghsv[...,1]
	else:
		img = img_gray

# utilize bounding box selected during scanning process
	a = img_gray < 0.98

	a = median(a.astype(np.float), disk(3))
	a = remove_small_objects(a.astype(np.bool), min_size=50, connectivity=2, in_place=False)
	column_labels, n_columns = label(a, neighbors=8, return_num=True, background=0)
	column_props = regionprops(column_labels+1)
	column_bboxes = np.array([p.bbox for p in column_props])
	indices = np.argsort(column_bboxes[:,1])

	mask = np.zeros_like(img_gray, dtype=np.bool)
	col_margin = 2
	row_margin = 2
	for i, b in enumerate(column_bboxes[indices, :]):
		minr, minc, maxr, maxc = b
		mask[minr+row_margin:maxr-row_margin, minc+col_margin:maxc-col_margin] = 1

	section_masks, bboxes, exists = foreground_mask_morphsnakes_slide(img, levelset=mask, 
				num_section_per_slide=num_section_per_slide, min_iters=400)

	ids = np.where(exists)[0]
	#     section_images = []
	for _, ((minr, minc, maxr, maxc), mask, section_id) in enumerate(zip(bboxes, section_masks, ids)):
		img = imgcolor[minr:maxr+1, minc:maxc+1].copy()
		img[~mask] = 0
		#         section_images.append(img)

		image_filepath = os.path.join(out_dir, '%s_x0.3125_%02d_%d.tif'%(stack, slide_ind, section_id))
		imsave(image_filepath, img)

	# bboxes_percent = np.array(bboxes, dtype=np.float)/np.r_[img_gray.shape[:2], img_gray.shape[:2]][np.newaxis, :]

	return bboxes
Esempio n. 17
0
	def ZOI_selection(self, eclick, erelease):
		x1, y1 = eclick.xdata, eclick.ydata
		x2, y2 = erelease.xdata, erelease.ydata
		xmin=round(min(x1,x2))
		xmax=round(max(x1,x2))
		ymin=round(min(y1,y2))
		ymax=round(max(y1,y2))
		# update dimension of the image:
		self.height = (ymax-ymin)
		self.width = (xmax-xmin)
		self.yoffset = ymin
		self.xoffset = xmin
		#print "1"
		###################################################################### camera INIT with ZOI selection
		if self.videoextenso['enabled']:
			# the following is to initialise the spot detection
			image=self.cam.getImage()
			#plt.imsave("/home/corentin/Bureau/image_originale.tiff",image)
			croped_image = image[self.yoffset:self.height+self.yoffset,self.xoffset:self.xoffset+self.width]
			image = croped_image
			#plt.imsave("/home/corentin/Bureau/image.tiff",image)
			image=rank.median(image,square(15)) # median filter to smooth the image and avoid little reflection that may appear as spots.
			self.thresh = threshold_otsu(image) # calculate most effective threshold
			bw= image>self.thresh 
			#print "1"
			#applying threshold
			if not (self.videoextenso['white_spot']):
				bw=(1-bw).astype(np.uint8)
			#still smoothing
			bw = dilation(bw,square(3))
			bw = erosion(bw,square(3))
			#plt.imsave("/home/corentin/Bureau/bw.tiff",bw)
			# Remove artifacts connected to image border
			cleared = bw.copy()
			clear_border(cleared)
			# Label image regions
			label_image = label(cleared)
			borders = np.logical_xor(bw, cleared)
			label_image[borders] = -1
			#plt.imsave("/home/corentin/Bureau/label_image.tiff",label_image)
			# Create the empty vectors for corners of each ZOI
			regions=regionprops(label_image)
			print [region.area for region in regions]
			#mean_area=np.mean[region.area for region in regions]
			regions=[region for region in regions if region.area>200]
Esempio n. 18
0
def label_from_thresh(frame, thresh, parameters):

    smooth = parameters['smooth']
    min_distance = np.int(parameters['nuc_distance'])
    image = rank.median(frame, disk(smooth))
    image = rank.enhance_contrast(image, disk(smooth))
    im_max = image.max()
    if im_max < thresh:
        return np.zeros(image.shape, dtype=np.int32)
    else:
        image = image > thresh
        distance = ndimage.distance_transform_edt(image)
        local_maxi = peak_local_max(distance,
                                    footprint=disk(min_distance),
                                    #min_distance=min_distance,
                                    indices=False, labels=image)
        markers = ndimage.label(local_maxi)[0]
        return watershed(-distance, markers, mask=image)
Esempio n. 19
0
    def __call__(self, image, range_, *args, **kw):
        """Binary dilation without merging of foreground objects."""

        radius = np.abs(range_[1])
        # just in case vigra is 10x faster, plays a role for large radii
        # img_exp = vigra.filters.discDilation(image.astype("uint8"), radius)

        if radius > 0:
            struct = median(disk(radius), disk(1))
            img_exp = ndi.morphology.binary_dilation(image, structure=struct)
        elif radius == 0:
            img_exp = image.copy().astype(bool)
        else:
            img_exp = ndi.morphology.binary_dilation(image, iterations=0)

        edt = ndi.morphology.distance_transform_edt(
            np.invert(image.astype(bool)))

        return watershed(edt, image, mask=img_exp)
Esempio n. 20
0
def test_line_profile_dynamic():
    """Test a line profile updating after an image transform"""
    image = data.coins()[:-50, :]  # shave some off to make the line lower
    image = skimage.img_as_float(image)
    viewer = ImageViewer(image)

    lp = LineProfile(limits='dtype')
    viewer += lp

    line = lp.get_profiles()[-1][0]
    assert line.size == 129
    assert_almost_equal(np.std(viewer.image), 0.208, 3)
    assert_almost_equal(np.std(line), 0.229, 3)
    assert_almost_equal(np.max(line) - np.min(line), 0.725, 1)

    viewer.image = skimage.img_as_float(median(image, selem=disk(radius=3)))

    line = lp.get_profiles()[-1][0]
    assert_almost_equal(np.std(viewer.image), 0.198, 3)
    assert_almost_equal(np.std(line), 0.220, 3)
    assert_almost_equal(np.max(line) - np.min(line), 0.639, 1)
Esempio n. 21
0
def test_line_profile_dynamic():
    """Test a line profile updating after an image transform"""
    image = data.coins()[:-50, :]  # shave some off to make the line lower
    image = skimage.img_as_float(image)
    viewer = ImageViewer(image)

    lp = LineProfile(limits='dtype')
    viewer += lp

    line = lp.get_profiles()[-1][0]
    assert line.size == 129
    assert_almost_equal(np.std(viewer.image), 0.208, 3)
    assert_almost_equal(np.std(line), 0.229, 3)
    assert_almost_equal(np.max(line) - np.min(line), 0.725, 1)

    viewer.image = skimage.img_as_float(median(image,
                                               selem=disk(radius=3)))

    line = lp.get_profiles()[-1][0]
    assert_almost_equal(np.std(viewer.image), 0.198, 3)
    assert_almost_equal(np.std(line), 0.220, 3)
    assert_almost_equal(np.max(line) - np.min(line), 0.639, 1)
Esempio n. 22
0
def segment(img, outimg):
    img = np.array(Image.open(img))
    den = rank.median(img, morph.disk(3))
    # continuous regions (low gradient)
    markers  = rank.gradient(den, morph.disk(5)) < 10
    mrk, tot = ndimage.label(markers)
    grad     = rank.gradient(den, morph.disk(2))
    labels   = morph.watershed(grad, mrk)
    print 'Total regions:', tot
    regs = big_regions(labels, tot)

    spr = 3
    spc = 6
    plot_seg(spr, spc, 1, img,    cm.gray,     1,   'image')
    plot_seg(spr, spc, 2, den,    cm.gray,     1,   'denoised')
    plot_seg(spr, spc, 3, grad,   cm.spectral, 1,   'gradient')
    plot_seg(spr, spc, 4, mrk,    cm.spectral, 1,   'markers')
    plot_seg(spr, spc, 5, labels, cm.spectral, 1,   'regions\n%i' % tot)
    plot_seg(spr, spc, 6, img,    cm.gray,     1,   'composite')
    plot_seg(spr, spc, 6, labels, cm.spectral, 0.7, 'composite')

    plot_mask(spr, spc, 7,  0, labels, regs, cm.spectral, 'main region')
    plot_mask(spr, spc, 8,  1, labels, regs, cm.spectral, '2nd region')
    plot_mask(spr, spc, 9,  2, labels, regs, cm.spectral, '3rd region')
    plot_mask(spr, spc, 10, 3, labels, regs, cm.spectral, '4th region')
    plot_mask(spr, spc, 11, 4, labels, regs, cm.spectral, '5th region')
    plot_mask(spr, spc, 12, 5, labels, regs, cm.spectral, '6th region')

    plot_crop(spr, spc, 13, 0, img, labels, regs, cm.gray)
    plot_crop(spr, spc, 14, 1, img, labels, regs, cm.gray)
    plot_crop(spr, spc, 15, 2, img, labels, regs, cm.gray)
    plot_crop(spr, spc, 16, 3, img, labels, regs, cm.gray)
    plot_crop(spr, spc, 17, 4, img, labels, regs, cm.gray)
    plot_crop(spr, spc, 18, 5, img, labels, regs, cm.gray)

    #plt.savefig('test.eps', dpi=300)
    plt.savefig(outimg, dpi=300)
Esempio n. 23
0
def gredientSeg(preprocessedImage1, raw_image, minAreaSize, maxAreaSize, thre):
    """
    :param preprocessedImage: a greyscale image
    :param raw_image: a raw image for highlighting segmented cells
    :param minAreaSize: the minimum area size of the cell
    :param maxAreaSize: maximum area size of the cell

    :return:
    """
    preprocessedImage, denoised, gradientIm, thresh, imgDisplay, contours = [], [], [], [], [], []
    imgDisplay = raw_image
    preprocessedImage = preprocessedImage1.astype(np.uint8)

    if len(preprocessedImage.shape) > 2:
        preprocessedImage = cv2.cvtColor(preprocessedImage, cv2.COLOR_BGR2GRAY)

    preprocessedImage = img_as_ubyte(preprocessedImage)  # denoise image
    denoised = rank.median(preprocessedImage, disk(2))
    # local ingredient
    gradientIm = rank.gradient(denoised, disk(thre))

    thresh = cv2.threshold(gradientIm, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    # fused the the mask and the original image for extracting morph features
    thresh = cv2.bitwise_and(thresh, preprocessedImage)

    # find contours in the the mask
    _, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                              cv2.CHAIN_APPROX_SIMPLE)

    color = np.random.randint(0, 255, (len(contours), 3))
    mask_colored = np.zeros_like(raw_image, )
    pts, rectang, cellFeatures = [], [], []

    for (ii, cnt) in enumerate(contours):
        ((x, y), radius) = cv2.minEnclosingCircle(cnt)
        tmp_perim = cv2.arcLength(cnt, True)
        tmp_approx = cv2.approxPolyDP(cnt, 0.04 * tmp_perim, True)
        (x, y, tmp_w, tmp_h) = cv2.boundingRect(tmp_approx)
        ar = tmp_w / float(tmp_h)

        # if ar >= 0.95 and ar <= 1.05:
        #     "sss"
        # else:
        #     del cnt
        #     continue

        area = cv2.contourArea(cnt)
        rect = cv2.boundingRect(cnt)
        (x1, y1, w1, h1) = rect
        cntIndex, mergedCnt = mergeOverSegmentedContours(contours, cnt)
        if mergedCnt is not None:
            cnt = []
            cnt = mergedCnt
            del contours[cntIndex]
        if area < minAreaSize or area > maxAreaSize or hierarchy[0, ii,
                                                                 3] != -1:
            # del contours[ii]
            continue
        else:
            cellMorph = getCellIntensityModule(cnt, imgDisplay)
            cellFeatures.append(cellMorph)
            cv2.drawContours(
                mask_colored,
                [cnt],
                -1,
                color[ii].tolist(),
                thickness=cv2.FILLED,
            )
            cv2.drawContours(imgDisplay, [cnt], -1, (0, 255, 0), 1)
            rectang.append((x1, y1, w1, h1))
            pts.append([x, y])
    del thresh, raw_image, preprocessedImage, preprocessedImage1, denoised, gradientIm
    return pts, rectang, mask_colored, imgDisplay, cellFeatures
Esempio n. 24
0
from skimage.filter.rank import median
from skimage.morphology import disk

noise = np.random.random(noisy_image.shape)
noisy_image = img_as_ubyte(data.camera())
noisy_image[noise > 0.99] = 255
noisy_image[noise < 0.01] = 0

fig, ax = plt.subplots(2, 2, figsize=(10, 7))
ax1, ax2, ax3, ax4 = ax.ravel()

ax1.imshow(noisy_image, vmin=0, vmax=255, cmap=plt.cm.gray)
ax1.set_title('Noisy image')
ax1.axis('off')

ax2.imshow(median(noisy_image, disk(1)), vmin=0, vmax=255, cmap=plt.cm.gray)
ax2.set_title('Median $r=1$')
ax2.axis('off')

ax3.imshow(median(noisy_image, disk(5)), vmin=0, vmax=255, cmap=plt.cm.gray)
ax3.set_title('Median $r=5$')
ax3.axis('off')

ax4.imshow(median(noisy_image, disk(20)), vmin=0, vmax=255, cmap=plt.cm.gray)
ax4.set_title('Median $r=20$')
ax4.axis('off')

"""

.. image:: PLOT2RST.current_figure
Esempio n. 25
0
 def median_filter(img, radius=3):
     return median(img, selem=disk(radius=radius))
Esempio n. 26
0
def median_filter(image, radius):
    return median(image, selem=disk(radius))
Esempio n. 27
0
"""


from scipy import ndimage
import matplotlib.pyplot as plt

from skimage.morphology import watershed, disk
from skimage import data
from skimage.filter import rank
from skimage.util import img_as_ubyte


image = img_as_ubyte(data.camera())

# denoise image
denoised = rank.median(image, disk(2))

# find continuous region (low gradient) --> markers
markers = rank.gradient(denoised, disk(5)) < 10
markers = ndimage.label(markers)[0]

#local gradient
gradient = rank.gradient(denoised, disk(2))

# process the watershed
labels = watershed(gradient, markers)

# display results
fig, axes = plt.subplots(ncols=4, figsize=(8, 2.7))
fig,suptitle('George - Sementation (scikit-image)')
ax0, ax1, ax2, ax3 = axes
Esempio n. 28
0
def median_filter(image, radius):
    return median(image, selem=disk(radius))
Esempio n. 29
0
def processing(mc, radii=[[11, 11]*u.degree],
               clip_limit=None,
               histogram_clip=[0.0, 99.],
               func=np.sqrt,
               develop=False,
               verbose=True):
    """
    Image processing steps used to isolate the EUV wave from the data.  Use
    this part of AWARE to perform the image processing steps that segment
    propagating features that brighten new pixels as they propagate.

    Parameters
    ----------

    mc : sunpy.map.MapCube
    radii : list of lists. Each list contains a pair of numbers that describe the
    radius of the median filter and the closing operation
    histogram_clip
    func
    """

    # Define the disks that will be used on all the images.
    # The first disk in each pair is the disk that is used by the median
    # filter.  The second disk is used by the morphological closing
    # operation.
    disks = []
    for r in radii:
        e1 = (r[0]/mc[0].scale.x).to('pixel').value  # median ellipse width - across wavefront
        e2 = (r[0]/mc[0].scale.y).to('pixel').value  # median ellipse height - along wavefront

        e3 = (r[1]/mc[0].scale.x).to('pixel').value  # closing ellipse width - across wavefront
        e4 = (r[1]/mc[0].scale.y).to('pixel').value  # closing ellipse height - along wavefront

        disks.append([disk(e1), disk(e3)])

    # For the dump images
    rstring = ''
    for r in radii:
        z = '%i_%i__' % (r[0].value, r[1].value)
        rstring += z

    # Calculate the persistence
    new = mapcube_tools.persistence(mc)
    if develop:
        aware_utils.dump_images(new, rstring, '%s_1_persistence' % rstring)

    # Calculate the running difference
    new = mapcube_tools.running_difference(new)
    if develop:
        aware_utils.dump_images(new, rstring, '%s_2_rdiff' % rstring)

    # Storage for the processed mapcube.
    new_mc = []

    # Only want positive differences, so everything lower than zero
    # should be set to zero
    mc_data = func(new.as_array())
    mc_data[mc_data < 0.0] = 0.0

    # Clip the data to be within a range, and then normalize it.
    if clip_limit is None:
        cl = np.nanpercentile(mc_data, histogram_clip)
    mc_data[mc_data > cl[1]] = cl[1]
    mc_data = (mc_data - cl[0]) / (cl[1]-cl[0])

    # Get each map out of the cube an clean it up to better isolate the wave
    # front
    for im, m in enumerate(new):
        if verbose:
            print("  AWARE: processing map %i out of %i" % (im, len(new)))
        # Dump images - identities
        ident = (rstring, im)

        # Rescale the data using the input function, and subtract the lower
        # clip limit so it begins at zero.
        f_data = mc_data[:, :, im]

        # Replace the nans with zeros - the reason for doing this rather than
        # something more sophisticated is that nans will not contribute
        # greatly to the final answer.  The nans are put back in at the end
        # and get carried through in the maps.
        nans_here = np.logical_not(np.isfinite(f_data))
        nans_replaced = deepcopy(f_data)
        nans_replaced[nans_here] = 0.0

        # Byte scale the data - recommended input type for the median.
        new_data = bytescale(nans_replaced)
        if develop:
            aware_utils.dump_image(new_data, rstring, '%s_345_bytscale_%i_%05d.png' % (rstring, im, im))

        # Final image used to measure the location of the wave front
        final_image = np.zeros_like(new_data, dtype=np.float32)

        # Clean the data to isolate the wave front.
        for j, d in enumerate(disks):
            # Get rid of noise by applying the median filter.  Although the
            # input is a byte array make sure that the output is a floating
            # point array for use with the morphological closing operation.
            new_d = 1.0*median(new_data, d[0])
            if develop:
                aware_utils.dump_image(new_d, rstring, '%s_6_median_%i_%05d.png' % (rstring, radii[j][0].value, im))

            # Apply the morphological closing operation to rejoin separated
            # parts of the wave front.
            new_d = closing(new_d, d[1])
            if develop:
                aware_utils.dump_image(new_d, rstring, '%s_7_closing_%i_%05d.png' % (rstring, radii[j][1].value, im))

            # Further insurance that we get floating point arrays which are
            # summed below.
            final_image += new_d*1.0

        if develop:
            aware_utils.dump_image(final_image, rstring, '%s_final_%05d.png' % ident)

        # Put the NANs back in - useful to have them in.
        final_image[nans_here] = np.nan

        # New mapcube list
        new_mc.append(Map(ma.masked_array(final_image, mask=nans_here), m.meta))

    # Return the cleaned mapcube
    return Map(new_mc, cube=True)
Esempio n. 30
0
for i in range(0,experimentLength):
    frameCounts=[] # Estimate of number of objects detected per 'frame' (image taken this minute)
    for j in range(0,FPM): # Iterate over 5 images taken this minute
        if os.path.isfile(filePath+baseName+str(i).zfill(6)+'-'+str(j).zfill(2)+'.pgm'): # (Do nothing otherwise)
            loadedImage=True # Be optimistic
            try:
                image_gray= io.imread(filePath+baseName+str(i).zfill(6)+'-'+str(j).zfill(2)+'.pgm') # Load the image!
            except:
                loadedImage=False
                  
            if loadedImage: # (Do nothing otherwise)
                # We run a median filter over the image to eliminate both isolated 'hot' and 'cold' pixels,
                # due to the camera itself or random noise, without actually blurring the image. This adds
                # slightly unpredictable distortion to the image, but because we only consider a tiny local
                # neighborhood of each pixel, in practice it doesn't matter.
                image_gray=median(image_gray, disk(1))
                
                # The bilateral mean is effectively a selective Gaussian blur,
                # smoothing the image without mixing across edges of substantially
                # different structures.
                image_gray= rank.mean_bilateral(image_gray,selem=disk10, s0=5, s1=5)
                
                # Average and max brightness across entire image...
                imAvBrightness=np.mean(image_gray)
                imMaxBrightness=np.max(image_gray)
                            
                # Use DoG method to find 'blobs' (hypothetical cells in general size range of cells)
                # in image, using slightly dynamic thresholding based on overall image brightness
                blobs_dog = blob_dog(image_gray,min_sigma = 2, max_sigma=8, threshold=0.02+0.02*imMaxBrightness/255.0,overlap=0.9)
                if len(blobs_dog)>0: # Proceed with cell analysis if we see any blobs
                    blobs_dog[:,2]=blobs_dog[:,2]*sqrt(2) # Convert column to approximate radius of blob 
Esempio n. 31
0
def gen_mask(slide_ind=None,
             in_dir=None,
             stack=None,
             stain='nissl',
             out_dir=None,
             num_section_per_slide=5,
             mirror=None,
             rotate=None,
             show=False,
             resol='x0.3125'):

    scaling = 4**['x0.3125', 'x1.25', 'x5'].index(resol)

    # scaling = 1

    try:
        img_rgb = np.array(
            Image.open(
                os.path.join(in_dir, '%s_%02d_%s_z0.tif' %
                             (stack, slide_ind, resol))).convert('RGB'))
    except IOError as e:
        print e
        return

    img_gray = rgb2gray(img_rgb)

    if stain == 'nissl':
        imghsv = rgb2hsv(img_rgb)
        img = imghsv[..., 0]
    # imghue = imghsv[...,0]
    elif stain == 'fluorescent':
        img = img_gray

    # plt.imshow(imghsv[...,2], cmap=plt.cm.gray)
    # plt.colorbar()
    # plt.show()

    # plt.imshow(img_gray, cmap=plt.cm.gray)
    # plt.colorbar()
    # plt.show()

    # detect scanned zones on whole-slide images

    if stain == 'nissl':
        scanzone_mask = img_gray < 0.98
    elif stain == 'fluorescent':
        scanzone_mask = img_gray > 0.0001

    scanzone_mask = median(scanzone_mask.astype(np.float), disk(3 * scaling))
    scanzone_mask = remove_small_objects(scanzone_mask.astype(np.bool),
                                         min_size=50 * scaling,
                                         connectivity=2,
                                         in_place=False)
    # min_size=2000*scaling, connectivity=2, in_place=False)

    if show:
        plt.imshow(scanzone_mask, cmap=plt.cm.gray)
        plt.title('detect scanned zones')
        plt.show()

    column_labels, n_columns = label(scanzone_mask,
                                     neighbors=8,
                                     return_num=True,
                                     background=0)
    column_props = regionprops(column_labels + 1)

    print len(column_props), 'scanned zones detected'

    mask = np.zeros_like(img_gray, dtype=np.bool)

    section_masks_all = []
    section_bboxes_all = []
    centers_slide_all = []

    for i, cp in enumerate(column_props):

        minr, minc, maxr, maxc = cp.bbox

        sys.stderr.write('start slide %d column %d\n' % (slide_ind, i))

        section_masks, section_bboxes = foreground_mask_simple(img[minr:maxr,
                                                                   minc:maxc],
                                                               show=show,
                                                               scaling=scaling)

        sys.stderr.write('finish slide %d column %d\n' % (slide_ind, i))

        if len(section_masks) > 0:
            section_masks_all += section_masks
            bboxes_slide = section_bboxes + np.r_[minr, minc, minr,
                                                  minc][np.newaxis, :]

            section_bboxes_all += list(bboxes_slide)

            centers_slide = np.atleast_2d(
                np.column_stack([
                    bboxes_slide[:, [1, 3]].mean(axis=1),
                    bboxes_slide[:, [0, 2]].mean(axis=1)
                ]))

            centers_slide_all += list(centers_slide)

    numbering, n_slots = renumber_blobs(np.array(centers_slide_all), img.shape)

    # if len(section_bboxes_all) != len(numbering):
    # 	raise Exception('Error: number of section boxes does not match length of numbering: slide %d' % (slide_ind))

    if len(section_bboxes_all) != len(numbering):
        print 'Error: number of section boxes does not match length of numbering: slide %d' % (
            slide_ind)
        return []

    for (minr, minc, maxr,
         maxc), mask, section_id in zip(section_bboxes_all, section_masks_all,
                                        numbering):

        img = img_rgb[minr:maxr, minc:maxc]

        masked_img = np.dstack([img, img_as_ubyte(mask)])

        if show:
            plt.imshow(masked_img)
            plt.title(section_id)
            plt.show()

        if out_dir is not None:
            image_filepath = os.path.join(
                out_dir,
                '%s_%s_%02d_%d.tif' % (stack, resol, slide_ind, section_id))
            imsave(image_filepath, masked_img)

    filled_bboxes = -1 * np.ones((n_slots, 4), dtype=np.int)
    filled_bboxes[numbering, :] = section_bboxes_all

    return filled_bboxes
Esempio n. 32
0
    there are different implementations of median filter :
    `skimage.filter.median_filter` and `skimage.filter.rank.median`

"""

noise = np.random.random(ima.shape)
nima = data.camera()
nima[noise > 0.99] = 255
nima[noise < 0.01] = 0

from skimage.filter.rank import median
from skimage.morphology import disk

fig = plt.figure(figsize=[10, 7])

lo = median(nima, disk(1))
hi = median(nima, disk(5))
ext = median(nima, disk(20))
plt.subplot(2, 2, 1)
plt.imshow(nima, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.xlabel('noised image')
plt.subplot(2, 2, 2)
plt.imshow(lo, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.xlabel('median $r=1$')
plt.subplot(2, 2, 3)
plt.imshow(hi, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.xlabel('median $r=5$')
plt.subplot(2, 2, 4)
plt.imshow(ext, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.xlabel('median $r=20$')
"""
Esempio n. 33
0
from skimage.morphology import disk

noise = np.random.random(noisy_image.shape)
noisy_image = img_as_ubyte(data.camera())
noisy_image[noise > 0.99] = 255
noisy_image[noise < 0.01] = 0

fig = plt.figure(figsize=(10, 7))

plt.subplot(2, 2, 1)
plt.imshow(noisy_image, vmin=0, vmax=255)
plt.title('Noisy image')
plt.axis('off')

plt.subplot(2, 2, 2)
plt.imshow(median(noisy_image, disk(1)), vmin=0, vmax=255)
plt.title('Median $r=1$')
plt.axis('off')

plt.subplot(2, 2, 3)
plt.imshow(median(noisy_image, disk(5)), vmin=0, vmax=255)
plt.title('Median $r=5$')
plt.axis('off')

plt.subplot(2, 2, 4)
plt.imshow(median(noisy_image, disk(20)), vmin=0, vmax=255)
plt.title('Median $r=20$')
plt.axis('off')
"""

.. image:: PLOT2RST.current_figure
from skimage.morphology import disk

noise = np.random.random(noisy_image.shape)
noisy_image = img_as_ubyte(data.camera())
noisy_image[noise > 0.99] = 255
noisy_image[noise < 0.01] = 0

fig = plt.figure(figsize=(10, 7))

plt.subplot(2, 2, 1)
plt.imshow(noisy_image, vmin=0, vmax=255)
plt.title('Noisy image')
plt.axis('off')

plt.subplot(2, 2, 2)
plt.imshow(median(noisy_image, disk(1)), vmin=0, vmax=255)
plt.title('Median $r=1$')
plt.axis('off')

plt.subplot(2, 2, 3)
plt.imshow(median(noisy_image, disk(5)), vmin=0, vmax=255)
plt.title('Median $r=5$')
plt.axis('off')

plt.subplot(2, 2, 4)
plt.imshow(median(noisy_image, disk(20)), vmin=0, vmax=255)
plt.title('Median $r=20$')
plt.axis('off')

"""
Esempio n. 35
0
def gen_mask(slide_ind=None,
             in_dir=None,
             stack=None,
             use_hsv=True,
             out_dir=None,
             num_section_per_slide=5,
             mirror=None,
             rotate=None):

    imgcolor = Image.open(
        os.path.join(in_dir, '%s_%02d_x0.3125_z0.tif' % (stack, slide_ind)))
    # imgcolor = Image.open(os.path.join(in_dir, '%s_%02d_x5_z0.tif'%(stack, slide_ind)))
    imgcolor = imgcolor.convert('RGB')
    imgcolor = np.array(imgcolor)
    img_gray = rgb2gray(imgcolor)
    # slide_height, slide_width = img_gray.shape[:2]

    if use_hsv:
        imghsv = rgb2hsv(imgcolor)
        img = imghsv[..., 1]
    else:
        img = img_gray

    # utilize bounding box selected during scanning process
    a = img_gray < 0.98

    a = median(a.astype(np.float), disk(3))
    a = remove_small_objects(a.astype(np.bool),
                             min_size=50,
                             connectivity=2,
                             in_place=False)
    column_labels, n_columns = label(a,
                                     neighbors=8,
                                     return_num=True,
                                     background=0)
    column_props = regionprops(column_labels + 1)
    column_bboxes = np.array([p.bbox for p in column_props])
    indices = np.argsort(column_bboxes[:, 1])

    mask = np.zeros_like(img_gray, dtype=np.bool)
    for i, box in enumerate(column_bboxes[indices, :]):
        minr, minc, maxr, maxc = box

        # touch_top
        t = 0 if np.std(img[minr, minc:maxc + 1]) > .06 else 5
        # touch_bottom
        b = 0 if np.std(img[maxr - 1, minc:maxc + 1]) > .06 else 5
        # touch_left
        l = 0 if np.std(img[minr:maxr + 1, minc]) > .06 else 5
        # touch_right
        r = 0 if np.std(img[minr:maxr + 1, maxc - 1]) > .06 else 5

        mask[minr + t:maxr - b, minc + l:maxc - r] = 1

    section_masks, bboxes, exists = foreground_mask_morphsnakes_slide(
        img,
        levelset=mask,
        num_section_per_slide=num_section_per_slide,
        min_iters=400,
        dilate_iters=1)

    ids = np.where(exists)[0]
    #     section_images = []
    for _, ((minr, minc, maxr, maxc), mask,
            section_id) in enumerate(zip(bboxes, section_masks, ids)):
        img = imgcolor[minr:maxr + 1, minc:maxc + 1].copy()
        mask = 255 * mask.astype(np.uint8)
        masked_img = np.dstack([img, mask])

        # img[~mask] = 0
        #         section_images.append(img)

        image_filepath = os.path.join(
            out_dir, '%s_x0.3125_%02d_%d.tif' % (stack, slide_ind, section_id))
        # image_filepath = os.path.join(out_dir, '%s_x5_%02d_%d.tif'%(stack, slide_ind, section_id))
        imsave(image_filepath, masked_img)

    # bboxes_percent = np.array(bboxes, dtype=np.float)/np.r_[img_gray.shape[:2], img_gray.shape[:2]][np.newaxis, :]

    filled_bboxes = -1 * np.ones((len(exists), 4), dtype=np.int)
    filled_bboxes[ids, :] = bboxes

    return filled_bboxes
def preprocessing(image, smooth_size, folder):
    """
    'The image low contrast and under segmentation
    problem is not yet addressed by most of the researchers'
    
    'Other researchers also proposed different method to
    remedy the problem of watershed.  Li, El-
    moataz, Fadili, and Ruan, S. (2003) proposed an improved
    image segmentation approach based 
    on level set and mathematical morphology'
    
    THE SPHERES MUST BE ALMOST ALONG THE SAME PLANES IN Z DIRECTION
    IF THEY ARE TOUCHING AND OVERLAP, WHILE BEING ALMOST MERGED
    IT IS IMPOSSIBLE TO RESOLVE THEM
    
    ONE IDEA MIGHT BE TO DETECT CENTRES ALONG ONE AXIS AND THEN ANOTHER
    AFTER ALL THE CENTRES WERE FOUND COMBINE THEM SOMEHOW... 
    """
    from skimage.restoration import denoise_tv_chambolle

    dim = int(image.shape[0] / 50.)
    smoothed = rank.median(image, disk(smooth_size))
    #smoothed = denoise_tv_chambolle(image, weight=0.002)
    smoothed = rank.enhance_contrast(smoothed, disk(smooth_size))

    pl.subplot(2, 3, 1)
    pl.title("after median")
    pl.imshow(smoothed)
    pl.gray()
    # If after smoothing the "dot" disappears
    # use the image value

    # TODO: wat do with thresh?
    try:
        im_max = smoothed.max()
        thresh = threshold_otsu(image)
    except:
        im_max = image.max()
        thresh = threshold_otsu(image)

    if im_max < thresh:
        labeled = np.zeros(smoothed.shape, dtype=np.int32)

    else:
        binary = smoothed > thresh

        # TODO: this array size is the fault of errors
        bin_open = binary_opening(binary, np.ones((dim, dim)), iterations=5)
        bin_close = binary_closing(bin_open, np.ones((5, 5)), iterations=5)

        pl.subplot(2, 3, 2)
        pl.title("threshold")
        pl.imshow(binary, interpolation='nearest')
        pl.subplot(2, 3, 3)
        pl.title("opening")
        pl.imshow(bin_open, interpolation='nearest')
        pl.subplot(2, 3, 4)
        pl.title("closing")
        pl.imshow(bin_close, interpolation='nearest')

        distance = ndimage.distance_transform_edt(bin_open)
        local_maxi = peak_local_max(distance, indices=False, labels=bin_open)

        markers = ndimage.label(local_maxi)[0]

        labeled = watershed(-distance, markers, mask=bin_open)
        pl.subplot(2, 3, 5)
        pl.title("label")
        pl.imshow(labeled)
        #pl.show()
        pl.savefig(folder)
        pl.close('all')

        #misc.imsave(folder, labeled)


#         labels_rw = random_walker(bin_close, markers, mode='cg_mg')
#
#         pl.imshow(labels_rw, interpolation='nearest')
#         pl.show()

    return labeled
Esempio n. 37
0
def recognize(img, digit_templates):
	
	# table selection

	table_coord = table(img)
	img = img[img.shape[0]*0.02:img.shape[0]*0.95, \
	img.shape[1]*0.05:img.shape[1]*0.95]
	img = img[table_coord[1]:table_coord[3], \
	table_coord[0]:table_coord[2]]

	# end of table selection

	st_img = img.copy()

	# image processing

	img = rescale_intensity(img)
	img = rank.median(img, disk(2))

	img = ssr(img, 9)
	img = unsharp(img, 3)

	# end of image processing

	result = giveNumbers(img, digit_templates, 0, 0)


	# numbers very near: without median(else connect) 
	# and low sigma in gaussian in ssr(else connect too)

	if result == (-1, -1, -1):
		img = st_img.copy()
		img = rescale_intensity(img)
		img = ssr(img, 3)
		img = unsharp(img, 3)
		imsave('tmp.bmp', img)
		result = giveNumbers(img, digit_templates, 0, 0)
	

	# very bright picture: need closing in giveNumbers

	if result == (-1, -1, -1):
		img = st_img.copy()
		img = rescale_intensity(img)
		img = rank.median(img, disk(2))
		img = ssr(img, 9)
		img = unsharp(img, 3)
		result = giveNumbers(img, digit_templates, 1, 0)
	

	# and play with threshold of binarization

	delta = 0
	bool = -1
	while result == (-1, -1, -1) and abs(delta) <= (240):
		delta *= -1
		if bool == -1:
			delta -= 5
		bool *= -1
		img = st_img.copy()
		img = rescale_intensity(img)
		img = rank.median(img, disk(2))
		img = ssr(img, 9)
		img = unsharp(img, 3)
		result = giveNumbers(img, digit_templates, 0, delta)

	if result == (-1, -1, -1):
		result = (0, 0, 0)


	return result
Esempio n. 38
0
def sharpness(image, edge_threshold=0.0001, w=5, t=1.0):
    """
    Code implemented by the following article:
    Sharpness Estimation for Document and Scene Images,
    Kumar, Chen, Doermann
    """
    # Median filtering
    w_size = (w * 2) + 1
    image = util.img_as_float(image)
    image_m = util.img_as_float(median(image, mph.square(3)))

    # Window functions
    def dom_func(window):
        # import pdb; pdb.set_trace()
        return abs(
            abs(window[4] - window[2]) - abs(window[2] - window[0])
        )

    def contrast_func(window):
        # print window
        s = 0.0
        for i in xrange(0, len(window) - 1):
            # print i
            s += abs(window[i] - window[i+1])
        return s

    # Delta DoM in horizontal direction
    dom_x_values = generic_filter(image_m, dom_func,
        size=(1, 5),
        mode='reflect')
    # Delta DoM in vertical direction
    dom_y_values = generic_filter(image_m, dom_func,
        size=(5, 1),
        mode='reflect')

    dom_x = generic_filter(
        dom_x_values, lambda w: sum(w),
        size=(1, w_size), mode='reflect')
    dom_y = generic_filter(
        dom_y_values, lambda w: sum(w),
        size=(w_size, 1), mode='reflect')


    edges_x = vsobel(image)
    # Normalize
    edges_x *= (1.0 / edges_x.max())
    edges_x_pixels = len(edges_x[edges_x > edge_threshold].ravel())

    edges_y = hsobel(image)
    # Normalize
    edges_y *= (1.0 / edges_y.max())
    edges_y_pixels = len(edges_y[edges_y > edge_threshold].ravel())

    # Contrast in horizontal direction
    contrast_x = generic_filter(image, contrast_func,
        size=(1, w_size + 1),
        mode='reflect')
    # Contrast in vertical direction
    contrast_y = generic_filter(image, contrast_func,
        size=(w_size + 1, 1),
        mode='reflect')

    sharpness_x = dom_x / contrast_x
    sharpness_y = dom_y / contrast_y

    # import pdb; pdb.set_trace()

    sharp_x_pixels = len(np.where(
        sharpness_x[edges_x > edge_threshold] > t
    )[0])
    sharp_y_pixels = len(np.where(
        sharpness_y[edges_y > edge_threshold] > t
    )[0])

    # import pdb; pdb.set_trace()

    if edges_x_pixels > 0:
        rx = (float(sharp_x_pixels) / edges_x_pixels)
    else:
        rx = 1

    if edges_y_pixels > 0:
        ry = (float(sharp_y_pixels) / edges_y_pixels)
    else:
        ry = 1

    final_estimate = np.sqrt(
        (rx ** 2) + (ry ** 2)
    )    
    return final_estimate
Esempio n. 39
0
def table(img):

	img = rescale_intensity(img)
	img = ssr(img, 3)
	img = rank.median(img, disk(2))
	img = img[img.shape[0]*0.02:img.shape[0]*0.95, img.shape[1]*0.05:img.shape[1]*0.95]
	height = img.shape[0]
	width = img.shape[1]

	img = (img < threshold_otsu(img[height/5:height/5*4, width/5:width/5*4], nbins = 256))

	# Horizontal

	whiteList = []

	for i in range((height/2), height-4):
		img_lbl = label(img[i:i+4, 0:width], 4, background = 255)
		props = regionprops(img_lbl)
		whiteList.append(max_area(props))

	max_wh = max(whiteList)
	bool = 0

	for i in arange(len(whiteList))[::-1]:
		if (whiteList[i] > (0.95*max_wh)) and (bool == 0):
			h_top = i
			bool = 1

	whiteList = []

	for i in  arange(4, height/2)[::-1]:
		img_lbl = label(img[i-4:i, 0:width], 4, background = 255)
		props = regionprops(img_lbl)
		whiteList.append(max_area(props))

	max_wh = max(whiteList)
	bool = 0

	for i in arange(len(whiteList))[::-1]:
		if (whiteList[i] > (0.95*max_wh)) and (bool == 0):
			h_bot = i
			bool = 1

	img = img[(height/2) - h_bot:(height/2) + h_top,] * 255
	x_min = (height/2) - h_bot
	x_max = (height/2) + h_top

	# Vertical

	whiteList = []

	for i in range((width/2), width):
		whiteList.append(img[height/4:height/4*3, i].sum())

	max_wh = max(whiteList)
	bool = 0

	for i in arange(len(whiteList))[::-1]:
		if (whiteList[i] > (0.95*max_wh)) and (bool == 0):
			v_top = i
			bool = 1

	whiteList = []

	for i in arange(width/2)[::-1]:
		whiteList.append(img[height/4:height/4*3,i].sum())

	max_wh = max(whiteList)
	bool = 0

	for i in arange(len(whiteList))[::-1]:
		if (whiteList[i] > (0.95*max_wh)) and (bool == 0):
			v_bot = i
			bool = 1

	y_min = (width/2) - v_bot
	y_max = (width/2) + v_top

	if float(v_top + v_bot)/height < 1.5:
		y_min = 0
		y_max = width

	return (y_min, x_min, y_max, x_max)
Esempio n. 40
0
from skimage.morphology import disk, closing, watershed, remove_small_objects, disk, label, convex_hull_image, binary_dilation
from skimage.morphology import octagon, opening, diamond, reconstruction
from skimage.feature import peak_local_max
from skimage.transform import hough_ellipse
from skimage.filter import threshold_yen, rank
from skimage.measure import regionprops
from skimage.draw import circle_perimeter
####################################################

#################################### Distance Image - Internal Marker Locus
image = io.imread("/Users/rishabgargeya/Desktop/database/DRIVE/training/images/22_training.tif")
plt.imshow(image)

gray_image = rgb2gray(image)
io.imshow(gray_image)
med = median(gray_image, disk(3))
io.imshow(med)
clahe_image = exposure.equalize_adapthist(med)
io.imshow(clahe_image)
clahe_image = img_as_ubyte(clahe_image)
binary = clahe_image > threshold_yen(clahe_image)
io.imshow(binary)

binary_label = label(binary)
rps = regionprops(binary_label)[1:] #regionprops, ignore background
areas = [rp.area for rp in rps]
region_with_largest_area = np.argmax(areas)

binary = binary_label == rps[region_with_largest_area].label
#binary = convex_hull_image(binary)
#binary = binary_dilation(binary, disk(15)) != 0
    there are different implementations of median filter :
    `skimage.filter.median_filter` and `skimage.filter.rank.median`

"""

noise = np.random.random(ima.shape)
nima = data.camera()
nima[noise > 0.99] = 255
nima[noise < 0.01] = 0

from skimage.filter.rank import median
from skimage.morphology import disk

fig = plt.figure(figsize=[10, 7])

lo = median(nima, disk(1))
hi = median(nima, disk(5))
ext = median(nima, disk(20))
plt.subplot(2, 2, 1)
plt.imshow(nima, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.xlabel('noised image')
plt.subplot(2, 2, 2)
plt.imshow(lo, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.xlabel('median $r=1$')
plt.subplot(2, 2, 3)
plt.imshow(hi, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.xlabel('median $r=5$')
plt.subplot(2, 2, 4)
plt.imshow(ext, cmap=plt.cm.gray, vmin=0, vmax=255)
plt.xlabel('median $r=20$')
Esempio n. 42
0
    def process(self):
        if verbose:
            print "Opening", self.input_file

        ds = gdal.Open(self.input_file)
        if ds is None:
            print('ERROR: file no data:')
            sys.exit(-1)

        RasterXSize = ds.RasterXSize
        RasterYSize = ds.RasterYSize
        RasterCount = ds.RasterCount

        projection = ds.GetProjection()
        geotransform = ds.GetGeoTransform()

        xorg = geotransform[0]
        yorg = geotransform[3]

        xres = geotransform[1]
        yres = geotransform[5]

        xmax = xorg + geotransform[1] * RasterXSize
        ymax = yorg + geotransform[5] * RasterYSize

        if verbose:
            print "size", RasterXSize, RasterYSize, RasterCount
            print "xorg", xorg, "yorg", yorg
            print "xmax", xmax, "ymax", ymax

        print "Read red"
        rb = ds.GetRasterBand(1)
        red = (rb.ReadAsArray(0, 0, ds.RasterXSize,
                              ds.RasterYSize)).astype(float)

        print "Read green"
        gb = ds.GetRasterBand(2)
        green = (gb.ReadAsArray(0, 0, ds.RasterXSize,
                                ds.RasterYSize)).astype(float)

        print "Read blue"
        bb = ds.GetRasterBand(3)
        blue = (bb.ReadAsArray(0, 0, ds.RasterXSize,
                               ds.RasterYSize)).astype(float)

        data = self.gray1(red, green, blue, RasterXSize, RasterYSize)
        #data 	= self.rgb2gray(red, green, blue, RasterXSize, RasterYSize)

        #data 		= equalize_hist(data)
        #data *= 255.0

        mean = numpy.mean(data)
        minimum = numpy.min(data)
        maximum = numpy.max(data)

        #threshold 	= mean
        print minimum, mean, maximum

        #threshold = mean

        #data = equalize_hist(data)
        #data *= 255
        #print numpy.min(data), numpy.mean(data), numpy.max(data)

        #data[data>threshold] = 0

        thresh2 = threshold_otsu(data, nbins=100)
        print "otsu threshold", thresh2
        data[data > thresh2] = 0

        data[data > 0] = 1

        print "de-speckle..."
        data = median(data.astype(numpy.uint8), disk(3))
        print "Write data..."

        data[data > 0] = 255

        driver = gdal.GetDriverByName("GTiff")
        dst_ds = driver.Create(self.output_file, RasterXSize, RasterYSize, 1,
                               gdal.GDT_Byte,
                               ['INTERLEAVE=PIXEL', 'COMPRESS=DEFLATE'])
        band = dst_ds.GetRasterBand(1)
        band.WriteArray(data.astype(int), 0, 0)
        band.SetNoDataValue(0)

        dst_ds = None
        ds = None

        # Convert to BMP
        cmd = "gdal_translate -q  -scale 0 255 0 65535 " + self.output_file + " -of BMP  " + self.bmp_file
        self.execute(cmd)
        sys.exit(0)

        #if force or not os.path.exists(self.geojson_file):
        cmd = str.format(
            "potrace -z black -a 1.5 -t 2 -i -b geojson -o {0} {1} -x {2}x{3} -L {4} -B {5} ",
            self.geojson_file, self.bmp_file, xres, -yres, xorg, ymax)
        self.execute(cmd)

        # We need to add a water property
        # This may or may not be necessary based on styling or multi color requirements
        #cmd = str.format("node add_geojson_props.js {0}", self.geojson_file)
        #self.execute(cmd)

        # Convert to Topojson
        cmd = str.format(
            "topojson -o {0} --simplify-proportion 0.5 -- surface_water={1}",
            self.topojson_file, self.geojson_file)
        self.execute(cmd)

        # Convert back to geojson now that it has been simplified
        cmd = str.format("topojson-geojson --precision 5 -o {0} {1}",
                         self.outpath, self.topojson_file)
        self.execute(cmd)

        # convert it to OSM to visualize in JOSM and update reference water
        data_source = "WorldView-2"
        cmd = str.format("node geojson2osm {0} {1}", self.surface_water_json,
                         data_source)
        self.execute(cmd)

        # gzip it
        cmd = str.format("gzip {0} ", self.topojson_file)
        self.execute(cmd)

        # compress it
        cmd = "bzip2 " + self.surface_water_osm
        self.execute(cmd)

        cmd = str.format("gzip {0} ", self.surface_water_json)
        self.execute(cmd)
def cr_med(image, selem):
    return median(image=image, selem=selem)
Esempio n. 44
0
        (stack, stack, slide_ind))
    # imgcolor = Image.open("/home/yuncong/DavidData2015slides/%s/x5/%s_%02d_x5_z0.tif" % (stack, stack, slide_ind))
    imgcolor = imgcolor.convert('RGB')
    imgcolor = np.array(imgcolor)
    img_gray = rgb2gray(imgcolor)
    slide_height, slide_width = img_gray.shape[:2]

    if use_hsv:
        imghsv = rgb2hsv(imgcolor)
        img = imghsv[..., 1]
    else:
        img = img_gray

    # utilize bounding box selected during scanning process
    a = img_gray < 0.98
    a = median(a.astype(np.float), disk(3))
    a = remove_small_objects(a.astype(np.bool),
                             min_size=50,
                             connectivity=2,
                             in_place=False)
    column_labels, n_columns = label(a,
                                     neighbors=8,
                                     return_num=True,
                                     background=0)
    print n_columns, 'columns detected'
    column_props = regionprops(column_labels + 1)
    column_bboxes = np.array([p.bbox for p in column_props])
    indices = np.argsort(column_bboxes[:, 1])

    mask = np.zeros_like(img_gray, dtype=np.bool)
    for i, box in enumerate(column_bboxes[indices, :]):
Esempio n. 45
0
#Transformation en image en niveau de gris

polDia_g = skimage.color.rgb2gray(polDia)

polDia_g = polDia_g * laser_copy

plt.subplot(3,3,2)
plt.title("Image en gris")
plt.imshow(polDia_g,cmap = plt.cm.gray)
plt.colorbar()


# applique un filtre median sur l'image en niveau de gris, ceci afin
#de flouter et lisser les bords

polDia_med = rank2.median((polDia_g*255).astype('uint8'),disk(8))

plt.subplot(3,3,3)
plt.title("Image apres filtre median")
plt.imshow(polDia_med,cmap = plt.cm.gray)
plt.colorbar()
type(polDia_med)


# applique une normalisation adaptive a contraste limitee de l'histogramme
# (CLAHE)
polDia_clahe = exposure.equalize_adapthist(polDia_med, clip_limit=0.03)
polDia_clahe_stretch = exposure.rescale_intensity(polDia_clahe, out_range=(0, 256))
#revient a faire polDia_clahe multiplie par 256

plt.subplot(3,3,4)
Esempio n. 46
0
from skimage.filter.rank import median
from skimage.morphology import disk

noise = np.random.random(noisy_image.shape)
noisy_image = img_as_ubyte(data.camera())
noisy_image[noise > 0.99] = 255
noisy_image[noise < 0.01] = 0

fig, ax = plt.subplots(2, 2, figsize=(10, 7))
ax1, ax2, ax3, ax4 = ax.ravel()

ax1.imshow(noisy_image, vmin=0, vmax=255, cmap=plt.cm.gray)
ax1.set_title('Noisy image')
ax1.axis('off')

ax2.imshow(median(noisy_image, disk(1)), vmin=0, vmax=255, cmap=plt.cm.gray)
ax2.set_title('Median $r=1$')
ax2.axis('off')

ax3.imshow(median(noisy_image, disk(5)), vmin=0, vmax=255, cmap=plt.cm.gray)
ax3.set_title('Median $r=5$')
ax3.axis('off')

ax4.imshow(median(noisy_image, disk(20)), vmin=0, vmax=255, cmap=plt.cm.gray)
ax4.set_title('Median $r=20$')
ax4.axis('off')
"""

.. image:: PLOT2RST.current_figure

The added noise is efficiently removed, as the image defaults are small (1
.. _Wikipedia: http://en.wikipedia.org/wiki/Watershed_(image_processing)

"""

from scipy import ndimage
import matplotlib.pyplot as plt
from skimage.morphology import watershed, disk
from skimage import data

# original data
from skimage.filter import rank

image = data.camera()

# denoise image
denoised = rank.median(image, disk(2))

# find continuous region (low gradient) --> markers
markers = rank.gradient(denoised, disk(5)) < 10
markers = ndimage.label(markers)[0]

#local gradient
gradient = rank.gradient(denoised, disk(2))

# process the watershed
labels = watershed(gradient, markers)

# display results
fig, axes = plt.subplots(ncols=4, figsize=(8, 2.7))
ax0, ax1, ax2, ax3 = axes
    def process(self):
        if verbose:
            print "Opening", self.input_file

        ds = gdal.Open(self.input_file)
        if ds is None:
            print('ERROR: file no data:')
            sys.exit(-1)

        RasterXSize = ds.RasterXSize
        RasterYSize = ds.RasterYSize
        RasterCount = ds.RasterCount

        projection = ds.GetProjection()
        geotransform = ds.GetGeoTransform()

        xorg = geotransform[0]
        yorg = geotransform[3]

        xres = geotransform[1]
        yres = geotransform[5]

        xmax = xorg + geotransform[1] * RasterXSize
        ymax = yorg + geotransform[5] * RasterYSize

        if verbose:
            print "size", RasterXSize, RasterYSize, RasterCount
            print "xorg", xorg, "yorg", yorg
            print "xmax", xmax, "ymax", ymax

        band = ds.GetRasterBand(1)
        data = band.ReadAsArray(0, 0, ds.RasterXSize, ds.RasterYSize)

        mean = numpy.mean(data)
        minimum = numpy.min(data)

        #threshold 	= (mean+minimum)/2
        threshold = mean

        print minimum, mean, threshold

        data[data > threshold] = 0

        thresh2 = threshold_otsu(data, nbins=100)
        print "otsu threshold", thresh2

        data[data > thresh2] = 0

        #data = denoise_bilateral(data, sigma_range=0.05, sigma_spatial=15)
        #data = binary_opening(data, disk(3))
        data = median(data, disk(3))

        data[data > 0] = 255

        # Compute the Canny filter for two values of sigma
        #edges = filter.canny(data, sigma=3)

        driver = gdal.GetDriverByName("GTiff")
        dst_ds = driver.Create(self.output_file, RasterXSize, RasterYSize, 1,
                               gdal.GDT_Byte,
                               ['INTERLEAVE=PIXEL', 'COMPRESS=DEFLATE'])
        band = dst_ds.GetRasterBand(1)
        band.WriteArray(data, 0, 0)
        band.SetNoDataValue(0)

        if verbose:
            print "Written", self.output_file

        dst_ds = None
        ds = None

        # Convert to PGM
        #cmd = "gdal_translate -q  -scale 0 255 0 65535 " + self.output_file + " -of PNM  "+self.pgm_file
        cmd = "gdal_translate -q  -scale 0 255 0 65535 " + self.output_file + " -of BMP  " + self.bmp_file
        self.execute(cmd)

        #if force or not os.path.exists(self.geojson_file):
        cmd = str.format(
            "potrace -z black -a 1.5 -t 2 -i -b geojson -o {0} {1} -x {2}x{3} -L {4} -B {5} ",
            self.geojson_file, self.bmp_file, xres, -yres, xorg, ymax)
        self.execute(cmd)

        # We need to add a water property
        # This may or may not be nexessary based on styling or multi color requirements
        #cmd = str.format("node add_geojson_props.js {0}", self.geojson_file)
        #self.execute(cmd)

        # Convert to Topojson
        cmd = str.format(
            "topojson -o {0} --simplify-proportion 0.5 -- surface_water={1}",
            self.topojson_file, self.geojson_file)
        self.execute(cmd)

        # Convert back to geojson now that it has been simplified
        cmd = str.format("topojson-geojson --precision 5 -o {0} {1}",
                         self.outpath, self.topojson_file)
        self.execute(cmd)

        # convert it to OSM to visualize in JOSM and update reference water
        cmd = str.format("node geojson2osm {0}", self.surface_water_json)
        self.execute(cmd)

        # gzip it
        cmd = str.format("gzip {0} ", self.topojson_file)
        self.execute(cmd)

        # compress it
        cmd = "bzip2 " + self.surface_water_osm
        self.execute(cmd)

        cmd = str.format("gzip {0} ", self.surface_water_json)
        self.execute(cmd)
Esempio n. 49
0
from scipy import ndimage as ndi
import matplotlib.pyplot as plt

from skimage import io
from skimage.morphology import watershed, disk
from skimage import data
from skimage.filter import rank
import cv2

image = io.imread('cerebro.jpg')

# makes the image grey colormap
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# denoise image
denoised = rank.median(gray, disk(1))

# create the sheds
markers = rank.gradient(denoised, disk(1)) < 15
markers = ndi.label(markers)[0]

# process the watershed
labels = watershed(gray, markers)

# display results
fig, axes = plt.subplots(nrows=2,
                         ncols=2,
                         figsize=(8, 8),
                         sharex=True,
                         sharey=True)
ax = axes.ravel()
Esempio n. 50
0
def cr_med(image, selem):
    return median(image=image, selem=selem)
Esempio n. 51
0
 def median_filter(img, radius=3):
     return median(img, selem=disk(radius=radius))