Ejemplo n.º 1
0
Archivo: tools.py Proyecto: mjirik/lisa
def scharr(data, sliceId=2):
    edges = np.zeros(data.shape)
    if sliceId == 2:
        for idx in range(data.shape[2]):
            edges[:, :, idx] = skifil.scharr(data[:, :, idx])
    elif sliceId == 0:
        for idx in range(data.shape[0]):
            edges[idx, :, :] = skifil.scharr(data[idx, :, :])
    return edges
Ejemplo n.º 2
0
def test_scharr_vertical():
    """Scharr on a vertical edge should be a vertical line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (j >= 0).astype(float)
    result = filters.scharr(image) * np.sqrt(2)
    j[np.abs(i) == 5] = 10000
    assert_allclose(result[j == 0], 1)
    assert (np.all(result[np.abs(j) > 1] == 0))
Ejemplo n.º 3
0
def test_scharr_horizontal():
    """Scharr on an edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.scharr(image) * np.sqrt(2)
    # Fudge the eroded points
    i[np.abs(j) == 5] = 10000
    assert_allclose(result[i == 0], 1)
    assert (np.all(result[np.abs(i) > 1] == 0))
Ejemplo n.º 4
0
def test_scharr_horizontal():
    """Scharr on an edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.scharr(image) * np.sqrt(2)
    # Fudge the eroded points
    i[np.abs(j) == 5] = 10000
    assert_allclose(result[i == 0], 1)
    assert (np.all(result[np.abs(i) > 1] == 0))
Ejemplo n.º 5
0
def test_scharr_horizontal():
    """Scharr on an edge should be a horizontal line."""
    i, j = np.mgrid[-5:6, -5:6]
    image = (i >= 0).astype(float)
    result = filters.scharr(image) * np.sqrt(2)
    # Check if result match transform direction
    i[np.abs(j) == 5] = 10000
    assert_allclose(result[i == 0], 1)
    assert (np.all(result[np.abs(i) > 1] == 0))
Ejemplo n.º 6
0
 def edge_method(method_num, im, sigma=2):
     print("reached")
     return {
         1:feature.canny(im),
         2:feature.canny(im, sigma=2),
         3:roberts(im),
         4:sobel(im),
         5:scharr(im),
         6:prewitt(im)
     }[method_num]
Ejemplo n.º 7
0
def computeModels(img):
    '''Calcule les resultats des differentes methodes de detection d'objets'''
    #results = {'Image original': ing}
    results = {}
    results['Canny'] = feature.canny(img, sigma=1)
    results['Roberts'] = roberts(img)
    results['Sobel'] = sobel(img)
    results['Scharr'] = scharr(img)
    results['Prewitt'] = prewitt(img)
    results['Laplace'] = laplace(img)
    return results
Ejemplo n.º 8
0
 def calculate_scharr(self, X, axis=0):
     axis = axis
     trans = []
     for n in X:
         p = []
         for i in range(0, n.shape[axis]):
             layer = n[:, :, i]
             p.append(scharr(layer))
         trans.append(np.asarray(p))
     self.transformed = np.asarray(trans)
     return self
Ejemplo n.º 9
0
 def contour_map(self, image):
     ratio = 0.75
     image = gaussian(image, sigma=1)
     contour = scharr(image)
     contour = (contour - contour.min()) / (contour.max() - contour.min() +
                                            1e-11)
     mid_v = np.sort(contour.reshape(-1))
     value = mid_v[int(ratio * len(mid_v))]
     contour[contour < value] = 0
     contour[contour >= value] = 1
     return contour
Ejemplo n.º 10
0
    def Get_Parameters(self):
        copy_img = self.img
        if len(self.img.shape) == 3:
            copy_img = rgb2gray(self.img)

        self.Variance = variance(laplace(copy_img, ksize=3))
        self.MaxVariance = np.amax(laplace(copy_img, ksize=3))
        self.Noise = estimate_sigma(copy_img)
        self.Scharr = variance(scharr(copy_img))

        print(self.Variance, self.MaxVariance, self.Scharr, self.Noise)
Ejemplo n.º 11
0
def mia_cmpedge(binary, verbose):
    gradmag = scharr(binary)
    thresh_gradmag = threshold_otsu(gradmag)
    gradmag = gradmag > thresh_gradmag
    gradmag = gradmag.astype(int)
    gradmag_thin = skeletonize(gradmag)
    plt.imshow(gradmag_thin)
    gradmag_thin = gradmag_thin.astype(int)
    [x, y] = np.nonzero(gradmag_thin)
    [dx, dy] = smoothGradient(binary, 2)
    return gradmag_thin, x, y, dx, dy
Ejemplo n.º 12
0
def read_mask(mask_id, shape):
    mask_path = TRAIN_MASK_PATTERN.format(mask_id, mask_id)
    masks = imread_collection(mask_path).concatenate()
    height, width, _ = shape
    num_masks = masks.shape[0]
    mask = np.zeros((height, width), np.uint32)
    dmask = np.zeros((height, width), np.uint32)
    for index in range(0, num_masks):
        dm = scharr(masks[index])
        mask[masks[index] > 0] = 1
        dmask[dm > 0] = 1
    return np.dstack([mask, dmask])
Ejemplo n.º 13
0
def process_images(files):
    n = len(files)
    for i in range(n):
        # Load image and use scharr to get edges
        image = io.imread(files[i][0],as_grey=True)
        image = scharr(img)
        
        # Scale the intensity to whiten the edges
        p95 = np.percentile(img, (95))
        image = exposure.rescale_intensity(image, in_range=(0, p95))
        
    return image
Ejemplo n.º 14
0
def binary_BF(image,
              meanse=disk(10),
              edgefilt='prewitt',
              opense=disk(10),
              fill_first=False,
              bi_thresh=0.000025,
              tophatse=disk(20)):

    #convertim = img_as_ubyte(image)
    meanim = rank.mean(image, meanse)
    if edgefilt is 'prewitt':
        edgeim = prewitt(meanim)
    elif edgefilt is 'sobel':
        edgeim = sobel(meanim)
    elif edgefilt is 'scharr':
        edgeim = scharr(meanim)
    elif edgefilt is 'roberts':
        edgeim = roberts(meanim)

    closeim = closing(edgeim, opense)
    openim = opening(closeim, opense)
    if fill_first:
        seed = np.copy(openim)
        seed[1:-1, 1:-1] = openim.max()
        mask = openim
        filledim = reconstruction(seed, mask, method='erosion')
        binarim = filledim > bi_thresh
    else:
        binarim = openim > bi_thresh * np.mean(openim)
        seed = np.copy(binarim)
        seed[1:-1, 1:-1] = binarim.max()
        mask = binarim
        filledim = reconstruction(seed, mask, method='erosion')

    tophim = filledim - closing(white_tophat(filledim, tophatse),
                                opense) > 0.01

    fig, ax = plt.subplots(nrows=2, ncols=4, figsize=(16, 8))
    ax[0][0].imshow(image, cmap='gray')
    ax[0][1].imshow(meanim, cmap='gray')
    ax[0][2].imshow(edgeim, cmap='gray', vmax=4 * np.mean(edgeim))
    ax[0][3].imshow(closeim, cmap='gray', vmax=4 * np.mean(closeim))
    ax[1][0].imshow(openim, cmap='gray', vmax=4 * np.mean(openim))
    ax[1][1].imshow(binarim, cmap='gray')
    ax[1][2].imshow(filledim, cmap='gray')
    ax[1][3].imshow(tophim, cmap='gray')
    for axes in ax:
        for axe in axes:
            axe.axis('off')
    fig.tight_layout()

    return tophim
Ejemplo n.º 15
0
 def execute(input_image, settings):
     filter_number = settings.boundaries_settings[0]
     grayscale = input_image.convert('L')
     if filter_number == 1:
         edges = roberts(grayscale)
     elif filter_number == 2:
         edges = prewitt(grayscale)
     elif filter_number == 3:
         edges = scharr(grayscale)
     elif filter_number == 4:
         edges = sobel(grayscale)
     array = np.uint8(plt.cm.gist_earth(edges) * 255)
     return Image.fromarray(array).convert(mode='L')
Ejemplo n.º 16
0
def get_contour_interior(mask):
    if 'camunet' == config['param']['model']:
        # 2-pixel contour (1out+1in), 2-pixel shrinked interior
        outer = dilation(mask)
        inner = erosion(mask)
        contour = ((outer != inner) > 0).astype(np.uint8) * 255
        interior = (erosion(inner) > 0).astype(np.uint8) * 255
    else:
        contour = filters.scharr(mask)
        scharr_threshold = np.amax(abs(contour)) / 2.
        contour = (np.abs(contour) > scharr_threshold).astype(np.uint8) * 255
        interior = (mask - contour > 0).astype(np.uint8) * 255
    return contour, interior
def addEdges(df, gray):
    canny_edges = canny(gray, 0.6)
    roberts_edges = roberts(gray)
    sobel_edges = sobel(gray)
    scharr_edges = scharr(gray)
    prewitt_edges = prewitt(gray)
    df['canny_edges'] = canny_edges.reshape(-1)
    df['roberts_edge'] = roberts_edges.reshape(-1)
    df['sobel_edge'] = sobel_edges.reshape(-1)
    df['scharr_edge'] = scharr_edges.reshape(-1)
    df['prewitt_edge'] = prewitt_edges.reshape(-1)

    return df
Ejemplo n.º 18
0
def edge_entropy(img, bal=0.1):
    """
    Weights pixels based on a weighted edge-detection and entropy balance.

    Parameters
    ----------
    img : ndarray
        Image to weight.
    bal : float (optional)
        How much to value entropy (bal) versus edge-detection (1 - bal)

    Returns
    -------
    ndarray :
        Noramlized weight matrix for pixel sampling.
    """
    dn_img = skimage.restoration.denoise_tv_bregman(img, 0.1)
    img_gray = rgb2gray(dn_img)
    img_lab = rgb2lab(dn_img)

    entropy_img = gaussian(
        img_as_float64(
            dilation(entropy(img_as_ubyte(img_gray), disk(5)), disk(5))))
    edges_img = dilation(
        np.mean(np.array(
            [scharr(img_lab[:, :, channel]) for channel in range(3)]),
                axis=0), disk(3))

    weight = (bal * entropy_img) + ((1 - bal) * edges_img)
    weight /= np.mean(weight)
    weight /= np.amax(weight)

    if args.debug:
        fig, (ax1, ax2, ax3) = plt.subplots(nrows=1,
                                            ncols=3,
                                            figsize=(8, 3),
                                            sharex=True,
                                            sharey=True)
        ax1.imshow(entropy_img)
        ax1.axis('off')

        ax2.imshow(edges_img)
        ax2.axis('off')

        ax3.imshow(weight)
        ax3.axis('off')

        fig.tight_layout()
        plt.show()

    return weight
Ejemplo n.º 19
0
def loco(D, algorithm='muddy', detail=12, cutoff=0.04):
    """
    Estimate local contrast for the given bitmap. Zero indicates a total
    absence of local intensity variation, and one indicates ultimate contrast.
    With other words, this sounds like a job for a traditional edge detector.

    Parameters
    ----------
    D : greyscale image array
        Desaturated difference with the background.
    algorithm : str, optional
        The method to use. Choose between muddy (home grown, see notes below)
        or one of Scharr / Sobel / Prewitt / Roberts (classics from scikits).
    cutoff : float, optional
        Eualization clipping limit, somewhere in the range 0 - 1.
        Note that a value of 0.1 is already quite agressive.
    detail : int or float, optional
        A measure for the evaluation radius, in pixels.
        Set this to the typical line width or edge gradient width.
        Only relevant for the muddy algorithm.

    Notes
    -----
    This is supposed to yield a map that approximates the intensity difference
    between nearby light and dark zones for all points. Points with high local
    contrast are eligible for serving as morph nodes. Our way of working here:

        1. Normalize contrast through adaptive histogram equalization
        2. Apply the selected algorithm. In case of muddy:
            a) Subtract a Gaussian blur
            b) Take the absolute value of this difference.
    """

    G = desaturate(D, cutoff=cutoff)

    algorithm = algorithm.lower().strip()

    if algorithm == 'muddy':
        F = gaussian_filter(G, sigma=detail)
        C = abs(G - F)

    elif algorithm == 'scharr':
        C = scharr(G)
    elif algorithm == 'sobel':
        C = sobel(G)
    elif algorithm == 'prewitt':
        C = prewitt(G)
    elif algorithm == 'roberts':
        C = roberts(G)

    return C
Ejemplo n.º 20
0
def preprocess():
    images = []
    for filename in os.listdir("/home/pi/self-driving/captured/all"):
        if any([filename.endswith('.jpg')]):
            img = imageio.imread(
                os.path.join("/home/pi/self-driving/captured/all", filename))
            if img is not None:
                images.append(img)
    for img in images:
        img = rgb2gray(img)
        img = img[200:480, 0:640]
        edge_scharr = scharr(img)
        imageio.imwrite("/home/pi/self-driving/processed/all/image.jpg",
                        edge_scharr)
Ejemplo n.º 21
0
def create_boundary_map(np_masks):
    adjusted_masks = []
    for i in range(np.shape(np_masks)[0]):
        mask = np.squeeze(np_masks[i, :, :])
        adjusted_mask = scharr(mask)
        adjusted_mask[adjusted_mask > 0] = 1
        adjusted_mask = adjusted_mask.astype(np.uint8)
        kernel = np.ones((3, 3), np.uint8)
        adjusted_mask = cv2.dilate(adjusted_mask, kernel, iterations=1)
        adjusted_mask = np.invert(adjusted_mask)  # * 255
        adjusted_masks.append(adjusted_mask)

    adjusted_masks = np.array(adjusted_masks)
    return adjusted_masks - 254
Ejemplo n.º 22
0
 def get_edges(self, detector='sobel'):
     if detector == 'sobel':
         img = filters.sobel(self.img_gray)
     elif detector == 'canny1':
         img = feature.canny(self.img_gray, sigma=1)
     elif detector == 'canny3':
         img = feature.canny(self.img_gray, sigma=3)
     elif detector == 'scharr':
         img = filters.scharr(self.img_gray)
     elif detector == 'prewitt':
         img = filters.prewitt(self.img_gray)
     elif detector == 'roberts':
         img = filters.roberts(self.img_gray)
     return img
Ejemplo n.º 23
0
def match_ppl_xpl(data, crop_size, work_dir):

    ppl_reference = scharr(data['ppl']['crops'][data['ppl']['ref-index']])
    xpl_ref, xpl_ref_bbox, xpl_ref_shift = get_registered_averaged(
        data['xpl'], crop_size)
    delta_pair = [xpl_ref, crop_to_bounding_box(ppl_reference, xpl_ref_bbox)]
    delta_xp = xpl_ref_shift + compute_displacements_direct(delta_pair, 0)[1]

    ppl_reg_file = os.path.join(work_dir, "ref_dir_ppl.png")
    cvt.cv2_to_file(cvt.simple_grayscale_stretch(ppl_reference), ppl_reg_file)

    xpl_reg_file = os.path.join(work_dir, "ref_avg_xpl.png")
    cvt.cv2_to_file(xpl_ref, xpl_reg_file)

    return delta_xp
Ejemplo n.º 24
0
def get_contour_interior(mask, bold=False):
    if True:  #'camunet' == config['param']['model']:
        # 2-pixel contour (1out+1in), 2-pixel shrinked interior
        outer = binary_dilation(mask)  #, square(9))
        if bold:
            outer = binary_dilation(outer)  #, square(9))
        inner = binary_erosion(mask)  #, square(9))
        contour = ((outer != inner) > 0).astype(np.uint8)
        interior = (erosion(inner) > 0).astype(np.uint8)
    else:
        contour = filters.scharr(mask)
        scharr_threshold = np.amax(abs(contour)) / 2.
        contour = (np.abs(contour) > scharr_threshold).astype(np.uint8)
        interior = (mask - contour > 0).astype(np.uint8)
    return contour, interior
Ejemplo n.º 25
0
def build_gradient(image_data, band_list, gauss_sigma):

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        smooth_im_blue = ndimage.filters.gaussian_filter(
            image_data[band_list[2]], sigma=gauss_sigma)
        grad_image = img_as_ubyte(filters.scharr(smooth_im_blue))

    # Prevent the watersheds from 'leaking' along the sides of the image
    grad_image[:, 0] = grad_image[:, 1]
    grad_image[:, -1] = grad_image[:, -2]
    grad_image[0, :] = grad_image[1, :]
    grad_image[-1, :] = grad_image[-2, :]

    return grad_image
Ejemplo n.º 26
0
 def contour_map(self, image):
     ratio = 0.75
     image = gaussian(image, sigma=1)
     contour = scharr(image)
     contour = (contour - contour.min()) / (contour.max() - contour.min() +
                                            1e-11)
     mid_v = np.sort(contour.reshape(-1))
     value = mid_v[int(ratio * len(mid_v))]
     # value = 0.1
     # print(value)
     contour[contour < value] = 0
     contour[contour >= value] = 1
     # cont = contour * 255
     # cont = Image.fromarray(np.array(cont,np.uint8)).convert('L')
     # cont.save(os.path.join("/root/chujiajia/Results/test/",str(contour.mean())+"img.pgm"))
     return contour
Ejemplo n.º 27
0
def scharr_filter(image):
    new_img = filters.scharr(image)

    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    new_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    fig, ax = plt.subplots(nrows=2)

    ax[0].imshow(cv2.cvtColor(new_img, cv2.COLOR_BGR2RGB))
    ax[0].set_title("Original image")

    ax[1].imshow(cv2.cvtColor(new_img, cv2.COLOR_BGR2RGB))
    ax[1].set_title("Image with Scharr filter")

    plt.tight_layout()
    plt.show()
Ejemplo n.º 28
0
def filter_show(data):
    for k in [12, 3088]:
        img = np.array(data[k]).reshape((28, 28))

        image_scharr = scharr(img)
        image_sobel = sobel(img)
        image_prewitt = prewitt(img)
        image_gabor_real, image_gabor_im = gabor(img, frequency=0.65)
        image_roberts = roberts(img)
        image_roberts_pos = roberts_pos_diag(img)
        image_roberts_neg = roberts_neg_diag(img)
        image_frangi = frangi(img)
        image_laplace = laplace(img)
        image_hessian = hessian(img)
        image_threshold_local_3 = threshold_local(img, 3)
        image_threshold_local_5 = threshold_local(img, 5)
        image_threshold_local_7 = threshold_local(img, 7)
        image_threshold_niblack = threshold_niblack(img, window_size=5, k=0.1)
        image_threshold_sauvola = threshold_sauvola(img)
        image_threshold_triangle = img > threshold_triangle(img)

        fig, axes = plt.subplots(nrows=2,
                                 ncols=2,
                                 sharex=True,
                                 sharey=True,
                                 figsize=(8, 8))
        ax = axes.ravel()

        ax[0].imshow(img, cmap=plt.cm.gray)
        ax[0].set_title('Original image')

        ax[1].imshow(image_threshold_niblack, cmap=plt.cm.gray)
        ax[1].set_title('Niblack')

        ax[2].imshow(image_threshold_sauvola, cmap=plt.cm.gray)
        ax[2].set_title('Sauvola')

        ax[3].imshow(image_threshold_triangle, cmap=plt.cm.gray)
        ax[3].set_title('Triangle')

        for a in ax:
            a.axis('off')

        plt.tight_layout()
        plt.show()
        plt.close()
Ejemplo n.º 29
0
def apply_filter(img, name="equalizer"):
    """
        applying filter to the input image
    :param img:  input image
    :param name: requested filter
    :return:  image after applying the filter
    """
    if name == "equalizer":
        return exposure.equalize_hist(img)
    elif name == "adaptive_equalization":
        return exposure.equalize_adapthist(img)
    elif name == "sobel_op":
        return scharr(img, )
    elif name == "canny_feature":
        return canny(rgb2gray(img), sigma=4)
    else:
        return exposure.equalize_hist(img)
Ejemplo n.º 30
0
    def __getitem__(self, index):
        img = np.array(Image.open(self.data_path[index]))
        lab = np.array(
            Image.open(self.data_path[index].replace("image", "label").replace(
                "Slide", "GT")).convert("L"))
        img, lab = data_geneartor(img, lab, patch_size=self.patch_size)
        marker = erosion(lab, square(3))
        contour = filters.scharr(lab)
        contour = dilation(contour, square(1))
        contour[contour > 0] = 1

        img = img.transpose((2, 0, 1))
        img = torch.from_numpy(img).float()
        lab = torch.from_numpy(np.array([lab])).float()
        marker = torch.from_numpy(np.array([marker])).float()
        contour = torch.from_numpy(np.array([contour])).float()
        return img, lab, marker, contour
Ejemplo n.º 31
0
def all_feature_extractor(imgpath):
    """
        Applies 58 filters and
        returns the dictionary containing
        the features obatined after applying on those images
    """

    image = cv2.imread(imgpath)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Extracting Gabor Features
    feature_dict = gabor_feature_extractor(image)

    feature_dict['Original'] = image

    entropy_img = entropy(image, disk(1))
    feature_dict['Entropy'] = entropy_img

    gaussian3_img = nd.gaussian_filter(image, sigma=3)
    feature_dict['Gaussian3'] = gaussian3_img

    gaussian7_img = nd.gaussian_filter(image, sigma=7)
    feature_dict['Gaussian7'] = gaussian7_img

    sobel_img = sobel(image)
    feature_dict['Sobel'] = sobel_img

    canny_edge_img = cv2.Canny(image, 100, 200)
    feature_dict['Canny'] = canny_edge_img

    robert_edge_img = roberts(image)
    feature_dict['Robert'] = robert_edge_img

    scharr_edge = scharr(image)
    feature_dict['Scharr'] = scharr_edge

    prewitt_edge = prewitt(image)
    feature_dict['Prewitt'] = prewitt_edge

    median_img = nd.median_filter(image, size=3)
    feature_dict['Median'] = median_img

    variance_img = nd.generic_filter(image, np.var, size=3)
    feature_dict['Variance'] = variance_img

    return feature_dict
Ejemplo n.º 32
0
def test4():
    # im = np.array(Image.open("pentagon.png").convert("L"))
    im = np.array(Image.open("/Users/MichaelMason/Desktop/original-small.jpg").convert("L"))
    n, m = im.shape
    edges = scharr(im)
    # edges = gaussian(scharr(im))
    plt.imshow(edges)

    ixs = list(bresenham(0, n//2, m-1,  n//2))
    line = [float(ix) for ix, (j, i) in enumerate(ixs) if edges[i, j] > 0.1]
    # line = [edges[i,j] for ix, (j, i) in enumerate(ixs)]
    cluster_ixs = kdecluster(line)
    print(cluster_ixs)
    for i in cluster_ixs:
        plt.plot(*ixs[int(np.round(i))], marker='x', color='k')

    # plt.plot(*zip(*bresenham(0, n//2, m,  n//2)))
    plt.show()
Ejemplo n.º 33
0
def get_regions(slice_or_arr, fill_holes=False, clear_borders=True, threshold='otsu'):
    """Get the skimage regions of a black & white image."""
    if threshold == 'otsu':
        thresmeth = filters.threshold_otsu
    elif threshold == 'mean':
        thresmeth = np.mean
    edges = filters.scharr(slice_or_arr.astype(np.float))
    edges = filters.gaussian(edges, sigma=1)

    thres = thresmeth(edges)
    bw = edges > thres
    if clear_borders:
        segmentation.clear_border(bw, buffer_size=int(max(bw.shape)/50), in_place=True)
    if fill_holes:
        bw = ndimage.binary_fill_holes(bw)
    labeled_arr, num_roi = measure.label(bw, return_num=True)
    regionprops = measure.regionprops(labeled_arr, edges)
    return labeled_arr, regionprops, num_roi
Ejemplo n.º 34
0
def preprocess(img):
	img = img.convert('L')
	bw_img = imgToGrayAndResize(img, 512)
	scharr_image = arrayThreshold(scharr(bw_img), 0.04) # ocistime obrazok od slabych pixlov, aby sa trebars dal lahsie spoznat horizont
	#canny_image = feature.canny(np.array(bw_img))
	hog = feature.hog(scharr_image, orientations=16, pixels_per_cell=(32, 32), cells_per_block=(1, 1), normalise = True, visualise=False)	
	
	image_blocks = vectorBlockshaped(np.array(bw_img), 128, 128)
	blocks_color_histogram = []
	counter = 0
	for block in image_blocks:
		hist, bin_edges = np.histogram(block, bins=range(0,256))
		blocks_color_histogram.append(hist)

	blocks_color_histogram = np.array(blocks_color_histogram).ravel()
		
	img_face_rotation_data, guessed_rotation_by_faces = np.array(getFaceCountByRotation(img))

		
	return hog, blocks_color_histogram, img_face_rotation_data, guessed_rotation_by_faces
indistinguishable.

.. [1] https://en.wikipedia.org/wiki/Sobel_operator#Alternative_operators

.. [2] B. Jaehne, H. Scharr, and S. Koerkel. Principles of filter design. In
       Handbook of Computer Vision and Applications. Academic Press, 1999.

.. [3] https://en.wikipedia.org/wiki/Prewitt_operator
"""

x, y = np.ogrid[:100, :100]
# Rotation-invariant image with different spatial frequencies
img = np.exp(1j * np.hypot(x, y)**1.3 / 20.).real

edge_sobel = sobel(img)
edge_scharr = scharr(img)
edge_prewitt = prewitt(img)

diff_scharr_prewitt = edge_scharr - edge_prewitt
diff_scharr_sobel = edge_scharr - edge_sobel
max_diff = np.max(np.maximum(diff_scharr_prewitt, diff_scharr_sobel))

fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'})

ax0.imshow(img, cmap=plt.cm.gray)
ax0.set_title('Original image')
ax0.axis('off')

ax1.imshow(edge_scharr, cmap=plt.cm.gray)
ax1.set_title('Scharr Edge Detection')
ax1.axis('off')
Ejemplo n.º 36
0
def clusterDetector(imgstr, filetype, clustercolor):

	def diameter(num_px):
		if filetype=='tiff':
			R = 0.16415411
		elif filetype=='jpeg':
			R = 24.95816
		Apx = (50/R)**2 # area of single pixel, about 4 um^2 / sq px
		A = Apx*num_px
		return ((A/pi)**(0.5))*2

	buf = cStringIO.StringIO(imgstr) 
	
	if clustercolor=='dark':
		img = 255-imread(buf, as_grey=True)
	elif clustercolor=='light':
		img = imread(buf, as_grey=True)

	print "Image read by worker, starting image processing"
	med_img = filters.median(img, disk(3))


	scharr_edges = filters.scharr(med_img)
	edges = scharr_edges > filters.threshold_otsu(scharr_edges)

	foreground = med_img > 50

	watershed_image = closing(closing(foreground, disk(2)) ^ (edges), disk(2))

	distance = ndimage.distance_transform_edt(watershed_image)
	filtered_distance = ndimage.gaussian_filter(distance, 3)

	local_maxi = peak_local_max(filtered_distance, indices=False,
	                             min_distance=20, exclude_border=False)
	markers = dilation(ndimage.label(local_maxi)[0], disk(10))
	labels = watershed(-distance, markers, mask=erosion(watershed_image, disk(2)))

	j = 1
	cleared_labels = zeros_like(labels)
	for i in range(1, labels.max()+1):
		cleared = clear_border(labels==i).astype(int)
		if cleared.max()>0:
			cleared_labels += cleared * j
			j += 1

	props = regionprops(cleared_labels)

	offset = 0

	output = []
	for i, prop in enumerate(props):

		box = prop.bbox
	
		fig = Figure()
		canvas = FigureCanvasAgg(fig)
		ax = fig.add_subplot(111)
		ax.imshow(img[box[0]-offset:box[2]+offset, box[1]-offset:box[3]+offset], interpolation='nearest', cmap='Greys')
		ax.imshow(prop.image, cmap="Reds", alpha=0.3)
		ax.axis('off')
		buf = cStringIO.StringIO()
		fig.savefig(buf,format='png')

		data = buf.getvalue().encode("base64").strip()

		diam = diameter(prop.image.sum())

		output.append({'data':data,'diameter':diam})
	print "job finished"
	return output
Ejemplo n.º 37
0
def test_scharr_zeros():
    """Scharr on an array of all zeros."""
    result = filters.scharr(np.zeros((10, 10)), np.ones((10, 10), bool))
    assert (np.all(result < 1e-16))
Ejemplo n.º 38
0
t2 = 160
t3 = 200
t4 = 200

s_Q = ( 2*imgQ + dstQ + t4 )  / ( imgQ**2 + dstQ**2 + t4 )

s_I = ( 2*imgI + dstI + t3 )  / ( imgI**2 + dstI**2 + t3 )

pc1 = phasepack.phasecong(imgY, nscale = 4, norient = 4, minWaveLength = 6, mult = 2, sigmaOnf=0.55)
pc2 = phasepack.phasecong(dstY, nscale = 4, norient = 4, minWaveLength = 6, mult = 2, sigmaOnf=0.55)
pc1 = pc1[0]
pc2 = pc2[0]

s_PC = ( 2*pc1 + pc2 + t1 )  / ( pc1**2 + pc2**2 + t1 )

g1 = scharr( imgY )
g2 = scharr( dstY )
s_G = ( 2*g1 + g2 + t2 )  / ( g1**2 + g2**2 + t2 )

s_L = s_PC * s_G
s_C = s_I * s_Q

pcM = np.maximum(pc1,pc2)


fsim = round( np.nansum( s_L * pcM) / np.nansum(pcM), 3)

fsimc = round( np.nansum( s_L * s_C**0.3 * pcM) / np.nansum(pcM), 3)

print 'FSIM: ' + str(fsim)
print 'FSIMC: ' + str(fsimc)
Ejemplo n.º 39
0
edge_sobel = sobel(img_name_gs)

fig, (ax0, ax1) = plt.subplots(ncols=2, sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'})

ax0.imshow(edge_roberts, cmap=plt.cm.gray)
ax0.set_title('Roberts Edge Detection')
ax0.axis('off')

ax1.imshow(edge_sobel, cmap=plt.cm.gray)
ax1.set_title('Sobel Edge Detection')
ax1.axis('off')
plt.tight_layout()


edge_sobel = sobel(img_name_gs)
edge_scharr = scharr(img_name_gs)
edge_prewitt = prewitt(img_name_gs)

diff_scharr_prewitt = edge_scharr - edge_prewitt
diff_scharr_sobel = edge_scharr - edge_sobel
max_diff = np.max(np.maximum(diff_scharr_prewitt, diff_scharr_sobel))

fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'})

ax0.imshow(img_name_gs, cmap=plt.cm.gray)
ax0.set_title('Original image')
ax0.axis('off')

ax1.imshow(edge_scharr, cmap=plt.cm.gray)
ax1.set_title('Scharr Edge Detection')
ax1.axis('off')
Ejemplo n.º 40
0
def getEdge(img,mode=0):
    return {0: sobel(img),
        1:scharr(img),
        2:prewitt(img),
        3:roberts(img)}.get(mode, sobel(img))
Ejemplo n.º 41
0
plt.imshow(gauss2)

lap=laplace(gray0,ksize=100)
plt.imshow(lap)

pre=prewitt(gray0, mask=None)
plt.imshow(pre)

pre_v=prewitt_v(gray0, mask=None)
plt.imshow(pre_v)

from skimage import filters
edges2 = filters.roberts(gray0)
plt.imshow(edges2)

plt.imshow(scharr(gray0))
plt.imshow(threshold_mean(gray0))
plt.imshow(wiener(gray0))

#######################################
plt.imshow(img)
plt.imshow(gray0)
plt.imshow(image)
### TREES
plt.imshow(segmentation)
### CONTOURS
plt.imshow(img_back, cmap = 'gray')
### STREET
plt.imshow(gy)
plt.imshow(angle)
plt.imshow(binary_adaptive)
Ejemplo n.º 42
0
def process(filename):
    #
    # Conventions:
    # a_i, b_i
    #    are variables in image space, units are pixels
    # a_w, b_w
    #    are variables in world space, units are meters
    #
    print '\n========================================'
    print '  File: ' + filename
    print '========================================\n'

    im = imread(filename)
    im = rgb2gray(im)
    im = (im * 255.).astype(np.uint8)

    tag_mosaic = TagMosaic(0.0254)
    detections = AprilTagDetector().detect(im)
    print '  %d tags detected.' % len(detections)

    #
    # Sort detections by distance to center
    #
    c_i = np.array([im.shape[1], im.shape[0]]) / 2.
    dist = lambda p_i: np.linalg.norm(p_i - c_i)
    closer_to_center = lambda d1, d2: int(dist(d1.c) - dist(d2.c))
    detections.sort(cmp=closer_to_center)

    mosaic_pos = lambda det: tag_mosaic.get_position_meters(det.id)

    det_i = np.array([ d.c for d in detections ])
    det_w = np.array([ mosaic_pos(d) for d in detections ])

    #
    # To learn a weighted local homography, we find the weighting
    # function parameters that minimize reprojection error across
    # leave-one-out validation folds of the data. Since the
    # homography is local at the center, we only use 9 nearest
    # detections to the center
    #
    det_i9 = det_i[:9]
    det_w9 = det_w[:9]

    def local_homography_loocv_error(theta, args):
        src, tgt = args
        errs = [ local_homography_error(theta, src[t_ix], tgt[t_ix], src[v_ix], tgt[v_ix])
                    for t_ix, v_ix in LeaveOneOut(len(src)) ]
        return np.mean(errs)

    def learn_homography_i2w():
        result = minimize( local_homography_loocv_error,
                    x0=[ 50, 1, 1e-3 ],
                    args=[ det_i9, det_w9 ],
                    method='Powell',
                    options={'ftol': 1e-3} )

        print '\nHomography: i->w'
        print '------------------'
        print '  params:', result.x
        print '    rmse: %.6f' % sqrt(result.fun)
        print '\n  Optimization detail:'
        print '  ' + str(result).replace('\n', '\n      ')

        H = create_local_homography_object(*result.x)
        for i, w in zip(det_i9, det_w9):
            H.add_correspondence(i, w)

        return H

    def learn_homography_w2i():
        result = minimize( local_homography_loocv_error,
                    x0=[ 0.0254, 1, 1e-3 ],
                    method='Powell',
                    args=[ det_w9, det_i9 ],
                    options={'ftol': 1e-3} )

        print '\nHomography: w->i'
        print '------------------'
        print '  params:', result.x
        print '    rmse: %.6f' % sqrt(result.fun)
        print '\n  Optimization detail:'
        print '  ' + str(result).replace('\n', '\n      ')

        H = create_local_homography_object(*result.x)
        for w, i in zip(det_w9, det_i9):
            H.add_correspondence(w, i)

        return H

    #
    # We assume that the distortion is zero at the center of
    # the image and we are interesting in the word to image
    # homography at the center of the image. However, we don't
    # know the center of the image in world coordinates.
    # So we follow a procedure as explained below:
    #
    # First, we learn a homography from image to world
    # Next, we find where the image center `c_i` maps to in
    # world coordinates (`c_w`). Finally, we find the local
    # homography `LH0` from world to image at `c_w`
    #
    H_iw = learn_homography_i2w()
    c_i = np.array([im.shape[1], im.shape[0]]) / 2.
    c_w = H_iw.map(c_i)[:2]
    H_wi = learn_homography_w2i()
    LH0 = H_wi.get_homography_at(c_w)

    print '\nHomography at center'
    print '----------------------'
    print '      c_w =', c_w
    print '      c_i =', c_i
    print 'LH0 * c_w =', H_wi.map(c_w)

    #
    # Obtain distortion estimate
    #       detected + undistortion = mapped
    #  (or) undistortion = mapped - detected
    #
    def homogeneous_coords(arr):
        return np.hstack([ arr, np.ones((len(arr), 1)) ])

    mapped_i = LH0.dot(homogeneous_coords(det_w).T).T
    mapped_i = np.array([ p / p[2] for p in mapped_i ])
    mapped_i = mapped_i[:,:2]

    undistortion = mapped_i - det_i # image + undistortion = mapped
    max_distortion = np.max([np.linalg.norm(u) for u in undistortion])
    print '\nMaximum distortion is %.2f pixels' % max_distortion

    #
    # Fit non-parametric model to the observations
    #
    model = GPModel(det_i, undistortion)

    print '\nGP Hyper-parameters'
    print '---------------------'
    print '  x: ', model._gp_x._covf.theta
    print '        log-likelihood: %.4f' % model._gp_x.model_evidence()
    print '  y: ', model._gp_y._covf.theta
    print '        log-likelihood: %.4f' % model._gp_y.model_evidence()
    print ''
    print '  Optimization detail:'
    print '  [ x ]'
    print '  ' + str(model._gp_x.fit_result).replace('\n', '\n      ')
    print '  [ y ]'
    print '  ' + str(model._gp_y.fit_result).replace('\n', '\n      ')

    #
    # Visualization
    #
    from skimage.filters import scharr
    from matplotlib import pyplot as plt

    plt.style.use('ggplot')
    plt.figure(figsize=(16,10))

    #__1__
    plt.subplot(221)
    plt.title(filename.split('/')[-1])
    plt.imshow(im, cmap='gray')
    plt.plot(det_i[:,0], det_i[:,1], 'o')
    for i, d in enumerate(detections):
        plt.text(d.c[0], d.c[1], str(i),
            fontsize=8, color='white', bbox=dict(facecolor='maroon', alpha=0.75))
    plt.grid()
    plt.axis('equal')

    #__2__
    plt.subplot(222)
    plt.title('Non-linear Homography')

    if False:
        plt.imshow(1.-scharr(im), cmap='bone', interpolation='gaussian')

        from collections import defaultdict
        row_groups = defaultdict(list)
        col_groups = defaultdict(list)

        for d in detections:
            row, col = tag_mosaic.get_row(d.id), tag_mosaic.get_col(d.id)
            row_groups[row] += [ d.id ]
            col_groups[col] += [ d.id ]

        for k, v in row_groups.iteritems():
            a = tag_mosaic.get_position_meters(np.min(v))
            b = tag_mosaic.get_position_meters(np.max(v))
            x_coords = np.linspace(a[0], b[0], 100)
            points = np.array([ H_wi.map([x, a[1]]) for x in x_coords ])
            plt.plot(points[:,0], points[:,1], '-',color='#CF4457', linewidth=2)

        for k, v in col_groups.iteritems():
            a = tag_mosaic.get_position_meters(np.min(v))
            b = tag_mosaic.get_position_meters(np.max(v))
            y_coords = np.linspace(a[1], b[1], 100)
            points = np.array([ H_wi.map([a[0], y]) for y in y_coords ])
            plt.plot(points[:,0], points[:,1], '-',color='#CF4457', linewidth=2)

        plt.plot(det_i[:,0], det_i[:,1], 'kx')
        plt.grid()
        plt.axis('equal')

    #__3__
    plt.subplot(223)
    plt.title('Qualitative Estimates')

    for i, d in enumerate(detections):
        plt.text(d.c[0], d.c[1], str(i), fontsize=8, color='#999999')

    X, Y = det_i[:,0], det_i[:,1]
    U, V = undistortion[:,0], undistortion[:,1]
    plt.quiver(X, Y, U, -V, units='dots')
    # plt.quiver(X, Y, U, -V, angles='xy', scale_units='xy', scale=1) # plot exact
    plt.text( 0.5, 0, 'max observed distortion: %.2f px' % max_distortion, color='#CF4457', fontsize=10,
        horizontalalignment='center', verticalalignment='bottom', transform=plt.gca().transAxes)
    plt.gca().invert_yaxis()
    plt.axis('equal')

    #__4__
    plt.subplot(224)
    plt.title('Qualitative Undistortion')
    H, W = im.shape
    grid = np.array([[x, y] for y in xrange(0, H, 80) for x in xrange(0, W, 80)])
    predicted = model.predict(grid)
    X, Y = grid[:,0], grid[:,1]
    U, V = predicted[:,0], predicted[:,1]
    plt.quiver(X, Y, U, -V, units='dots')
    #plt.quiver(X, Y, U, -V, angles='xy', scale_units='xy', scale=1)) # plot exact
    plt.gca().invert_yaxis()
    plt.axis('equal')

    plt.tight_layout()
    plt.gcf().patch.set_facecolor('#dddddd')
    #plt.show()
    plt.savefig(filename + '.svg', bbox_inches='tight')
Ejemplo n.º 43
0
def frangi_segmentation(image, 
                        colors,
                        frangi_args, 
                        threshold_args,
                        separate_objects=True, 
                        contrast_kernel_size='skip',
                        color_args_1='skip',
                        color_args_2='skip', 
                        color_args_3='skip', 
                        neighborhood_args='skip',
                        morphology_args_1='skip', 
                        morphology_args_2='skip', 
                        hollow_args='skip', 
                        fill_gaps_args='skip', 
                        diameter_args='skip', 
                        diameter_bins='skip', 
                        image_name='image', 
                        verbose=False):
    """
    Possible approach to object detection using frangi filters. Selects colorbands for
    analysis, runs frangi filter, thresholds to identify candidate objects, then removes
    spurrious objects by color and morphology characteristics. See frangi_approach.ipynb. 
    
    Unless noted, the dictionaries are called by their respective functions in order.
    
    Parameters
    ----------
    image : ndarray
        RGB image to analyze
    colors : dict or str
        Parameters for picking the colorspace. See `pyroots.band_selector`. 
    frangi_args : list of dict or dict
        Parameters to pass to `skimage.filters.frangi`
    threshold_args : list of dict or dict
        Parameters to pass to `skimage.filters.threshold_adaptive`
    contrast_kernel_size : int, str, or None
        Kernel size for `skimage.exposure.equalize_adapthist`. If `int`, then gives the size of the kernel used
        for adaptive contrast enhancement. If `None`, uses default (1/8 shortest image dimension). If `skip`,
        then skips. 
    color_args_1 : dict
        Parameters to pass to `pyroots.color_filter`.
    color_args_2 : dict
        Parameters to pass to `pyroots.color_filter`. Combines with color_args_1
        in an 'and' statement.
    color_args_3 : dict
        Parameters to pass to `pyroots.color_filter`. Combines with color_args_1, 2
        in an 'and' statement.
    neighborhood_args : dict
        Parameters to pass to 'pyroots.neighborhood_filter'. 
    morphology_args_1 : dict
        Parameters to pass to `pyroots.morphology_filter`    
    morphology_args_2 : dict
        Parameters to pass to `pyroots.morphology_filter`. Happens after fill_gaps_args in the algorithm.
    hollow_args : dict
        Parameters to pass to `pyroots.hollow_filter`
    fill_gaps_args : dict
        Paramaters to pass to `pyroots.fill_gaps`
    diameter_bins : list
        To pass to `pyroots.bin_by_diameter`
    image_name : str
        Identifier of image for summarizing
    
    Returns
    -------
    A dictionary containing:
        1. `"geometry"` summary `pandas.DataFrame`
        2. `"objects"` binary image
        3. `"length"` medial axis image
        4. `"diameter"` medial axis image
 
    """

    # Pull band from colorspace
    working_image = band_selector(image, colors)  # expects dictionary (lazy coding)
    nbands = len(working_image)
    if verbose is True:
        print("Color bands selected")
    
    ## Count nubmer of dictionaries in threshold_args and frangi_args. Should equal number of bands. Convert to list if necessary
    try:
        len(threshold_args[0])
    except:
        threshold_args = [threshold_args]
        if nbands != len(threshold_args):
            raise ValueError(
                """Number of dictionaries in `threshold_args` doesn't
                equal the number of bands in `colors['band']`!"""
            )
        pass 
    
    try:
        len(frangi_args[0])
    except:
        frangi_args = [frangi_args]
        if nbands != len(frangi_args):
            raise ValueError(
                """Number of dictionaries in `frangi_args` doesn't 
                equal the number of bands in `colors['band']`!"""
            )
        pass    
    
    working_image = [img_as_float(i) for i in working_image]
    
    # Contrast enhancement
    try:
        for i in range(nbands):
            temp = exposure.equalize_adapthist(working_image[i], 
                                               kernel_size = contrast_kernel_size)
            working_image[i] = img_as_float(temp)
        if verbose:
            print("Contrast enhanced")
    except:
        if contrast_kernel_size is not 'skip':
            warn('Skipping contrast enhancement')
        pass
        
    # invert if necessary
    for i in range(nbands):
        if not colors['dark_on_light'][i]:
            working_image[i] = 1 - working_image[i]
    
    # Identify smoothing sigma for edges and frangi thresholding
    # simultaneously detect edges (computationally cheaper than multiple frangi enhancements)
    edges = [np.ones_like(working_image[0]) == 1] * nbands    # all True
    sigma_val = [0.125] * nbands  # step is 0, 0.25, 0.5, 1, 2, 4, 8, 16
    for i in range(nbands):
        edge_val = 1
        while edge_val > 0.1 and sigma_val[i] < 10:
            sigma_val[i] = 2*sigma_val[i]
            temp = filters.gaussian(working_image[i], sigma=sigma_val[i])
            temp = filters.scharr(temp)
            temp = temp > filters.threshold_otsu(temp)
            edge_val = np.sum(temp) / np.sum(np.ones_like(temp))

            edges_temp = temp.copy()

        if sigma_val[i] == 0.25: # try without smoothing
            temp = filters.scharr(working_image[i])
            temp = temp > filters.threshold_otsu(temp)
            edge_val = np.sum(temp) / np.sum(np.ones_like(temp))
            if edge_val <= 0.1:
                sigma_val[i] = 0
                edges_temp = temp.copy()
            
        if separate_objects:
            edges[i] = morphology.skeletonize(edges_temp)
    
    if verbose:
        print("Sigma value: {}".format(sigma_val))
        if separate_objects:
            print("Edges found")
    
    # Frangi vessel enhancement
    for i in range(nbands):
        temp = filters.gaussian(working_image[i], sigma=sigma_val[i])
        temp = filters.frangi(temp, **frangi_args[i])
        temp = 1 - temp/np.max(temp)
        temp = temp < filters.threshold_local(temp, **threshold_args[i])
        working_image[i] = temp.copy()
    
    frangi = working_image.copy()
    if verbose:
        print("Frangi filter, threshold complete")
    
    
    # Combine bands, separate objects
    combined = working_image[0] * ~edges[0]
    for i in range(1, nbands):
        combined = combined * working_image[i] * ~edges[i]
    working_image = combined.copy()
    
    # Filter candidate objects by color
    try:
        color1 = color_filter(image, working_image, **color_args_1)  #colorspace, target_band, low, high, percent)
        if verbose:
            print("Color filter 1 complete")
    except:
        if color_args_1 is not 'skip':
            warn("Skipping Color Filter 1")
        color1 = np.ones(working_image.shape)  # no filtering      

    try:
        color2 = color_filter(image, working_image, **color_args_2)  # nesting equates to an "and" statement.
        if verbose:
            print("Color filter 2 complete")   
    except:
        if color_args_2 is not 'skip':
            warn("Skipping Color Filter 2")
        color2 = np.ones(working_image.shape)  # no filtering
    
    try:
        color3 = color_filter(image, working_image, **color_args_3)  # nesting equates to an "and" statement.
        if verbose:
            print("Color filter 3 complete")
    except:
        if color_args_3 is not 'skip':
            warn("Skipping Color Filter 3")
        color3 = np.ones(working_image.shape)  # no filtering
    
    # Combine bands
    working_image = color1 * color2 * color3
    del color1
    del color2
    del color3
    
    # Re-expand to area
    if separate_objects:
    
        # find edges removed
        temp = [frangi[i] * edges[i] for i in range(nbands)]
        rm_edges = temp[0].copy()
        for i in range(1, nbands):
            rm_edges = rm_edges * temp[i]
        
        # filter by color per criteria above
        try:    color1 = color_filter(image, rm_edges, **color_args_1)
        except: color1 = np.ones(rm_edges.shape)
        try:    color2 = color_filter(image, rm_edges, **color_args_2)
        except: color2 = np.ones(rm_edges.shape)
        try:    color3 = color_filter(image, rm_edges, **color_args_3)
        except: color3 = np.ones(rm_edges.shape)
        
        # Combine color filters
        expanded = color1 * color2 * color3
    else:
        expanded = np.zeros(colorfilt.shape) == 1  # evaluate to false
    
    
    working_image = expanded ^ working_image  # bitwise or
    
    try:    # remove little objects (for computational efficiency)
        working_image = morphology.remove_small_objects(
            working_image, 
            min_size=morphology_args_1['min_size']
        )
    except:
        pass
    if verbose:
        print("Edges re-added")

    # Filter candidate objects by morphology
    try:
        working_image = morphology_filter(working_image, **morphology_args_1)
        if verbose:
            print("Morphology filter 1 complete")
    except:
        if morphology_args_1 is not 'skip':
            warn("Skipping morphology filter 1")
        pass        
    
    # Filter objects by neighborhood colors
    try:
        working_image = neighborhood_filter(image, working_image, **neighborhood_args)
        if verbose:
            print("Neighborhood filter complete")
    except:
        if neighborhood_args is not 'skip':
            warn("Skipping neighborhood filter")
        pass
    
    # Filter candidate objects by hollowness
    if hollow_args is not 'skip':  
        temp = morphology.remove_small_holes(working_image, min_size=10)
        try:
            if np.sum(temp) > 0:
                working_image = hollow_filter(temp, **hollow_args)
            if verbose:
                print("Hollow filter complete")
        except:
            warn("Skipping hollow filter")
            pass
    
    # Close small gaps and holes in accepted objects
    try:
        working_image = fill_gaps(working_image, **fill_gaps_args)
        if verbose:
            print("Gap filling complete")
    except:
        if fill_gaps_args is not 'skip':
            warn("Skipping filling gaps")
        pass
    
    # Filter candidate objects by morphology
    try:
        working_image = morphology_filter(working_image, **morphology_args_2)
        if verbose:
            print("Morphology filter 2 complete")
    except:
        if morphology_args_2 is not 'skip':
            warn("Skipping morphology filter 2")
        pass
        
    # Skeletonize. Now working with a dictionary of objects.
    skel = skeleton_with_distance(working_image)
    if verbose:
        print("Skeletonization complete")
    
    # Diameter filter
    try:
        diam = diameter_filter(skel, **diameter_args)
        if verbose:
            print("Diameter filter complete")
    except:
        diam = skel.copy()
        if diameter_args is not 'skip':
            warn("Skipping diameter filter")
        pass
    
    # Summarize
    if diameter_bins is None or diameter_bins is 'skip':
        summary_df = summarize_geometry(diam['geometry'], image_name)

    else:
        diam_out, summary_df = bin_by_diameter(diam['length'],
                                               diam['diameter'],
                                               diameter_bins,
                                               image_name)
        diam['diameter'] = diam_out
    
    out = {'geometry' : summary_df,
           'objects'  : diam['objects'],
           'length'   : diam['length'],
           'diameter' : diam['diameter']}

    if verbose is True:
        print("Done")

    return(out)
Ejemplo n.º 44
0
def ScharrMetric(self, field, unused_opts):
  # Scharr uses [3, 10, 3; 0, 0, 0; -3, -10, -3]
  return filters.scharr(numpy.abs(field))
Ejemplo n.º 45
0
def register_bands(image, template_band=1, ECC_criterion=True):
    """
    Fix chromatic abberation in images by calculating and applying an affine
    transformation. Chromatic abberation is a result of uneven refraction of light
    of different wavelengths. It shows up as systematic blue and red edges in
    high-contrast areas.

    This should be done before other processing to minimize artifacts.

    Parameters
    ----------
    image : ndarray
        3- or 4-channel image, probably RGB.
    template : int
        Band to which to align the other bands. Usually G in RGB.
    ECC_criterion : bool
        Use ECC criterion to find optimal warp? Improves results, but increases
        processing time 5x.

    Returns
    -------
    An ndarray of `image.size`

    Notes
    -----
    Uses `skimage.filters.scharr` to find edges in each band, then finds and
    applies an affine transformation to register the images using
    `cv2.estimateRigidTransform` and `cv2.warpAffine`. If `ECC_criterion=True`,
    the matrix from `estimateRigidTransform` is updated using `cv2.findTransformECC`.
    """

    #find dimensions
    height, width, depth = image.shape

    #define bands to analyze
    analyze = []
    for i in range(depth):
        if i != template_band:
            analyze.append(i)
            
    # Extract bands, find edges
    bands = img_split(image)
    edges = [np.ones_like(i) for i in bands]
    for i in range(len(bands)):
        sigma_val = 0.25
        edge_val = 1
        while edge_val > 0.1 and sigma_val < 10:
            temp = filters.gaussian(bands[i], sigma=sigma_val)
            scharr = filters.scharr(temp)
            temp = scharr > filters.threshold_otsu(scharr)
            edge_val = np.sum(temp) / np.sum(np.ones_like(temp))
            sigma_val = 2*sigma_val
        edges[i] = img_as_ubyte(scharr * temp)

    #make output image
    out = np.zeros((height, width, depth), dtype=np.uint8)
    out[:, :, template_band] = bands[template_band]

    try:
        for i in analyze:
            # Estimate transformation
            warp_matrix = np.array(cv2.estimateRigidTransform(edges[template_band],
                                                     edges[i],
                                                     fullAffine=False), dtype=np.float32)

            if ECC_criterion == True:
                # Optimize using ECC criterion and default settings
                warp_matrix = cv2.findTransformECC(edges[template_band],
                                                   edges[i],
                                                   warpMatrix=warp_matrix)[1]
            # transform
            aligned = cv2.warpAffine(bands[i],
                                     warp_matrix,
                                     (width, height),
                                     flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP,  # otherwise the transformation goes the wrong way
                                     borderMode=cv2.BORDER_CONSTANT)

            # add to color image
            out[:, :, i] = aligned
    
    except:
        # Probably few objects, so no smoothing and no thresholding to have as much info as possible
        edges = [img_as_ubyte(filters.scharr(i)) for i in edges]
        
        for i in analyze:
            # Estimate transformation
            warp_matrix = np.array(cv2.estimateRigidTransform(edges[template_band],
                                                     edges[i],
                                                     fullAffine=False), dtype=np.float32)

            if ECC_criterion == True:
                # Optimize using ECC criterion and default settings
                warp_matrix = cv2.findTransformECC(edges[template_band],
                                                   edges[i],
                                                   warpMatrix=warp_matrix)[1]
            # transform
            aligned = cv2.warpAffine(bands[i],
                                     warp_matrix,
                                     (width, height),
                                     flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP,  # otherwise the transformation goes the wrong way
                                     borderMode=cv2.BORDER_CONSTANT)

            # add to color image
            out[:, :, i] = aligned
            
    return(img_as_ubyte(out))
Ejemplo n.º 46
0
 def _scharr(self, img):
     # Invert the image to ease edge detection.
     img = 1. - img
     grey = skc.rgb2grey(img)
     return skf.scharr(grey)
Ejemplo n.º 47
0
 def _compute_gradient_image(self):
     from skimage import filters
     self.edges = filters.scharr(self._image)
Ejemplo n.º 48
0
def test_scharr_mask():
    """Scharr on a masked array should be zero."""
    np.random.seed(0)
    result = filters.scharr(np.random.uniform(size=(10, 10)),
                            np.zeros((10, 10), bool))
    assert_allclose(result, 0)