def main():
    """Load image, apply filter, plot."""
    img = data.camera()

    # just like sobel, but no -2/+2 in the middle
    prewitt_y = np.array([
        [-1, -1, -1],
        [0, 0, 0],
        [1, 1, 1]
    ])
    prewitt_x = np.rot90(prewitt_y) # rotates counter-clockwise

    img_sx = signal.correlate(img, prewitt_x, mode="same")
    img_sy = signal.correlate(img, prewitt_y, mode="same")
    g_magnitude = np.sqrt(img_sx**2 + img_sy**2)

    ground_truth = skifilters.prewitt(data.camera())

    util.plot_images_grayscale(
        [img, img_sx, img_sy, g_magnitude, ground_truth],
        ["Image", "Prewitt (x)", "Prewitt (y)", "Prewitt (both/magnitude)",
         "Prewitt (Ground Truth)"]
    )
def main():
    """Load image, apply sobel (to get x/y gradients), plot the results."""
    img = data.camera()

    sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
    sobel_x = np.rot90(sobel_y)  # rotates counter-clockwise

    # apply x/y sobel filter to get x/y gradients
    img_sx = signal.correlate(img, sobel_x, mode="same")
    img_sy = signal.correlate(img, sobel_y, mode="same")

    # combine x/y gradients to gradient magnitude
    # scikit-image's implementation divides by sqrt(2), not sure why
    img_s = np.sqrt(img_sx ** 2 + img_sy ** 2) / np.sqrt(2)

    # create binarized image
    threshold = np.average(img_s)
    img_s_bin = np.zeros(img_s.shape)
    img_s_bin[img_s > threshold] = 1

    # generate ground truth (scikit-image method)
    ground_truth = skifilters.sobel(data.camera())

    # plot
    util.plot_images_grayscale(
        [img, img_sx, img_sy, img_s, img_s_bin, ground_truth],
        [
            "Image",
            "Sobel (x)",
            "Sobel (y)",
            "Sobel (magnitude)",
            "Sobel (magnitude, binarized)",
            "Sobel (Ground Truth)",
        ],
    )
def main():
    """Load image, apply sobel (to get x/y gradients), plot the results."""
    img = data.camera()

    sobel_y = np.array([
        [-1, -2, -1],
        [0, 0, 0],
        [1, 2, 1]
    ])
    sobel_x = np.rot90(sobel_y) # rotates counter-clockwise

    # apply x/y sobel filter to get x/y gradients
    img_sx = signal.correlate(img, sobel_x, mode="same")
    img_sy = signal.correlate(img, sobel_y, mode="same")

    # combine x/y gradients to gradient magnitude
    # scikit-image's implementation divides by sqrt(2), not sure why
    img_s = np.sqrt(img_sx**2 + img_sy**2) / np.sqrt(2)

    # create binarized image
    threshold = np.average(img_s)
    img_s_bin = np.zeros(img_s.shape)
    img_s_bin[img_s > threshold] = 1

    # generate ground truth (scikit-image method)
    ground_truth = skifilters.sobel(data.camera())

    # plot
    util.plot_images_grayscale(
        [img, img_sx, img_sy, img_s, img_s_bin, ground_truth],
        ["Image", "Sobel (x)", "Sobel (y)", "Sobel (magnitude)",
         "Sobel (magnitude, binarized)", "Sobel (Ground Truth)"]
    )
Beispiel #4
0
def main():
    """Load template image (needle) and the large example image (haystack),
    generate matching score per pixel, plot the results."""
    img_haystack = skiutil.img_as_float(data.camera()) # the image in which to search
    img_needle = img_haystack[140:190, 220:270] # the template to search for
    img_sad = np.zeros(img_haystack.shape) # score image

    height_h, width_h = img_haystack.shape
    height_n, width_n = img_needle.shape

    # calculate score for each pixel
    # stop iterating over pixels when the whole template cannot any more (i.e. stop
    # at bottom and right border)
    for y in range(height_h - height_n):
        for x in range(width_h - width_n):
            patch = img_haystack[y:y+height_n, x:x+width_n]
            img_sad[y, x] = sad(img_needle, patch)
    img_sad = img_sad / np.max(img_sad)

    # add highest score to bottom and right borders
    img_sad[height_h-height_n:, :] = np.max(img_sad[0:height_h, 0:width_h])
    img_sad[:, width_h-width_n:] = np.max(img_sad[0:height_h, 0:width_h])

    # plot results
    util.plot_images_grayscale(
        [img_haystack, img_needle, img_sad],
        ["Image", "Image (Search Template)", "Matching (darkest = best match)"]
    )
def main():
    """Load image, apply filters and plot the results."""
    img = data.moon()
    util.plot_images_grayscale(
        [img, median(img), morphological_edge(img),
         erosion(img), dilation(img),
         opening(img), closing(img)],
        ["Image", "Median", "Morphological Edge", "Erosion", "Dilation", "Opening", "Closing"]
    )
def main():
    """Load image, calculate harris scores (window functions: matrix of ones, gauss)
    and plot the results."""
    img = data.checkerboard()
    score_window = harris_ones(img, 7)
    score_gauss = harris_gauss(img)
    util.plot_images_grayscale(
        [img, score_window, score_gauss, feature.corner_harris(img)],
        ["Image", "Harris-Score (ones)", "Harris-Score (gauss)", "Harris-Score (ground truth)"]
    )
def main():
    """Load image, calculate optimal threshold, binarize, plot."""
    # load image
    img = data.coins()
    height, width = img.shape
    nb_pixels = height * width

    # precalculate some values for speedup
    # average pixel value
    g_avg = np.average(img)
    # P(pixel-value), i.e. #pixels-with-value / #all-pixels
    p_g = [0] * 256
    for g in range(0, 256):
        p_g[g] = np.sum(img == g) / nb_pixels

    # Otsu method
    # calculations are based on standard formulas
    q_best = None
    threshold_best = None
    img_bin_best = None
    # iterate over all possible thresholds
    for t in range(1, 255):
        img_bin = np.zeros(img.shape)
        img_bin[img >= t] = 1

        p1 = np.sum(img_bin) / nb_pixels
        p0 = 1 - p1

        g0 = np.average(img[img_bin == 0]) if np.sum(img[img_bin == 0]) > 0 else 0
        g1 = np.average(img[img_bin == 1]) if np.sum(img[img_bin == 1]) > 0 else 0

        var0 = sum([(g-g0)**2 * p_g[g] for g in range(0, t+1)])
        var1 = sum([(g-g1)**2 * p_g[g] for g in range(t+1, 256)])

        var_between = p0 * (g0 - g_avg)**2 + p1 * (g1 - g_avg)**2
        var_inner = p0 * var0**2 + p1 * var1**2

        # q is the relation of variance between classes and variance within classes
        q = var_between / var_inner if var_inner > 0 else 0

        print(t, p0, p1, g0, g1, g_avg, var_between, var_inner, q)
        if q_best is None or q_best < q:
            q_best = q
            threshold_best = t
            img_bin_best = img <= t

    # ground truth, based on scikit-image
    gt_tresh = skifilters.threshold_otsu(img)
    ground_truth = img <= gt_tresh

    # plot
    util.plot_images_grayscale(
        [img, img_bin_best, ground_truth],
        ["Image", "Otsu", "Otsu (Ground Truth)"]
    )
def main():
    """Load image, calculate optimal threshold, binarize, plot."""
    # load image
    img = data.coins()
    height, width = img.shape
    nb_pixels = height * width

    # precalculate some values for speedup
    # average pixel value
    g_avg = np.average(img)
    # P(pixel-value), i.e. #pixels-with-value / #all-pixels
    p_g = [0] * 256
    for g in range(0, 256):
        p_g[g] = np.sum(img == g) / nb_pixels

    # Otsu method
    # calculations are based on standard formulas
    q_best = None
    threshold_best = None
    img_bin_best = None
    # iterate over all possible thresholds
    for t in range(1, 255):
        img_bin = np.zeros(img.shape)
        img_bin[img >= t] = 1

        p1 = np.sum(img_bin) / nb_pixels
        p0 = 1 - p1

        g0 = np.average(
            img[img_bin == 0]) if np.sum(img[img_bin == 0]) > 0 else 0
        g1 = np.average(
            img[img_bin == 1]) if np.sum(img[img_bin == 1]) > 0 else 0

        var0 = sum([(g - g0)**2 * p_g[g] for g in range(0, t + 1)])
        var1 = sum([(g - g1)**2 * p_g[g] for g in range(t + 1, 256)])

        var_between = p0 * (g0 - g_avg)**2 + p1 * (g1 - g_avg)**2
        var_inner = p0 * var0**2 + p1 * var1**2

        # q is the relation of variance between classes and variance within classes
        q = var_between / var_inner if var_inner > 0 else 0

        print(t, p0, p1, g0, g1, g_avg, var_between, var_inner, q)
        if q_best is None or q_best < q:
            q_best = q
            threshold_best = t
            img_bin_best = img <= t

    # ground truth, based on scikit-image
    gt_tresh = skifilters.threshold_otsu(img)
    ground_truth = img <= gt_tresh

    # plot
    util.plot_images_grayscale([img, img_bin_best, ground_truth],
                               ["Image", "Otsu", "Otsu (Ground Truth)"])
def main():
    """Load image, convert into graph, segment."""
    img = data.camera()
    # worked decently for 32x32, not so well and slow for 64x64
    img = misc.imresize(img, (32, 32))

    graph = image_to_graph(img)

    # Merge subgraphs until convergence
    converged = False
    while not converged:
        print("Merging (%d subgraphs left)..." % (len(graph.subgraphs)))
        converged = True
        merged = []
        ediffs = graph.get_ediffs()
        # Iterate over all pairs subgraphs that have a connecting edge
        # Merge them if possible.
        # Do not stop after the first merge, so that we don't have to
        # recalculate the external differences between subgraphs many times
        for subgraph_a, subgraph_b, ediff in ediffs:
            # Try to merge the subgraphs to one subgraph
            if subgraph_a not in merged and subgraph_b not in merged:
                idiff_a = subgraph_a.get_internal_difference()
                idiff_b = subgraph_b.get_internal_difference()
                # Merge if externel difference (between two subgraphs)
                # is below both internal differences
                if ediff < K * idiff_a and ediff < K * idiff_b:
                    graph.merge_subgraphs(subgraph_a, subgraph_b)
                    merged.extend([subgraph_a, subgraph_b])
                    converged = False

    print("Found %d segments" % (len(graph.subgraphs)))

    # Create images of segments
    subgraphs = sorted(graph.subgraphs,
                       key=lambda sg: len(sg.nodes),
                       reverse=True)
    nb_segments = 8
    segment_images = []
    segment_titles = []
    for i in range(min(nb_segments, len(subgraphs))):
        segment_titles.append("Segment %d/%d" % (i, len(subgraphs)))
        segment_image = np.zeros(img.shape)
        for node in subgraphs[i].nodes:
            segment_image[node.y, node.x] = 255
        segment_images.append(segment_image)

    # plot images
    images = [img]
    images.extend(segment_images)
    titles = ["Image"]
    titles.extend(segment_titles)
    util.plot_images_grayscale(images, titles)
def main():
    """Load image, calculate harris scores (window functions: matrix of ones, gauss)
    and plot the results."""
    img = data.checkerboard()
    score_window = harris_ones(img, 7)
    score_gauss = harris_gauss(img)
    util.plot_images_grayscale(
        [img, score_window, score_gauss,
         feature.corner_harris(img)], [
             "Image", "Harris-Score (ones)", "Harris-Score (gauss)",
             "Harris-Score (ground truth)"
         ])
Beispiel #11
0
def main():
    """Apply several gaussian filters one by one and plot the results each time."""
    img = data.checkerboard()
    shapes = [(5, 5), (7, 7), (11, 11), (17, 17), (31, 31)]
    sigmas = [0.5, 1.0, 2.0, 4.0, 8.0]
    smoothed = []
    for shape, sigma in zip(shapes, sigmas):
        smoothed = apply_gauss(img, gaussian_kernel(shape, sigma=sigma))
        ground_truth = filters.gaussian_filter(img, sigma)
        util.plot_images_grayscale([img, smoothed, ground_truth], [
            "Image",
            "After gauss (sigma=%.1f)" % (sigma), "Ground Truth (scipy)"
        ])
def main():
    """Apply several gaussian filters one by one and plot the results each time."""
    img = data.checkerboard()
    shapes = [(5, 5), (7, 7), (11, 11), (17, 17), (31, 31)]
    sigmas = [0.5, 1.0, 2.0, 4.0, 8.0]
    smoothed = []
    for shape, sigma in zip(shapes, sigmas):
        smoothed = apply_gauss(img, gaussian_kernel(shape, sigma=sigma))
        ground_truth = filters.gaussian_filter(img, sigma)
        util.plot_images_grayscale(
            [img, smoothed, ground_truth],
            ["Image", "After gauss (sigma=%.1f)" % (sigma), "Ground Truth (scipy)"]
        )
def main():
    """Load image, convert into graph, segment."""
    img = data.camera()
    # worked decently for 32x32, not so well and slow for 64x64
    img = misc.imresize(img, (32, 32))

    graph = image_to_graph(img)

    # Merge subgraphs until convergence
    converged = False
    while not converged:
        print("Merging (%d subgraphs left)..." % (len(graph.subgraphs)))
        converged = True
        merged = []
        ediffs = graph.get_ediffs()
        # Iterate over all pairs subgraphs that have a connecting edge
        # Merge them if possible.
        # Do not stop after the first merge, so that we don't have to
        # recalculate the external differences between subgraphs many times
        for subgraph_a, subgraph_b, ediff in ediffs:
            # Try to merge the subgraphs to one subgraph
            if subgraph_a not in merged and subgraph_b not in merged:
                idiff_a = subgraph_a.get_internal_difference()
                idiff_b = subgraph_b.get_internal_difference()
                # Merge if externel difference (between two subgraphs)
                # is below both internal differences
                if ediff < K * idiff_a and ediff < K * idiff_b:
                    graph.merge_subgraphs(subgraph_a, subgraph_b)
                    merged.extend([subgraph_a, subgraph_b])
                    converged = False

    print("Found %d segments" % (len(graph.subgraphs)))

    # Create images of segments
    subgraphs = sorted(graph.subgraphs, key=lambda sg: len(sg.nodes), reverse=True)
    nb_segments = 8
    segment_images = []
    segment_titles = []
    for i in range(min(nb_segments, len(subgraphs))):
        segment_titles.append("Segment %d/%d" % (i, len(subgraphs)))
        segment_image = np.zeros(img.shape)
        for node in subgraphs[i].nodes:
            segment_image[node.y, node.x] = 255
        segment_images.append(segment_image)

    # plot images
    images = [img]
    images.extend(segment_images)
    titles = ["Image"]
    titles.extend(segment_titles)
    util.plot_images_grayscale(images, titles)
def main():
    """Initialize kernel, apply it to an image (via crosscorrelation, convolution)."""
    img = data.camera()
    kernel = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])

    cc_response = crosscorrelate(img, kernel)
    cc_gt = signal.correlate(img, kernel, mode="same")

    conv_response = convolve(img, kernel)
    conv_gt = signal.convolve(img, kernel, mode="same")

    util.plot_images_grayscale(
        [img, cc_response, cc_gt, conv_response, conv_gt], [
            "Image", "Cross-Correlation", "Cross-Correlation (Ground Truth)",
            "Convolution", "Convolution (Ground Truth)"
        ])
def main():
    """Load example faces, extract principal components, create Eigenfaces from
    PCs, plot results."""
    # load images, resize them, convert to 1D-vectors
    images_filepaths = get_images_in_directory("images/faces/")
    images = [ndimage.imread(fp, flatten=True) for fp in images_filepaths]
    images = [misc.imresize(image, (SCALE, SCALE)) for image in images]
    images_vecs = np.array([image.flatten() for image in images])

    # ------------
    # Custom Implementation of PCA
    # ------------
    pcs, images_vecs_transformed, images_vecs_reversed = custom_pca(images_vecs, NB_COMPONENTS)
    pcs = pcs.reshape((NB_COMPONENTS, SCALE, SCALE))

    # plot (First example image, recovered first example image, first 8 PCs)
    plots_imgs = [images[0], images_vecs_reversed[0].reshape((SCALE, SCALE))]
    plots_titles = ["Image 0", "Image 0\n(reconstructed)"]
    for i in range(NB_COMPONENTS):
        plots_imgs.append(pcs[i])
        plots_titles.append("Eigenface %d" % (i))

    util.plot_images_grayscale(plots_imgs, plots_titles)


    # ------------
    # Using the PCA implementation from scikit
    # ------------
    # train PCA, embed image vectors, reverse the embedding (lossy)
    pca = PCA(NB_COMPONENTS)
    images_vecs_transformed = pca.fit_transform(images_vecs)
    images_vecs_reversed = pca.inverse_transform(images_vecs_transformed)

    # Extract Eigenfaces. The Eigenfaces are the principal components.
    pcs = pca.components_.reshape((NB_COMPONENTS, SCALE, SCALE))

    # plot (First example image, recovered first example image, first 8 PCs)
    plots_imgs = [images[0], images_vecs_reversed[0].reshape((SCALE, SCALE))]
    plots_titles = ["Image 0", "Image 0\n(reconstructed)"]
    for i in range(NB_COMPONENTS):
        plots_imgs.append(pcs[i])
        plots_titles.append("Eigenface %d" % (i))

    util.plot_images_grayscale(plots_imgs, plots_titles)
def main():
    """Create a noisy line image, recover the line via hough transform and
    plot an unnoise image with that line."""
    # generate example image (noisy lines)
    img = np.zeros((128, 128))
    for y in range(12, 120):
        img[y, y] = 1
    for y in range(40, 75):
        img[y, 12] = 1
    for x in range(16, 64):
        img[int(10 + x*0.2), x] = 1
    img = (img * 100) + np.random.binomial(80, 0.5, (img.shape))

    accumulator, local_maxima, img_hough = hough(img, 5)

    util.plot_images_grayscale(
        [img, accumulator, local_maxima, img_hough],
        ["Image", "Accumulator content", "Local Maxima", "Line from Hough Transform"]
    )
def main():
    """Create a noisy line image, recover the line via hough transform and
    plot an unnoise image with that line."""
    # generate example image (noisy lines)
    img = np.zeros((128, 128))
    for y in range(12, 120):
        img[y, y] = 1
    for y in range(40, 75):
        img[y, 12] = 1
    for x in range(16, 64):
        img[int(10 + x * 0.2), x] = 1
    img = (img * 100) + np.random.binomial(80, 0.5, (img.shape))

    accumulator, local_maxima, img_hough = hough(img, 5)

    util.plot_images_grayscale([img, accumulator, local_maxima, img_hough], [
        "Image", "Accumulator content", "Local Maxima",
        "Line from Hough Transform"
    ])
def main():
    """Load image, compute derivatives, plot."""
    img = data.camera()
    imgy_np, imgx_np = np.gradient(img)
    imgx_ski, imgy_ski = _compute_derivatives(img)

    # dx
    util.plot_images_grayscale(
        [img, dx_symmetric(img), dx_forward(img), dx_backward(img), imgx_np, imgx_ski],
        ["Image", "dx (symmetric)", "dx (forward)", "dx (backward)",
         "Ground Truth (numpy)", "Ground Truth (scikit-image)"]
    )

    # dy
    util.plot_images_grayscale(
        [img, dy_symmetric(img), dy_forward(img), dy_backward(img), imgy_np, imgy_ski],
        ["Image", "dy (symmetric)", "dy (forward)", "dy (backward)",
         "Ground Truth (numpy)", "Ground Truth (scikit-image)"]
    )
def main():
    """Initialize kernel, apply it to an image (via crosscorrelation, convolution)."""
    img = data.camera()
    kernel = np.array([
        [-1, -2, -1],
        [0, 0, 0],
        [1, 2, 1]
    ])

    cc_response = crosscorrelate(img, kernel)
    cc_gt = signal.correlate(img, kernel, mode="same")

    conv_response = convolve(img, kernel)
    conv_gt = signal.convolve(img, kernel, mode="same")

    util.plot_images_grayscale(
        [img, cc_response, cc_gt, conv_response, conv_gt],
        ["Image", "Cross-Correlation", "Cross-Correlation (Ground Truth)", "Convolution", "Convolution (Ground Truth)"]
    )
Beispiel #20
0
def main():
    """Main function."""
    img = np.zeros((128, 128), dtype=np.uint8)
    img[0, 0:15] = 255
    img[5:10, 5:10] = 255
    img[64:71, 64:71] = 255
    img[72:75, 72] = 255
    img[91:95, 24:29] = 255
    img[91:92, 29:31] = 255
    img[30:60, 40:80] = 255
    img[10:70, 100:102] = 255
    img[50:60, 98:100] = 255
    img[15, 20:100] = 255
    img[90:110, 80:120] = 255
    img[80:120, 90:110] = 255
    img[100:105, 100:105] = 0

    util.plot_images_grayscale(
        [img, dilation(img), erosion(img), opening(img), closing(img)],
        ["Binary Image", "Binary Dilation", "Binary Erosion", "Binary Opening", "Binary Closing"]
    )
def main():
    """Load image, apply Canny, plot."""
    img = skiutil.img_as_float(data.camera())
    img = filters.gaussian_filter(img, sigma=1.0)

    sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
    sobel_x = np.rot90(sobel_y)  # rotates counter-clockwise

    img_sx = signal.correlate(img, sobel_x, mode="same")
    img_sy = signal.correlate(img, sobel_y, mode="same")

    g_magnitudes = np.sqrt(img_sx ** 2 + img_sy ** 2) / np.sqrt(2)
    g_orientations = np.arctan2(img_sy, img_sx)

    g_mag_nonmax = non_maximum_suppression(g_magnitudes, g_orientations)
    canny = hysteresis(g_mag_nonmax, 0.35, 0.05)

    ground_truth = feature.canny(data.camera())

    util.plot_images_grayscale(
        [img, g_magnitudes, g_mag_nonmax, canny, ground_truth],
        ["Image", "After Sobel", "After nonmax", "Canny", "Canny (Ground Truth)"],
    )
def main():
    """Load image, apply Canny, plot."""
    img = skiutil.img_as_float(data.camera())
    img = filters.gaussian_filter(img, sigma=1.0)

    sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
    sobel_x = np.rot90(sobel_y)  # rotates counter-clockwise

    img_sx = signal.correlate(img, sobel_x, mode="same")
    img_sy = signal.correlate(img, sobel_y, mode="same")

    g_magnitudes = np.sqrt(img_sx**2 + img_sy**2) / np.sqrt(2)
    g_orientations = np.arctan2(img_sy, img_sx)

    g_mag_nonmax = non_maximum_suppression(g_magnitudes, g_orientations)
    canny = hysteresis(g_mag_nonmax, 0.35, 0.05)

    ground_truth = feature.canny(data.camera())

    util.plot_images_grayscale(
        [img, g_magnitudes, g_mag_nonmax, canny, ground_truth], [
            "Image", "After Sobel", "After nonmax", "Canny",
            "Canny (Ground Truth)"
        ])