Ejemplo n.º 1
0
def ApplyThresholdToImageRegion(image2, Tb, Bb, Lb, Rb,shouldThresholdImage):

    image = rgb2gray(image2)

    global foregroundPixelValue
    global backgroundPixelValue

    thresholdValue = threshold_otsu(image)

    NumberOfRows = image.shape[0]
    NumberOfColumns = image.shape[1]

    numberOfBlackPixels = 0
    numberOfWhitePixels = 0
    selem = disk(3)

    # simpe thresholding
    for y in range(NumberOfRows):
        for x in range(NumberOfColumns):

            isWithinBoundary = IsWithinBoundary(y,x,image2, Tb, Bb, Lb, Rb,shouldThresholdImage)

            if (isWithinBoundary):
                if image[y,x] > thresholdValue:
                    #black
                    image[y,x] = 0
                    numberOfBlackPixels += 1
                else:
                    #white
                    image[y,x] = 1
                    numberOfWhitePixels += 1

    # assume foreground has more pixels in face region
    if (numberOfWhitePixels > numberOfBlackPixels):
        foregroundPixelValue = 1
        backgroundPixelValue = 0
        #print("foreground color is white")
    else:
        foregroundPixelValue = 0
        backgroundPixelValue = 1
        #print("foreground color is black")

    image = opening(image,selem)
    if (foregroundPixelValue == 0):
        image = opening(image,selem)
    else:
        image = closing(image,selem)


    if drawFaceImages:
        io.imshow(image)
        io.show()

    return image
Ejemplo n.º 2
0
def smooth_disk(index_array, threshold, disk_size):
    """Applies morphological opening to a numpy array
    User inputs numpy array to be opened, threshold and disk_size for disk shaped
    structuring element

    Parameters
    ----------------
    morph_index : numpy array (either MSI or MBI)
    threshold : numerical input (integer, float)
    disk_size : numerical input (integer) size of structuring elements (number of pixels)

    Returns
    ---------------
    Morphologically opened and "smoothed" numpy arrays
    Return is a mask (values of 0 or 1) that has had smal
    """
    # Create a mask based on user defined threshold values
    index_threshold_mask = (index_array >= threshold).astype(int)

    # Define the structure element based on the user defined disk size
    selem = disk(disk_size)

    # Apply the morphological opening function to the thresholded raster
    index_opened = opening(index_threshold_mask, selem)

    return index_opened
Ejemplo n.º 3
0
def cleanForFigure(czi_f, coords_f, dapi_f, gfp_f):

    coords = []
    for line in open(coords_f):
        fname, xmin, xmax, ymin, ymax = line.strip().split()
        coords.append(map(int, [xmin, xmax, ymin, ymax]))

    image_arrays = openFile(czi_f)
    images = image_arrays[0, :, 0, :, :, :, 0]
    colors, zsize, xsize, ysize = images.shape
    dapi = images[0, :, :, :]
    gfp = images[1, :, :, :]

    newdapi = zeros((zsize, xsize, ysize), dtype=float)
    newgfp = zeros((zsize, xsize, ysize), dtype=float)

    for xmin, xmax, ymin, ymax in coords:
        print xmin, xmax, ymin, ymax

        # Subtract backgrounds.
        dapiblock = img_as_float(dapi[:, xmin:xmax, ymin:ymax])

        gfpblock = img_as_float(gfp[:, xmin:xmax, ymin:ymax])

        print 'GFP Opening'
        gfpbg = 3 * morphology.opening(gfpblock, morphology.ball(3))

        newdapi[:, xmin:xmax, ymin:ymax] = dapiblock
        newgfp[:, xmin:xmax, ymin:ymax] = gfpblock - gfpbg

    print dapi.shape
    io.imsave(dapi_f, img_as_ubyte(newdapi.max(axis=0)))
    io.imsave(gfp_f, img_as_ubyte(newgfp.max(axis=0)))
Ejemplo n.º 4
0
def rmSpecales(img):
    '''
    img should be greyscale
  '''
    selem = disk(4)
    exp = opening(img, selem)
    return (exp)
Ejemplo n.º 5
0
def compute_spiculation(orig, segmented_mask):

    # filter approach based on Huo and Giger (1995)
    # they use Sobel but Scharr is supposed to be rotation invariant (?)
    theta_A, magnitude_A = compute_scharr(orig, mask=segmented_mask)
    std_dev_A = compute_gradient_std(theta_A, magnitude_A)

    # B: just use a 3? pixel border
    border_mask = get_border_pixels(segmented_mask)
    theta_B, magnitude_B = compute_scharr(orig, mask=border_mask)
    std_dev_B = compute_gradient_std(theta_B, magnitude_B)

    # C: use the whole ROI
    # ideally would reduce to 20 pixel adjacent area but not working right now
    # ymin, xmin, ymax, xmax = regionprops(segmented_mask[0]).bbox
    # bbox_mask = np.zeros(orig.shape)
    # bbox_mask[ymin:ymax, xmin:xmax] = 1
    # region_bbox = orig * bbox_mask
    theta_C, magnitude_C = compute_scharr(orig)
    std_dev_C = compute_gradient_std(theta_C, magnitude_C)

    # D: use non-region ROI after applying opening (circular, arbitrary size right now)
    open_nonregion = opening(np.where(segmented_mask == 0, 1, 0), disk(5))
    theta_D, magnitude_D = compute_scharr(orig, mask=open_nonregion)
    std_dev_D = compute_gradient_std(theta_D, magnitude_D)

    # higher standard deviation here indicates more spiculation
    return {'A': std_dev_A, 'B': std_dev_B, 'C': std_dev_C, 'D': std_dev_D}
Ejemplo n.º 6
0
def get_results(file_list, cfg):
    for img_path in file_list:
        img_name = img_path.split('\\')[-1][:-4]
        c = '' if not cfg.sub_folder else k
        test_img, rec_img, ssim_residual_map, l1_residual_map = get_residual_map(
            img_path, cfg)

        ssim_residual_map *= cfg.depr_mask
        if 'ssim' in cfg.loss:
            l1_residual_map *= cfg.depr_mask

        mask = np.zeros((cfg.im_resize, cfg.im_resize))
        mask[ssim_residual_map > cfg.ssim_threshold] = 1
        mask[l1_residual_map > cfg.l1_threshold] = 1

        kernel = morphology.disk(4)
        mask = morphology.opening(mask, kernel)
        mask *= 255

        vis_img = set_img_color(test_img.copy(),
                                mask,
                                weight_foreground=0.3,
                                grayscale=cfg.grayscale)

        cv2.imwrite(cfg.save_dir + '/' + c + '_' + img_name + '_residual.png',
                    mask)
        cv2.imwrite(cfg.save_dir + '/' + c + '_' + img_name + '_origin.png',
                    test_img)
        cv2.imwrite(cfg.save_dir + '/' + c + '_' + img_name + '_rec.png',
                    rec_img)
        cv2.imwrite(cfg.save_dir + '/' + c + '_' + img_name + '_visual.png',
                    vis_img)
Ejemplo n.º 7
0
def smooth(image):
  filename_split = os.path.splitext(image)
  filename_zero, fileext = filename_split
  basename = os.path.basename(filename_zero)
  im = np.array(Image.open(image))
  with rasterio.open(image) as r:
    im = r.read()
    p = r.profile
  im = im.squeeze()
  selem = disk(1)
  print("image min and max: ", im.min(),im.max())
  dilated = skimage.morphology.dilation(im, selem)
  print("dilated image min and max: ", dilated.min(),dilated.max())
  eroded = skimage.morphology.erosion(im, selem)
  print("eroded image min and max: ", eroded.min(),eroded.max())
  opened = opening(im, selem)
  print("opened image min and max: ", opened.min(),opened.max())
  closed = closing(im, selem)
  print("closed image min and max: ", closed.min(),closed.max())
  #im[im==1]=0
  #im[im==2]=1
  median = cv2.medianBlur(im,9)
  average = cv2.blur(im,(9,9))
  #gaussian = cv2.GaussianBlur(im,(9,9),0)
  gaussian = cv2.GaussianBlur(dilated,(9,9),0)
  #bilateral = cv2.bilateralFilter(im,9,75,75)
  bilateral = cv2.bilateralFilter(gaussian,9,75,75)
  with rasterio.open(outPath+basename+fileext, 'w', **p) as dst:
      dst.write(bilateral, 1)
  color_outPath = outPath+'color/'
  if not os.path.exists(color_outPath):
        os.mkdir(color_outPath) 
  colored_image = color_outPath+basename+'.png'
  os.system("gdaldem color-relief", bilateral, colorfile, colored_image)
  return im, dilated, eroded, opened, closed, median, average, gaussian, bilateral
Ejemplo n.º 8
0
def lightness_mask(img_rgb, lightness=0.45, gamma=3, remove_disk=2, rejoin=8):
    # params:
    lightness_param = lightness
    gamma_param = gamma
    remove_disk_param = remove_disk
    rejoin_square_param = rejoin

    # enhance contrast
    img = exposure.adjust_gamma(img_rgb, gamma_param, 1)

    # split to h s v
    img = rgb2hsv(img)
    h = img[:, :, 0]
    s = img[:, :, 1]
    v = img[:, :, 2]

    # make lightness mask
    mask = (v > lightness_param).astype(np.uint8)

    # remove small regions from mask
    disk_elem = disk(remove_disk_param)
    opened = opening(mask, selem=disk_elem)

    # rejoin colored pionts
    square_elem = square(rejoin_square_param)
    dilated = dilation(opened, selem=square_elem)

    io.imshow(dilated)
    plt.show()

    return dilated.astype(bool)
def Mask_ROI_op(im,disk_size,thresh=None,black_spots=None,with_morph=False):
    l=np.array([0,0])
    if not isinstance(im,l.__class__):
        numpy_array=np.array(im)
    else:
        numpy_array=im 
    if len(numpy_array.shape)==3:
        numpy_array=numpy_array[:,:,0:3].mean(axis=2)        
    selem = disk(disk_size)
    openin = opening(numpy_array, selem)
    if thresh is None:
        thresh = threshold_otsu(openin)
    binary = openin > thresh
    if binary.dtype=='bool':
        binary=binary+0
    if black_spots is not None:    
        binary2 = openin > black_spots
        binary2 = binary2 + 0
        binary = binary - binary2 
    else:
        binary -=1
    binary=binary * -1
    if with_morph:
        return(binary,openin)
    else:
        return(binary)                     
Ejemplo n.º 10
0
def upsample_smooth(image):
  filename_split = os.path.splitext(image)
  filename_zero, fileext = filename_split
  basename = os.path.basename(filename_zero)
  upsampleRes = int(upsampleRes)
  upsampled_image = outPath+basename+'_cubicSpline.png'
  os.system("gdalwarp -tr", upsample_res, upsample_res," -r cubicspline ", image, upsampled_image)
  im = np.array(Image.open(upsampled_image))
  with rasterio.open(image) as r:
    im = r.read()
    p = r.profile
  im = im.squeeze()
  selem = disk(1)
  print("image min and max: ", im.min(),im.max())
  dilated = skimage.morphology.dilation(im, selem)
  print("dilated image min and max: ", dilated.min(),dilated.max())
  eroded = skimage.morphology.erosion(im, selem)
  print("eroded image min and max: ", eroded.min(),eroded.max())
  opened = opening(im, selem)
  print("opened image min and max: ", opened.min(),opened.max())
  closed = closing(im, selem)
  print("closed image min and max: ", closed.min(),closed.max())
  dilated = Image.fromarray(dilated)
  dilated.save(outPath+basename+'.png')
  with rasterio.open(outPath+basename+fileext, 'w', **p) as dst:
      dst.write(dilated, 1)
  color_outPath = outPath+'color/'
  if not os.path.exists(color_outPath):
        os.mkdir(color_outPath) 
  colored_image = color_outPath+basename+'.png'
  os.system("gdaldem color-relief", dilated, colorfile, colored_image)
  return im, dilated, eroded, opened, closed
Ejemplo n.º 11
0
def open_image(img, mask_length):
    # Morphological opening on greyscale/binary image
    
    img = img.astype(np.uint8)
    img = opening(img, rectangle(mask_length,1))
    
    return(img)
Ejemplo n.º 12
0
def opening(gray_img, kernel=None):
    """Wrapper for scikit-image opening functions. Opening can remove small bright spots (i.e. salt).

    Inputs:
    gray_img = input image (grayscale or binary)
    kernel   = optional neighborhood, expressed as an array of 1s and 0s. If None, use cross-shaped structuring element.

    :param gray_img: ndarray
    :param kernel = ndarray
    :return filtered_img: ndarray
    """

    params.device += 1

    # Make sure the image is binary/grayscale
    if len(np.shape(gray_img)) != 2:
        fatal_error("Input image must be grayscale or binary")

    # If image is binary use the faster method
    if len(np.unique(gray_img)) == 2:
        bool_img = morphology.binary_opening(gray_img, kernel)
        filtered_img = np.copy(bool_img.astype(np.uint8) * 255)
    # Otherwise use method appropriate for grayscale images
    else:
        filtered_img = morphology.opening(gray_img, kernel)

    if params.debug == 'print':
        print_image(filtered_img, os.path.join(params.debug_outdir, str(params.device) + '_opening' + '.png'))
    elif params.debug == 'plot':
        plot_image(filtered_img, cmap='gray')

    return filtered_img
Ejemplo n.º 13
0
def detectOpticDisc(image):
    kernel = octagon(10, 10)
    thresh = threshold_otsu(image[:,:,1])
    binary = image > thresh
    print binary.dtype
    luminance = convertToHLS(image)[:,:,2]
    t = threshold_otsu(luminance)
    t = erosion(luminance, kernel)
    
    
    labels = segmentation.slic(image[:,:,1], n_segments = 3)
    out = color.label2rgb(labels, image[:,:,1], kind='avg')
    skio.imshow(out)
    
    x, y = computeCentroid(t)
    print x, y
    rows, cols, _ = image.shape
    p1 = closing(image[:,:,1],kernel)
    p2 = opening(p1, kernel)
    p3 = reconstruction(p2, p1, 'dilation')
    p3 = p3.astype(np.uint8)
    #g = dilation(p3, kernel)-erosion(p3, kernel)
    #g = rank.gradient(p3, disk(5))
    g = cv2.morphologyEx(p3, cv2.MORPH_GRADIENT, kernel)
    #markers = rank.gradient(p3, disk(5)) < 10
    markers = drawCircle(rows, cols, x, y, 85)
    #markers = ndimage.label(markers)[0]
    #skio.imshow(markers)
    g = g.astype(np.uint8)
    #g = cv2.cvtColor(g, cv2.COLOR_GRAY2RGB)
    w = watershed(g, markers)
    print np.max(w), np.min(w)
    w = w.astype(np.uint8)
    #skio.imshow(w)
    return w
Ejemplo n.º 14
0
def opening(img):
    orig_img = util.img_as_ubyte(img)
    selem = morphology.disk(6)
    eroded = morphology.opening(orig_img, selem)
    plot_comparison(orig_img, eroded, 'Opening')
    plt.tight_layout()
    plt.show()
Ejemplo n.º 15
0
def judge(img):  #判断能否识别验证码
    img = img[97:, :]
    img = gaussian(img, sigma=0.85)
    img = equalize_hist(img)
    img = (img > 0.7) * 1.0
    img = opening(img, selem=np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]))
    image_region = img * 0
    img = label(img, connectivity=1)
    props = regionprops(img)
    cnt = 0
    centerx = []
    width = []
    for i in range(np.max(img)):
        if props[i]['area'] > 290:
            image_region = image_region + (img == props[i]['label'])
            centerx.append(props[i]['centroid'][1])
            width.append(props[i]['image'].shape[1])
            cnt += 1
    # 八个连通域
    if cnt != 8:
        return False
    # 连通域之间距离;宽高比
    centerx.sort()
    if np.std(np.diff(np.array(centerx))) > 10 or np.min(np.array(width)) < 18:
        return False
    return True
Ejemplo n.º 16
0
def apply_opening(binary_img, selem_parameter=7, remove_objects=1000):
    '''
    This function applies opening algorithm to the input binary map and remove small objects afterwards. Opening can remove small bright spots (i.e. “salt”) and connect small dark cracks. This tends to “open” up (dark) gaps between (bright) features.
    
    Input
    ----------
        binary_img: ndarray
            input binary image, where foreground denotes detected violet cells
        selem_parameter: int, default 7
            parameter affects the neighborhood structure in opening algorithm
        remove_objects: int, default 1000
            the smallest allowable object size.
    
    Returns
    -------
        opened_image: ndarray
            the binary image after opening algorithm and removing small objects
    
    '''
    selem = disk(selem_parameter)
    opened_image = opening(binary_img, selem)
    opened_image = remove_small_objects(opened_image.astype(bool),
                                        remove_objects).astype(np.int64)

    return opened_image
Ejemplo n.º 17
0
def binarization(mat, ratio=0.3, thres=None, denoise=True):
    """
    Apply a list of operations: binarizing an 2D array; inverting the contrast
    of dots if needs to; removing border components; cleaning salty noise;
    and filling holes.
    
    Parameters
    ----------
    mat : array_like
        2D array.
    ratio : float 
        Used to select the ROI around the middle of the image for calculating
        threshold.
    thres : float, optional
        Threshold for binarizing. Automatically calculated if None.
    
    Returns
    -------
    array_like
        2D binary array.
    """
    if denoise:
        mat = ndi.median_filter(np.abs(mat), (2, 2))
    if thres is None:
        thres = threshold_otsu(_select_roi(mat, ratio), nbins=512)
    mat = np.asarray(mat > thres, dtype=np.float32)
    mat = _invert_dots_contrast(mat)
    mat = clear_border(mat)
    mat = morph.opening(mat, morph.disk(1))
    mat = np.int16(ndi.binary_fill_holes(mat))
    return mat
Ejemplo n.º 18
0
def imagePreProcess(imgPath, imgName):

    fpath = "%s/%s" % (imgPath, imgName)
    tdata = fits.getdata(fpath)
    '''
    imgAvg = np.average(tdata)
    imgRms = np.std(tdata)
    thred1 = imgAvg + 0.5 * imgRms
    
    img2 = np.zeros(tdata.shape)
    img2[tdata<thred1]=0
    img2[tdata>=thred1]=1
    kernel = np.ones((3,3))
    sub1 = cv2.filter2D(img2, -1, kernel)
    sub1[sub1<6]=0
    sub1[sub1>0]=255
    '''
    sub1 = morphology.opening(tdata, morphology.square(3))  #闭运算

    timg = getThumbnail_(sub1, stampSize=(100, 100), grid=(5, 5), innerSpace=1)
    timg = scipy.ndimage.zoom(timg, 4, order=0)
    import matplotlib.pyplot as plt
    plt.figure(figsize=(12, 12))
    plt.imshow(timg, cmap='gray')
    plt.show()
Ejemplo n.º 19
0
def opening_ski(image,
                opening_para,
                shape_type=MorpShapeSKI.disk,
                is_binary=True):
    """
    开运算

    :param image: 输入的图像
    :param shape_type: { 'square'  # 正方形
                    'disk' # 平面圆形
                    'ball'  # 球形
                    'cube' # 立方体形
                    'diamond' # 钻石形
                    'rectangle' # 矩形
                    'star'   # 星形
                    'octagon' # 八角形
                    'octahedron' # 八面体
                    }
    :param opening_para: 开运算系数
    :param is_binary: 输入的图像是否是二值化的, 如果是会调用 binary_opening 方法加速, 默认为 True
    :return: 处理后的图像
    """

    if is_binary:
        image = morphology.binary_opening(image, shape_type(opening_para))
    else:
        image = morphology.opening(image, shape_type(opening_para))

    return image
Ejemplo n.º 20
0
	def _generate_tissue_mask(wsi, level):
        """
        To generate a tissue mask for the WSI.
        This is achieved by Otsu thresholding on the saturation channel, then to remove the noise
        morphological closing and opening operations are applied.

        :param wsi: openSlide object for the WSI
        :param level:the level to process the mask generation at
        :return: the generated mask
        """

        temp = wsi.read_region(location=(0, 0), level=level, size=wsi.level_dimensions[level])
        # Convert to Hue-Saturation-Value.
        hsv = color.convert_colorspace(temp, 'RGB', 'HSV')  
        # Get saturation channel.
        saturation = hsv[:, :, 1] 
        # Otsu threshold.
        threshold = filters.threshold_otsu(saturation)  
        # Tissue is 'high saturation' region.
        mask = (saturation > threshold)  

        # Morphological operations-----------------
        # radius of disk for morphological operations.
        disk_r = 10  
        disk_o = disk(disk_r)
        # remove pepper noise
        mask = closing(mask, disk_o)  
        # remove salt noise
        mask = opening(mask, disk_o)  
        return mask
Ejemplo n.º 21
0
def create_mask(img, background_probability=0.75, use_triangle=False, use_otsu=False):
    test_mask = None
    if use_triangle:
        test_mask = sitk.GetArrayFromImage(sitk.TriangleThreshold(img, 0, 1))
    elif use_otsu:
        test_mask = sitk.GetArrayFromImage(sitk.OtsuThreshold(img, 0, 1))
    else:
        if type(img) is sitk.SimpleITK.Image:
            img = sitk.GetArrayFromImage(img)
        gmix = GaussianMixture(n_components=3, covariance_type='full', init_params='kmeans', verbose=0)
        gmix.fit(img.ravel().reshape(-1, 1))
        covariances = gmix.covariances_
        mean_background = gmix.means_.min()
        covariance_background = covariances[np.where( gmix.means_ == mean_background ) ][0][0]
        z_score = st.norm.ppf(background_probability)
        threshold = z_score * np.sqrt(covariance_background) + mean_background
        test_mask = (img > threshold)
    eroded_im = morphology.opening(test_mask, selem=morphology.ball(2))
    connected_comp = skimage.measure.label(eroded_im)
    out = skimage.measure.regionprops(connected_comp)
    area_max = 0.0
    idx_max = 0
    for i in range(len(out)):
        if out[i].area > area_max:
            area_max = out[i].area
            idx_max = i+1
    connected_comp[ connected_comp != idx_max ] = 0
    mask = connected_comp
    mask_sitk = sitk.GetImageFromArray(mask)
    mask_sitk.CopyInformation(img)
    return mask_sitk
Ejemplo n.º 22
0
def init_Segmentation(phase_contrast_img, shape_indexing_sigma=2):
    #
    shape_indexed = Shape_indexing_normalization(phase_contrast_img, \
                                                 shape_indexing_sigma=shape_indexing_sigma)
    #
    ph_gau = filters.gaussian(phase_contrast_img, sigma=1)
    ph_binary_glob = (ph_gau < filters.threshold_isodata(ph_gau)) * 1
    ph_binary_local = (ph_gau < filters.threshold_local(
        ph_gau, method="mean", block_size=15))
    ph_binary_local[morphology.dilation(ph_binary_glob, morphology.disk(5)) ==
                    0] = 0
    ph_binary_local.dtype = bool
    ph_binary_local = morphology.remove_small_objects(ph_binary_local,
                                                      min_size=80)
    ph_binary_local = morphology.remove_small_holes(ph_binary_local,
                                                    area_threshold=80) * 1
    ph_binary_local = morphology.opening(ph_binary_local,
                                         morphology.disk(1)) * 1
    # deprecated
    #if use_shape_indexed_binary:
    #    shape_indexed_binary = util.invert(shape_indexed > filters.threshold_local(shape_indexed, 27))
    #    shape_indexed_binary[ph_binary_local == 0] = 0
    #    shape_indexed_binary = (filters.median(shape_indexed_binary, morphology.disk(3)))*1
    #    shape_indexed_binary = (morphology.remove_small_holes(shape_indexed_binary, area_threshold=100))*1
    #
    microcolony_labels = measure.label(ph_binary_local, connectivity=1)
    region_info = measure.regionprops(microcolony_labels, coordinates='xy')
    #return ph_binary_local, shape_indexed, shape_indexed_binary, microcolony_labels, region_info
    return ph_binary_local, shape_indexed, microcolony_labels, region_info
Ejemplo n.º 23
0
def central_pixel_without_cells(pixels: np.array):
    """
    Find the location closest to the center that does not have an object (cell) near by
    :param pixels: Input image as n.array
    :return: location as a tuple, or False if not found
    """
    s2 = disk(2)
    s15 = disk(15)
    binary = pixels > threshold_triangle(pixels)
    opened = opening(binary, s2)
    dilated = dilation(opened, s15)
    location = [pixels.shape[0] // 2, pixels.shape[1] // 2]
    center = [pixels.shape[0] // 2, pixels.shape[1] // 2]
    distance = 1
    test = dilated[center[0], center[1]]
    while test and distance < pixels.shape[0] // 2:
        subarray = dilated[center[0] - distance:center[0] + distance,
                           center[1] - distance:center[1] + distance]
        if False in subarray:
            rows, cols = np.where(subarray == False)
            location = [
                rows[0] + center[0] - distance, cols[0] + center[1] - distance
            ]
            test = False
        else:
            distance += 1

    if not test:
        return location

    return False
Ejemplo n.º 24
0
def process_image(img):
    img = rank.median(img, mp.disk(1))
    p1, p99 = np.percentile(img, (1, 99))
    img = rescale_intensity(img, in_range=(p1, p99))
    img = mp.erosion(img)
    img = mp.erosion(img)
    img = mp.opening(img, mp.square(15))
    img = mp.erosion(img)

    MIN = 0
    MAX = 100
    norm = ((img - MIN) / (MAX - MIN)) * 255
    norm[norm > 255] = 255
    norm[norm < 0] = 0
    norm = mp.erosion(norm)
    norm = (norm > 100) * 255

    elevation_map = filters.sobel(img)
    markers = np.zeros_like(img)
    markers[img < 5] = 1
    markers[img > 200] = 2
    img = mp.watershed(elevation_map, markers)

    mean_img = (img + norm / 255) / 2

    if np.mean(np.array(mean_img[:, :])) < 1.4:
        img = norm

    img = img_as_ubyte(img)
    img = 1 - img
    contours = measure.find_contours(img, 1, fully_connected='high')
    return contours
def opening(gray_img, kernel=None):
    """Wrapper for scikit-image opening functions. Opening can remove small bright spots (i.e. salt).

    Inputs:
    gray_img = input image (grayscale or binary)
    kernel   = optional neighborhood, expressed as an array of 1s and 0s. If None, use cross-shaped structuring element.

    :param gray_img: ndarray
    :param kernel = ndarray
    :return filtered_img: ndarray
    """

    params.device += 1

    # Make sure the image is binary/grayscale
    if len(np.shape(gray_img)) != 2:
        fatal_error("Input image must be grayscale or binary")

    # If image is binary use the faster method
    if len(np.unique(gray_img)) == 2:
        bool_img = morphology.binary_opening(gray_img, kernel)
        filtered_img = np.copy(bool_img.astype(np.uint8) * 255)
    # Otherwise use method appropriate for grayscale images
    else:
        filtered_img = morphology.opening(gray_img, kernel)

    if params.debug == 'print':
        print_image(
            filtered_img,
            os.path.join(params.debug_outdir,
                         str(params.device) + '_opening.png'))
    elif params.debug == 'plot':
        plot_image(filtered_img, cmap='gray')

    return filtered_img
Ejemplo n.º 26
0
def apply_pipeline(im, pipeline):
    for pip in pipeline:
        name = pip['name']
        pms = pip['params']
        if (name == 'gabor'):
            im = filters.gabor(im,
                               frequency=pms['frequency'],
                               theta=pms['theta'],
                               bandwidth=pms['bandwidth'],
                               mode=pms['mode'])
        elif (name == 'gaussian'):
            im = filters.gaussian(im,
                                  sigma=float(pms['sigma']),
                                  mode=pms['mode'])
        elif (name == 'median'):
            im = filters.median(im)
        elif (name == 'scharr'):
            im = filters.scharr(im)
        elif (name == 'roberts'):
            im = filters.roberts(im)
        # Morphology
        elif (name == 'closing'):
            im = morphology.closing(im)
        elif (name == 'dilation'):
            im = morphology.dilation(im)
        elif (name == 'erosion'):
            im = morphology.erosion(im)
        elif (name == 'opening'):
            im = morphology.opening(im)
        # Transforms
        elif (name == 'rgb2gray'):
            im = color.rgb2gray(im)
        else:
            print '$$$ Error: ' + name + ' not valid kernel.'
    return im
Ejemplo n.º 27
0
def process_regions(image, blur_sigma=3, opening_size=3, orientation_deviation=15, overlap_minimum=0.8):
    '''
    Attempt to find any possible marker corner regions in a given image

    Inputs:
        - image: grayscale image that may contain a marker
        - blur_sigma: parameter for Gaussian blur to use on image
        - opening_size: parameter for morphological opening to use on image
        - orientation_deviation: see orientation parameter used by region_filter_heuristic(...)
        - overlap_minimum: see similarity parameter used by region_filter_heuristic(...)
    Returns: a 2-tuple of:
        - the image after pre-processing steps like blurring, thresholding, etc.
        - the list of regionprops that may be possible marker corners
    '''
    # Blur and equalize the image
    image = exposure.equalize_hist(image)
    image = filters.gaussian(image, sigma=blur_sigma)
    
    # Use local thresholding
    image = (image <= filters.threshold_sauvola(image, k=0.1))
    image = morphology.opening(image, selem=morphology.disk(opening_size))
    
    # Label components in the image
    labeled = measure.label(image, connectivity=2)
    components = measure.regionprops(labeled, intensity_image=image)
    
    # Sort the components by our rectangle heuristic
    return image, labeled, [r for r in components if region_filter_heuristic(r, orientation_deviation, overlap_minimum)]
Ejemplo n.º 28
0
    def _floodfill(self, img):
        back = Back._scharr(img)
        #print(type(back))
        # Binary thresholding.
        back = back > 0.05

        #print(type(back))
        # Thin all edges to be 1-pixel wide.
        back = skm.skeletonize(back)

        # Edges are not detected on the borders, make artificial ones.
        back[0, :] = back[-1, :] = True
        back[:, 0] = back[:, -1] = True

        # Label adjacent pixels of the same color.
        labels = label(back, background=-1, connectivity=1)

        # Count as background all pixels labeled like one of the corners.
        corners = [(1, 1), (-2, 1), (1, -2), (-2, -2)]
        for l in (labels[i, j] for i, j in corners):
            back[labels == l] = True

        # Remove remaining inner edges.

        return skm.opening(back)
Ejemplo n.º 29
0
def motion_segment_morphology(frame0, frame1):
    #img0 = lm(cv2.imread('../input_data/cleaned_gray_tunnel_sequence/0025.png'))
    #img1 = lm(cv2.imread('../input_data/cleaned_gray_tunnel_sequence/0026.png'))
    #canny_edges0 = canny(img0).astype(int) * 255
    #canny_edges1 = canny(img1).astype(int) * 255
    sobel_edges0 = sobel(frame0)
    sobel_edges1 = sobel(frame1)
    sobel_edges0 = np.where(sobel_edges0 > 10, 1, 0)
    sobel_edges1 = np.where(sobel_edges1 > 10, 1, 0)
    sobel_diff = sobel_edges1 - sobel_edges0

    # sobel_diff = erosion(sobel_diff, np.ones((3,3)))
    #sobel_diff = erosion(sobel_diff, selem=np.ones((3,3)))
    # sobel_diff = erosion(sobel_diff, selem=np.ones((5,5)))
    sobel_diff = opening(sobel_diff, selem=np.ones((7, 7)))
    """
    sobel_diff = dilation(sobel_diff, selem=np.ones((11,11)))
    sobel_diff = dilation(sobel_diff, selem=np.ones((11,11)))
    sobel_diff = dilation(sobel_diff, selem=np.ones((3,3)))
    sobel_diff = dilation(sobel_diff, selem=np.ones((5,5)))
    sobel_diff = dilation(sobel_diff, selem=np.ones((7,7)))
    sobel_diff = dilation(sobel_diff, selem=np.ones((9,9)))
    sobel_diff = closing(sobel_diff, selem=np.ones((11,11)))
    sobel_diff = closing(sobel_diff, np.ones((15,15)))
    """
    sobel_diff = closing(sobel_diff, selem=np.ones((41, 41)))
    sobel_diff = dilation(sobel_diff, selem=np.ones((25, 25)))
    sobel_diff = closing(sobel_diff, selem=np.ones((1, 27)))
    sobel_diff = closing(sobel_diff, selem=np.ones((27, 1)))
    #sobel_diff = closing(sobel_diff, selem=np.ones((25,25)))
    r = sobel_diff.astype(int) * 255
    return r
Ejemplo n.º 30
0
def polar_hull(slc):
    """Find the largest convex region using a polar transform."""
    old_dtype = slc.dtype
    # Convert to polar coordinates
    slc = slc.astype('uint8') * 256
    center = center_of_mass(slc)
    center_is_valid = not np.any(np.isnan(center))
    if not center_is_valid:
        log.warning("Unvalid center by center of mass method. "
                    "Defaulting to center of image")
        center = None
    log.debug("Performing polar transform")
    pim, ptSettings = polarTransform.convertToPolarImage(slc, center=center)
    log.debug("Applying anisotropic morphology filters to polar image.")
    pim = pim.astype('bool')
    pim = ndimage.median_filter(pim, size=9)
    pim = closing(pim, selem=np.ones((10, 1)))
    pim = opening(pim, selem=np.ones((1, 10)))
    # Mesh grid in order to compare edge positions
    my, mx = np.mgrid[:pim.shape[0], :pim.shape[1]]
    # Find the outside of the shape in the x direction
    log.debug("Calculating hull boundary")
    edge_r_idx = pim.shape[1] - np.argmax(pim[:, ::-1], axis=1)
    edge_r_idx[edge_r_idx == pim.shape[1]] = 0
    edge_r = mx < edge_r_idx[:, np.newaxis]
    edge_r = ndimage.median_filter(edge_r, size=9)
    log.debug("Performing inverse polar transform")
    hull, uptSetting = polarTransform.convertToCartesianImage(
        edge_r.astype('uint8') * 256, settings=ptSettings)
    return hull.astype(old_dtype)
Ejemplo n.º 31
0
def preprocess_nucl(
    im_nucl,
    shift_planes=0,
    filter='median',
    sigma=1,
    outstem='',
    save_steps=False,
):
    """Preprocess nuclear channel.

    - shift in z by an integer number of planes.
    - greyscale opening.
    - in-plane smoothing.
    - difference of gaussians.
    """

    # preprocess dapi: shift in z, opening and smoothing
    # TODO: move this step outside of this segmentation pipeline
    # it's too data-acquisition specific NOTE: (into sumsplit?)
    nucl_pp = shift_channel(im_nucl.ds[:], n_planes=shift_planes)
    if save_steps: write(nucl_pp, outstem, '_shifted', im_nucl)

    selem = None  # TODO
    nucl_pp = opening(nucl_pp, selem=selem, out=nucl_pp)
    if save_steps: write(nucl_pp, outstem, '_opened', im_nucl)

    nucl_pp = smooth_channel_inplane(nucl_pp, sigma, filter)
    im_nucl_pp = write(nucl_pp, outstem, '_preprocess', im_nucl)

    return im_nucl_pp
Ejemplo n.º 32
0
def open_binary(img_binary, k=5, plot=True, verbose=True):
    if verbose:
        print('\t - Removing artifacts with disk of radius %d' % k)
    opened = opening(img_binary, disk(k))
    if plot:
        d.compare2(img_binary, opened, 'Opening: k = %d' % k)
    return opened
Ejemplo n.º 33
0
 def preprocess(self):
     # edges = filters.sobel(self.gray)
     # edges = img_as_ubyte(edges)
     ret, binary = cv2.threshold(self.gray, 0, 255,
                                 cv2.THRESH_OTSU + cv2.THRESH_BINARY)
     binary = 255 - binary
     # if SAVE_IMAGES_TAG:
     #     cv2.imwrite(IMAGE_PATH + str(self.num) + "_binary.png", binary)
     lines = cv2.HoughLinesP(binary,
                             rho=1.0,
                             theta=np.pi / 180,
                             threshold=PREPROCESS_THRESHOLD,
                             lines=None,
                             minLineLength=PREPROCESS_MINLINELENGTH,
                             maxLineGap=PREPROCESS_MAXLINEGAP)
     if type(lines) == np.ndarray:
         for line in lines:
             x1, y1, x2, y2 = line[0][0], line[0][1], line[0][2], line[0][3]
             cv2.line(binary, (x1, y1), (x2, y2), 0, PREPROCESS_WIPEWIDTH)
             # cv2.line(self.img, (x1, y1), (x2, y2), (0, 255, 255), 5)
     binary = sm.opening(binary, sm.square(PREPROCESS_FIRSTSQUARE))
     # if SAVE_IMAGES_TAG:
     #     cv2.imwrite(IMAGE_PATH + str(self.num) + "_binary1.png", binary)
     binary = sm.dilation(binary, sm.square(PREPROCESS_FIRSTSQUARE))
     self.dilation = sm.dilation(binary, sm.square(PREPROCESS_SECONDSQUARE))
     # self.dilation = sm.dilation(binary, sm.square(PREPROCESS_FIRSTSQUARE))
     if SAVE_IMAGES_TAG:
         cv2.imwrite(IMAGE_PATH + '\\' + str(self.num) + ".png", self.img)
Ejemplo n.º 34
0
    def extended_morphological_profile(self, components, disk_radius):
        """

        :param components:
        :param disk_radius:
        :return:2-dim emp
        """
        rows, cols, bands = components.shape
        n = disk_radius.__len__()
        import numpy as np
        emp = np.zeros((rows * cols, bands * (2 * n + 1)))
        from skimage.morphology import opening, closing, disk
        for band in range(bands):
            position = band * (n * 2 + 1) + n
            emp_ = np.zeros((rows, cols, 2 * n + 1))
            emp_[:, :, n] = components[:, :, band]
            i = 1
            for r in disk_radius:
                closed = closing(components[:, :, band], selem=disk(r))
                opened = opening(components[:, :, band], selem=disk(r))
                emp_[:, :, n - i] = closed
                emp_[:, :, n + i] = opened
                i += 1
            emp[:, position - n:position + n + 1] = emp_.reshape((rows * cols, 2 * n + 1))
        return emp.reshape(rows, cols, bands * (2 * n + 1))
Ejemplo n.º 35
0
def create_mask(img, use_triangle=False):
    """Creates a mask of the image to separate brain from background using triangle or otsu thresholding. Otsu thresholding is the default.

    Parameters:
    ----------
    img : {SimpleITK.SimpleITK.Image}
        Image to compute the mask on.
    use_triangle : {bool}, optional
        Set to True if you want to use triangle thresholding. (the default is False, which results in Otsu thresholding)

    Returns
    -------
    SimpleITK.SimpleITK.Image
        Binary mask with 1s as the foreground and 0s as the background.
    """

    test_mask = None
    if use_triangle:
        test_mask = sitk.GetArrayFromImage(sitk.TriangleThreshold(img, 0, 1))
    else:
        test_mask = sitk.GetArrayFromImage(sitk.OtsuThreshold(img, 0, 1))
    eroded_im = morphology.opening(test_mask, selem=morphology.ball(2))
    connected_comp = skimage.measure.label(eroded_im)
    out = skimage.measure.regionprops(connected_comp)
    area_max = 0.0
    idx_max = 0
    for i in range(len(out)):
        if out[i].area > area_max:
            area_max = out[i].area
            idx_max = i + 1
    connected_comp[connected_comp != idx_max] = 0
    mask = connected_comp
    mask_sitk = sitk.GetImageFromArray(mask)
    mask_sitk.CopyInformation(img)
    return mask_sitk
Ejemplo n.º 36
0
def saturation_mask(img_rgb, saturation=0.75, remove=0, rejoin=5):
    # params:
    saturation_param = saturation
    remove_disk_param = remove
    rejoin_square_param = rejoin

    # split to h s v
    img = rgb2hsv(img_rgb)
    h = img[:, :, 0]
    s = img[:, :, 1]
    v = img[:, :, 2]
    # create mask
    mask = (s > saturation_param).astype(np.uint8)

    # remove small regions from mask
    disk_elem = disk(remove_disk_param)
    opened = opening(mask, selem=disk_elem)

    # rejoin colored pionts
    square_elem = square(rejoin_square_param)
    dilated = dilation(opened, selem=square_elem)

    io.imshow(dilated)
    plt.show()

    return dilated.astype(bool)
Ejemplo n.º 37
0
    def predict(self, file_path):
        X = load_imgs([file_path], im_shape=(512, 256))
        xx_ = X[0, :, :, :]
        xx = xx_[None, ...]
        inp_shape = X[0].shape

        pred = self.UNet.predict(xx)[..., 0].reshape(inp_shape[:2])

        # Binarize masks
        pr = pred > 0.5

        pr_bin = img_as_ubyte(pr)
        pr_openned = morphology.opening(pr_bin)

        im_x_ray_original_size = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
        height, width = im_x_ray_original_size.shape[:
                                                     2]  # height, width  -- original image size
        ratio = float(height) / width
        new_shape = (4 * 256, int(4 * 256 * ratio))
        im_x_ray_4x = cv2.resize(im_x_ray_original_size, new_shape)
        pr_openned_4x = cv2.resize(pr_openned, new_shape)
        gt_4x = cv2.resize(img_as_ubyte(pr), new_shape)
        gt_4x = gt_4x > 0.5
        pr_openned_4x = pr_openned_4x > 0.5
        im_masked_4x = masked(im_x_ray_4x, gt_4x, pr_openned_4x,
                              0.5)  # img.max()=1.0 gt.max()=True pr.max()=True
        im_masked_4x = img_as_ubyte(im_masked_4x)
        io.imsave(file_path, im_masked_4x)
Ejemplo n.º 38
0
def get_nuclei(img, opening_radius=6, block_size=80, threshold_offset=0):
    s = Sample(DOWNSAMPLE)
    binary = threshold_adaptive(s.downsample(img), int(block_size / s.rate), offset=threshold_offset)
    filled = fill_holes(binary)
    opened = opening(filled, selem=disk(opening_radius / s.rate))
    nuclei = apply_watershed(opened)
    nuclei = s.upsample(nuclei)
    return img_as_uint(nuclei)
Ejemplo n.º 39
0
def ProcessImage(im, targetDim = 250, doDenoiseOpening = True):

	#Resize to specified pixels max edge size
	scaling = 1.
	if im.shape[0] > im.shape[1]:
		if im.shape[0] != targetDim:
			scaling = float(targetDim) / im.shape[0]
			im = misc.imresize(im, (targetDim, int(round(im.shape[1] * scaling))))
	else:
		if im.shape[1] != targetDim:
			scaling = float(targetDim) / im.shape[1]
			im = misc.imresize(im, (int(round(im.shape[0] * scaling)), targetDim))
	#print "scaling", scaling

	greyim = 0.2126 * im[:,:,0] + 0.7152 * im[:,:,1] + 0.0722 * im[:,:,2]

	#Highlight number plate
	imnorm = np.array(greyim, dtype=np.uint8)
	se = np.ones((3, 30), dtype=np.uint8)
	opim = morph.opening(imnorm, se)
	diff = greyim - opim + 128.

	misc.imsave("diff.png", diff)

	#Binarize image
	vals = diff.copy()
	vals = vals.reshape((vals.size))

	meanVal = vals.mean()
	stdVal = vals.std()
	threshold = meanVal + stdVal

	#print "Threshold", threshold

	binIm = diff > threshold
	misc.imsave("threshold.png", binIm)
	#print vals.shape
	#plt.plot(vals)
	#plt.show()

	#Denoise
	diamond = morph.diamond(2)
	if doDenoiseOpening:
		currentIm = morph.binary_opening(binIm, diamond)
	else:
		currentIm = binIm
	denoiseIm2 = morph.binary_closing(currentIm, np.ones((3, 13)))

	#print "currentIm", currentIm.min(), currentIm.max(), currentIm.mean()
	#print "denoiseIm2", denoiseIm2.min(), denoiseIm2.max(), currentIm.mean()
	#misc.imsave("denoised1.png", currentIm * 255)
	#misc.imsave("denoised2.png", denoiseIm2 * 255)

	#Number candidate regions
	#print "Numbering regions"
	numberedRegions, maxRegionNum = morph.label(denoiseIm2, 4, 0, return_num = True)
	return numberedRegions, scaling
Ejemplo n.º 40
0
 def func(frame):
     _dtype = frame.dtype
     kernel = mor.disk(3)
     frameWP = frame - mor.white_tophat(frame, kernel) * (mor.white_tophat(frame, kernel) > 1000).astype(float)
     kernel = mor.rectangle(25, 1)
     closed = mor.closing(frameWP, kernel)
     opened = mor.opening(closed, kernel)
     result = ((frameWP.astype(float) / opened.astype(float)) * 3000.0)
     return result.astype(_dtype)
def segment_image(image, plantid, threshold, locations, opening_size=5):
    """
        Segments an image based on simple thresholding
        
        Inputs:
        - image : the image (as a numpy array)
        - plantid : which plant on the image (int)
        - threshold : the threshold to use
        - locations : the coordinates of the plants in the
        
        Output:
        - mask of the image
        """
    image_part = get_piece(image, locations[plantid])
    mask = median_filter(image_part.mean(2), (4,4)) > threshold
    opening(mask, selem=square(opening_size), out=mask)  # open image, remove
    # clutter
    return mask  # gaussian_filter(image_part[mask, :], sigma=convolve_sigma)
def find_border_centroids(hfp, keys, areas, largeobjkey, disttransfkey, resultkey):

    for k, bounds in keys.iteritems():

        # bounds = (shp[0],) * 2
        for lbl, lblim in hfp['faces', largeobjkey].label_image_iterator(key=k, background=0, area=areas[k]):

            hfp.logging('---\nLabel {} found in image {}', lbl, k)

            # Avoid very small artifacts
            lblim = morphology.opening(lblim)

            # Connected component analysis to detect when a label touches the border multiple times
            conncomp = vigra.analysis.labelImageWithBackground(lblim.astype(np.uint32), neighborhood=8, background_value=0)

            for l in np.unique(conncomp):
                # Ignore background
                if l == 0: continue

                # Get the current label object
                curobj = conncomp == l

                # Get disttancetransf of the object
                curdist = np.array(hfp['faces', disttransfkey, k])
                curdist[curobj == False] = 0

                # Detect the global maximum of this object
                amax = np.amax(curdist)
                curdist[curdist < amax] = 0
                curdist[curdist > 0] = lbl
                # Only one pixel is allowed to be selected
                bds = lib.find_bounding_rect(curdist)
                centroid = (int((bds[1][0] + bds[1][1]-1) / 2), int((bds[0][0] + bds[0][1]-1) / 2))

                # Now translate the calculated centroid to the position within the orignial 3D volume
                centroidm = (centroid[0] - bounds, centroid[1] - bounds)
                # hfp.logging('centroidxy = {}', centroidm)
                # Set the pixel
                try:
                    if centroidm[0] < 0 or centroidm[1] < 0:
                        raise IndexError
                    else:
                        if k == 'xyf':
                            hfp[resultkey][centroidm[0], centroidm[1], 0] = lbl
                        elif k == 'xyb':
                            hfp[resultkey][centroidm[0], centroidm[1], -1] = lbl
                        elif k == 'xzf':
                            hfp[resultkey][centroidm[0], 0, centroidm[1]] = lbl
                        elif k == 'xzb':
                            hfp[resultkey][centroidm[0], -1, centroidm[1]] = lbl
                        elif k == 'yzf':
                            hfp[resultkey][0, centroidm[0], centroidm[1]] = lbl
                        elif k == 'yzb':
                            hfp[resultkey][-1, centroidm[0], centroidm[1]] = lbl
                except IndexError:
                    pass
Ejemplo n.º 43
0
def GrayMM(img,thresh,size):
    structElement = morphology.rectangle(size,size)
  
    img[img == -9999] = 0
    img = img/5000
    img[img < 0] = 0
    img[img > 1] = 1
    
    outdata = morphology.opening(img, structElement)
    #threshold after bin
    imgOut = outdata > 255*thresh/5000
    
    return imgOut
def ROI_binary_mask(sample, size=5, ticket=(80, 80, 80)):
    #  very slow function at resolution 4
    PreprocRes = np.copy(sample)

    for i in range(3):  # RGB
        # this one is painfully slow..
        PreprocRes[:, :, i] = Preprocessing(sample[:, :, i])
    res = combining(PreprocRes)
    ticket = FindTicket(sample, ticket)
    res = res - ticket
    res[res > 0] = 1
    res[res < 0] = 0
    res = opening(res, disk(size))
    return res
Ejemplo n.º 45
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.º 46
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.º 47
0
	def clean_footprint(self, OPENING_THRESH = 5):
		self.FOOTPRINT_cleaned_opening = opening(self.FOOTPRINT_added_boundary, square(OPENING_THRESH))

		labeled_image, num_of_labels = ndimage.measurements.label(self.FOOTPRINT_cleaned_opening, [[1,1,1],[1,1,1],[1,1,1]])
		
		if num_of_labels > 1:
			b_slices = ndimage.find_objects(labeled_image)
			area = []
			for idx,s in enumerate(b_slices):
				area.append(len(self.FOOTPRINT_cleaned_opening[self.FOOTPRINT_cleaned_opening[s] != 0]))
			self.FOOTPRINT_cleaned_opening[labeled_image != (area.index( max(area))) + 1] = 0


		# area = np.bincount(obj.flatten())[indexedObject[0]]
		return len(self.FOOTPRINT_cleaned_opening[self.FOOTPRINT_cleaned_opening!=0])
def FindTicket(RGB_image, _3tuple=(80, 80, 80)):
    # Find the "black ticket on the images"
    temp_image_3 = np.copy(RGB_image)
    temp_image_3[:, :, :] = 0
    for i in range(3):
        temp_image_1 = np.zeros(shape=RGB_image.shape[0:2])
        temp_image_1[np.where(RGB_image[:, :, i] < _3tuple[i])] = 1
        temp_image_3[:, :, i] = temp_image_1

    temp_resultat = temp_image_3.sum(axis=2)

    temp_resultat[temp_resultat > 2] = 3
    temp_resultat[temp_resultat < 3] = 0
    temp_resultat[temp_resultat == 3] = 1
    
    #temp_resultat = Filling_holes_2(temp_resultat)
    temp_resultat = closing(temp_resultat, disk(20))
    temp_resultat = opening(temp_resultat, disk(20))
    temp_resultat = RemoveBorder(temp_resultat)
    return temp_resultat
Ejemplo n.º 49
0
    def _floodfill(self, img):
        back = self._scharr(img)
        # Binary thresholding.
        back = back > 0.05

        # Thin all edges to be 1-pixel wide.

        back = skm.skeletonize(back)
        # Edges are not detected on the borders, make artificial ones.
        back[0, :] = back[-1, :] = True
        back[:, 0] = back[:, -1] = True

        # Label adjacent pixels of the same color.
        labels = label(back, background=-1, connectivity=1)

        # Count as background all pixels labeled like one of the corners.
        corners = [(1, 1), (-2, 1), (1, -2), (-2, -2)]
        for l in (labels[i, j] for i, j in corners):
            back[labels == l] = True

        # Remove remaining inner edges.
        return skm.opening(back)
Ejemplo n.º 50
0
def change_bg(orig, background=None,
              threshold_yellow=[(80, 145), (65, 145), (0, 50)],
              threshold_black=[(0, 30), (0, 30), (0, 30)],
              threshold_face=[(42, 90), (20, 70), (0, 42)],
              show_pic=False,
              ):
    thresholds = [threshold_yellow, threshold_black, threshold_face]
    if background is None:
        background = orig * 0
    background = array(Image.fromarray(background).resize(orig.shape[:2][::-1]))
    masks = [orig.copy() for i in range(len(thresholds))]
    for mask_i, threshold in enumerate(thresholds):
        for i, (th_lo, th_hi) in enumerate(threshold):
            masks[mask_i][:, :, i] = (th_lo <= masks[mask_i][:, :, i]).astype(int) * (masks[mask_i][:, :, i] <= th_hi).astype(int)
        mask = masks[mask_i][:, :, 0] * masks[mask_i][:, :, 1] * masks[mask_i][:, :, 2]
        masks[mask_i][:, :, 0], masks[mask_i][:, :, 1], masks[mask_i][:, :, 2] = [mask for i in range(3)]
    mask = 1-reduce(lambda x, y: x * y, [1-w for w in masks])
    mask_orig = mask.copy()
    mask = morphology.dilation(mask, ones([10, 3, 1]))
    mask = morphology.opening(mask, ones([18, 12, 1]))
    mask = morphology.dilation(mask, ones([3, 6, 1]))

    result = (orig * mask + background * (1 - mask))
    if show_pic:
        subplot(221)
        imshow(orig)
        title('orig')
        subplot(222)
        imshow(mask*255)
        title('mask')
        subplot(223)
        imshow(mask_orig*255)
        title('mask_orig')
        subplot(224)
        imshow(result)
        title('result')
        show()
    return result, mask, mask_orig
Ejemplo n.º 51
0
Notice how the white boundary of the image thickens, or gets dilated, as we
increase the size of the disk. Also notice the decrease in size of the two
black ellipses in the centre, and the thickening of the light grey circle in
the center and the 3 patches in the lower part of the image.


Opening
=======

Morphological ``opening`` on an image is defined as an *erosion followed by a
dilation*. Opening can remove small bright spots (i.e. "salt") and connect
small dark cracks.
"""

opened = opening(phantom, selem)
plot_comparison(phantom, opened, 'opening')

"""
.. image:: PLOT2RST.current_figure

Since ``opening`` an image starts with an erosion operation, light regions that
are *smaller* than the structuring element are removed. The dilation operation
that follows ensures that light regions that are *larger* than the structuring
element retain their original size. Notice how the light and dark shapes in the
center their original thickness but the 3 lighter patches in the bottom get
completely eroded. The size dependence is highlighted by the outer white ring:
The parts of the ring thinner than the structuring element were completely
erased, while the thicker region at the top retains its original thickness.

    def demoMorphologicalOperationsImage(self, filename='/Users/mangotee/Dropbox/Teaching/CDTM ARdrones Lecture/imgTennisBallBed.jpg'):
        img = cv2.imread(filename)
        mask = self.utilFilterColorRGB(img,np.array([0, 255, 255]), 8)
        displayOpenCV = False

        if displayOpenCV:
            cv2.imshow('img',img)
            cv2.imshow('mask',mask)
            cv2.waitKey(0)
            cv2.destroyAllWindows()
        else:
            #fname = '/Users/mangotee/Dropbox/Teaching/CDTM ARdrones Lecture/imgTennisBallBed_v1_colfilSingle.png'
            #cv2.imwrite(fname,np.uint8(mask))
            maskLargestComponent = self.findLargestComponent(mask)


            fig1 = plt.figure(facecolor='w')
            #fig1.set_tight_layout(True)
            #plt.use('Agg')
            plt.subplot(121),plt.imshow(self.utilCvColorOpenCV2Matplotlib(img)),plt.title('Image')
            plt.axis('off')
            plt.subplot(122),plt.imshow(mask, cmap='gray'),plt.title('Mask')
            plt.axis('off')
            #fname = '/Users/mangotee/Dropbox/Teaching/CDTM ARdrones Lecture/imgTennisBallBed_v1_colfil.png'
            #plt.savefig(fname)
            plt.show()

            fig2 = plt.figure(facecolor='w')
            plt.subplot(131),plt.imshow(self.utilCvColorOpenCV2Matplotlib(img)),plt.title('Image')
            plt.axis('off')
            plt.subplot(132),plt.imshow(mask, cmap='gray'),plt.title('Mask')
            plt.axis('off')
            plt.subplot(133),plt.imshow(maskLargestComponent, cmap='gray'),plt.title('Largest Component')
            plt.axis('off')
            fname = '/Users/mangotee/Dropbox/Teaching/CDTM ARdrones Lecture/imgTennisBallBed_v2_LargestComp.png'
            plt.savefig(fname)
            plt.show()

            # morphological operations on the largest component
            se = morphology.disk(9)
            maskLCmorph = morphology.opening(maskLargestComponent, se)
            maskLCmorph = morphology.closing(maskLCmorph, se)
            fig3 = plt.figure(facecolor='w')
            plt.subplot(131),plt.imshow(self.utilCvColorOpenCV2Matplotlib(img)),plt.title('Image')
            plt.axis('off')
            plt.subplot(132),plt.imshow(maskLargestComponent, cmap='gray'),plt.title('Largest Component')
            plt.axis('off')
            plt.subplot(133),plt.imshow(maskLCmorph, cmap='gray'),plt.title('Opening/Closing')
            plt.axis('off')
            fname = '/Users/mangotee/Dropbox/Teaching/CDTM ARdrones Lecture/imgTennisBallBed_v3_LargestCompMorph.png'
            plt.savefig(fname)
            plt.show()

            # hough circle detection on the image
            circles = cv2.HoughCircles(maskLCmorph, cv2.cv.CV_HOUGH_GRADIENT, 1, param1=200, param2=10, minDist=250, minRadius=80) # and here

            #print "start Hough"
            if circles is not None:
                #print circles
                circles = np.round(circles[0, :]).astype("int")
                for (x, y, r) in circles:
                    print 'x=%0.2f y=%0.2f r=%0.2f' % (x,y,r)
                    cv2.circle(img, (x, y), r, color=[0,0,255], thickness=4)
                    #cv2.rectangle(gray, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

                cv2.imshow('result',img)
                fname = '/Users/mangotee/Dropbox/Teaching/CDTM ARdrones Lecture/imgTennisBallBed_v4_LargestCompMorphWithHoughCircle.png'
                cv2.imwrite(fname,img)
                fname = '/Users/mangotee/Dropbox/Teaching/CDTM ARdrones Lecture/imgTennisBallBed_v4_LargestCompMorph.png'
                cv2.imwrite(fname,maskLCmorph)
Ejemplo n.º 53
0
def special(video_name):
	if not os.path.exists(global_path + video_name[video_name.rfind('/') + 1:video_name.rfind('.')]):
		os.makedirs(global_path + video_name[video_name.rfind('/') + 1:video_name.rfind('.')])
		print video_name[video_name.rfind('/') + 1:video_name.rfind('.')]
	#main loop
	first_detect = True
	video = VideoFileClip(video_name)
	folder_name = global_path + video_name[video_name.rfind('/') + 1:video_name.rfind('.')] + "/"
	print folder_name
	mean_r = np.zeros((video.size[1], video.size[0], size))
	mean_g = np.zeros((video.size[1], video.size[0], size))
	mean_b = np.zeros((video.size[1], video.size[0], size))
	for i in range(0, size):
		mean_r[:,:,i] = video.get_frame(i * step / video.fps)[:,:,0]
		mean_b[:,:,i] = video.get_frame(i * step / video.fps)[:,:,1]
		mean_g[:,:,i] = video.get_frame(i * step / video.fps)[:,:,2]
	cell_size = 300
	#detect = np.zeros((int(round((float(video.size[1]) / cell_size))), int(round(float(video.size[0]) / cell_size))))
	#detect = np.zeros((amount_height, amount_width, length_size))
	detect = []
	start = time.time()
	num_frame_in_background = 0
	for frame in range(size * step + step, int(video.duration * video.fps), step):
		print  frame
		background = np.dstack((np.median(mean_r, axis = 2), np.median(mean_g, axis = 2), np.median(mean_b, axis = 2)))
		cur_image = video.get_frame(frame / video.fps)
		cur_image_copy = np.copy(cur_image)
		cur_image_with_rectangle = np.copy(cur_image)
		cur_image = rgb2gray(abs(cur_image - background))
		cur_image = (cur_image > 5) * 255
		cur_image = opening(cur_image, disk(2))
		image_label = label(cur_image)
		i = 0
		asdf = 0
		for region in regionprops(image_label):
			if region.area < 50:
				continue
			i = i + 1
			print i
			y, x, y_end, x_end = region.bbox
			x = (x + x_end) / 2 - 50
			y = (y + y_end) / 2 - 50
			x_end = x + 100
			y_end = y + 100
			if x_end > video.size[0] - 1:
				x_end = video.size[0] - 1
				x = x_end - 100
			if y_end > video.size[1] - 1:
				y_end = video.size[1] - 1
				y = y_end - 100
			if x < 0:
				x = 0
				x_end = x + 100
			if y < 0:
				y = 0
				y_end = y + 100
			frame_info = np.zeros((100, 100, 3, 4), dtype = np.uint8)
			mean_r1 = np.zeros((100, 100, 4))
			mean_g1 = np.zeros((100, 100, 4))
			mean_b1 = np.zeros((100, 100, 4))
			temp1 = np.zeros(())
			for j in range(0, 4):
				temp1 = video.get_frame((frame + j * 5) / float(video.fps))
				# imsave(folder_name + str(i) + ".jpg", temp1[y:y_end, x:x_end, :])
				frame_info[:,:,:,j] = temp1[y:y_end, x:x_end,:]
				mean_r1[:,:,j] = temp1[y:y_end,x:x_end,0]
				mean_g1[:,:,j] = temp1[y:y_end,x:x_end,1]
				mean_b1[:,:,j] = temp1[y:y_end,x:x_end,2]

			temp22 = np.dstack((np.median(mean_r1, axis = 2), np.median(mean_g1, axis = 2), np.median(mean_b1, axis = 2)))
			# for j in range(0, 4):
			# 	temp1 = video.get_frame((frame + j * 5) / float(video.fps))
			# 	imsave(folder_name + "%06d" % (50 * asdf + j * 5 + 1) + ".jpg", np.abs(temp1[y:y_end, x:x_end, :] - temp22))
			# asdf+=1
			for j in range(0, 4):
				frame_info[:,:,:,j] = np.abs(frame_info[:,:,:,j] - temp22)
				for k in range(0,100):
					for m in range(0,100):
						print frame_info[k,m,:,j]
				print frame_info[0,0,:,j]
				frame_info[:,:,:,j] = frame_info[:,:,[2, 1, 0],j].astype(np.uint8)
				print frame_info[10,10,:,j]

				imsave(global_path + "temp.jpg",frame_info[:,:,:,j])
				frame_info[:,:,:,j] = cv.imread(global_path + "temp.jpg",cv.IMREAD_UNCHANGED)
				for k in range(0,100):
					for m in range(0,100):
						print frame_info[k,m,:,j]
				exit()
			prediction = c3d_classify(
				images= frame_info,
				image_mean=image_mean,
				net=net,
				prob_layer='prob'
				)
			print prediction
			res_cnn = int(prediction.argmax())
			color = (0, 0, 0)
			if (res_cnn == 0):
				color = (255, 0, 0)
				for i, n in enumerate(detect):

					center_x = (x + x_end) / 2
					center_y = (y + y_end) / 2
					if (n[0][0] - cell_size <= center_y <= n[0][0] + cell_size) and (n[0][1] - cell_size <= center_x <= n[0][1] + cell_size) and n[2] != frame:
						n[1][0] = 1
						break
				else:
					detect.append((((y+y_end)/2, (x + x_end) /2), np.array([1, 0, 0, 0]), frame))
					cv.rectangle(cur_image_with_rectangle, ((x + x_end) /2 - cell_size, (y+y_end)/2 - cell_size), ((x + x_end) /2 + cell_size, (y+y_end)/2 + cell_size), (0,0, 255), 2)
			else:
				color = (0, 255, 0)

			cv.rectangle(cur_image_with_rectangle, (x, y), (x_end, y_end), color, 2)
		print frame
		##print detect
		#print
		for i, n in enumerate(detect):
			if (np.sum(n[1]) == 3 or np.sum(n[1]) == 4):
				#print frame / video.fps
				if first_detect:
					# f.write(' %f' % (frame / video.fps))
					first_detect = False
				cv.rectangle(cur_image_with_rectangle,(n[0][1] - cell_size, n[0][0] - cell_size), (n[0][1] + cell_size, n[0][0] + cell_size), (255, 0, 0), 2)
			if (n[1][0] == 0 and n[1][1] == 1 and n[1][2] == 1):
				continue
			if (n[1][0] == 0 and n[1][1] == 1 and n[1][2] == 0):
				del detect[i]
		for i, n in enumerate(detect):
			n[1][1:] = n[1][0:-1]
			n[1][0] = 0
		imsave(folder_name + str(frame) + ".png", cur_image_with_rectangle)
		mean_r[:,:,num_frame_in_background] = cur_image_copy[:,:,0]
		mean_g[:,:,num_frame_in_background] = cur_image_copy[:,:,1]
		mean_b[:,:,num_frame_in_background] = cur_image_copy[:,:,2]
		num_frame_in_background += 1
		num_frame_in_background = num_frame_in_background % size
	print video_name, time.time() - start
Ejemplo n.º 54
0
def detectVessels(image):
    kernel = square(2)
    image = ndimage.gaussian_filter(image[:,:,2], 2)
    image = opening(image, kernel)
    return image
Ejemplo n.º 55
0
binary = binary_label == rps[region_with_largest_area].label
#binary = convex_hull_image(binary)
#binary = binary_dilation(binary, disk(15)) != 0
distance = distance_transform_edt(binary)
local_maxi = peak_local_max(distance) # internal marker / center of [circle] <- (external marker)

####################################

#################################### Gradient Image
red_chan = gray_image
io.imshow(red_chan)

close = closing(red_chan, octagon(4,4))
io.imshow(close)

opening = opening(close, octagon(6,6))
io.imshow(opening)

rec = reconstruction(opening, close)

rec = exposure.rescale_intensity(rec, out_range=(0,1))

gradient = gradient(rec, disk(5))
######################################################## Apply watershed to gradient using imposed markers
### This is where I am having trouble. I have the gradient image and the internal marker location. I want
### to create a marker image with the internal marker (located at the center of the optic disk) and
### the external marker drawn as a circle around the optic disk so that the watershed algorithm can
### evaluate the specific contour of the disk when feeding in the gradient image and the marker image.
### What I need to know is how to create that marker image.
### The radius of the circle (external marker) with center as the internal marker can be anything as long as it is
### larger than the optic disk, completely covering it.
Ejemplo n.º 56
0
plot_comparison(orig_phantom, dilated, 'dilation')

######################################################################
# Notice how the white boundary of the image thickens, or gets dilated, as we
#increase the size of the disk. Also notice the decrease in size of the two
#black ellipses in the centre, and the thickening of the light grey circle
#in the center and the 3 patches in the lower part of the image.
#
#Opening
#=======
#
#Morphological ``opening`` on an image is defined as an *erosion followed by
#a dilation*. Opening can remove small bright spots (i.e. "salt") and
#connect small dark cracks.

opened = opening(orig_phantom, selem)
plot_comparison(orig_phantom, opened, 'opening')

######################################################################
#Since ``opening`` an image starts with an erosion operation, light regions
#that are *smaller* than the structuring element are removed. The dilation
#operation that follows ensures that light regions that are *larger* than
#the structuring element retain their original size. Notice how the light
#and dark shapes in the center their original thickness but the 3 lighter
#patches in the bottom get completely eroded. The size dependence is
#highlighted by the outer white ring: The parts of the ring thinner than the
#structuring element were completely erased, while the thicker region at the
#top retains its original thickness.
#
#Closing
#=======
Ejemplo n.º 57
0
def default_masking(snr,snr_min=5.0):
    planemask = (snr>snr_min) 
    planemask = remove_small_objects(planemask,min_size=40)
    planemask = opening(planemask,disk(1))
    return(planemask)
Ejemplo n.º 58
0
file_thin= 'fits/Core2_N2Hp_thin_fitted_parameters_snr3.fits'
snr_min = 3.

cube = pyspeckit.Cube(file_in)
cube.xarr.refX = freq_line
cube.xarr.velocity_convention = 'radio'
cube.xarr.convert_to_unit('km/s')

xmax=4; ymax=6
vmin=5.0; vmax=8.0

rms_map = cube.slice(-12, -4, unit='km/s').cube.std(axis=0)
peaksnr =  cube.slice(vmin, vmax, unit='km/s').cube.max(axis=0)/rms_map
planemask = (peaksnr>snr_min) 
planemask = remove_small_objects(planemask,min_size=40)
planemask = opening(planemask,disk(1))

F=False
T=True
multicore=4

import matplotlib.pyplot as plt
plt.ion()

if Optically_Thin:
    cube.Registry.add_fitter('n2hp_vtau', pyspeckit.models.n2hp.n2hp_vtau_fitter, 4)

    print('start optically thin fit')
    cube.fiteach(fittype='n2hp_vtau',  guesses=[80.0, 0.1, 6.7, 0.1], # Tex=5K, tau=0.1, v_center=7.1, \sigma_v=0.3 km/s
                 verbose_level=1, signal_cut=snr_min,
                 limitedmax=[F,F,T,T],
Ejemplo n.º 59
0
def find_map(url, min_int=0.03, max_int=0.97, disk_sz=2, opt=None):
    """Find the map in an image (using morphological operations) and return it.
    Heuristic assumption the map is the largest object in the map.
    Parameters
    ----------
    img: (M, N, 3) or (M, N, 4) 
        An RGB or RGBA image.
    min_int : threshold value to eliminate ~black background.
        If min_int is not given, a default value of 0.03 is uded.
    max_int : threshold value to eliminate ~white background.
        If max_int is not given, a default value of 0.97 is uded. 
    disk_sz : size of disk-shaped structuring element for opening.
        If disk_sz is not given, a default value of 2 is uded.
    opt			:	optional flag. Default is None; if set to not None, 
    		the convex hull of the largest detected object is returned.
    Returns
    -------
    out : (M, N, 3) array
        An image with only the main map.
    """

    # rgb from url
    rspns = requests.get(url)
    img = np.asarray(Image.open(StringIO(rspns.content)))[:, :, :3]

    # image must be RGB or RGB(A)
    if not len(img.shape) > 2:
        raise ValueError("Sorry, image has to be RGB (M, N, 3) or RGBA (M, N, 4)")

    # remove alpha channel
    img = img[:, :, :3]

    # stretch contrast
    p2, p98 = np.percentile(img, (2, 98))
    rescale = exposure.rescale_intensity(img, in_range=(p2, p98))

    # binary from rgb
    binary = np.logical_and(color.rgb2gray(rescale) > min_int, color.rgb2gray(rescale) < max_int)

    # apply very mild opening
    binary = opening(binary, disk(disk_sz))

    # keep only largest white object
    label_objects, nb_labels = ndi.label(binary)
    sizes = np.bincount(label_objects.ravel())
    sizes[0] = 0
    if nb_labels < 2:  # background not included in the count
        binary_objects = binary  # in case the image already contained only the map
    else:
        binary_objects = remove_small_objects(binary, max(sizes))

    # remove holes from it
    binary_holes = ndi.morphology.binary_fill_holes(binary_objects)

    # optional: get convex hull image (smallest convex polygon that surround all white pixels)
    if opt is not None:
        binary_holes = convex_hull_image(binary_holes)

    # use it to make 3D mask
    mask3 = np.zeros(img.shape)
    mask3[:, :, 0] = binary_holes
    mask3[:, :, 1] = binary_holes
    mask3[:, :, 2] = binary_holes

    # use mask to get only map in original image
    final = np.ma.masked_where(mask3 == 0, img)
    final = final.filled(0)

    # crop zero columns and zero rows
    # see http://stackoverflow.com/a/31402351/1034648
    # plus a few columns and rows to counter the initial opening
    non_empty = np.where(final != 0)
    out = final[np.min(non_empty[0]) : np.max(non_empty[0]), np.min(non_empty[1]) : np.max(non_empty[1])][
        disk_sz:-disk_sz, disk_sz:-disk_sz
    ]

    # output
    return out
Ejemplo n.º 60
0
def find_border_centroids(ipl, faces, key, facesinfo, facesd, resultkey, resultshp):
    """
    :param ipl: the result is stored here using resultkey
    :param faces: ipl containing faces as returned by compute_faces
    :param key: key of the image in ipl
    :param facesinfo: ipl containing facesinfo as returned by compute_faces
    :param facesd: ipl containing the faces of the distance transform as returned by compute_faces
    :param resultkey:
    :return:
    """

    ipl[resultkey, key] = np.zeros(resultshp)

    for k, startpoint in facesinfo[key, 'startpoints'].iteritems():

        # bounds = (shp[0],) * 2
        for lbl, lblim in faces[key].label_image_iterator(key=k, background=0, area=facesinfo[key, 'areas', k]):

            ipl.logging('---\nLabel {} found in image {}', lbl, k)

            # Avoid very small artifacts
            lblim = morphology.opening(lblim)

            # Connected component analysis to detect when a label touches the border multiple times
            conncomp = vigra.analysis.labelImageWithBackground(lblim.astype(np.uint32), neighborhood=8, background_value=0)

            for l in np.unique(conncomp):
                # Ignore background
                if l == 0: continue

                # Get the current label object
                curobj = conncomp == l

                # Get disttancetransf of the object
                curdist = np.array(facesd[key, k])
                curdist[curobj == False] = 0

                # Detect the global maximum of this object
                amax = np.amax(curdist)
                curdist[curdist < amax] = 0
                curdist[curdist > 0] = lbl
                # Only one pixel is allowed to be selected
                try:
                    bds = lib.find_bounding_rect(curdist)
                except ValueError:
                    # A value error is thrown when the current object is just one pixel in size
                    # This can be ignored without ignoring relevant border contacts
                    pass

                centroid = (int((bds[1][0] + bds[1][1]-1) / 2), int((bds[0][0] + bds[0][1]-1) / 2))

                # Now translate the calculated centroid to the position within the orignial 3D volume
                centroidm = (centroid[0] - startpoint, centroid[1] - startpoint)
                # ipl.logging('centroidxy = {}', centroidm)
                # Set the pixel
                try:
                    if centroidm[0] < 0 or centroidm[1] < 0:
                        raise IndexError
                    else:
                        if k == 'xyf':
                            ipl[resultkey, key][centroidm[0], centroidm[1], 0] = lbl
                        elif k == 'xyb':
                            ipl[resultkey, key][centroidm[0], centroidm[1], -1] = lbl
                        elif k == 'xzf':
                            ipl[resultkey, key][centroidm[0], 0, centroidm[1]] = lbl
                        elif k == 'xzb':
                            ipl[resultkey, key][centroidm[0], -1, centroidm[1]] = lbl
                        elif k == 'yzf':
                            ipl[resultkey, key][0, centroidm[0], centroidm[1]] = lbl
                        elif k == 'yzb':
                            ipl[resultkey, key][-1, centroidm[0], centroidm[1]] = lbl
                except IndexError:
                    pass