Как реализовать метод Ши-Томаси без использования GoodFeatures для отслеживания? - PullRequest
0 голосов
/ 17 июня 2020

Я пытаюсь реализовать версию Shi-Tomasi без использования функции GoodFeaturesToTrack (). Как мне это сделать, используя нелокальное максимальное подавление? Код прилагается, если это помогает: Попытка сохранить местоположение каждого максимального значения x, каждого максимального значения y и Q в результатах массива

def nonMaximalSupress(image,NHoodSize):
#
dX = NHoodSize
dY = NHoodSize
M, N = image.shape
for x in range(0,M-dX+1):
    for y in range(0,N-dY+1):
        window = image[x:x+dX, y:y+dY]
        if np.sum(window)==0:
            localMax=0
        else:
            localMax = np.amax(window)
        maxCoord = np.argmax(window)
        # zero all but the localMax in the window
        window[:] = 0
        window.flat[maxCoord] = localMax
return image
################################
NUMRESULTS = 5000
def detectKeypoints(img, alpha):
    I = np.float32(img)
    results = np.zeros( (NUMRESULTS,3), dtype = float)

    sigma = float(1.414)
    ksize = int(1 + np.sqrt(-2*sigma**2*np.log(0.005)))

    #############my code here############
    # Shi-Tomasi Method 

    # Find the X and Y gradient components using Sobel kernels
    Ix = cv2.Sobel(I, cv2.CV_64F,1,0,ksize)
    Iy = cv2.Sobel(I,cv2.CV_64F,0,1,ksize)

    # components of the local structure matrix
    A = np.square(Ix)
    B = np.square(Iy)
    C = np.multiply(Ix,Iy)

    M = [[A,C], # local structure matrix 
         [C,B]] 

    # get a Gaussian kernel for set ksize and signma value
    gauss = cv2.getGaussianKernel(ksize,sigma)

    # smooth individual components of the local structure matrix
    A_filter = cv2.filter2D(A, cv2.CV_64F, gauss)
    B_filter = cv2.filter2D(B, cv2.CV_64F, gauss)
    C_filter = cv2.filter2D(C, cv2.CV_64F, gauss)


    Q = 0
    yloc = 0
    xloc = 0
    for c in range(img.shape[0]):
        for r in range(img.shape[1]):

            M_bar = ([[A_filter[c,r], C_filter[c,r]],
                      [C_filter[c,r], B_filter[c,r]]])


            eg = np.linalg.eigvals(M_bar)
           #img[c,r] = min(eg[0], eg[1])
        Q = min(eg[0],eg[1])

        possible_corner_locations = nonMaximalSupress(img, ksize)

        for c1 in range(possible_corner_locations.shape[0]):
            for r1 in range(possible_corner_locations.shape[1]):
                if possible_corner_locations[c1,r1] != 0:
                    yloc = c1
                    xloc = r1
                    for i in range(NUMRESULTS):
                          results[i,0] = xloc
                           results[i,1] = yloc
                           results[i,2] = Q
                    else:
                        pass



    return results
...