Python skimage.feature.match_template() Examples

The following are 5 code examples of skimage.feature.match_template(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module skimage.feature , or try the search function .
Example #1
Source File: match.py    From kog-money with MIT License 5 votes vote down vote up
def match_template1(template, img, plot=False, method=cv2.TM_SQDIFF_NORMED):
    img = cv2.imread(img, 0).copy()
    template = cv2.imread(template, 0)
    w, h = template.shape[::-1]
    if lib == OPENCV:
        res = cv2.matchTemplate(img, template, method)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
            top_left = min_loc
        else:
            top_left = max_loc
    else:
        result = match_template(img, template)
        ij = np.unravel_index(np.argmax(result), result.shape)
        top_left = ij[::-1]

    bottom_right = (top_left[0] + w, top_left[1] + h)

    if plot:
        cv2.rectangle(img, top_left, bottom_right, 255, 5)
        plt.subplot(121)
        plt.imshow(img)
        plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
        plt.subplot(122)
        plt.imshow(template)

        plt.show()

    return top_left, bottom_right 
Example #2
Source File: peakfinders2D.py    From pyxem with GNU General Public License v3.0 5 votes vote down vote up
def find_peaks_xc(z, disc_image, min_distance=5, peak_threshold=0.2):
    """
    Find peaks using the the correlation between the image and a reference peaks

    Parameters
    ----------

    z: numpy.ndarray
        Array of image intensities.
    disc_image: numpy.ndarray (square)
        Array containing a single bright disc, similar to those to detect.
    min_distance: int
        The minimum expected distance between peaks (in pixels)
    peak_threshold: float between 0 and 1
        Larger values will lead to fewer peaks in the output.

    Returns
    -------
    peaks : numpy.ndarray
        Array of peak pixel coordinates with shape (n_peaks, 2).

    """
    response_image = match_template(z, disc_image, pad_input=True)
    peaks = corner_peaks(
        response_image, min_distance=min_distance, threshold_rel=peak_threshold
    )
    # make return format the same as the other peak finders
    peaks -= 1

    return clean_peaks(peaks) 
Example #3
Source File: dask_tools.py    From pyxem with GNU General Public License v3.0 5 votes vote down vote up
def _template_match_binary_image_single_frame(frame, binary_image):
    """Template match a binary image (template) with a single image.

    Parameters
    ----------
    frame : NumPy 2D array
    binary_image : NumPy 2D array
        Must be smaller than frame

    Returns
    -------
    template_match : NumPy 2D array
        Same size as frame

    Examples
    --------
    >>> frame = np.random.randint(1000, size=(256, 256))
    >>> from skimage import morphology
    >>> binary_image = morphology.disk(4, np.uint16)
    >>> import pyxem.utils.dask_tools as dt
    >>> template_match = dt._template_match_binary_image_single_frame(
    ...     frame, binary_image)

    """
    template_match = match_template(frame, binary_image, pad_input=True)
    template_match = template_match - np.min(template_match)
    return template_match 
Example #4
Source File: util.py    From self-driving-truck with MIT License 5 votes vote down vote up
def template_match(needle, haystack):
    result = feature.match_template(haystack, needle)
    ij = np.unravel_index(np.argmax(result), result.shape)
    x, y = ij[1], ij[0]
    score = result[y, x]
    return x, y, score 
Example #5
Source File: template_matching.py    From Pic-Numero with MIT License 4 votes vote down vote up
def match_templates_1(search_image, template_image, n=0):
    '''
    Calculates the n closest matches of some template image in another image and
    displays a figure illustrating the results.

    Args:
        search_image: image within which to match template.
        template_image: image to be matched.
        n: number of matches to be found. ie. closest n matches.
    '''
    Point = namedtuple('Point', ['x', 'y'])

    # Calculate template matches
    match_result = match_template(search_image, template_image);

    # Get closest n matches
    print(match_result.shape)
    if(n == 0):
        n = int(match_result.shape[1]);
    matched_point_list = []
    max_indices = get_n_max_indices(match_result, n)
    for index in max_indices:
        ij = np.unravel_index(int(index), match_result.shape)
        x, y = ij[::-1]
        point = Point(x,y)
        #print(point)
        matched_point_list.append(point)

    # Display
    fig = plt.figure(figsize=(8, 3))
    plt.gray()
    ax1 = plt.subplot(1, 3, 1)
    ax2 = plt.subplot(1, 3, 2, adjustable='box-forced')
    ax3 = plt.subplot(1, 3, 3, sharex=ax2, sharey=ax2, adjustable='box-forced')

    ax1.imshow(template_image)
    ax1.set_axis_off()
    ax1.set_title('grain template')

    # highlight matched regions
    ax2.imshow(search_image)
    ax2.set_axis_off()
    ax2.set_title('image')
    himage, wimage = template_image.shape
    for point in matched_point_list:
        rect = plt.Rectangle((point.x, point.y), wimage, himage, edgecolor='r', facecolor='none')
        ax2.add_patch(rect)

    # highlight matched regions
    ax3.imshow(match_result)
    ax3.set_axis_off()
    ax3.set_title('`match_template`\nresult')
    ax3.autoscale(False)
    for point in matched_point_list:
        ax3.plot(point.x, point.y, 'o', markeredgecolor='r', markerfacecolor='none', markersize=10)

    plt.show()