2.1 Harris角点検出器


第2章では,画像間の対応点と対応領域を探すことを目的とした局所画像記述子について述べる.パノラマの作成、現実技術の強化、画像の3 D再構築の計算など、画像マッチングによって行うことができます.
Harris角点検出アルゴリズム(HarrisまたはStephens角点検出器とも呼ばれる)は、画素の周囲に複数の方向のエッジが表示されている場合、その点を興味点とする.角点と呼びます.
# 2.1 Harris     
from pylab import *
from numpy import *
from PIL import Image
from numpy import  random
from scipy.ndimage import filters
from imageio import imwrite


def computer_harris_response(im, sigma = 3):
    """        ,       Harris         """

    #     
    imx = zeros(im.shape)
    filters.gaussian_filter(im, (sigma, sigma), (0, 1), imx)
    imy = zeros(im.shape)
    filters.gaussian_filter(im, (sigma, sigma), (1, 0), imy)
    #   Harris     
    Wxx = filters.gaussian_filter(imx*imx, sigma)
    Wxy = filters.gaussian_filter(imx * imy, sigma)
    Wyy = filters.gaussian_filter(imy * imy, sigma)
    #        
    Wdet = Wxx*Wyy-Wxy**2
    Wtr = Wxx + Wyy

    return Wdet/Wtr


def get_harris_points(harrisim, min_dist=10, threshold=0.1):
    """   Harris         。min_dist                 """

    #            
    corner_threshold = harrisim.max()*threshold
    harrisim_t = (harrisim > corner_threshold)*1
    #         
    coords = array(harrisim_t.nonzero()).T
    #      Harris   
    candidate_values = [harrisim[c[0], c[1]] for c in coords]
    #       Harris       
    index = argsort(candidate_values)
    #              
    allowed_locations = zeros(harrisim.shape)
    allowed_locations[min_dist:-min_dist, min_dist:-min_dist] = 1
    #   min_distance  ,    Harris 
    filtered_coords = []
    for i in index:
        if allowed_locations[coords[i,0], coords[i,1]] == 1:
            filtered_coords.append(coords[i])
            allowed_locations[(coords[i,0]-min_dist):(coords[i,0]+min_dist),
            (coords[i,1]-min_dist):(coords[i,1]+min_dist)] = 0

    return filtered_coords


def plot_harris_points(image, filtered_coords):
    """           """

    figure()
    gray()
    imshow(image)
    plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords], '*')
    axis('off')
    show()


im = array(Image.open('empire.jpg').convert('L'))
harrisim = computer_harris_response(im)
filtered_coords = get_harris_points(harrisim, threshold=0.2)
plot_harris_points(im, filtered_coords)

以上は,Harris角点検出アルゴリズムの実装例である.Harris角検出器の改良とさらなる開発応用を含む角検出の異なる方法を理解するには、次のようなリソースを検索します.
Webサイト
 
画像間で対応点を探す
from pylab import *
from numpy import *
from PIL import Image
from numpy import random
from scipy.ndimage import filters
from imageio import imwrite


def get_descriptors(image, filtered_coords, wid=5):
    """
            ,     2*wid+1     
    (      min_distance>wid)
    """
    desc = []
    for coords in filtered_coords:
        patch = image[coords[0] - wid:coords[0] + wid + 1,
                      coords[1] - wid:coords[1] + wid + 1].flatten()
        desc.append(patch)
    return desc


def match(desc1, desc2, threshold=0.5):
    """                ,        ,
                   """
    n = len(desc1[0])
    #      
    d = -ones((len(desc1), len(desc2)))
    for i in range(len(desc1)):
        for j in range(len(desc2)):
            d1 = (desc1[i]-mean(desc1[i]))/std(desc1[i])
            d2 = (desc2[j]-mean(desc2[j]))/std(desc2[j])
            ncc_value = sum(d1*d2)/(n-1)  #          
            if ncc_value > threshold:
                d[i, j] = ncc_value

    ndx = argsort(-d)  # Returns the indices that would sort an array.     
    matchscores = ndx[:, 0]

    return matchscores


def match_twosided(desc1, desc2, threshold=0.5):
    """       match()"""
    matches_12 = match(desc1, desc2, threshold)
    matches_21 = match(desc2, desc1, threshold)

    ndx_12 = where(matches_12 >= 0)[0]

    #         
    for n in ndx_12:
        if matches_21[matches_12[n]] != n:
            matches_12[n] = -1

    return matches_12


def appendimages(im1, im2):
    """                  """

    #            ,         
    rows1 = im1.shape[0]
    rows2 = im2.shape[0]

    if rows1 < rows2:
        im1 = concatenate((im1, zeros((rows2-rows1, im1.shape[1]))), axis=0)
    elif rows1 > rows2:
        im2 = concatenate((im1, zeros((rows1 - rows2, im2.shape[1]))), axis=0)
    #          ,         ,       

    return concatenate((im1, im2), axis=1)


def plot_matches(im1, im2, locs1, locs2, matchscores, show_below=True):
    """                 
        :im1,im2(    ),locs1,locs2(    ),matchscores(match()   )
      show_below(              """

    im3 = appendimages(im1, im2)
    if show_below:
        im3 = vstack((im3, im3))  # ?

    imshow(im3)

    cols1 = im1.shape[1]
    for i, m in enumerate(matchscores):
        if m > 0:
            plot([locs1[i][1], locs2[m][1]+cols1], [locs1[i][0], locs2[m][0]], 'c')
    axis('off')


def computer_harris_response(im, sigma=3):
    """        ,       Harris         """

    #     
    imx = zeros(im.shape)
    filters.gaussian_filter(im, (sigma, sigma), (0, 1), imx)
    imy = zeros(im.shape)
    filters.gaussian_filter(im, (sigma, sigma), (1, 0), imy)
    #   Harris     
    Wxx = filters.gaussian_filter(imx * imx, sigma)
    Wxy = filters.gaussian_filter(imx * imy, sigma)
    Wyy = filters.gaussian_filter(imy * imy, sigma)
    #        
    Wdet = Wxx * Wyy - Wxy ** 2
    Wtr = Wxx + Wyy

    return Wdet / Wtr


def get_harris_points(harrisim, min_dist=10, threshold=0.1):
    """   Harris         。min_dist                 """

    #            
    corner_threshold = harrisim.max() * threshold
    harrisim_t = (harrisim > corner_threshold) * 1
    #         
    coords = array(harrisim_t.nonzero()).T
    #      Harris   
    candidate_values = [harrisim[c[0], c[1]] for c in coords]
    #       Harris       
    index = argsort(candidate_values)
    #              
    allowed_locations = zeros(harrisim.shape)
    allowed_locations[min_dist:-min_dist, min_dist:-min_dist] = 1
    #   min_distance  ,    Harris 
    filtered_coords = []
    for i in index:
        if allowed_locations[coords[i, 0], coords[i, 1]] == 1:
            filtered_coords.append(coords[i])
            allowed_locations[(coords[i, 0] - min_dist):(coords[i, 0] + min_dist),
                              (coords[i, 1] - min_dist):(coords[i, 1] + min_dist)] = 0

    return filtered_coords


wid = 5

im1 = array(Image.open('crans_1_small.jpg').convert('L'))
harrisim = computer_harris_response(im1, 5)
filtered_coords1 = get_harris_points(harrisim, wid+1)
d1 = get_descriptors(im1, filtered_coords1, wid)

im2 = array(Image.open('crans_2_small.jpg').convert('L'))
harrisim = computer_harris_response(im2, 5)
filtered_coords2 = get_harris_points(harrisim, wid+1)
d2 = get_descriptors(im2, filtered_coords2, wid)

print("starting matching")
matches = match_twosided(d1, d2)

figure()
gray()
plot_matches(im1, im2, filtered_coords1, filtered_coords2, matches[:100])
show()

この方法は、画像画素ブロックの相互相関行列がより弱い記述性を有するため、正しく一致しない.さらに、これらの記述子はスケール不変性および回転不変性を有さず、アルゴリズム内の画素ブロックのサイズも対応するマッチングの結果に影響を及ぼす.