Ejemplo n.º 1
0
def label_image(image):
    
    ROI = np.zeros((470,400,3), dtype=np.uint8)
    for c in range(3):
        for i in range(50,520):
            for j in range(240,640):
                ROI[i-50,j-240,c] = image[i,j,c]

    
    gray_ROI = cv2.cvtColor(ROI,cv2.COLOR_BGR2GRAY)
    
    ROI_flou = cv2.medianBlur((ROI).astype('uint8'),3)
    
    Laser = Detecte_laser.Detect_laser(ROI_flou)
    
    open_laser = cv2.morphologyEx(Laser, cv2.MORPH_DILATE, disk(3))
    
    skel = skeletonize(open_laser > 0)
    
    tranche = Detecte_laser.tranche(skel,90,30)    
    
    ret, thresh = cv2.threshold(gray_ROI*tranche.astype('uint8'),0,1,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    thresh01 = thresh<1.0
    
    open_thresh = cv2.morphologyEx(thresh01.astype('uint8'), cv2.MORPH_OPEN, disk(10))
    
    labelised = (label(open_thresh,8,0))+1
    
    return gray_ROI,labelised
    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
Ejemplo n.º 3
0
def test_compare_8bit_vs_16bit():
    # filters applied on 8-bit image ore 16-bit image (having only real 8-bit
    # of dynamic) should be identical

    image8 = util.img_as_ubyte(data.camera())
    image16 = image8.astype(np.uint16)
    assert_equal(image8, image16)

    methods = [
        "autolevel",
        "bottomhat",
        "equalize",
        "gradient",
        "maximum",
        "mean",
        "subtract_mean",
        "median",
        "minimum",
        "modal",
        "enhance_contrast",
        "pop",
        "threshold",
        "tophat",
    ]

    for method in methods:
        func = getattr(rank, method)
        f8 = func(image8, disk(3))
        f16 = func(image16, disk(3))
        assert_equal(f8, f16)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def find_grains(input_file, output_dir=None):
    """Return tuple of segmentaitons (grains, difficult_regions)."""
    name = fpath2name(input_file)
    name = "grains-" + name + ".png"
    if output_dir:
        name = os.path.join(output_dir, name)

    image = Image.from_file(input_file)
    intensity = mean_intensity_projection(image)
    image = remove_scalebar(intensity, np.mean(intensity))
    image = threshold_abs(image, 75)
    image = invert(image)
    image = fill_holes(image, min_size=500)
    image = erode_binary(image, selem=disk(4))
    image = remove_small_objects(image, min_size=500)
    image = dilate_binary(image, selem=disk(4))

    dist = distance(image)
    seeds = local_maxima(dist)
    seeds = dilate_binary(seeds)  # Merge spurious double peaks.
    seeds = connected_components(seeds, background=0)

    segmentation = watershed_with_seeds(dist, seeds=seeds, mask=image)
    # Need a copy to avoid over-writing original.
    initial_segmentation = np.copy(segmentation)

    # Remove spurious blobs.
    segmentation = remove_large_segments(segmentation, max_size=3000)
    segmentation = remove_small_segments(segmentation, min_size=500)
    props = skimage.measure.regionprops(segmentation)
    segmentation = remove_non_round(segmentation, props, 0.6)

    difficult = initial_segmentation - segmentation

    return segmentation, difficult
def find_grains(input_file, output_dir=None):
    """Return tuple of segmentaitons (grains, difficult_regions)."""
    name = fpath2name(input_file)
    name = "grains-" + name + ".png"
    if output_dir:
        name = os.path.join(output_dir, name)

    image = Image.from_file(input_file)
    intensity = mean_intensity_projection(image)

# Median filter seems more robust than Otsu.
#   image = threshold_otsu(intensity)
    image = threshold_median(intensity, scale=0.8)

    image = invert(image)
    image = erode_binary(image, selem=disk(2))
    image = dilate_binary(image, selem=disk(2))
    image = remove_small_objects(image, min_size=200)
    image = fill_holes(image, min_size=50)

    dist = distance(image)
    seeds = local_maxima(dist)
    seeds = dilate_binary(seeds)  # Merge spurious double peaks.
    seeds = connected_components(seeds, background=0)

    segmentation = watershed_with_seeds(dist, seeds=seeds, mask=image)

    # Remove spurious blobs.
    segmentation = remove_large_segments(segmentation, max_size=3000)
    segmentation = remove_small_segments(segmentation, min_size=100)

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

		######## randomly add samples

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

		
		#random morphological operation
		r_for_mf_1 = random()
		if 0.05 < r_for_mf_1 < 0.25: # small vessel
			selem1 = disk(0.5+r_for_mf_1)
			cur_img = dilation(cur_img,selem1)
			cur_img = erosion(cur_img,selem1)
		elif 0.25 < r_for_mf_1 < 0.5: # large vessel
			selem2 = disk(2.5+r_for_mf_1*3)
			cur_img = dilation(cur_img,selem2)
			cur_img = erosion(cur_img,selem2)
		elif 0.5 < r_for_mf_1 < 0.75: # exudate
			selem1 = disk(9.21)
			selem2 = disk(7.21)
			dilated1 = dilation(cur_img, selem1)
			dilated2 = dilation(cur_img, selem2)
			cur_img = np.subtract(dilated1, dilated2)
		
		cur_img = img_as_float(cur_img)
		X.append([cur_img.tolist()])
	# X = np.array(X , dtype = theano.config.floatX)
	que.put(X)
	return X
Ejemplo n.º 8
0
def get_segmented_lungs(im, plot=False):
    # Step 1: Convert into a binary image.
    binary = im < -400
    # Step 2: Remove the blobs connected to the border of the image.
    cleared = clear_border(binary)
    # Step 3: Label the image.
    label_image = label(cleared)
    # Step 4: Keep the labels with 2 largest areas.
    areas = [r.area for r in regionprops(label_image)]
    areas.sort()
    if len(areas) > 2:
        for region in regionprops(label_image):
            if region.area < areas[-2]:
                for coordinates in region.coords:
                       label_image[coordinates[0], coordinates[1]] = 0
    binary = label_image > 0
    # Step 5: Erosion operation with a disk of radius 2. This operation is seperate the lung nodules attached to the blood vessels.
    selem = disk(2)
    binary = binary_erosion(binary, selem)
    # Step 6: Closure operation with a disk of radius 10. This operation is    to keep nodules attached to the lung wall.
    selem = disk(10) # CHANGE BACK TO 10
    binary = binary_closing(binary, selem)
    # Step 7: Fill in the small holes inside the binary mask of lungs.
    edges = roberts(binary)
    binary = ndi.binary_fill_holes(edges)
    # Step 8: Superimpose the binary mask on the input image.
    get_high_vals = binary == 0
    im[get_high_vals] = -2000
    return im, binary
Ejemplo n.º 9
0
def get_segmented_lungs(im):

    binary = im < -320
    cleared = clear_border(binary) 
    cleared=morph(cleared,5)
    label_image = label(cleared)
  
    areas = [r.area for r in regionprops(label_image)]
    areas.sort()
    if len(areas) > 2:
        for region in regionprops(label_image):
            if region.area < areas[-2]:
                for coordinates in region.coords:
                       label_image[coordinates[0], coordinates[1]] = 0
    binary = label_image > 0  
    selem = disk(2)
    binary = binary_erosion(binary, selem)
 
    selem = disk(10)
    binary = binary_closing(binary, selem)
    edges = roberts(binary)
    binary = ndi.binary_fill_holes(edges)
 
    get_high_vals = binary == 0
    im[get_high_vals] = 0
  
    binary = morphology.dilation(binary,np.ones([5,5]))
    return binary
def extract_region_opening(img, is_demo=False):
    """
    Extracts fingerprint region of image via mophological opening
    """

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

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

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

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

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

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

    return t_dil_img
Ejemplo n.º 11
0
def watershed(image):
    hsv_image = color.rgb2hsv(image)

    low_res_image = rescale(hsv_image[:, :, 0], SCALE)
    local_mean = mean(low_res_image, disk(50))
    local_minimum_flat = np.argmin(local_mean)
    local_minimum = np.multiply(np.unravel_index(local_minimum_flat, low_res_image.shape), round(1 / SCALE))

    certain_bone_pixels = np.full_like(hsv_image[:, :, 0], False, bool)
    certain_bone_pixels[
    local_minimum[0] - INITIAL_WINDOW_SIZE/2:local_minimum[0]+INITIAL_WINDOW_SIZE/2,
    local_minimum[1] - INITIAL_WINDOW_SIZE/2:local_minimum[1]+INITIAL_WINDOW_SIZE/2
    ] = True

    certain_non_bone_pixels = np.full_like(hsv_image[:, :, 0], False, bool)
    certain_non_bone_pixels[0:BORDER_SIZE, :] = True
    certain_non_bone_pixels[-BORDER_SIZE:-1, :] = True
    certain_non_bone_pixels[:, 0:BORDER_SIZE] = True
    certain_non_bone_pixels[:, -BORDER_SIZE:-1] = True

    smoothed_hsv = median(hsv_image[:, :, 0], disk(50))
    threshold = MU * np.median(smoothed_hsv[certain_bone_pixels])

    possible_bones = np.zeros_like(hsv_image[:, :, 0])
    possible_bones[smoothed_hsv < threshold] = 1

    markers = np.zeros_like(possible_bones)
    markers[certain_bone_pixels] = 1
    markers[certain_non_bone_pixels] = 2

    labels = morphology.watershed(-possible_bones, markers)

    return labels
Ejemplo n.º 12
0
    def compute(self, src):
        image = img_as_ubyte(src)

        # denoise image
        denoised = denoise_tv_chambolle(image, weight=0.05)
        denoised_equalize= exposure.equalize_hist(denoised)

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

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

        # labels
        labels = watershed(gradient, markers)

        # display results
        fig, axes = plt.subplots(2,3)
        axes[0, 0].imshow(image)#, cmap=plt.cm.spectral, interpolation='nearest')
        axes[0, 1].imshow(denoised, cmap=plt.cm.spectral, interpolation='nearest')
        axes[0, 2].imshow(markers, cmap=plt.cm.spectral, interpolation='nearest')
        axes[1, 0].imshow(gradient, cmap=plt.cm.spectral, interpolation='nearest')
        axes[1, 1].imshow(labels, cmap=plt.cm.spectral, interpolation='nearest', alpha=.7)
        plt.show()
Ejemplo n.º 13
0
Archivo: toolbox.py Proyecto: 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)
Ejemplo n.º 14
0
def watershed_segmentation(im):
    gray=skimage.color.rgb2gray(im)
    denoised = rank.median(gray, disk(2))
    # find continuous region (low gradient) --> markers
    markers = rank.gradient(denoised, disk(5)) < 10
    markers = nd.label(markers)[0]
    seg = watershed(gray,markers)
    return seg
Ejemplo n.º 15
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)
Ejemplo n.º 16
0
def db_eval_boundary(foreground_mask,gt_mask,bound_th=0.008):
	"""
	Compute mean,recall and decay from per-frame evaluation.
	Calculates precision/recall for boundaries between foreground_mask and
	gt_mask using morphological operators to speed it up.

	Arguments:
		foreground_mask (ndarray): binary segmentation image.
		gt_mask         (ndarray): binary annotated image.

	Returns:
		F (float): boundaries F-measure
		P (float): boundaries precision
		R (float): boundaries recall
	"""
	assert np.atleast_3d(foreground_mask).shape[2] == 1

	bound_pix = bound_th if bound_th >= 1 else \
			np.ceil(bound_th*np.linalg.norm(foreground_mask.shape))

	# Get the pixel boundaries of both masks
	fg_boundary = seg2bmap(foreground_mask);
	gt_boundary = seg2bmap(gt_mask);

	from skimage.morphology import binary_dilation,disk

	fg_dil = binary_dilation(fg_boundary,disk(bound_pix))
	gt_dil = binary_dilation(gt_boundary,disk(bound_pix))

	# Get the intersection
	gt_match = gt_boundary * fg_dil
	fg_match = fg_boundary * gt_dil

	# Area of the intersection
	n_fg     = np.sum(fg_boundary)
	n_gt     = np.sum(gt_boundary)

	#% Compute precision and recall
	if n_fg == 0 and  n_gt > 0:
		precision = 1
		recall = 0
	elif n_fg > 0 and n_gt == 0:
		precision = 0
		recall = 1
	elif n_fg == 0  and n_gt == 0:
		precision = 1
		recall = 1
	else:
		precision = np.sum(fg_match)/float(n_fg)
		recall    = np.sum(gt_match)/float(n_gt)

	# Compute F measure
	if precision + recall == 0:
		F = 0
	else:
		F = 2*precision*recall/(precision+recall);

	return F
Ejemplo n.º 17
0
def breakup_region(component):
    distance = ndi.distance_transform_edt(component)
    skel = skeletonize(component)
    skeldist = distance*skel
    local_maxi = peak_local_max(skeldist, indices=False, footprint=disk(10))
    local_maxi=ndi.binary_closing(local_maxi,structure = disk(4),iterations = 2)
    markers = ndi.label(local_maxi)[0]
    labels = watershed(-distance, markers, mask=component)
    return(labels)
    def smooth(self):
        # TODO: there is non nan in the ff img, or?
        mask = self.flatField == 0
        from skimage.filters.rank import median, mean
        from skimage.morphology import disk

        ff = mean(median(self.flatField, disk(5), mask=~mask),
                  disk(13), mask=~mask)

        return ff.astype(float) / ff.max(), mask
Ejemplo n.º 19
0
def EllipsoidFit(image,threshold = "otsu global",voxelscale=[1,1,1],
	outfile=None,minsize = 30,tol = 0.01,
	outputdata = True,output3D = False, outputhist = True,
	display3D=False, displayhist=False, hull = True,
	fittype = "ellipsoid",smooth = "median"):
	
	starttime = time.time()
	# If outfile is not provided, set automatically
	if outfile is None:
		if isinstance(image,str):
			outfile = image.split(".")[0]
		else:
			outfile = time.asctime(time.localtime(time.time()))
	# Read image into array
	if isinstance(image,str): 
		imgtiff = tiff.TIFFfile(image)
		image = imgtiff.asarray()

	ishape = image.shape #image shape
	if smooth == "median":
		for i in range(ishape[0]):
			image[i] = filters.rank.median(image[i],morphology.disk(1))
	elif smooth == "mean":
		for i in range(ishape[0]):
			image[i] = filters.rank.mean(image[i],morphology.disk(1))
	print image.shape
	# Rescale voxels to be cubic
	if len(set(voxelscale)) > 1:
		image = cubifyvoxels(image,voxelscale)
	print image.shape

	# Binarize images and label objects
	binimg, thresh = thresholding(image,threshold)
	lbimg,objnum = ndimage.label(binimg)
	print "Objects: ",objnum
	print "Fitting Ellipsoids:"
	# Fit ellipsoids
	ellipsoids = get_ellipsoids(lbimg,objnum,minsize=minsize,tol=tol,hull=hull,voxelscale=voxelscale)
	print " Done!"
	print "Calculating Ellipsoid Information:"
	# Calculate information on ellipsoids
	data = elldatacalc(ellipsoids)
	print "Done!"
	endtime = time.time()
	# Output various results of the fitting
	print "Graphing and Writing to files: "
	if outputdata:
		datafileprint(data,outfile)
		summaryfileprint(data,tol,minsize,outfile,threshold,hull,objnum,thresh,endtime-starttime,smooth)
	if display3D or output3D:
		graphrods(data,binimg,outfile,display3D,output3D,voxelscale=voxelscale)
	if displayhist or outputhist:
		if len(data)>1: graphdata(data,outfile,displayhist,outputhist)
	print "Done!"
	return data
Ejemplo n.º 20
0
    def test_compare_8bit_vs_16bit(self, method):
        # filters applied on 8-bit image ore 16-bit image (having only real 8-bit
        # of dynamic) should be identical
        image8 = util.img_as_ubyte(data.camera())[::2, ::2]
        image16 = image8.astype(np.uint16)
        assert_equal(image8, image16)

        func = getattr(rank, method)
        f8 = func(image8, disk(3))
        f16 = func(image16, disk(3))
        assert_equal(f8, f16)
Ejemplo n.º 21
0
def process_cell(img):

    # la binariza en caso de que sea escala de grises
    if not img.dtype == 'bool':
        img = img > 0  # Binarizar

    # Calcular máscaras para limpiar lineas largas verticales
    h_k = 0.8
    sum0 = np.sum(img, 0)  # Aplastar la matriz a una fila con las sumas de los valores de cada columna.
    thr0 = sum0 < h_k * img.shape[0]
    thr0 = thr0.reshape(len(thr0), 1) # Convertirlo a vector de una dimensión

    # Calcular máscaras para limpiar lineas largas horizontales
    w_k = 0.5
    sum1 = np.sum(img, 1)
    thr1 = sum1 < w_k * img.shape[1]
    thr1 = thr1.reshape(len(thr1), 1)

    mask = thr0.transpose() * thr1 # Generar máscara final para la celda
    mask_lines = mask.copy()

    elem = morphology.square(5)
    mask = morphology.binary_erosion(mask, elem) # Eliminar ruido

    img1 = np.bitwise_and(mask, img) # Imagen filtrada

    # segmentación del bloque de números
    kerw = 5  # Kernel width
    thr_k = 0.8

    # Calcular mascara para marcar inicio y fin de región con dígitos horizontalmente
    sum0 = np.sum(img1, 0)
    sum0 = signal.medfilt(sum0, kerw)
    thr0 = sum0 > thr_k * np.median(sum0)
    thr0 = np.bitwise_and(thr0.cumsum() > 0, np.flipud(np.flipud(thr0).cumsum() > 0))
    thr0 = thr0.reshape(len(thr0), 1)

    # Calcular mascara para marcar inicio y fin de región con dígitos verticalmente
    sum1 = np.sum(img1, 1)
    sum1 = signal.medfilt(sum1, kerw)
    thr1 = sum1 > thr_k * np.median(sum1)
    thr1 = np.bitwise_and(thr1.cumsum() > 0, np.flipud(np.flipud(thr1).cumsum() > 0))
    thr1 = thr1.reshape(len(thr1), 1)

    # Mascara final para inicio y fin de caracteres (bounding box of digit region)
    mask = thr0.transpose() * thr1
    mask = morphology.binary_dilation(mask, morphology.square(2))


    img = np.bitwise_and(mask_lines.astype(img.dtype), img)  # Aplicar máscara para quitar lineas
    img = morphology.binary_dilation(img, morphology.disk(1)) # Dilatación para unir números quebrados por la máscara anterior
    img = morphology.binary_erosion(img, morphology.disk(1)) # Volver a la fomorma 'original' con los bordes unidos

    return np.bitwise_and(mask, img)
Ejemplo n.º 22
0
    def pred_f(image, param=param):
        # pdb.set_trace()
        prob_image1, bin_image1 = PredImageFromNet(
            net_1, image, with_depross=True)
        segmentation_mask = DynamicWatershedAlias(prob_image1, param)
        segmentation_mask[segmentation_mask > 0] = 1
        contours = dilation(segmentation_mask, disk(2)) - \
            erosion(segmentation_mask, disk(2))

        x, y = np.where(contours == 1)
        image[x, y] = np.array([0, 0, 0])
        return image
Ejemplo n.º 23
0
def filter_images(path, filename):
    """
    Applies filters to image, that is located in folder
    :param path:        define path to temp folder
    :param filename:    name of image for processing
    :return:            filtered image
    """
    img_data = io.imread(path + filename, as_grey=True)
    # Set better contrast to raw data with disk r = 20
    img_data = enhance_contrast(img_data, disk(20))
    img_data_dil = dilation(img_data, disk(1))

    return img_data_dil
Ejemplo n.º 24
0
def buffer_mask(stuff):
    stuff_shape = stuff.shape
    stuff_buff = np.array(np.copy(stuff))
    for x in range(1,stuff_shape[0]):
        for y in range(1,stuff_shape[1]):
            if stuff[x,y] == False:
                if np.count_nonzero(stuff[x-1:x+2,y-1:y+2]) > 4:
                    # print x,y
                    stuff_buff[x,y] = True
    selem = morphology.disk(1)
    stuff_buff = morphology.opening(stuff_buff, selem)
    selem = morphology.disk(1)
    stuff_buff = morphology.closing(stuff_buff, selem)
    return stuff_buff
Ejemplo n.º 25
0
def noise_reduction(image, background, window_size = 5, mode = 0):
	#  GrayScale image use mode = 0
	#  mode = 0: dealing RGB image with each channel 
	#  mode!= 0: dealing RGB image with HSV value 
	if(mode == 0):
		med_image = median_each(image, disk(window_size))
		med_bckg  = median_each(background, disk(window_size))

	else :
		med_image = img_as_ubyte(median_hsv(image, disk(window_size)))
		med_bckg  = img_as_ubyte(median_hsv(background, disk(window_size)))

	norm_image = np.true_divide(med_image, (med_bckg/255*254+1))/255	
	return norm_image
Ejemplo n.º 26
0
def test_compare_ubyte_vs_float():

    # Create signed int8 image that and convert it to uint8
    image_uint = img_as_ubyte(data.camera()[:50, :50])
    image_float = img_as_float(image_uint)

    methods = ['autolevel', 'bottomhat', 'equalize', 'gradient', 'threshold',
               'subtract_mean', 'enhance_contrast', 'pop', 'tophat']

    for method in methods:
        func = getattr(rank, method)
        out_u = func(image_uint, disk(3))
        out_f = func(image_float, disk(3))
        assert_array_equal(out_u, out_f)
Ejemplo n.º 27
0
    def get_femur(self, initial_segmentation, otsu_low_threshold):
        segmented_leg = initial_segmentation.copy()
        coordinates = []

        if self.leg_key == self.LEFT_LEG:
            for z in range(0, segmented_leg.shape[0]):
                segmented_leg[z, :, :] = np.fliplr(segmented_leg[z, :, :])

        image = segmented_leg.copy()

        for z in range(1, segmented_leg.shape[0]):
            seed_points = set()
            white_pxs = np.argwhere(image[z - 1, :, :] == 1)
            for px in white_pxs:
                seed_points.add((z, px[0], px[1]))

            label_image = label(image[z - 1, :, :])

            coordinates = [region.centroid[1] for region in regionprops(label_image)]
            promedio = np.mean(coordinates)
            coordinates = [promedio]

            region = region_growing.simple_2d_binary_region_growing(segmented_leg[z, :, :], seed_points, promedio)

            image[z, :, :] = 0
            for px in region:
                image[z, px[1], px[2]] = 1

            image[z, :, :] = self.fill_holes(image[z, :, :], disk(2), 3)

        if self.leg_key == self.LEFT_LEG:
            for z in range(0, image.shape[0]):
                image[z, :, :] = np.fliplr(image[z, :, :])

        result = np.zeros_like(image)
        for z in range(0, image.shape[0]):
            seed_points = set()
            white_pxs = np.argwhere(image[z, :, :] == 1)
            for px in white_pxs:
                seed_points.add((z, px[0], px[1]))

            region = region_growing.simple_2d_binary_region_growing2(otsu_low_threshold[z, :, :], seed_points)

            for px in region:
                result[z, px[1], px[2]] = 1

            result[z, :, :] = self.fill_holes(result[z, :, :], disk(2), 1)
        result = result.astype('i4')
        return result
Ejemplo n.º 28
0
def label_clouds(pcp, opening_selem, closing_selem):
    # img = buffer_pcl()
    # pcp = calc_pcp
    img = pcp
    _img = np.zeros((img.shape[0]+30, img.shape[1]+30))
    _img[15:-15, 15:-15] = img
    
    o_selem   = morphology.disk(opening_selem)
    _img = morphology.opening(_img, o_selem)
    c_selem   = morphology.disk(closing_selem)
    bin_im = morphology.closing(_img, c_selem)
    
    labels, nbr_objs = measurements.label(bin_im)
    print(nbr_objs)
    return labels[15:-15, 15:-15], nbr_objs
Ejemplo n.º 29
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)
Ejemplo n.º 30
0
def entropy(A, disk_size=5):
    """
    Returns the entropy [1]_ computed locally. Entropy is computed using
    base 2 logarithm i.e. the filter returns the minimum number of bits
    needed to encode local greylevel distribution.
    """
    return _entropy(A, disk(disk_size))
Ejemplo n.º 31
0
def volspike(pars):
    """ Function for finding spikes of one single cell with given ROI in 
        voltage imaging. Using function denoiseSpikes to find spikes 
        of one dimensional signal, using ridge regression to find the 
        best spatial filters. Do these two steps iteratively to find
        best spike time.   
    
        Args:
            pars: a list with four variables
                fnames: str
                    the path of memory map file for the entire video
            
                index: int
                    the index of the cell to process
            
                ROI: 2-D array
                    the corresponding matrix of the ROI
            
                sampleRate: int 
                    the sample rate of the video
            
        Returns: 
            output: a dictionary
                output including spike times, spatial filters etc      
    """
    opts = {
        'doCrossVal':
        False,  #cross-validate to optimize regression regularization parameters?
        'doGlobalSubtract': False,
        'contextSize':
        50,  #65; #number of pixels surrounding the ROI to use as context
        'censorSize':
        12,  #number of pixels surrounding the ROI to censor from the background PCA; roughly the spatial scale of scattered/dendritic neural signals, in pixels.
        'nPC_bg':
        8,  #number of principle components used for background subtraction
        'tau_lp':
        3,  #time window for lowpass filter (seconds); signals slower than this will be ignored
        'tau_pred':
        1,  #time window in seconds for high pass filtering to make predictor for regression
        'sigmas':
        np.array([1, 1.5,
                  2]),  #spatial smoothing radius imposed on spatial filter;
        'nIter':
        5,  #number of iterations alternating between estimating temporal and spatial filters.
        'localAlign': False,
        'globalAlign': True,
        'highPassRegression':
        False  #regress on a high-passed version of the data. Slightly improves detection of spikes, but makes subthreshold unreliable.
    }
    output = {'rawROI': {}}
    fname_new, cellN, bw, sampleRate = pars
    opts[
        'windowLength'] = sampleRate * 0.02  #window length for spike templates
    print('processing cell:', cellN)

    Yr, dims, T = cm.load_memmap(fname_new)
    if bw.shape == dims:
        images = np.reshape(Yr.T, [T] + list(dims), order='F')
    elif bw.shape == dims[::-1]:
        images = np.reshape(Yr.T, [T] + list(dims),
                            order='F').transpose([0, 2, 1])
    else:
        print('size of ROI and video does not accrod')

    # extract relevant region and align
    bwexp = dilation(bw,
                     np.ones([opts['contextSize'], opts['contextSize']]),
                     shift_x=True,
                     shift_y=True)
    Xinds = np.where(np.any(bwexp > 0, axis=1) > 0)[0]
    Yinds = np.where(np.any(bwexp > 0, axis=0) > 0)[0]
    bw = bw[Xinds[0]:Xinds[-1] + 1, Yinds[0]:Yinds[-1] + 1]
    notbw = 1 - dilation(bw, disk(opts['censorSize']))
    data = np.array(images[:, Xinds[0]:Xinds[-1] + 1, Yinds[0]:Yinds[-1] + 1])
    bw = (bw > 0)
    notbw = (notbw > 0)
    ref = np.median(data[:500, :, :], axis=0)

    # visualize ROI
    #fig = plt.figure()
    #plt.subplot(131);plt.imshow(ref);plt.axis('image');plt.xlabel('mean Intensity')
    #plt.subplot(132);plt.imshow(bw);plt.axis('image');plt.xlabel('initial ROI')
    #plt.subplot(133);plt.imshow(notbw);plt.axis('image');plt.xlabel('background')
    #fig.suptitle('ROI selection')
    #plt.show()

    output['meanIM'] = np.mean(data, axis=0)
    data = np.reshape(data, (data.shape[0], -1))
    data = data - np.mean(data, 0)
    data = data - np.mean(data, 0)

    # remove low frequency components
    data_hp = highpassVideo(data.T, 1 / opts['tau_lp'], sampleRate).T
    data_lp = data - data_hp
    data_pred = np.empty_like(data_hp)
    if opts['highPassRegression']:
        data_pred[:] = highpassVideo(data, 1 / opts['tau_pred'], sampleRate)
    else:
        data_pred[:] = data_hp

    # initial trace
    t = np.nanmean(data_hp[:, bw.ravel()], 1)
    t = t - np.mean(t)

    # remove any variance in trace that can be predicted from the background principal components
    Ub, Sb, Vb = svds(data_hp[:, notbw.ravel()], opts['nPC_bg'])
    reg = LinearRegression(fit_intercept=False).fit(Ub, t)
    t = np.double(t - np.matmul(Ub, reg.coef_))

    # find out spikes of initial trace
    Xspikes, spikeTimes, guessData, output['rawROI']['falsePosRate'], output[
        'rawROI']['detectionRate'], output['rawROI'][
            'templates'], low_spk = denoiseSpikes(-t, opts['windowLength'],
                                                  sampleRate, False, 100)

    Xspikes = -Xspikes
    output['rawROI']['X'] = t.copy()
    output['rawROI']['Xspikes'] = Xspikes.copy()
    output['rawROI']['spikeTimes'] = spikeTimes.copy()
    output['rawROI']['spatialFilter'] = bw.copy()
    output['rawROI']['X'] = output['rawROI']['X'] * np.mean(
        t[output['rawROI']['spikeTimes']]) / np.mean(output['rawROI']['X'][
            output['rawROI']['spikeTimes']])  # correct shrinkage
    output['num_spikes'] = [spikeTimes.shape[0]]
    templates = output['rawROI']['templates']
    selectSpikes = np.zeros(Xspikes.shape)
    selectSpikes[spikeTimes] = 1
    sgn = np.mean(Xspikes[selectSpikes > 0])
    noise = np.std(Xspikes[selectSpikes == 0])
    snr = sgn / noise

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

    # Cross-validation of regularized regression parameters
    lambdamax = np.single(np.linalg.norm(pred[:, 1:], ord='fro')**2)
    lambdas = lambdamax * np.logspace(-4, -2, 3)
    I0 = np.eye(pred.shape[1], dtype=np.single)
    I0[0, 0] = 0

    if opts['doCrossVal']:
        # need to add
        print('doing cross validation')
    else:
        s_max = 1
        l_max = 2
        opts['lambda'] = lambdas[l_max]
        opts['sigma'] = opts['sigmas'][s_max]
        opts['lambda_ix'] = l_max

    selectPred = np.ones(data_hp.shape[0])
    if opts['highPassRegression']:
        selectPred[:np.int16(sampleRate / 2 + 1)] = 0
        selectPred[-1 - np.int16(sampleRate / 2):] = 0
    sigma = opts['sigmas'][s_max]

    pred = np.empty_like(data_pred)
    pred[:] = data_pred
    pred = np.hstack(
        (np.ones((data_pred.shape[0], 1), dtype=np.single),
         np.reshape(
             movie.gaussian_blur_2D(
                 np.reshape(pred,
                            (data_pred.shape[0], ref.shape[0], ref.shape[1])),
                 kernel_size_x=np.int(2 * np.ceil(2 * sigma) + 1),
                 kernel_size_y=np.int(2 * np.ceil(2 * sigma) + 1),
                 kernel_std_x=sigma,
                 kernel_std_y=sigma,
                 borderType=cv2.BORDER_REPLICATE), data_pred.shape)))

    recon = np.empty_like(data_hp)
    recon[:] = data_hp
    recon = np.hstack(
        (np.ones((data_hp.shape[0], 1), dtype=np.single),
         np.reshape(
             movie.gaussian_blur_2D(
                 np.reshape(recon,
                            (data_hp.shape[0], ref.shape[0], ref.shape[1])),
                 kernel_size_x=np.int(2 * np.ceil(2 * sigma) + 1),
                 kernel_size_y=np.int(2 * np.ceil(2 * sigma) + 1),
                 kernel_std_x=sigma,
                 kernel_std_y=sigma,
                 borderType=cv2.BORDER_REPLICATE), data_hp.shape)))

    temp = np.linalg.inv(
        np.matmul(np.transpose(pred[selectPred > 0, :]), pred[
            selectPred > 0, :]) + lambdas[l_max] * I0)
    kk = np.matmul(temp, np.transpose(pred[selectPred > 0, :]))

    # Identify spatial filters with regularized regression
    for iteration in range(opts['nIter']):
        doPlot = False
        if iteration == opts['nIter'] - 1:
            doPlot = True

        #print('Identifying spatial filters')
        #print(iteration)

        gD = np.single(guessData[selectPred > 0])
        select = (gD != 0)
        weights = np.matmul(kk[:, select], gD[select])

        X = np.matmul(recon, weights)
        X = X - np.mean(X)

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

        if doPlot == True:
            plt.figure()
            plt.imshow(spatialFilter)
            plt.show()

        if iteration < opts['nIter'] - 1:
            b = LinearRegression(fit_intercept=False).fit(Ub, X).coef_
            if doPlot:
                plt.figure()
                plt.plot(X)
                plt.plot(np.matmul(Ub, b))
                plt.title('Denoised trace vs background')
                plt.show()
            X = X - np.matmul(Ub, b)
        else:
            if opts['doGlobalSubtract']:
                print('do global subtract')
            # need to add

        # correct shrinkage
        X = np.double(X * np.mean(t[spikeTimes]) / np.mean(X[spikeTimes]))

        # generate the new trace and the new denoised trace
        Xspikes, spikeTimes, guessData, falsePosRate, detectionRate, templates, _ = denoiseSpikes(
            -X, opts['windowLength'], sampleRate, doPlot)

        selectSpikes = np.zeros(Xspikes.shape)
        selectSpikes[spikeTimes] = 1
        sgn = np.mean(Xspikes[selectSpikes > 0])
        noise = np.std(Xspikes[selectSpikes == 0])
        snr = sgn / noise

        output['num_spikes'].append(spikeTimes.shape[0])

    # ensure that the maximum of the spatial filter is within the ROI
    matrix = np.matmul(np.transpose(pred[:, 1:]), -guessData)
    sigmax = np.sqrt(np.sum(np.multiply(pred[:, 1:], pred[:, 1:]), axis=0))
    sigmay = np.sqrt(np.dot(guessData, guessData))
    IMcorr = matrix / sigmax / sigmay
    maxCorrInROI = np.max(IMcorr[bw.ravel()])
    if np.any(IMcorr[notbw.ravel()] > maxCorrInROI):
        output['passedLocalityTest'] = False
    else:
        output['passedLocalityTest'] = True

    # compute SNR
    selectSpikes = np.zeros(Xspikes.shape)
    selectSpikes[spikeTimes] = 1
    sgn = np.mean(Xspikes[selectSpikes > 0])
    noise = np.std(Xspikes[selectSpikes == 0])
    snr = sgn / noise
    output['snr'] = snr

    # output
    output['y'] = X
    output['yFilt'] = -Xspikes
    output['ROI'] = np.transpose(np.vstack((Xinds[[0, -1]], Yinds[[0, -1]])))
    output['ROIbw'] = bw
    output['spatialFilter'] = spatialFilter
    output['falsePosRate'] = falsePosRate
    output['detectionRate'] = detectionRate
    output['templates'] = templates
    output['spikeTimes'] = spikeTimes
    output['opts'] = opts
    output['F0'] = np.nanmean(
        data_lp[:, bw.flatten()] + output['meanIM'][bw][np.newaxis, :], 1)
    output['dFF'] = X / output['F0']
    output['rawROI']['dFF'] = output['rawROI']['X'] / output['F0']
    output['bg_pc'] = Ub  # background components
    output['low_spk'] = low_spk
    output['weights'] = weights
    output['cellN'] = cellN

    return output
Ejemplo n.º 32
0
def hough_num_circles(input_binary_img, min_r=15, max_r=35, step=2):
    '''
	Helper function for cell_split
	Runs hough transform on a cell cluster in a binary image to determine where
	individual cells may lie. Does not take in a whole binary image, only takes in a contour converted
	to a binary image for a single cluster

	:param input_binary_img: [np.ndarray] Input binary image of the single cell group
	:param min_r: [float] minimum radius acceptable for a cell
	:param max_r: [float] maximum radius acceptable for a cell
	:param step: [float] rate at which minimum radius will be stepped up to maximum radius size
	:return: [np.ndarray] cropped and split version of input binary image
	'''
    print "> Performing Hough cell splitting"
    # Create a list of radii to test and perform hough transform to recover circle centers (x,y) and radii
    hough_radii = np.arange(min_r, max_r, 2)
    hough_res = hough_circle(input_binary_img, hough_radii)
    accums, cx, cy, radii = hough_circle_peaks(hough_res,
                                               hough_radii,
                                               total_num_peaks=3)
    circles = zip(cy, cx, radii)
    # remove any circles too close to each other

    no_duplicates = crop_close(circles, max_sep=10)

    # HYPOTHETICAL # of cells
    N_cells = len(no_duplicates)
    # view_2d_img(input_binary_img)
    print "\t> Number cells in subsection: {}".format(N_cells)
    # print no_duplicates
    if N_cells > 1:
        # Set initial radius to 1
        for rows in no_duplicates:
            rows[-1] == 1
        # Create mask to divide both cells
        # Grow circle size until there is a collision followed by no more collisions
        actual_mask = np.zeros_like(input_binary_img)
        # Create Conditions for Whileloop
        collision = False
        end_collision = False
        stop_condition = False
        n = 0
        max_iter = 100

        # while end_collision == False or n < 10:
        while (n < max_iter and stop_condition == False):
            # Create empty mask to grow
            mask = np.zeros_like(input_binary_img)
            for center_y, center_x, radius in no_duplicates:
                # Create mask for each circle in no_duplicates
                sub_mask = np.zeros_like(input_binary_img)
                # List of coodinates for perimeter of a circle and remove any negative points to prevent edge looparound
                circy, circx = circle_perimeter(center_y, center_x, radius)
                no_negs = remove_neg_pts(zip(circy, circx))
                for y, x in no_negs:
                    # for each circle point, if it falls within the dimensions of the submask, plot it.
                    if y < sub_mask.shape[0] and x < sub_mask.shape[1]:
                        sub_mask[y, x] = 1
                # Append submask to growing mask after dilation (aka making the circle boundaries wide as f**k)
                mask += dilation(sub_mask, disk(2))
            # Determine if there is a collision between circles (max element in grow mask is more than just one submask)
            # montage_n_x((actual_mask, mask))
            # print np.amax(mask.flatten())
            if np.amax(mask.flatten()) > 1:
                collision = True
                # collision_pt = np.where(mask >= 2)
                mask[mask < 2] = 0
                # view_2d_img(mask)
                actual_mask += mask
                actual_mask[actual_mask != 0] = 1
            # montage_n_x((actual_mask, mask))
            if collision == True and np.amax(mask.flatten()) <= 1:
                end_collision = True
                stop_condition = True

            # Grow circle radius by 8% per
            for rows in no_duplicates:
                rows[-1] *= 1.08
                rows[-1] = np.int(rows[-1])
            n += 1
            # print n, collision, end_collision, stop_condition
        if stop_condition == False:
            # montage_n_x((actual_mask, actual_mask + input_binary_img, filled_cells, filled_cells * (1 - actual_mask)))
            return np.ones_like(input_binary_img)
            # return binary_fill_holes(input_binary_img).astype(int)
        else:
            # Fill edges to create mask
            # filled_cells = binary_fill_holes(input_binary_img).astype(int)
            # montage_n_x((actual_mask, actual_mask + input_binary_img, filled_cells, filled_cells * (1 - actual_mask)))
            # view_2d_img(filled_cells * (1 - dm))
            # return filled_cells * (1 - actual_mask)
            return actual_mask
    else:
        # view_2d_img(input_binary_img)
        return np.ones_like(input_binary_img)
Ejemplo n.º 33
0
    for z in range(len(pixles)):
        x, y = pixles[z]
        features[z][index] = matrix[x][y]

    return features


#%% read image

image = data.immunohistochemistry()
plt.imshow(image)
image = rgb2gray(image)
plt.imshow(image, cmap="gray")

#noise removal (median filter is an edge preserver (performs better than Gaussian))
med = median(image, disk(5))
plt.imshow(med, cmap="gray")

#threasholding
thresh_min = threshold_minimum(med)
binary_min = med > thresh_min

#invert image (make the cells white and background black)
inverted = np.invert(binary_min)

plt.imshow(inverted, cmap="gray")

#%% sample positive pixles (512 pixles)

pos_pixles = set()
random.seed(0)
Ejemplo n.º 34
0
def m_test(weight_path,file_name):

    os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
    ex_name = 'dense_normal'
    model_name = 'dense_normal'
    batch_size = 12
    TTA = True
    CCF = True
    IMAGE_SIZE = 768
    TTA_list = {
        'n':flip_n,
        'v':flip_v,
        'h':flip_h,
        'vh':flip_vh,
    }


    valid_loader = make_loader(mode='valall',
                               img_size=IMAGE_SIZE,
                               batch_size=batch_size,
                               shuffle=False)


    init_path = weight_path




    net, starting_epoch = build_network(init_path, model_name)


    with torch.no_grad():

        net.eval()
        out_pred_rows = []
        for batch_num, (inputs,_, paths) in enumerate(tqdm(valid_loader, desc='Predict')):

            if not TTA:

                inputs = Variable(inputs).cuda()

                outputs = net(inputs)

                outputs = torch.sigmoid(outputs)
                outputs = torch.squeeze(outputs.data.cpu(), dim=1)
                outputs = outputs.numpy()

            else:

                outputs = []

                all_inputs = []

                inputs = inputs.numpy()

                for t in range(4):
                    inpt = np.rot90(inputs, axes=(2, 3), k=t)

                    inpt = torch.from_numpy(inpt.copy())

                    inpt = Variable(inpt).cuda()

                    all_inputs.append(inpt)

                    # print(t)

                inpt1 = np.flip(inputs, axis=2)

                inpt1 = torch.from_numpy(inpt1.copy())

                inpt2 = np.flip(inputs, axis=3)

                inpt2 = torch.from_numpy(inpt2.copy())

                all_inputs.append(Variable(inpt1).cuda())

                all_inputs.append(Variable(inpt2).cuda())

                for p, sinput in enumerate(all_inputs):

                    output = net(sinput)
                    output = torch.sigmoid(output)

                    output = torch.squeeze(output.data.cpu(), dim=1).numpy()

                    if p < 4:
                        output = np.rot90(output, axes=(1, 2), k=4 - p)

                    if p == 4:
                        output = np.flip(output, axis=1)

                    if p == 5:
                        output = np.flip(output, axis=2)

                    if output.shape[-1] != 768:

                        output768 = np.zeros((output.shape[0], 768, 768), dtype=np.float32)

                        for x in range(output.shape[0]):
                            output768[x, :, :] = cv2.resize(output[x, :, :], (768, 768))

                        outputs.append(output768)

                    else:

                        outputs.append(output)

                outputs = np.stack(outputs, axis=-1)

                outputs = np.mean(outputs, axis=-1)

            for i, image_name in enumerate(paths):

                mask = outputs[i,:,:]

                # mask = np.where(mask>0.5,1,0)
                # plt.imshow(mask)
                # plt.show()
                #split_mask = plain_split(mask)
                cur_seg = binary_opening(mask > 0.5, disk(2))
                cur_rles = util.multi_rle_encode(cur_seg)
                if len(cur_rles) > 0:
                    for c_rle in cur_rles:
                        out_pred_rows += [{'ImageId': image_name, 'EncodedPixels': c_rle}]
                else:
                    out_pred_rows += [{'ImageId': image_name, 'EncodedPixels': None}]

            # if batch_num>20:
            #     break
        submission_df = pd.DataFrame(out_pred_rows)[['ImageId', 'EncodedPixels']]

        submission_df.to_csv('off_sub/{}_sig.csv'.format(file_name), index=False)

        if CCF:
            boat_df = pd.read_csv('val_score.csv')
            is_boat = boat_df.score > 0.5
            has_boat_list = list(boat_df[is_boat]['ImageId'])

            out_pred_rows = []

            for i in tqdm(range(len(submission_df))):
                id = submission_df['ImageId'][i]
                rle = submission_df['EncodedPixels'][i]

                if id in has_boat_list:
                    if not isinstance(rle,str):
                        out_pred_rows += [{'ImageId': id, 'EncodedPixels': None}]
                    else:
                        out_pred_rows += [{'ImageId': id, 'EncodedPixels': rle}]
                else:
                    out_pred_rows += [{'ImageId': id, 'EncodedPixels': None}]

            submission_df = pd.DataFrame(out_pred_rows)[['ImageId', 'EncodedPixels']]
            submission_df.to_csv('off_sub/{}_sig_ns5.csv'.format(file_name), index=False)
Ejemplo n.º 35
0
output_base_filename = output_directory + "/" + os.path.basename(
    os.path.splitext(input_filename)[0])

#def smooth_mask_all(input_file,output_file):
nin = Dataset(input_filename, "r")
in_ssh = nin["SSH"]
(ntime, nlat, nlon) = in_ssh.shape

print("Interpolating masked data and least-squares cosine-transforming ",
      input_filename, ": ", in_ssh.shape, " to ", output_base_filename)

mask = in_ssh[0].mask.copy()
inner_region = np.zeros(mask.shape, dtype=np.bool)
inner_region[in_ssh[0].data == -1.0] = True
full_mask = np.logical_or(mask, inner_region)
edges = np.logical_and(morf.binary_dilation(full_mask, selem=morf.disk(3)),
                       np.logical_not(full_mask))
full_mask_plus = morf.binary_dilation(full_mask, selem=morf.disk(3))


def outputfile(method, nk1, nk2):
    nout = Dataset(
        output_base_filename + "_" + method + "_" + str(nk1) + "_" + str(nk2) +
        ".nc", "w")

    o_time = nout.createDimension('time', ntime)
    o_nlat = nout.createDimension('nlat', nlat)
    o_nlon = nout.createDimension('nlon', nlon)
    o_nlat = nout.createDimension('nk1', nk1)
    o_nlon = nout.createDimension('nk2', nk2)
Ejemplo n.º 36
0
def compute_entropy(im):
  # im is grayscale numpy
  return entropy(im, disk(10))
Ejemplo n.º 37
0
        global branch_struct

        Branch_Struct = namedtuple(
            "Branch_Struct",
            "label branch_points_rows branch_points_cols thickness sgl_blob sgl_blob_skel centroid orientation is_origin trunk_num trunk_branch"
        )
        branch_struct = []

        print("Blob # " + str(region.label) + " Analysis:")

        for region_skel in regions_skel:

            if region_skel.area > 3:
                sgl_blob_skel = labels_skel == region_skel.label
                selem = disk(4)
                sgl_blob_skel_dilate = dilation(sgl_blob_skel, selem)
                sgl_blob = np.bitwise_and(sgl_blob_skel_dilate, blob_img)

                thickness = np.sum(sgl_blob) / (np.sum(sgl_blob_skel))

                #region_skel.orientation()

                branch_points_rows = np.where(sgl_blob_skel)[0]
                branch_points_cols = np.where(sgl_blob_skel)[1]

                label = region.label
                # branches struct
                centroid = region_skel.centroid
                orientation = region_skel.orientation
Ejemplo n.º 38
0
    y0 = b * rho
    x1 = int(x0 + 1000 * (-b))
    y1 = int(y0 + 1000 * a)
    x2 = int(x0 - 1000 * (-b))
    y2 = int(y0 - 1000 * a)
    cv2.line(white_gray, (x1, y1), (x2, y2), 0, 2)
#Draw the contour lines to join the final set of clusters #detected as obstacles
contours = cv2.findContours(white_gray, cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)[-2]

for cnt in contours:
    cv2.drawContours(white_polygon, cnt, 0, 0, -1)
    man = []
    intense = []
    for col in range(cols):
        for row in range(rows):
            if cv2.pointPolygonTest(cnt, (col, row), False) == 1:
                man.append((row, col))
    for k in man:
        intense.append(im[k])
    intensity = mean(intense)
    if intensity > 170:
        cv2.drawContours(white_polygon, [cnt], 0, 0, -1)

white_gray1 = cv2.cvtColor(white_polygon, cv2.COLOR_BGR2GRAY)
opened = opening(white_gray1, selem=disk(4))
opened = Image.fromarray(opened)
opened.save('result.png')
plt.imshow(opened, cmap='gray')
plt.show()
Ejemplo n.º 39
0
hough_response = hough_circle(edges, hough_radii)

## Morphological operations
plt.rcParams['image.cmap'] = 'cubehelix'
plt.rcParams['image.interpolation'] = 'none'

from skimage import morphology
image = np.array(
    [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0],
     [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0]],
    dtype=np.uint8)
plt.imshow(image)
sq = morphology.square(width=3)
dia = morphology.diamond(radius=3)
disk = morphology.disk(radius=10)
compare2plots(sq, disk)
compare2plots(image, morphology.erosion(image, sq))
compare2plots(image, morphology.dilation(image, sq))
# opening erosion + dilation
# closing dilation + erosion

from skimage import data, color
hub = color.rgb2gray(data.hubble_deep_field()[350:450, 90:190])
# Plocka bort allt "brus" dvs små saker, men ha kvar den stora
compare2plots(hub, morphology.dilation(morphology.erosion(hub, disk), disk))
compare2plots(hub, morphology.opening(hub, disk))

# Segmentation
# Two different versions contrast-based and boundary-based
# SLIC K-means clustering
Ejemplo n.º 40
0
def apply_threshold(img, parameter, invert=False):
    if not len(parameter) == 1:
        raise ValueError("Thresholding parameter must be specified as a dictionary with one key and a list containing"
                         "supplementary arguments for that function as a value")

    method = list(parameter.keys())[0]
    if method not in ['none', 'otsu', 'multiotsu', 'local', 'min', 'mean', 'manual', 'triangle', 'manual_smoothing',
                     'li', 'niblack', 'sauvola']:
        raise ValueError("method must be one of ['none', otsu', 'multiotsu', 'local', 'min',"
                         " 'manual', 'triangle', 'manual_smoothing', 'li', 'niblack', 'sauvola']")

    if method in ['manual', 'manual_smoothing']:
        manual_initial_guess = np.round(img.mean() + img.std() * parameter[method][2], 0)
        print("initial threshold guess: " + str(manual_initial_guess))

    if method == 'none':
        thresh_val = 0
        thresh_img = img
    elif method == 'otsu':
        thresh_val = threshold_otsu(img)
        thresh_img = img > thresh_val
    elif method == 'multiotsu':
        kwargs = parameter[method]
        thresh_val = threshold_multiotsu(img, **kwargs)
        thresh_img = np.digitize(img, bins=thresh_val) # edit (10/11/20)
        #thresh_img = img > thresh_val[-1]               # original
    elif method == 'mean':
        thresh_val = threshold_mean(img)
        thresh_img = img > thresh_val
    elif method == 'min':
        thresh_val = threshold_minimum(img)
        thresh_img = img > thresh_val
    elif method == 'local':
        kwargs = parameter[method]
        assert isinstance(kwargs, dict)
        thresh_val = threshold_local(img, **kwargs)
        thresh_img = img > thresh_val
    elif method == 'triangle':
        thresh_val = threshold_triangle(img)
        thresh_img = img > thresh_val
    elif method == 'manual_smoothing':
        threshval = manual_initial_guess
        dividing = img > threshval
        smoother_dividing = filters.rank.mean(util.img_as_ubyte(dividing), morphology.disk(parameter[method][0]))
        thresh_img = smoother_dividing > parameter[method][1]
    elif method == 'li':
        threshval = threshold_li(img, initial_guess=threshold_otsu(img))
        thresh_img = img > threshval
    elif method == 'niblack':
        kwargs = parameter[method]
        assert isinstance(kwargs, dict)
        threshval = threshold_niblack(img, **kwargs)
        thresh_img = img > threshval
    elif method == 'sauvola':
        kwargs = parameter[method]
        assert isinstance(kwargs, dict)
        R = img.std()
        threshval = threshold_sauvola(img, r=R, **kwargs)
        thresh_img = img > threshval
    elif method == 'manual':
        args = parameter[method]
        if not isinstance(parameter[method], list):
            threshval = parameter[method]
        else:
            if not len(parameter[method]) == 1:
                raise ValueError("For manual thresholding only one parameter (the manual threshold) must be specified")
            threshval = manual_initial_guess
        thresh_img = img > threshval

    if invert:
        thresh_img = ~thresh_img

    return thresh_img
Ejemplo n.º 41
0
from skimage.io import imread, imsave
from skimage.util import img_as_ubyte
import cv2
import json
from pprint import pprint
from deoverlap import reduceRectDicts


# All files are saved in outPdfRoot.
outPdfRoot = os.path.abspath("pdf.output")

# DPI used for the rasters being tested
rasterDPI = 300

# Entropy is measured over the entropyKernel.
entropyKernel = disk(25)

# Entropy threshold. Regions with entropy above (below) this are considered natural (synthetic).
entropyThreshold = 4.0

# Outline of high-entropy region is morphologically closed with this kernel
outlineKernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (125, 125))

# We only save high-entropy rectangles at leas this many pixels of larger.
minArea = 90000  # 300 x 300 pixels = 1 x 1 inch

# Tolerance for polygon approximation. This is a fraction of the perimeter length.
contourEpsilon = 0.02

templSize = 13
searchSize = 29
Ejemplo n.º 42
0
# seg_cam = GigECam("/home/birdflight/birdflight/src/python/cfg/segmem.xml")
print("Initialized Camera")

# Import PenguTrack
import clickpoints
from PenguTrack.DataFileExtended import DataFileExtended
from PenguTrack.Filters import KalmanFilter
from PenguTrack.Filters import MultiFilter
from PenguTrack.Models import RandomWalk
from PenguTrack.Detectors import rgb2gray, TresholdSegmentation
from PenguTrack.Detectors import RegionPropDetector, RegionFilter, ExtendedRegionProps

from skimage.morphology import binary_closing, binary_dilation, binary_opening, binary_erosion
from skimage.morphology import disk
SELEM = disk(2, dtype=bool)

import scipy.stats as ss

# Load Database
a_min = 75
a_max = np.inf
file_path = "/home/alex/Desktop/PT_Cell_T850_A%s_%s_3d.cdb" % (a_min, a_max)

global db
db = DataFileExtended(file_path, "w")

db_start = DataFileExtended(input_file)
db_start2 = DataFileExtended(input_file2)
images = db_start.getImageIterator()
Ejemplo n.º 43
0
    def test_full_image(self,
                        forward,
                        mask_erosion_value=5,
                        num_images_per_class=2):
        """
        data_hs[hs_img_path] = (img, {"label_genotype": bbox_obj_dict["label_genotype"],
                                          "label_dai": bbox_obj_dict["label_dai"],
                                          "label_inoculated": bbox_obj_dict["label_inoculated"],
                                          "label_running": bbox_obj_dict["label_running"]})

        :return:
        """

        selected_inoculated = self.hyperparams[
            "inoculated"] * num_images_per_class
        #print(list(self.data_memmaps.keys())[0])
        #exit()

        res_samples = []
        for image_key in sorted(list(self.data_memmaps.keys())):
            sample = self.data_memmaps[image_key]
            sample_meta = sample[1]
            #print(sample_meta["label_genotype"], sample_meta["label_inoculated"], print(sample_meta["label_dai"]))
            if self.hyperparams["genotype"] is None or sample_meta[
                    "label_genotype"] in self.hyperparams["genotype"]:
                if self.hyperparams["dai"] is None or sample_meta[
                        "label_dai"] in self.hyperparams["dai"]:
                    for sample_bbox_filename in sample_meta['bbox_filename']:
                        if self.hyperparams["inoculated"] is None or (
                                sample_meta["label_inoculated"]
                                in self.hyperparams["inoculated"]
                                and sample_meta["label_inoculated"]
                                in selected_inoculated):
                            selected_inoculated.remove(
                                sample_meta["label_inoculated"])
                            img = sample[0]
                            bbox_obj_dict = pickle.load(
                                open(sample_bbox_filename, "rb"))
                            mask = bbox_obj_dict["mask"]
                            if mask_erosion_value > 0:
                                mask = cv2.copyMakeBorder(mask,
                                                          30,
                                                          30,
                                                          30,
                                                          30,
                                                          cv2.BORDER_CONSTANT,
                                                          value=0)
                                selem = disk(mask_erosion_value)
                                mask = binary_erosion(mask, selem)
                                mask = mask[30:-30, 30:-30]
                            [(min_y, min_x),
                             (max_y, max_x)] = bbox_obj_dict["bbox"]
                            classes = np.zeros(
                                shape=[img.shape[0], img.shape[1]]).squeeze()
                            classes[min_x:max_x, min_y:max_y] = mask
                            masked_pred_img = self.pixelwise_eval(
                                img, classes, bbox_obj_dict["bbox"], forward)

                            res_samples.append({
                                'img':
                                img[min_x:max_x, min_y:max_y, :],
                                'pred':
                                masked_pred_img[min_x:max_x, min_y:max_y],
                                'mask':
                                mask,
                                'label':
                                sample_meta["label_inoculated"]
                            })

                            if len(selected_inoculated) == 0:
                                break
        return res_samples
Ejemplo n.º 44
0
def _load_preprocessed_data(base_path_dataset,
                            base_path_dataset_parsed,
                            genotype=None,
                            inoculated=None,
                            dai=None,
                            max_num_balanced_inoculated=-1,
                            mask_erosion_value=5,
                            num_samples_file=-1,
                            superpixel=False,
                            bags=False):
    current_path = os.path.join(base_path_dataset_parsed, "*.p")

    filenames = sorted(list(set(glob.glob(current_path))))

    data_hs = dict()
    data_pos = []

    balance_inoculated_label_max_num = np.inf if max_num_balanced_inoculated == -1 else max_num_balanced_inoculated
    balance_inoculated_label_current = [0, 0]
    cnt_files_used = 0
    for filename in tqdm(filenames):
        data_pos_file = []
        bbox_obj_dict = pickle.load(open(filename, "rb"))

        if genotype is not None and bbox_obj_dict[
                "label_genotype"] not in genotype:
            continue
        if inoculated is not None and bbox_obj_dict[
                "label_inoculated"] not in inoculated:
            continue
        if dai is not None and bbox_obj_dict["label_dai"] not in dai:
            continue
        [(min_y, min_x), (max_y, max_x)] = bbox_obj_dict["bbox"]

        hs_img_path = os.path.join(
            os.path.join(base_path_dataset,
                         "{}dai".format(bbox_obj_dict["label_dai"])),
            bbox_obj_dict["filename"] + "/data.hdr")
        img = spectral.open_image(hs_img_path)
        classes = np.zeros(shape=[img.shape[0], img.shape[1]]).squeeze()

        mask = bbox_obj_dict["mask"]
        if mask_erosion_value > 0:
            mask = cv2.copyMakeBorder(mask,
                                      30,
                                      30,
                                      30,
                                      30,
                                      cv2.BORDER_CONSTANT,
                                      value=0)
            selem = disk(mask_erosion_value)
            mask = binary_erosion(mask, selem)
            mask = mask[30:-30, 30:-30]
        classes[min_x:max_x, min_y:max_y] = mask

        # view = spectral.imshow(img, classes=classes)
        # view.set_display_mode('overlay')
        # view.class_alpha = 0.5
        # input("")
        # print(bbox_obj_dict["bbox"])
        # print(bbox_obj_dict["filename"])
        # tmp_mask = bbox_obj_dict["mask"]
        # plt.figure(figsize=(20, 10))
        # plt.imshow(bbox_obj_dict["mask"])
        # plt.figure(figsize=(20, 10))
        # plt.imshow(bbox_obj_dict["image"])

        # plt.figure(figsize=(20, 10))
        # plt.imshow(tmp_mask_erod)

        # plt.show()
        # exit()
        # 1 / 0
        if hs_img_path not in list(data_hs.keys()):
            data_hs[hs_img_path] = (img, {
                "label_genotype":
                bbox_obj_dict["label_genotype"],
                "label_dai":
                bbox_obj_dict["label_dai"],
                "label_inoculated":
                bbox_obj_dict["label_inoculated"],
                "label_running":
                bbox_obj_dict["label_running"],
                "bbox_filename": [filename]
            })
        else:
            data_hs[hs_img_path][1]["bbox_filename"].append(filename)
            #print(data_hs[hs_img_path][1]["bbox_filename"])
        # in bbox
        '''
        x_diff = max_x - min_x
        new_x = int(round((x_diff - 25) / 2.))
        min_x = min_x + new_x
        max_x = min_x + 25

        y_diff = max_y - min_y
        new_y = int(round((y_diff - 631) / 2.))
        min_y = min_y + new_y
        max_y = min_y + 631
        '''
        if superpixel:
            step_size = 7
            for y in range(min_y + step_size, max_y - step_size,
                           step_size * 2):
                max_x_ = max_x + 1 if max_x + 1 < classes.shape[
                    0] else max_x  # fix for extreme case on border if bbox
                super_pixel_x_range = np.arange(min_x, max_x_)
                #print(classes.shape, super_pixel_x_range)
                test_classes = classes[super_pixel_x_range, y]
                if np.sum(test_classes) > 0:
                    data_pos_file.append({
                        # "hs_img_idx": len(data_hs) - 1,
                        "path":
                        hs_img_path,
                        "pos":
                        ((super_pixel_x_range[0], super_pixel_x_range[-1]),
                         (y - step_size, y + step_size + 1)),
                        "mask":
                        classes[super_pixel_x_range[0]:super_pixel_x_range[-1],
                                y - step_size:y + step_size + 1],
                        "label_genotype":
                        bbox_obj_dict["label_genotype"],
                        "label_dai":
                        bbox_obj_dict["label_dai"],
                        "label_inoculated":
                        bbox_obj_dict["label_inoculated"],
                        "label_obj":
                        bbox_obj_dict["label_obj"],
                        "label_running":
                        bbox_obj_dict["label_running"],
                    })
        else:
            for x in range(min_x, max_x):
                for y in range(min_y, max_y):
                    # if pixel is in segmentation component add
                    if classes[x, y] == 1:
                        data_pos_file.append({
                            # "hs_img_idx": len(data_hs) - 1,
                            "path":
                            hs_img_path,
                            "pos": (x, y),
                            "label_genotype":
                            bbox_obj_dict["label_genotype"],
                            "label_dai":
                            bbox_obj_dict["label_dai"],
                            "label_inoculated":
                            bbox_obj_dict["label_inoculated"],
                            "label_obj":
                            bbox_obj_dict["label_obj"],
                            "label_running":
                            bbox_obj_dict["label_running"],
                        })

        selected_file_samples = np.arange(len(data_pos_file))
        #print(len(data_pos_file))
        if num_samples_file > 0:
            np.random.shuffle(selected_file_samples)
            selected_file_samples = selected_file_samples[:num_samples_file]

        if balance_inoculated_label_current[
                bbox_obj_dict["label_inoculated"]] + len(
                    selected_file_samples) <= balance_inoculated_label_max_num:
            balance_inoculated_label_current[bbox_obj_dict[
                "label_inoculated"]] += len(selected_file_samples)

            selected_file_samples = [
                d for (i, d) in enumerate(data_pos_file)
                if i in selected_file_samples
            ]
            if bags:
                data_pos += [selected_file_samples]
            else:
                data_pos += selected_file_samples

            cnt_files_used += 1

    print("Leafs used", cnt_files_used, "total #sample", len(data_pos))
    print("Dataset num sample per class", balance_inoculated_label_current)
    return data_hs, data_pos
Ejemplo n.º 45
0
def get_mask_pos(mask):
    pad = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2))
    pad[1:mask.shape[0] + 1, 1:mask.shape[1] + 1] = mask
    edges = pad - erosion(pad, disk(1))
    pos = np.array(get_coord(edges, True)) - 1
    return pos
Ejemplo n.º 46
0
areas = [r.area for r in regionprops(label_image)]
areas.sort()
if len(areas) > 2:
    for region in regionprops(label_image):
        if region.area < areas[-2]:
            for coordinates in region.coords:
                label_image[coordinates[0], coordinates[1]] = 0
binary = label_image > 0


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


selem = disk(10)
binary = binary_closing(binary, selem)
if plot == True:
    plots[5].axis('off')
    plots[5].imshow(binary, cmap=plt.cm.bone)

edges = roberts(binary)
binary = ndi.binary_fill_holes(edges)
if plot == True:
    plots[6].axis('off')
    plots[6].imshow(binary, cmap=plt.cm.bone)

get_high_vals = binary == 0
img[get_high_vals] = 0
if plot == True:
    plots[7].axis('off')
Ejemplo n.º 47
0
def get_segmented_lungs(im, plot=False):
    '''
    This funtion segments the lungs from the given 2D slice.
    '''
    if plot == True:
        f, plots = plt.subplots(8, 1, figsize=(5, 40))
    '''
    Step 1: Convert into a binary image.
    '''
    binary = im < 604
    if plot == True:
        plots[0].axis('off')
        plots[0].imshow(binary, cmap=plt.cm.bone)
    '''
    Step 2: Remove the blobs connected to the border of the image.
    '''
    cleared = clear_border(binary)
    if plot == True:
        plots[1].axis('off')
        plots[1].imshow(cleared, cmap=plt.cm.bone)
    '''
    Step 3: Label the image.
    '''
    label_image = label(cleared)
    if plot == True:
        plots[2].axis('off')
        plots[2].imshow(label_image, cmap=plt.cm.bone)
    '''
    Step 4: Keep the labels with 2 largest areas.
    '''
    areas = [r.area for r in regionprops(label_image)]
    areas.sort()
    if len(areas) > 2:
        for region in regionprops(label_image):
            if region.area < areas[-2]:
                for coordinates in region.coords:
                    label_image[coordinates[0], coordinates[1]] = 0
    binary = label_image > 0
    if plot == True:
        plots[3].axis('off')
        plots[3].imshow(binary, cmap=plt.cm.bone)
    '''
    Step 5: Erosion operation with a disk of radius 2. This operation is
    seperate the lung nodules attached to the blood vessels.
    '''
    selem = disk(2)
    binary = binary_erosion(binary, selem)
    if plot == True:
        plots[4].axis('off')
        plots[4].imshow(binary, cmap=plt.cm.bone)
    '''
    Step 6: Closure operation with a disk of radius 10. This operation is
    to keep nodules attached to the lung wall.
    '''
    selem = disk(10)
    binary = binary_closing(binary, selem)
    if plot == True:
        plots[5].axis('off')
        plots[5].imshow(binary, cmap=plt.cm.bone)
    '''
    Step 7: Fill in the small holes inside the binary mask of lungs.
    '''
    edges = roberts(binary)
    binary = ndi.binary_fill_holes(edges)
    if plot == True:
        plots[6].axis('off')
        plots[6].imshow(binary, cmap=plt.cm.bone)
    '''
    Step 8: Superimpose the binary mask on the input image.
    '''
    get_high_vals = binary == 0
    im[get_high_vals] = 0
    if plot == True:
        plots[7].axis('off')
        plots[7].imshow(im, cmap=plt.cm.bone)

    return im
lena_sp = sk.img_as_float(io.imread('../../lena_sp.bmp'))

cameraman = sk.img_as_float(io.imread('../../cameraman.bmp'))
cameraman_uni = sk.img_as_float(io.imread('../../cameraman_uni.bmp'))
cameraman_nor = sk.img_as_float(io.imread('../../cameraman_nor.bmp'))
cameraman_ric = sk.img_as_float(io.imread('../../cameraman_ric.bmp'))
cameraman_sp = sk.img_as_float(io.imread('../../cameraman_sp.bmp'))

baboon = sk.img_as_float(io.imread('../../baboon.bmp'))
baboon_uni = sk.img_as_float(io.imread('../../baboon_uni.bmp'))
baboon_nor = sk.img_as_float(io.imread('../../baboon_nor.bmp'))
baboon_ric = sk.img_as_float(io.imread('../../baboon_ric.bmp'))
baboon_sp = sk.img_as_float(io.imread('../../baboon_sp.bmp'))

# Kernel
k = morph.disk(2) + 0.0  # +0.0 transforms k into float64

# Apply filters
lena_uni_f = sk.img_as_float(fil.median(lena_uni, k))
lena_nor_f = sk.img_as_float(fil.median(lena_nor, k))
lena_ric_f = sk.img_as_float(fil.median(lena_ric, k))
lena_sp_f = sk.img_as_float(fil.median(lena_sp, k))

cameraman_uni_f = sk.img_as_float(fil.median(cameraman_uni, k))
cameraman_nor_f = sk.img_as_float(fil.median(cameraman_nor, k))
cameraman_ric_f = sk.img_as_float(fil.median(cameraman_ric, k))
cameraman_sp_f = sk.img_as_float(fil.median(cameraman_sp, k))

baboon_uni_f = sk.img_as_float(fil.median(baboon_uni, k))
baboon_nor_f = sk.img_as_float(fil.median(baboon_nor, k))
baboon_ric_f = sk.img_as_float(fil.median(baboon_ric, k))
Ejemplo n.º 49
0
def compute_entropy(im):
  entr_img = entropy(im, disk(10))
Ejemplo n.º 50
0
def seeds(args):
    """
    %prog seeds [pngfile|jpgfile]

    Extract seed metrics from [pngfile|jpgfile]. Use --rows and --cols to crop image.
    """
    p = OptionParser(seeds.__doc__)
    p.set_outfile()
    opts, args, iopts = add_seeds_options(p, args)

    if len(args) != 1:
        sys.exit(not p.print_help())

    (pngfile,) = args
    pf = opts.prefix or op.basename(pngfile).rsplit(".", 1)[0]
    sigma, kernel = opts.sigma, opts.kernel
    rows, cols = opts.rows, opts.cols
    labelrows, labelcols = opts.labelrows, opts.labelcols
    ff = opts.filter
    calib = opts.calibrate
    outdir = opts.outdir
    if outdir != ".":
        mkdir(outdir)
    if calib:
        calib = json.load(must_open(calib))
        pixel_cm_ratio, tr = calib["PixelCMratio"], calib["RGBtransform"]
        tr = np.array(tr)
    nbcolor = opts.changeBackground
    pngfile = convert_background(pngfile, nbcolor)
    resizefile, mainfile, labelfile, exif = convert_image(
        pngfile,
        pf,
        outdir=outdir,
        rotate=opts.rotate,
        rows=rows,
        cols=cols,
        labelrows=labelrows,
        labelcols=labelcols,
    )
    oimg = load_image(resizefile)
    img = load_image(mainfile)

    fig, (ax1, ax2, ax3, ax4) = plt.subplots(
        ncols=4, nrows=1, figsize=(iopts.w, iopts.h)
    )
    # Edge detection
    img_gray = rgb2gray(img)
    logging.debug("Running {0} edge detection ...".format(ff))
    if ff == "canny":
        edges = canny(img_gray, sigma=opts.sigma)
    elif ff == "roberts":
        edges = roberts(img_gray)
    elif ff == "sobel":
        edges = sobel(img_gray)
    edges = clear_border(edges, buffer_size=opts.border)
    selem = disk(kernel)
    closed = closing(edges, selem) if kernel else edges
    filled = binary_fill_holes(closed)

    # Watershed algorithm
    if opts.watershed:
        distance = distance_transform_edt(filled)
        local_maxi = peak_local_max(distance, threshold_rel=0.05, indices=False)
        coordinates = peak_local_max(distance, threshold_rel=0.05)
        markers, nmarkers = label(local_maxi, return_num=True)
        logging.debug("Identified {0} watershed markers".format(nmarkers))
        labels = watershed(closed, markers, mask=filled)
    else:
        labels = label(filled)

    # Object size filtering
    w, h = img_gray.shape
    canvas_size = w * h
    min_size = int(round(canvas_size * opts.minsize / 100))
    max_size = int(round(canvas_size * opts.maxsize / 100))
    logging.debug(
        "Find objects with pixels between {0} ({1}%) and {2} ({3}%)".format(
            min_size, opts.minsize, max_size, opts.maxsize
        )
    )

    # Plotting
    ax1.set_title("Original picture")
    ax1.imshow(oimg)

    params = "{0}, $\sigma$={1}, $k$={2}".format(ff, sigma, kernel)
    if opts.watershed:
        params += ", watershed"
    ax2.set_title("Edge detection\n({0})".format(params))
    closed = gray2rgb(closed)
    ax2_img = labels
    if opts.edges:
        ax2_img = closed
    elif opts.watershed:
        ax2.plot(coordinates[:, 1], coordinates[:, 0], "g.")
    ax2.imshow(ax2_img, cmap=iopts.cmap)

    ax3.set_title("Object detection")
    ax3.imshow(img)

    filename = op.basename(pngfile)
    if labelfile:
        accession = extract_label(labelfile)
    else:
        accession = pf

    # Calculate region properties
    rp = regionprops(labels)
    rp = [x for x in rp if min_size <= x.area <= max_size]
    nb_labels = len(rp)
    logging.debug("A total of {0} objects identified.".format(nb_labels))
    objects = []
    for i, props in enumerate(rp):
        i += 1
        if i > opts.count:
            break

        y0, x0 = props.centroid
        orientation = props.orientation
        major, minor = props.major_axis_length, props.minor_axis_length
        major_dx = cos(orientation) * major / 2
        major_dy = sin(orientation) * major / 2
        minor_dx = sin(orientation) * minor / 2
        minor_dy = cos(orientation) * minor / 2
        ax2.plot((x0 - major_dx, x0 + major_dx), (y0 + major_dy, y0 - major_dy), "r-")
        ax2.plot((x0 - minor_dx, x0 + minor_dx), (y0 - minor_dy, y0 + minor_dy), "r-")

        npixels = int(props.area)
        # Sample the center of the blob for color
        d = min(int(round(minor / 2 * 0.35)) + 1, 50)
        x0d, y0d = int(round(x0)), int(round(y0))
        square = img[(y0d - d) : (y0d + d), (x0d - d) : (x0d + d)]
        pixels = []
        for row in square:
            pixels.extend(row)
        logging.debug(
            "Seed #{0}: {1} pixels ({2} sampled) - {3:.2f}%".format(
                i, npixels, len(pixels), 100.0 * npixels / canvas_size
            )
        )

        rgb = pixel_stats(pixels)
        objects.append(Seed(filename, accession, i, rgb, props, exif))
        minr, minc, maxr, maxc = props.bbox
        rect = Rectangle(
            (minc, minr), maxc - minc, maxr - minr, fill=False, ec="w", lw=1
        )
        ax3.add_patch(rect)
        mc, mr = (minc + maxc) / 2, (minr + maxr) / 2
        ax3.text(mc, mr, "{0}".format(i), color="w", ha="center", va="center", size=6)

    for ax in (ax2, ax3):
        ax.set_xlim(0, h)
        ax.set_ylim(w, 0)

    # Output identified seed stats
    ax4.text(0.1, 0.92, "File: {0}".format(latex(filename)), color="g")
    ax4.text(0.1, 0.86, "Label: {0}".format(latex(accession)), color="m")
    yy = 0.8
    fw = must_open(opts.outfile, "w")
    if not opts.noheader:
        print(Seed.header(calibrate=calib), file=fw)
    for o in objects:
        if calib:
            o.calibrate(pixel_cm_ratio, tr)
        print(o, file=fw)
        i = o.seedno
        if i > 7:
            continue
        ax4.text(0.01, yy, str(i), va="center", bbox=dict(fc="none", ec="k"))
        ax4.text(0.1, yy, o.pixeltag, va="center")
        yy -= 0.04
        ax4.add_patch(
            Rectangle((0.1, yy - 0.025), 0.12, 0.05, lw=0, fc=rgb_to_hex(o.rgb))
        )
        ax4.text(0.27, yy, o.hashtag, va="center")
        yy -= 0.06
    ax4.text(
        0.1,
        yy,
        "(A total of {0} objects displayed)".format(nb_labels),
        color="darkslategray",
    )
    normalize_axes(ax4)

    for ax in (ax1, ax2, ax3):
        xticklabels = [int(x) for x in ax.get_xticks()]
        yticklabels = [int(x) for x in ax.get_yticks()]
        ax.set_xticklabels(xticklabels, family="Helvetica", size=8)
        ax.set_yticklabels(yticklabels, family="Helvetica", size=8)

    image_name = op.join(outdir, pf + "." + iopts.format)
    savefig(image_name, dpi=iopts.dpi, iopts=iopts)
    return objects
Ejemplo n.º 51
0
def deep_watershed_mibi(model_output,
                        radius=10,
                        maxima_threshold=0.1,
                        interior_threshold=0.2,
                        small_objects_threshold=0,
                        fill_holes_threshold=0,
                        interior_model='pixelwise-interior',
                        maxima_model='inner-distance',
                        interior_model_smooth=1,
                        maxima_model_smooth=0,
                        pixel_expansion=None):
    """Postprocessing function for multiplexed deep watershed models. Thresholds the inner
    distance prediction to find cell centroids, which are used to seed a marker
    based watershed of the pixelwise interior prediction.

    Args:
        model_output (dict): DeepWatershed model output. A dictionary containing key: value pairs
            with the transform name and the corresponding output. Currently supported keys:

            - inner_distance: Prediction for the inner distance transform.
            - outer_distance: Prediction for the outer distance transform.
            - fgbg: Foreground prediction for the foregound/background transform.
            - pixelwise_interior: Interior prediction for the interior/border/background transform.

        radius (int): Radius of disk used to search for maxima
        maxima_threshold (float): Threshold for the maxima prediction.
        interior_threshold (float): Threshold for the interior prediction.
        small_objects_threshold (int): Removes objects smaller than this size.
        fill_holes_threshold (int): maximum size for holes within segmented objects to be filled
        interior_model: semantic head to use to predict interior of each object
        maxima_model: semantic head to use to predict maxima of each object
        interior_model_smooth: smoothing factor to apply to interior model predictions
        maxima_model_smooth: smoothing factor to apply to maxima model predictions
        pixel_expansion: optional number of pixels to expand segmentation labels

    Returns:
        numpy.array: Uniquely labeled mask.

    Raises:
        ValueError: if interior_model or maxima_model names not in valid_model_names
        ValueError: if interior_model or maxima_model predictions do not have length 4
    """

    interior_model, maxima_model = interior_model.lower(), maxima_model.lower()

    valid_model_names = {
        'inner-distance', 'outer-distance', 'fgbg-fg', 'pixelwise-interior'
    }

    for name, model in zip(['interior_model', 'maxima_model'],
                           [interior_model, maxima_model]):
        if model not in valid_model_names:
            raise ValueError('{} must be one of {}, got {}'.format(
                name, valid_model_names, model))

    interior_predictions = model_output[interior_model]
    maxima_predictions = model_output[maxima_model]

    zipped = zip(['interior_prediction', 'maxima_prediction'],
                 (interior_predictions, maxima_predictions))
    for name, arr in zipped:
        if len(arr.shape) != 4:
            raise ValueError('Model output must be of length 4. The {} model '
                             'provided was of shape {}'.format(
                                 name, arr.shape))

    label_images = []
    for batch in range(interior_predictions.shape[0]):
        interior_batch = interior_predictions[batch, ..., 0]
        interior_batch = nd.gaussian_filter(interior_batch,
                                            interior_model_smooth)

        if pixel_expansion is not None:
            interior_batch = dilation(interior_batch,
                                      selem=square(pixel_expansion * 2 + 1))

        maxima_batch = maxima_predictions[batch, ..., 0]
        maxima_batch = nd.gaussian_filter(maxima_batch, maxima_model_smooth)

        markers = h_maxima(image=maxima_batch,
                           h=maxima_threshold,
                           selem=disk(radius))

        markers = label(markers)

        label_image = watershed(-interior_batch,
                                markers,
                                mask=interior_batch > interior_threshold,
                                watershed_line=0)

        # Remove small objects
        label_image = remove_small_objects(label_image,
                                           min_size=small_objects_threshold)

        # fill in holes that lie completely within a segmentation label
        if fill_holes_threshold > 0:
            label_image = fill_holes(label_image, size=fill_holes_threshold)

        # Relabel the label image
        label_image, _, _ = relabel_sequential(label_image)

        label_images.append(label_image)

    label_images = np.stack(label_images, axis=0)
    label_images = np.expand_dims(label_images, axis=-1)

    return label_images
Ejemplo n.º 52
0
def find_beamstop_rect(img,
                       center=None,
                       threshold=0.5,
                       pad=1,
                       minsize=500,
                       savefig=False,
                       drc='.'):
    """Find rectangle fitting the beamstop.

    1. Radially scale the image (divide each point in the image by the radial average)
    2. Segment the image via thresholding. The beamstop is identified by the area where the image < radially scaled image
    3. Contour the segmented image.
    4. Find minimum bounding rectangle for points that define the contours
    5. The contour closest to the beam center is then taken as beamstop

    input:
        image, nxn 2D np.array defining the image. The average of the image stack works well
        center, 2x1 np.array defining the center of the image. If omitted, will be find automatically
        threshold, float representing the threshold value for segmentation
        pad, int defining the padding of the beamstop to make it seem a bit larger
        minsize, int defining minimum size of the beamstop
        savefig, boolean that defines whether the result should be saved
        drc, location where to place the image is saved

    output:
        4x2 np.array defining the corners of the rectangle
    """

    if center is None:
        center = find_beam_center_with_beamstop(img, z=99)

    # get radially averaged image
    r_map = radial_average(img, center=center, as_radial_map=True)

    radial_scaled = img / r_map

    # image segmentation
    seg = radial_scaled < threshold

    seg = morphology.remove_small_objects(seg, 64)
    seg = morphology.remove_small_holes(seg, 64)

    # pad the beamstop to make the outline a big bigger
    if pad:
        seg = morphology.binary_dilation(seg, selem=morphology.disk(pad))

    arr = find_contours(seg, 0.5)

    if len(arr) > 1:
        rects = [
            minimum_bounding_rectangle(a) for a in arr if len(a) > minsize
        ]

        a = [np.mean(rect, axis=0) for rect in rects]
        dists = [np.linalg.norm(b - center) for b in a]
        i = np.argmin(dists)

        rect = rects[i]
    else:
        a = arr[0]
        rect = minimum_bounding_rectangle(a)

    # This is not robust if there are other shaded areas
    # rect = sorted(arr, key=lambda x: len(x), reverse=True)[0]
    # rect = minimum_bounding_rectangle(beamstop)

    if savefig:
        import matplotlib
        matplotlib.use('pdf')
        import matplotlib.pyplot as plt

        fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(16, 8))
        for ax in ax1, ax2, ax3:
            ax.axis('off')

        ax1.imshow(radial_scaled, vmax=np.percentile(radial_scaled, 99))
        ax1.set_title('Radially scaled image')

        cx, cy = center
        ax1.scatter(cy, cx, marker='+', color='red')

        ax2.imshow(seg)
        ax2.set_title('Segmented image')

        ax3.imshow(img, vmax=np.percentile(img, 99))
        ax3.set_title('Mean image showing beamstop')
        ax3.scatter(cy, cx, marker='+')

        bx, by = np.vstack((rect, rect[0])).T
        ax3.plot(by, bx, 'r-o')

        fn = Path(drc) / 'beamstop.png'
        plt.savefig(fn, dpi=150, bbox_inches='tight', pad_inches=0.1)

    return rect
Ejemplo n.º 53
0
"""

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

from skimage.morphology import (square, rectangle, diamond, disk, cube,
                                octahedron, ball, octagon, star)

# Generate 2D and 3D structuring elements.
struc_2d = {
    "square(15)": square(15),
    "rectangle(15, 10)": rectangle(15, 10),
    "diamond(7)": diamond(7),
    "disk(7)": disk(7),
    "octagon(7, 4)": octagon(7, 4),
    "star(5)": star(5)
}

struc_3d = {
    "cube(11)": cube(11),
    "octahedron(5)": octahedron(5),
    "ball(5)": ball(5)
}

# Visualize the elements.
fig = plt.figure(figsize=(8, 8))

idx = 1
for title, struc in struc_2d.items():
Ejemplo n.º 54
0
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

from skimage.morphology import disk, ball
from skimage.draw import ellipsoid
from skimage import measure

# https://scikit-image.org/docs/stable/auto_examples/numpy_operations/plot_structuring_elements.html#sphx-glr-download-auto-examples-numpy-operations-plot-structuring-elements-py

# fig = plt.figure(figsize=(12, 4))
fig = plt.figure(figsize=(8, 8))
# create a disk 2D
# ax = fig.add_subplot(1, 3, 1)
ax = fig.add_subplot(111)
d = disk(4)
ax.imshow(d, cmap="Paired", vmin=0, vmax=12)
for i in range(d.shape[0]):
    for j in range(d.shape[1]):
        ax.text(j, i, d[i, j], ha="center", va="center", color="white")
fig.tight_layout()
plt.show()

# create a ball 3D
fig = plt.figure(figsize=(8, 8))
# ax = fig.add_subplot(1, 3, 2, projection="3d")
ax = fig.add_subplot(111, projection="3d")
b = ball(4)
ax.voxels(b)
dmax = 10
ax.set_xlim(0, dmax)
Ejemplo n.º 55
0
def process(video, filename, outdir, skipdir):
    global mask, value, drawing
    video2 = video.copy()
    current_it = 0

    cv.namedWindow('input')
    cv.setMouseCallback('input', onmouse)
    cv.moveWindow('input', video.shape[2] + 10, 90)

    video_as_num = color_as_num(video)
    original_fillmask = video_as_num != (256**3 - 1)

    paused = False

    while (1):
        current_frame = current_it // 25
        cv.imshow('input', video[current_frame % video.shape[0]])
        k = cv.waitKey(1)
        if not paused:
            current_it += 1

        # key bindings
        if k == 27:  # esc to exit
            break
        elif k == ord('0'):  # BG drawing
            print(" Mark region to fill with left mouse button \n")
            value = FILL_AREA
            mask = np.zeros(video.shape[1:3], dtype=np.uint8)
        elif k == ord('1'):  # FG drawing
            print("Mark colors to fill with left mouse button \n")
            value = FILL_COLOR
            mask = np.zeros(video.shape[1:3], dtype=np.uint8)
        elif k == ord('2'):  # PR_BG drawing
            print(
                "Mark area to fill (in  all frames) with left mouse button \n")
            value = FILL_ALL_COLOR
            mask = np.zeros(video.shape[1:3], dtype=np.uint8)
        elif k == ord('3'):  # PR_BG drawing
            print("Mark connected area to fill with left mouse button \n")
            value = FILL_CONNECTED
            mask = np.zeros(video.shape[1:3], dtype=np.uint8)
        elif k == ord('f'):  # fill with random background
            color = np.random.randint(255, size=3)
            video_as_num = color_as_num(video)
            fillmask = video_as_num != (256**3 - 1)
            for i in range(len(video)):
                video[i,
                      scipy.ndimage.morphology.binary_fill_holes(fillmask[i]
                                                                 )] = color
            video[original_fillmask] = (0, 0, 0)
        elif k == ord('d'):  #dilation of image
            video_as_num = color_as_num(video)
            fillmask = video_as_num != (256**3 - 1)
            for i in range(len(video)):
                video[i, binary_dilation(fillmask[i])] = (0, 0, 0)
        elif k == ord('e'):  #erosion of image
            video_as_num = color_as_num(video)
            fillmask = video_as_num != (256**3 - 1)
            for i in range(len(video)):
                video[i,
                      np.logical_not(binary_erosion(fillmask[i]))] = (255, 255,
                                                                      255)
        elif k == ord('i'):  #inversion of colors
            video = 255 - video
        elif k == ord('p'):  #image denoising
            video = np.array([
                img_as_ubyte(
                    np.concatenate([
                        median(frame[..., i], disk(1))[..., np.newaxis]
                        for i in range(3)
                    ],
                                   axis=-1)) for frame in video
            ])
        elif k == ord('l'):  #pause animation
            paused = not paused
        elif k == ord('n'):  # save image
            mimsave(os.path.join(outdir, filename), video[..., ::-1])
            break
        elif k == ord('s'):  # skip image
            mimsave(os.path.join(skipdir, filename), video2[..., ::-1])
            break
        elif k == ord('r'):  # reset everything
            print("resetting \n")
            drawing = False
            video = video2.copy()
            mask = np.zeros(video.shape[1:3], dtype=np.uint8)

        if mask.sum() == 0:
            continue

        if value == FILL_AREA:
            video[:, mask.astype(bool)] = (255, 255, 255)
            mask = np.zeros(video.shape[1:3], dtype=np.uint8)
        elif value == FILL_COLOR:
            colors = video[current_frame % video.shape[0]][mask.astype(bool)]
            colors = color_as_num(val=colors).reshape((-1, ))
            colors = np.unique(colors)
            video_as_num = color_as_num(video)
            for color in colors:
                video[video_as_num == color] = (255, 255, 255)
            mask = np.zeros(video.shape[1:3], dtype=np.uint8)
        elif value == FILL_ALL_COLOR:
            colors = video[:, mask.astype(bool)]
            colors = color_as_num(val=colors).reshape((-1, ))
            colors = np.unique(colors)
            video_as_num = color_as_num(video)
            for color in colors:
                video[video_as_num == color] = (255, 255, 255)
            mask = np.zeros(video.shape[1:3], dtype=np.uint8)
        elif value == FILL_CONNECTED:
            color = np.random.randint(255, size=3)
            video_as_num = color_as_num(video)
            fillmask = video_as_num != (256**3 - 1)
            for i in range(len(video)):
                labels = label(fillmask[i])
                index = labels[mask]
                video[i, labels == np.unique(index)] = color
            video[original_fillmask] = (0, 0, 0)
            mask = np.zeros(video.shape[1:3], dtype=np.uint8)

    cv.destroyAllWindows()
Ejemplo n.º 56
0
def Bim_segmowgli(J, R=None, Amin=20, sig=2):
    """  F, m = Bsegmowgli(J, R, Amin, sig)

     Toolbox: Balu
      Segmentation of regions in image J using LoG edge detection.
      R   : binary image of same size of J that indicates the piexels where
            the segmentation will be performed. Default R = ones(size(J));
      Amin: minimum area of the segmented details.
      sig : sigma of LoG edge detector.
      F   : labeled image of the segmentation.
      m   : numbers of segmented regions.

      Example 1:
            from balu.ImagesAndData import balu_imageload
            from balu.ImageProcessing import Bim_segmowgli
            from matplotlib.pyplot import figure, imshow, show, title

            I = balu_imageload('rice.png')
            figure(1), imshow(I, cmap='gray'), title('test image')
            F, m = Bim_segmowgli(I, R=None, Amin=40, sig=1.5)
            figure(2), imshow(F, cmap='gray'), title('segmented image')
            show()


      Example 2:
            from balu.ImagesAndData import balu_imageload
            from balu.ImageProcessing import Bim_segmowgli, Bim_segbalu
            from matplotlib.pyplot import figure, imshow, show, title

            I = balu_imageload('testimg4.jpg')
            figure(1), imshow(I), title('test image')
            R, E, J = Bim_segbalu(I, -0.1)
            figure(2), imshow(R, cmap='gray'), title('segmented object')
            G = I[:, :, 1]
            F, m = Bim_segmowgli(G, R=R, Amin=30, sig=2)
            figure(3), imshow(F), title('segmented regions')
            show()

         Another way to display the results:
            Bio_edgeview(I,F>0)

     See also Bsegbalu.

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

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

    if R is None:
        R = np.ones(J.shape, bool)

    if R.size == 0:
       R = np.ones(J.shape, bool)

    if len(J.shape) < 3:
        N, M = J.shape
        P = 1
    else:
        N, M, P = J.shape

    if P == 3:
        J = rgb2gray(J.astype(float))

    se = disk(3)
    Re = dilation(R, se)
    E = R - binary_erosion(R)

    #In the original implementation of Balu Matlab they used LoG edge detection.
    #Here we used edge detection via Canny algorithm. Then we used 0.5*sig because
    #that way we get similar results to Matlab version using edge command
    #L = canny(J, sigma=sig)
    L = edge_LoG(J, sigma=sig)
    L = np.logical_or(np.logical_and(L, Re), E)
    W = remove_small_objects(L, min_size=Amin, connectivity=2)
    F = label(np.logical_not(W), 4)
    m = int(F.max())
    for i in range(1, m + 1):
        ii, jj = np.where(F == i)
        if (ii.size < Amin) or (ii.size > N*M / 6.0):
            W[ii, jj] = 1

    F = label(np.logical_not(W), 4)
    m = int(F.max())
    print('{0} segmented regions.\n'.format(m))
    return F, m
Ejemplo n.º 57
0
from matplotlib import pyplot as plt
from skimage import data, feature, color, exposure, filters
import skimage.morphology as mp
import math
files = [
    'samolot01.jpg', 'samolot07.jpg', 'samolot08.jpg', 'samolot09.jpg',
    'samolot10.jpg', 'samolot16.jpg'
]
plt.figure(figsize=(30, 20))
for i, file in enumerate(files):
    img = data.imread(file, True)
    img = color.rgb2gray(img)
    img = feature.canny(img, sigma=6)
    img = filters.sobel(img)
    img = exposure.adjust_sigmoid(img, 0.1, 1000)
    img = mp.dilation(img, mp.disk(1))
    plt.subplot(math.ceil(len(files) / 3), 3, i + 1)
    plt.axis('off')
    plt.imshow(img, cmap="gray")

plt.tight_layout(pad=0)
plt.savefig('Contours_generated.jpg')
Ejemplo n.º 58
0
%matplotlib qt5


row=df_4.iloc[0]

file_selected=os.path.join(r'e:\OneDrive\KS-XR\X-ray képek\Test\roi',
#                           row['Date'].replace('.',''),
                           str(row['Rotor_ID'])+'-'+row['Orientation']+'.jpg')

im_orig=io.imread(file_selected)

im_adj=exposure.rescale_intensity(im_orig,in_range=(100, 200))

im_bilat=restoration.denoise_bilateral(im_adj,multichannel=False,sigma_spatial=2)
im_frangi=filters.frangi(im_bilat,scale_range=(1, 5),scale_step=1)
im_laplace=filters.laplace(im_bilat,10)
#im_hessian=filters.hessian(im_bilat,scale_range=(1, 5),scale_step=1)
im_ent = filters.rank.entropy(im_bilat, morphology.disk(5))

im_mask = segmentation.felzenszwalb(im_adj, scale=200, sigma=2,min_size=10)

im_bound=segmentation.mark_boundaries(im_orig,im_mask)

# ToDO: Watershed using DoG centers

segments = segmentation.slic(im_adj, n_segments=200, compactness=1)
im_bound=segmentation.mark_boundaries(im_orig,segments)


plt.imshow(im_mask,cmap='gray')
Ejemplo n.º 59
0
            thresh_img = np.where(image<0, 1.0, 0.)
            regions = regionprops(thresh_img.astype(int))
            
            for region in regions:

                if region.area > maxAllowedArea:
                    prediction = unet_predict([image],predict_fn)
                    prediction[prediction<THRESHOLD] = 0
                    prediction[prediction>0] = 1
                    prediction = prediction.reshape(324,324)

                    # localize the center of nodule
                    if np.amax(prediction)>0:
                        centers = []
                        selem = morphology.disk(1)
                        image_eroded = morphology.binary_erosion(prediction,selem=selem)

                        label_im, nb_labels = ndimage.label(image_eroded)
                        for i in xrange(1,nb_labels+1):
                            blob_i = np.where(label_im==i,1,0)
                            mass = center_of_mass(blob_i)
                            centers.append([mass[1],mass[0]])

                        for center in centers:
                            world_coords = voxel_2_world(np.asarray([center[0],center[1],slice_index]),np.asarray(origin),np.asarray([1,1,1]))
                            resnet_coords = world_2_voxel(np.asarray(world_coords),np.asarray(origin),np.asarray(OUTPUT_SPACING))
                            resnet_coords = np.floor(resnet_coords).astype('int16')
                            nodule_centers.append(resnet_coords)
                    continue
Ejemplo n.º 60
0
def findNodes(filename):
    # filename = '040519_P9_009_cropped.tif'
    # filename2 = '040519_E5_013.tif'

    image = plt.imread(filename)
    cells = np.asarray(image)

    scalebar_start = np.size(cells, 0)
    found_scalebar = False
    for row in range(np.size(cells, 0)):
        if not found_scalebar:
            if np.amin(cells[row]) == 0:
                scalebar_start = row
                found_scalebar = True

    cells = cells[:scalebar_start][:]

    rgb_arr = np.stack((cells, cells, cells), axis=-1)

    greyscale = color.rgb2gray(cells)
    gray = exposure.rescale_intensity(greyscale)
    im_thresh = filters.threshold_minimum(gray)


    # print(binary)

    cells = filters.rank.median(gray, morphology.disk(7))

    binary = cells > im_thresh * .6
    for i in range(5):
        binary = morphology.erosion(binary)

    binary = morphology.remove_small_objects(binary, min_size=64)
    binary = 1 - binary
    #plt.imshow(binary)
    #plt.show()

    #
    # plt.imshow(binary_denoise, cmap='Greys',  interpolation='nearest')
    #plt.imshow(cells, cmap='Greys',  interpolation='nearest')
    #plt.show()

    # edges = feature.canny(gray/255.)
    # # edges = filters.sobel(gray)
    # fill_cells = ndi.binary_fill_holes(edges)
    # label_objects, nb_labels = ndi.label(fill_cells)
    # sizes = np.bincount(label_objects.ravel())

    # mask_sizes = sizes > 20
    # mask_sizes[0] = 0
    # cells_cleaned = mask_sizes[label_objects]

    # Trying to get labels to change to individual cells
    # then can use that with find objects

    elevation_map = filters.sobel(cells)

    #markers = np.zeros_like(cells)
    #markers[cells < 50] = 1
    #markers[cells > 120] = 2

    markers = filters.rank.gradient(cells, morphology.disk(6), mask=binary) < 20
    markers = ndi.label(markers)[0]



    #gradient = filters.rank.gradient(cells, morphology.disk(2))



    segmentation = morphology.watershed(elevation_map, markers, compactness=100, mask=binary)

    # NEXT LINE IS ISSUE IF THERE IS A BUNCH OF CELLS BUT NECESSARY FOR
    # THE PROGRAM TO RECOGNIZE DIFFERENT CELLS- NOT SURE WHY
    segmentation = ndi.binary_fill_holes(segmentation - 1)
    labeled_cells, cell_num = ndi.label(segmentation)
    sizes = np.bincount(labeled_cells.ravel())
    for x, size in enumerate(sizes):
        if (size < 900):
            np.delete(sizes, [x])
    # print(sizes)
    # print(sizes.mean())
    # print(np.median(sizes))
    cellLocs = ndi.find_objects(labeled_cells)
    return [cellLocs, sizes.mean() + np.median(sizes) /2]