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)
Exemple #2
0
def white_top_func(filename):
    image = Image.open(filename)
    image = asarray(image)
    image = rgb2gray(image)
    image = morphology.white_tophat(image, disk(6))
    plt.imsave(filename, image, cmap='gray')
    return filename
Exemple #3
0
def segment_mask(img_GRAY):
    '''
        Computation of Segmentation Mask for grayscale images
        mask_pred = segment_mask(img_GRAY)
        INPUTS:
            img_GRAY ~ 2D array, dtype=float (N x M) [Grayscale input for which mask will be generated
        OUTPUTS:    
            mask_pred ~ Boolean array (N x M) [Output segmentation mask in [0,1]]
    '''

    # Increase contrast by adapative histogram equalization
    # The low contrast in large parts of the image, coupled with the high
    #   intensity for the bottom-right artifacts, motivated selection of a locally
    #   adaptive form of histogram equalization.
    img_HISTEQ = exposure.equalize_adapthist(img_GRAY)

    # Use triangle threshold for segmenting objects
    # Given the skewed and unimodal distribution of pixel values,
    #   the triangle threshold was selected as desirable for isolating the
    #   tail of peak pixel intensities without including brighter segments of the
    #   tissue (as was observed with Otsu)
    thresh = filters.threshold_triangle(img_HISTEQ)
    mask_init = img_HISTEQ > thresh

    # Use white tophat filtering to remove small artifacts
    # White top-hat filtering inflates objects larger than the structural element,
    #   and then returns the complement. The original mask can be used to crush
    #   small structures in the original image.
    strel = morphology.disk(2)
    noise = morphology.white_tophat(mask_init, strel)
    mask_pred = np.logical_and(mask_init, np.logical_not(noise))
    return mask_pred
Exemple #4
0
def subtract_background(image, elem='disk', radius=50, light_bg=False):
    """Background substraction using structure element.
    Slightly adapted from: https://forum.image.sc/t/background-subtraction-in-scikit-image/39118/4

    :param image: input image
    :type image: NumPy.Array
    :param elem: type of the structure element, defaults to 'disk'
    :type elem: str, optional
    :param radius: size of structure element [pixel], defaults to 50
    :type radius: int, optional
    :param light_bg: light background, defaults to False
    :type light_bg: bool, optional
    :return: image with background subtracted
    :rtype: NumPy.Array
    """
    # use 'ball' here to get a slightly smoother result at the cost of increased computing time
    if elem == 'disk':
        str_el = disk(radius)
    if elem == 'ball':
        str_el = ball(radius)

    if light_bg:
        img_subtracted = black_tophat(image, str_el)
    if not light_bg:
        img_subtracted = white_tophat(image, str_el)

    return img_subtracted
Exemple #5
0
 def _white_tophat(self, image: Union[xr.DataArray,
                                      np.ndarray]) -> np.ndarray:
     if self.is_volume:
         structuring_element = ball(self.masking_radius)
     else:
         structuring_element = disk(self.masking_radius)
     return white_tophat(image, selem=structuring_element)
Exemple #6
0
def white_hatting(file='zones/0.png', use_t=False, t='otsu', rad=3):
    from skimage.morphology import white_tophat
    from skimage.morphology import disk

    if use_t:
        img = threshold(file, t=t)
    else:
        img = misc.imread(file)

    selem = disk(rad)
    wh = white_tophat(img, selem)

    fig, (ax1, ax2) = plt.subplots(ncols=2,
                                   figsize=(8, 4),
                                   sharex=True,
                                   sharey=True)

    ax1.imshow(img, cmap=plt.cm.gray)
    ax1.set_title('original')
    ax1.axis('off')
    ax1.set_adjustable('box-forced')
    ax2.imshow(wh, cmap=plt.cm.gray)
    ax2.set_title('white')
    ax2.axis('off')
    ax2.set_adjustable('box-forced')
    plt.show()
def getImage(i, imOriginal, mean):
    if i == 0:  #return a central crop of 99x99 pixels
        resized = cv2.resize(imOriginal, (128, 128))
        resized = resized * 256.0 - mean
        return resized[14:113, 14:113]
    rot = np.random.uniform(0, 360, 1).astype(int)[0]  #Random rotations
    rot = 90 * np.random.uniform(0, 4, 1).astype(int)[0]  #Random rotations
    #    rot = 0
    im_size = imOriginal.shape[0]
    if (np.random.rand() > 0.5):
        if (np.random.rand() > 0.5):
            imOriginal = cv2.flip(imOriginal, 0)
        else:
            imOriginal = cv2.flip(imOriginal, 1)
    scale = np.random.uniform(0.9, 1.1)
    mat = cv2.getRotationMatrix2D((im_size / 2, im_size / 2), rot, scale=scale)
    resized = cv2.warpAffine(img_out,
                             mat, (im_size, im_size),
                             borderValue=(255, 255, 255))
    img_out = np.zeros((resized.shape[0], resized.shape[1], 3), dtype=np.uint8)
    img_orig = resized[:, :, 0]
    img_btop = 255 - black_tophat(img_orig, selem)
    img_wtop = 255 - white_tophat(img_orig, selem)
    img_out[:, :, 1] = img_btop
    img_out[:, :, 2] = img_wtop

    resized = cv2.resize(img_out, (128, 128))

    resized = resized * 256.0 - mean  #Geht richtig in den Keller auf Werte um 4, wenn man mean nicht abzieht
    #    offsetX = np.random.uniform(10,18,1).astype(int)[0]
    #    offsetY = np.random.uniform(10,18,1).astype(int)[0]
    offsetX = np.random.uniform(0, 28, 1).astype(int)[0]  #Random rotations
    offsetY = np.random.uniform(0, 28, 1).astype(int)[0]  #Random rotations
    return resized[offsetY:(99 + offsetY), offsetX:(99 + offsetX)]
Exemple #8
0
def generate_cost_image(image):
    """
    Convert an image to be used for A* route finding
    Generate an image where each pixel is a cost rather than an intensity. Since
    the path finding algorithms are designed to ascend gradients/follow high
    intensity paths, we need to invert the image before using it.
    """
    disk_size = 5
    original_scale = 10.0

    cost_image = image.copy()

    selem = disk(disk_size)
    image_processed = white_tophat(image, selem)

    # Cutoff anything over the max of the top-hatted image
    cost_image[cost_image > numpy.max(image_processed)] = numpy.max(
        image_processed)

    # Create a cost image scaled to 10% of the processed image
    cost_image = (cost_image / numpy.max(image_processed)) * (
        numpy.max(image_processed) / original_scale)

    # Add the cost image and the processed image together
    cost_image = cost_image + image_processed

    # Invert
    cost_image = (numpy.max(cost_image) - cost_image)

    return cost_image
def bscan_cut(bscans, onh_ybox=[-1, -1]):
    if onh_ybox != [-1, -1]:
        #either no coordinates were entered, or they weren't detected
        cut_scans = bscans[onh_ybox[0]:onh_ybox[1], :, :]
    else:
        cut_scans = bscans[120:136, :, :]
    avg_scans = np.mean(cut_scans, axis=0)
    #Email from Mayank to Eric on 8-15-2015 on Mayank and Robert's formula to resize bscans to 1 px**2
    #height = y_height*0.84
    #width = 1600 px
    height = np.round(avg_scans.shape[1] * 0.84)
    width = 1600
    scan = tf.resize(avg_scans, (height, width), order=3, mode="reflect")
    #drop to 8 bit, otherwise the averaging takes a very long time. Which seems strange...
    scan_s = scan.astype("uint8")
    scan_m = sf.rank.mean(scan_s, selem=mp.disk(50))
    #Worried there are issues with top hat. May smear in noise that becomes part of the center of mass computation.
    scan_b = mp.white_tophat(scan_m, selem=mp.square(500))
    m = sm.moments(scan_b, order=1)
    y = int(np.round(m[0, 1] / m[0, 0]))
    ymin = y - 300
    ymax = y + 300
    cut_scan = scan[ymin:ymax, :]

    return cut_scan.astype("float32")
def white_tophat():
    global filterimage
    kernel = morphology.disk(5)
    img_white = morphology.white_tophat(img, kernel)
    filterimage = img_white
    io.imshow(img_white)
    io.show()
Exemple #11
0
 def __init__(self, file, csv_file, nuclei_channel=1):
     self.raw_im = np.squeeze(czi.imread(file))[nuclei_channel]
     self.labeled_spots_im = np.squeeze(czi.imread(file))[0]
     self.mask_1 = cv2.threshold(
         self.raw_im,
         skimage.filters.threshold_otsu(self.raw_im) + 152, 1,
         cv2.THRESH_BINARY)[1]
     self.nuclei_image = skimage.exposure.equalize_hist(
         morphology.white_tophat(
             np.squeeze(czi.imread(file))[nuclei_channel], square(70)),
         mask=self.mask_1)
     self.corrected_ni = np.multiply(
         np.divide(self.nuclei_image, np.amax(self.nuclei_image)), 255)
     self.otsu = skimage.filters.threshold_otsu(self.corrected_ni)
     self.dtype = str(self.nuclei_image.dtype)
     self.nuclei_mask = cv2.threshold(self.corrected_ni, self.otsu, 1,
                                      cv2.THRESH_BINARY)[1]
     self.nuclei_mask_dense = cv2.threshold(self.corrected_ni,
                                            self.otsu + 80, 1,
                                            cv2.THRESH_BINARY)[1]
     self.nuc_chan = nuclei_channel
     self.input_shape = self.nuclei_image.shape
     self.filename = file
     self.csv_file = csv_file
     self.spatial_df = pd.read_csv(csv_file)
     print('csv: \n', self.csv_file)
     print('image: \n', self.filename)
Exemple #12
0
def ExtractCandidates(im_norm,h,radius,nbit):
    """extract signal candidates applying h_maxima transform 
        INPUTS:
        im_norm=normalised image,
        h=h_maxima threshold,
        radius=structuring element radius,
        nbit= encoding"""
    
    # Normalized top_hat filtering
    se=disk(radius)
    im=white_tophat(im_norm,se)
        
    #filtering local maxima
    h_maxima=extrema.h_maxima(im, h,selem=diamond(1))
    label_h_max=label(h_maxima,neighbors=4)
    labels=pd.DataFrame(data={'labels':np.sort(label_h_max[np.where(label_h_max!=0)])})
    dup=labels.index[labels.duplicated() == True].tolist() #find duplicates labels (=connected components) 
    
    #splitting connected regions to get only one local maxima    
    max_mask=np.zeros(im.shape)
    max_mask[label_h_max!=0]=np.iinfo(nbit).max
            
    for i in range (len(dup)):
        r,c=np.where(label_h_max==labels.loc[dup[i],'labels']) #find coord of points having the same label
        meanpoint_x=np.mean(c)
        meanpoint_y=np.mean(r)
        dist=[distance.euclidean([meanpoint_y,meanpoint_x],[r[j],c[j]]) for j in range(len(r))]
        ind=dist.index(min(dist))
        r,c=np.delete(r,ind),np.delete(c,ind) #delete values at ind position.
        max_mask[r,c]=0 #set to 0 points != medoid coordinates
             
    return max_mask
def getImage(i, imOriginal, mean):
    if i == 0: #return a central crop of 99x99 pixels
        resized = cv2.resize(imOriginal, (128, 128))
        resized = resized * 256.0 - mean
        return resized[14:113, 14:113]
    rot = np.random.uniform(0,360,1).astype(int)[0] #Random rotations
    rot = 90 * np.random.uniform(0,4,1).astype(int)[0] #Random rotations
#    rot = 0
    im_size = imOriginal.shape[0]
    if (np.random.rand() > 0.5):
       if (np.random.rand() > 0.5):
         imOriginal = cv2.flip(imOriginal,0)
       else:
         imOriginal = cv2.flip(imOriginal,1)
    scale = np.random.uniform(0.9,1.1)
    mat = cv2.getRotationMatrix2D((im_size / 2, im_size / 2), rot, scale=scale)
    resized = cv2.warpAffine(img_out, mat, (im_size, im_size), borderValue=(255,255,255))
    img_out = np.zeros((resized.shape[0], resized.shape[1], 3), dtype=np.uint8)
    img_orig = resized[:,:,0]
    img_btop = 255-black_tophat(img_orig, selem)
    img_wtop = 255-white_tophat(img_orig, selem)
    img_out[:, :, 1] = img_btop
    img_out[:, :, 2] = img_wtop
    
    resized = cv2.resize(img_out, (128, 128))
    
    resized = resized * 256.0 - mean #Geht richtig in den Keller auf Werte um 4, wenn man mean nicht abzieht
#    offsetX = np.random.uniform(10,18,1).astype(int)[0] 
#    offsetY = np.random.uniform(10,18,1).astype(int)[0] 
    offsetX = np.random.uniform(0,28,1).astype(int)[0] #Random rotations
    offsetY = np.random.uniform(0,28,1).astype(int)[0] #Random rotations
    return resized[offsetY:(99+offsetY), offsetX:(99+offsetX)]
def adjust_image(image,
                 lower_thresh=2, upper_thresh=98,
                 filter_size=0, 
                 cmap='jet'
                ):
    """
    Applies contrast stretching to an image, then
    uses a white top-hat filter to remove small patches
    of brightness that would cause false positives later.
    
    Input: image; values for min and max percentile
    brightnesses to keep; size below which bright patches
    will be removed; colourmap.
    
    Output: image, hopefully with most of the background stripped
    out. If it's worked well, it'll look like bright blobs on a
    dark background.
    """
    
    p2, p98 = np.percentile(image, (lower_thresh, upper_thresh))
    img_rescale = exposure.rescale_intensity(image, in_range=(p2, p98))
    selem = morphology.disk(filter_size)
    wht_tophat = morphology.white_tophat(img_rescale,selem=selem)
    io.imshow(img_rescale - wht_tophat, cmap=cmap)
    
    return img_rescale
Exemple #15
0
def detectCentroid(input):
    # Centroid Detection and contouring
    output = white_tophat(input, square(35))
    blur = cv2.GaussianBlur(output, (5, 5), 0)
    _, threshold = cv2.threshold(blur, 254, 255, 0)
    threshold = threshold.astype('uint8')
    contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    i = 0
    for cnt in contours:
        for i in tqdm(np.arange(len(contours))):
            cv2.drawContours(threshold, [cnt], -1, (78, 55, -128), 1)
            M = cv2.moments(cnt)
            time.sleep(0.1)
        i = i + 1
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    cv2.circle(threshold, (cX, cY), 2, (78, 55, -128), 1)
    cv2.putText(threshold, "center", (cX - 20, cY - 20),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
    cv2.imshow("Centroid", threshold)
    cv2.moveWindow("Centroid", 300, 30)
    print("X-Coordinate: " + str(cX))
    print("Y-Coordinate: " + str(cY))
    cv2.destroyWindow("Original Image")
    cv2.waitKey(0)
    return threshold, cX, cY
    def insert_db(self, mode, image, label, features, channel_no, inverse):
        if inverse:
            image_ubyte = 255 - img_as_ubyte(image)
        else:
            image_ubyte = img_as_ubyte(image)

        image_ubyte = numpy.transpose(image_ubyte, (2, 0, 1))
                
        image_string = image_ubyte.tostring()
        
        if features != None:
            delimeter = '!@#$'
            self.datum.data = image_string + delimeter + features
        elif channel_no > 3:
            selem = disk(6)
            w_tophat = white_tophat(image_ubyte, selem)
            b_tophat = black_tophat(image_ubyte, selem)
            self.datum.data = image_string + w_tophat.tostring() + b_tophat.tostring()
        else:
            self.datum.data = image_string
            
        if label != None:
            self.datum.label = int(label)                
    
        serialized = self.datum.SerializeToString()
        
        if mode == 'train':
            self.train_batch.Put("%08d" % self.train_no, serialized)                    
            self.train_no += 1
        elif mode == 'valid':
            self.valid_batch.Put("%08d" % self.valid_no, serialized)                    
            self.valid_no += 1
        elif mode == 'test':
            self.test_batch.Put("%08d" % self.test_no, serialized)                    
            self.test_no += 1
def findCorrespondencesMax_createInputs(candidates, normalized_images, cycles,
                                        channels, nbit, n_threads, radius):
    """INPUTS:
        candidates=binary max masks
        normalized_images=normalized images,
        cycles=number of sequencing cycles,
        nbit= encoding"""
    se = ball(radius)
    im_th = np.zeros(normalized_images.shape).astype(np.float64)
    for cycle in range(cycles):
        for ch in range(2, channels):
            im_th[cycle, ch, :, :, :] = white_tophat(
                normalized_images[cycle, ch, :, :, :], se)

    candidates[candidates == np.iinfo(nbit).max] = 1

    res = Parallel(n_jobs=n_threads)(
        delayed(Execute_Correspondences_CreateInputs)(
            candidates, normalized_images, im_th, cycle, channels, nbit)
        for cycle in range(cycles))

    max_df = pd.concat([res[i]['max_df'] for i in range(len(res))],
                       ignore_index=True)
    inputs_df = pd.concat([res[i]['inputs_df'] for i in range(len(res))],
                          ignore_index=True)
    inputs_df = inputs_df[[
        'cycle', 'ch', 'x', 'y', 'z', 'Intensities_window_5x5'
    ]]

    return {'inputs_df': inputs_df, 'max_df': max_df}
    def bw_transform(img_src):  # To finish
        tval = threshold_otsu(img_src)
        bn_src = (img_src > tval)
        # Reducing horizontal and vertical lines
        r_vertical = rectangle(bn_src.shape[1], 1)
        r_horizontal = rectangle(1, bn_src.shape[0])

        # Reducing horizontal lines
        bn_src = white_tophat(bn_src, r_horizontal)

        # Reducing vertical lines
        bn_src = white_tophat(bn_src, r_vertical)

        # Reducing salt
        bn_src = remove_small_objects(bn_src, connectivity=2, min_size=9)
        return bn_src
Exemple #19
0
def _segment_plate(image_path):
    # Učitaj sliku:
    car_image = imread(image_path, as_grey=True)

    # Postavi piksele da budu vrijednosti izmedu 0 i 255, umjesto izmedu 0 i 1:
    car_image = car_image * 255

    # White tophat:
    wt_car_image = white_tophat(car_image, disk(10))

    # U binary:
    block_size = 105
    threshold = threshold_local(wt_car_image, block_size, offset=-30)
    binary_image = wt_car_image > threshold

    # Dilation:
    dilated_image = dilation(binary_image, disk(2))

    label_image = label(dilated_image)

    plate_like_objects = []

    for region in regionprops(label_image):
        min_row, min_col, max_row, max_col = region.bbox
        aspect_ratio = (max_col - min_col) / (max_row - min_row)

        if (10000 > region.area > 2000) and (2.2 < aspect_ratio < 4.5):
            plate_like_objects.append(car_image[min_row:max_row,
                                                min_col:max_col])

    return plate_like_objects
def rolling_disk(image, radius=50, light_bg=False):
    from skimage.morphology import white_tophat, black_tophat, disk 
    str_el = disk(radius)
    print(image.shape)
    if light_bg:
        return black_tophat(image, str_el)
    else:
        return white_tophat(image, str_el)
def subtract_background(image, radius, light_bg=False):
    # you can also use 'ball' here to get a slightly smoother result at the
    # cost of increased computing time
    str_el = disk(radius)
    if light_bg:
        return black_tophat(image, str_el)
    else:
        return white_tophat(image, str_el)
def run(img):
    if len(img.shape) > 2 and img.shape[2] == 4:
        img = color.rgba2rgb(img)
    if len(img.shape) == 2:
        img = color.gray2rgb(img)
    img = color.rgb2gray(img)
    img = white_tophat(img, disk(1))
    return to_base64(img)
Exemple #23
0
    def morphology_filter(self, image):
        morph_img = np.asarray(image)

        kernel_dil = np.ones((3, 3), np.uint8)

        if len(morph_img.shape) >= 3:
            try:
                #print("***converting to grayscale***")
                morph_for_ski = img_as_float(color.rgb2gray(morph_img))

                # binearisation
                image_binary = morph_for_ski > 0.001
                morph_for_ski[np.logical_not(image_binary)] = 0

                # binearization - Black tophat
                wth = white_tophat(morph_img)

                ch_one = wth[:, :, 0] > 5
                ch_two = wth[:, :, 1] > 5
                ch_three = wth[:, :, 2] > 5

                morph_for_ski[ch_one] = 0
                morph_for_ski[ch_two] = 0
                morph_for_ski[ch_three] = 0

                return (morph_for_ski*255).astype(np.uint8)

            # image_binary = morphology.opening(morph_for_ski)
            # image_binary = cv2.dilate(np.float32(image_binary), kernel_dil, iterations=3)
            # out_skel = skeletonize(image_binary, method='lee')

            except ValueError:
                print(ValueError)
                print("Can't convert this image to grayscale")
                return image

        # good work with denoising, gamma, sv from hsv and knee filtering
        # this is much more precisly but, takes much longer time
        else:
            try:
                print("***Collecting data..***")
                morph_for_ski = img_as_float(morph_img)
                # binearisation
                image_binary = morph_for_ski > 0.001
                morph_for_ski[np.logical_not(image_binary)] = 0

                return (morph_for_ski * 255).astype(np.uint8)

                #image_binary = morphology.opening(morph_for_ski)
                #image_binary = cv2.dilate(np.float32(image_binary), kernel_dil, iterations=3)
                #out_skel = skeletonize(image_binary, method='lee')

                #return out_skel

            except ValueError:
                print(ValueError)
                print("Can't convert this image to grayscale")
                return image
Exemple #24
0
def mbi_feature(image_name, output=None, postprocess=True, smooth_factor=None):
    MBI_THRESHOLD = 5.5

    ds = gdal.Open(image_name)
    image = ds.ReadAsArray()
    geotran = ds.GetGeoTransform()
    ulx = geotran[0]
    uly = geotran[3]
    cell_width = geotran[1]
    cell_height = geotran[5]

    out_srs = osr.SpatialReference()
    out_srs.ImportFromEPSG(4326)
    out_srs_wkt = out_srs.ExportToWkt()
    # out_cell_width = block * cell_width
    # out_cell_height = block * cell_height

    ds = None
    image = np.moveaxis(image, 0, -1)  # rows, columns, channels
    # calculate brightness as a local max
    brightness = calc_stats(image, ["max"], 2)

    # could be useful to smooth...
    if smooth_factor:
        brightness = gaussian(brightness, smooth_factor)
    # a set of linear structural elements
    # for the white tophat transformation
    # dirs = [45, 90, 135, 180]
    se_sizes = [5, 9, 13, 19, 23, 27]
    se_set = get_se_set(se_sizes)
    # 'white' top-hat transformation
    # in this case, white top-hat is the brightness image minus morphological opening
    mean_w_tophats = []
    for s in se_set:  # for each size in the structural element set
        w_tophats = []
        for k in s:  # for each direction kernel in the structural element set for this size
            # directional top hat transformation using linear SE
            w_tophats.append(white_tophat(brightness, k))
        mean_w_tophat = calc_stats(w_tophats, ['mean'], 0)
        mean_w_tophats.append(mean_w_tophat)

    th_dmp = []
    th_idx = 0
    # calculate the differential morphological profile
    while th_idx + 1 < len(mean_w_tophats):
        th_dmp.append(
            np.absolute(mean_w_tophats[th_idx + 1] - mean_w_tophats[th_idx]))
        th_idx += 1
    mbi = calc_stats(np.array(th_dmp), ['mean'], 0)

    if postprocess:
        mbi = np.where(mbi >= MBI_THRESHOLD, 1, 0)
    if output:
        # out_geotran = (out_ulx, out_cell_width, 0, out_uly, 0, out_cell_height)
        write_geotiff(output, mbi, geotran, out_srs_wkt)
    return np.array(mbi)
Exemple #25
0
def getScalemask(grayimg):
    """
    create a mask to capture scale marks
    note: this is not pure - text labels are also captured

    grayimg  = input 2D image array
    """

    selem = morphology.disk(1)
    return morphology.white_tophat(grayimg, selem)
Exemple #26
0
def white_tophat(img):
    orig_img = util.img_as_ubyte(img)
    image = orig_img.copy()
    image[340:350, 200:210] = 255
    image[100:110, 200:210] = 0
    selem = morphology.disk(6)
    eroded = morphology.white_tophat(image, selem)
    plot_comparison(orig_img, eroded, 'White Tophat')
    plt.tight_layout()
    plt.show()
Exemple #27
0
    def BackgroundSubtraction_runnable(cls,
                                       image,
                                       strucsize=50,
                                       disk=disk,
                                       white_tophat=white_tophat):

        struc = disk(strucsize)

        image = white_tophat(image, struc)

        return image
Exemple #28
0
    def process_tophat(self, im_denoise):
        """
    Process image using tophat, closing, then dilation
    """
        im_wtophat = morphology.white_tophat(im_denoise,
                                             selem=morphology.disk(
                                                 self.C['WTOPHAT_FILTER_SZ']))
        im_closed = morphology.closing(im_wtophat)
        im_processed = morphology.dilation(im_closed)

        return im_processed
Exemple #29
0
def calc_mbi(raster_input, s_min, s_max, s_delta):
    """ Calculates morphological building index (MBI) per
    Huang et al. (2015)
    Assumes use of linear structuring element
    MBI = sum(dxs)(whitetop_hat(morphological profiles))/(D*S)
    d = degrees (0,45, 90, 135)
    D = 4
    s = size of linear structuring element (pixels)
    S = (s_max-s_min)/s_delta + 1

    Parameters
    ------------
    raster_input : 3-dim raster (tif) stack for calculating brightness
    s_min, s_max, s_delta: numerical input (integer)

    Returns
    ------------
    Calculated values for MBI in a numpy array
    """
    # Calculate brightness for cloud masked stack
    brightness = np.nanmax(raster_input, axis=0)

    # Cap brightness values at a max of 1. Replace all values greater than 1 with a value of 1
    brightness_cap = np.where(brightness > 1, 1, brightness)

    # Initialize inputs for MBI calculation
    selem = selemline(0, 0)
    w_tophat_array_sum = white_tophat(brightness_cap, selem)

    # Loop and sum black tophat morphological profiles for MSI calculation
    for i in range(s_min, s_max + s_delta, s_delta):
        for x in range(0, 4):
            selem = selemline(i, 45 * x)
            w_tophat = white_tophat(brightness_cap, selem)
            w_tophat_array_sum = w_tophat_array_sum.__add__(w_tophat)

    D = 4
    S = ((s_max - s_min) / s_delta) + 1
    mbi = w_tophat_array_sum / (D * S)

    return mbi
def filterWithWhiteTophat(image, radius):
    """Wrapper for skimage's white tophat filter

    Parameters
    ----------
    image : np.ndarray
        Image to be filtered.
    radius: int
        Radius of the morphological disk.
    """
    selem = disk(radius)
    return white_tophat(image, selem)
def stara(smap,
          circle_radius: u.deg = 100 * u.arcsec,
          median_box: u.deg = 10 * u.arcsec,
          threshold=6000,
          limb_filter: u.percent = None):
    """
    A method for automatically detecting sunspots in white-light data using morphological operations.

    Parameters
    ----------
    smap : `sunpy.map.GenericMap`
        The map to apply the algorithm to.

    circle_radius : `astropy.units.Quantity`, optional
        The angular size of the structuring element used in the
        `skimage.morphology.white_tophat`. This is the maximum radius of
        detected features.

    median_box : `astropy.units.Quantity`, optional
        The size of the structuring element for the median filter, features
        smaller than this will be averaged out.

    threshold : `int`, optional
        The threshold used for detection, this will be subject to detector
        degradation. The default is a reasonable value for HMI continuum images.

    limb_filter : `astropy.units.Quantity`, optional
        If set, ignore features close to the limb within a percentage of the
        radius of the disk. A value of 10% generally filters out false
        detections around the limb with HMI continuum images.

    """
    data = invert(smap.data)

    # Filter things that are close to limb to reduce false detections
    if limb_filter is not None:
        hpc_coords = sunpy.map.all_coordinates_from_map(smap)
        r = np.sqrt(hpc_coords.Tx**2 + hpc_coords.Ty**2) / (
            smap.rsun_obs - smap.rsun_obs * limb_filter)
        data[r > 1] = np.nan

    # Median filter to remove detections based on hot pixels
    m_pix = int((median_box / smap.scale[0]).to_value(u.pix))
    med = median(data, square(m_pix), behavior="ndimage")

    # Construct the pixel structuring element
    c_pix = int((circle_radius / smap.scale[0]).to_value(u.pix))
    circle = disk(c_pix / 2)

    finite = white_tophat(med, circle)
    finite[np.isnan(finite)] = 0  # Filter out nans

    return finite > threshold
Exemple #32
0
def white_tophat():
    image = io.imread("pics/Cosmos.jpg")
    white = morphology.white_tophat(image)
    fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, figsize=(8, 3),
                                   sharex=True, sharey=True)

    ax0.imshow(image, cmap=plt.cm.gray)
    ax0.axis('off')
    ax1.imshow(white, cmap=plt.cm.gray)
    ax1.axis('off')

    plt.show()
Exemple #33
0
def extract_leaf_stem(image, maxdisksize=30, minstempixels=100):
    width,height,depth = np.shape(image)
    X = image.reshape(width*height,depth)
    clast = KMeans(n_clusters=2, n_jobs=-2)
    clast.fit(X)
    lbls = np.array(clast.labels_).reshape(width,height)
    if np.mean(lbls[width/2-10:width/2+10,height/2-10:height/2+10])>0.5:
        rightlbl=1
    else:
        rightlbl=0
    lbls = (lbls==rightlbl).astype(np.int16)
    im_flt=white_tophat(lbls,disk(maxdisksize))
#     cnts=measure.find_contours(im_flt, 0.8)
    flb=measure.label(im_flt,connectivity=2) 
    props=measure.regionprops(flb)
    cc = [item['inertia_tensor_eigvals'][0]/item['inertia_tensor_eigvals'][1] if item['inertia_tensor_eigvals'][1]>0.0 and item['area']>=minstempixels else 0.0 for item in props]
    res =  lbls.copy()
    if any(cc):
        indmax = np.nanargmax(cc)
        stem  = (flb==indmax+1)
        res[stem]=0
    return res
Exemple #34
0
 def vesselEnhance(self, array=numpy.empty(0)):
     """
         @method vesselEnhance
         @param array {numpy array} the array the operation is carried out
             on, default is the image_array.
     """
     self.__printStatus("Vessel enhancement...");
     if not array.any():
         array = self.image_array
     # disk shaped mask with radius 8 
     disk_shape = disk(8)
     # the complimentary image is saved to hc:
     array = numpy.uint8(array)
     hc = 255 - array
     # Top Hat transform
     # https://en.wikipedia.org/wiki/Top-hat_transform
     # White top hat is defined as the difference between
     # the opened image and the original image. 
     # in this case the starting image is the complimentary image `hc`
     self.image_array = white_tophat(hc, selem=disk_shape) * self.mask
     self.__printStatus("[done]", True)
     return self
# mostly removed.
#
# White tophat
# ============
#
# The ``white_tophat`` of an image is defined as the *image minus its
# morphological opening*. This operation returns the bright spots of the
# image that are smaller than the structuring element.
#
# To make things interesting, we'll add bright and dark spots to the image:

phantom = orig_phantom.copy()
phantom[340:350, 200:210] = 255
phantom[100:110, 200:210] = 0

w_tophat = white_tophat(phantom, selem)
plot_comparison(phantom, w_tophat, 'white tophat')

######################################################################
# As you can see, the 10-pixel wide white square is highlighted since it is
# smaller than the structuring element. Also, the thin, white edges around
# most of the ellipse are retained because they're smaller than the
# structuring element, but the thicker region at the top disappears.
#
# Black tophat
# ============
#
# The ``black_tophat`` of an image is defined as its morphological **closing
# minus the original image**. This operation returns the *dark spots of the
# image that are smaller than the structuring element*.
        rots = np.random.uniform(0,360,10).astype(int) #10 random rotations
        for i, rot in enumerate(rots):
            im_size = img_org.shape[0]
            if (np.random.rand() > 0.5):
                if (np.random.rand() > 0.5):
                    img_org = cv2.flip(img_org,0)
                else:
                    img_org = cv2.flip(img_org,1)
            scale = np.random.uniform(0.7,1.3)
            mat = cv2.getRotationMatrix2D((im_size / 2, im_size / 2), rot, scale=scale)
            img_rotated = cv2.warpAffine(img_org, mat, (im_size, im_size), flags=cv2.INTER_LINEAR, borderValue=(255,255,255))

            img_out = np.zeros((img_rotated.shape[0], img_rotated.shape[1], 3), dtype=np.uint8)
            img_orig = img_rotated[:,:,0]
            img_btop = 255-black_tophat(img_orig, selem)
            img_wtop = 255-white_tophat(img_orig, selem)
            img_out[:, :, 1] = img_btop
            img_out[:, :, 2] = img_wtop

            img_rotated = img_out

            if show:
              cv2.imshow('Rot_' + str(i), img_rotated)
            writeImg(outPath, img_rotated, kind, file, i+1) #Original
        if show:
            cv2.waitKey(20000 )
            lineNum += 1
            if (lineNum > 20):
                break

Exemple #37
0
    # get 'structureness' for each channel at each pixel
    R = (K1 / K2)**2 
    S = K1**2 + K2**2 
    # conservative threshold
    R_thresh = ma.median(R)
    
    targets = (R < R_thresh).filled(0)

    # erode based on scale space
    erode_selem = disk(sigma+1)
    dilate_selem = disk(max(sigma-1, 1))
    min_size = dilate_selem.sum()*2

    targets = binary_erosion(targets, selem=erode_selem)
    #targets = binary_dilation(targets, selem=dilate_selem)
    targets = white_tophat(targets, selem=disk(sigma))

    labeled, n_labels = label(targets, background=0,
                                connectivity=1, return_num=True)
   
    sizes = np.bincount(labeled.flatten())
    rankings = np.argwhere(sizes >= min_size)

    # labels for the N largest regions
    N = rankings.size - 1 # number of regions to view
    largest = rankings[1:] # everything except background
    
    candidates = np.zeros_like(labeled)

    for region_label in largest:
        candidates = np.logical_or(candidates, labeled == region_label)
    def convert_test_data(self, data_set_folder, min_pixel, test_db_name, test_output_pickle_path, 
                          inverse, channel_no = 1):
        self.remove_folder(test_db_name)
            
        test_db = leveldb.LevelDB(test_db_name)
        
        pickleTestX = test_output_pickle_path + "/testX_size_" + str(min_pixel) + ".pickle"
        pickleFileNames = test_output_pickle_path + "/fileNames.pickle"
        
        if not os.path.exists(test_output_pickle_path):
            os.makedirs(test_output_pickle_path)

        numberofImages = 0    
    
        datum = caffe.proto.caffe_pb2.Datum()
        datum.channels = channel_no
        datum.width = min_pixel
        datum.height = min_pixel
        
        test_batch = leveldb.WriteBatch()
    
        print "Load test dataset from image files"
    
        for fileNameDir in os.walk(data_set_folder):   
            for index, fileName in enumerate(fileNameDir[2]):
                if fileName[-5:] != ".JPEG":
                  continue
                numberofImages += 1
        
        imageSize = min_pixel * min_pixel
        num_rows = numberofImages # one row for each image in the test dataset

        batch_size = 10000    
        data_size = min(batch_size, numberofImages)
        testX = numpy.zeros((data_size, channel_no, imageSize), dtype=numpy.uint8)
        
        files = []
        db_index = 0
        pickle_index = 0
        batch_no = 1
        
        print "Reading images"
        for fileNameDir in os.walk(data_set_folder):   
            for index, fileName in enumerate(fileNameDir[2]):
                if fileName[-5:] != ".JPEG":
                  continue
                
                nameFileImage = "{0}{1}{2}".format(fileNameDir[0], os.sep, fileName)            
                org_image = Image.open(nameFileImage)
                files.append(fileName)
                
                image = org_image.resize((min_pixel, min_pixel), Image.ANTIALIAS)
    
                """
                print fileName
                
                plt.figure(1, figsize=(1, 1), dpi=100)
                plt.gray();                
                plt.subplot(1, 1, 1)
                plt.imshow(image)
                plt.show()
                """
    
                if inverse:
                    image_ubyte = 255 - img_as_ubyte(image)
                else:
                    image_ubyte = img_as_ubyte(image)
                
                if channel_no > 1:
                    selem = disk(6)
                    w_tophat = white_tophat(image_ubyte, selem)
                    b_tophat = black_tophat(image_ubyte, selem)
                    datum.data = image_ubyte.tostring() + w_tophat.tostring() + b_tophat.tostring()
                    image_output = numpy.concatenate((image_ubyte, w_tophat, b_tophat), axis=1)
                else:
                    datum.data = image_ubyte.tostring()
                    image_output = image_ubyte
                
                    
                test_batch.Put("%08d" % db_index, datum.SerializeToString())
    
                testX[pickle_index] = numpy.reshape(image_output, (channel_no, imageSize))
    
                db_index += 1
                pickle_index += 1
                
                if db_index % 1000 == 0:
                    test_db.Write(test_batch, sync = True)
                    del test_batch
                    test_batch = leveldb.WriteBatch()
                    print 'Processed %i test images.' % db_index
    
                if pickle_index % batch_size == 0:
                    pickle_file_name = pickleTestX + "_" + str(batch_no)
                    with open(pickle_file_name,'wb') as fp:
                        cPickle.dump(testX, fp)
                        print "pickled %s" % pickle_file_name
                        data_size = min(batch_size, numberofImages - batch_size * batch_no)
                        testX = numpy.zeros((data_size, channel_no, imageSize), dtype=numpy.uint8)
                        batch_no += 1
                        pickle_index = 0
                
                report = [int((j+1)*num_rows/20.) for j in range(20)]
                if db_index in report: print numpy.ceil(db_index *100.0 / num_rows), "% done"
    
    
        # Write last batch of images
        if db_index % 1000 != 0:
            test_db.Write(test_batch, sync = True)
    
        if pickle_index % batch_size > 0:
            pickle_file_name = pickleTestX + "_" + str(batch_no)
            with open(pickle_file_name,'wb') as fp:
                cPickle.dump(testX, fp)
                print "pickled %s" % pickle_file_name
                        
        with open(pickleFileNames,'wb') as fp:
            cPickle.dump(files, fp)
    
        print 'Processed a total of %i images.' % db_index
Exemple #39
0
import numpy as np
from PIL import Image

from skimage.morphology import erosion, dilation, opening, closing
from skimage.morphology import black_tophat, white_tophat
from skimage.morphology import disk, diamond, rectangle, square, star

def my_imshow(arr, filter_name):
    f,ax = plt.subplots()
    ax.set_title(filter_name)
    #ax.axis('off')
    ax.set_adjustable('box-forced')
    ax.imshow(arr, cmap=cm.gray, interpolation='none')
    plt.show()
    
#Import an image.
image = np.array(Image.open('Butterfly.jpg'))
my_imshow(image, 'original')

#White tophat
fly = img.copy()
fly[340:350, 200:210] = 255
fly[100:110, 200:210] = 0

w_tophat = white_tophat(fly, selem)
my_imshow(w_tophat, 'white-tophat')

#Black tophat
b_tophat = black_tophat(fly, selem)
my_imshow(b_tophat, 'black-tophat')
Exemple #40
0
#t.reset();
#res2 = open(img, structureElement('Disk', (30,30)).astype('bool'));
#t.printElapsedTime('mahotas');

t.reset();
se = structureElement('Disk', (15,15)).astype('uint8');
res2 = cv2.morphologyEx(img, cv2.MORPH_OPEN, se)
t.printElapsedTime('opencv');

#from skimage.morphology import disk
#se = disk(10);

t.reset();
#res3 = opening(img.astype('uint16'), se);
res3 = white_tophat(img.astype('uint16'), se);

t.printElapsedTime('skimage');
t.reset();

x = (res3);
print (x.min(), x.max())

plotTiling(numpy.dstack((10 * img, 10 * (img - res2))))

#seems not to work correctly -> scipy gets very slow for larger images (??)




"""