From de972321c99f738bd2f60202958eb2823b94a00e Mon Sep 17 00:00:00 2001 From: QiLin Date: Wed, 16 Nov 2022 17:16:33 +0800 Subject: [PATCH 1/3] correct "*" to "**" in scalex and scale, flip y meshgrid, and add function "normalize". The results are now identical to the original Matlab code. --- lm.py | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lm.py b/lm.py index 4e87641..a9b098e 100644 --- a/lm.py +++ b/lm.py @@ -9,8 +9,7 @@ ''' import numpy as np -import cv2 -import matplotlib.pyplot as plt + def gaussian1d(sigma, mean, x, ord): x = np.array(x) @@ -24,7 +23,7 @@ def gaussian1d(sigma, mean, x, ord): g = g1 return g elif ord == 1: - g = -g1*((x_)/(var)) + g = -g1*(x_/var) return g else: g = g1*(((x_*x_) - var)/(var**2)) @@ -33,33 +32,39 @@ def gaussian1d(sigma, mean, x, ord): def gaussian2d(sup, scales): var = scales * scales shape = (sup,sup) - n,m = [(i - 1)/2 for i in shape] - x,y = np.ogrid[-m:m+1,-n:n+1] + n, m = [(i - 1)/2 for i in shape] + x, y = np.ogrid[-m:m+1,-n:n+1] g = (1/np.sqrt(2*np.pi*var))*np.exp( -(x*x + y*y) / (2*var) ) return g def log2d(sup, scales): var = scales * scales shape = (sup,sup) - n,m = [(i - 1)/2 for i in shape] - x,y = np.ogrid[-m:m+1,-n:n+1] - g = (1/np.sqrt(2*np.pi*var))*np.exp( -(x*x + y*y) / (2*var) ) + n, m = [(i - 1)/2 for i in shape] + x, y = np.ogrid[-m:m+1,-n:n+1] + g = (1/np.sqrt(2*np.pi*var))*np.exp(-(x*x + y*y) / (2*var)) h = g*((x*x + y*y) - var)/(var**2) return h +def normalise(f): + f = f - np.mean(f) + f = f / np.sum(np.abs(f)) + return f + def makefilter(scale, phasex, phasey, pts, sup): - gx = gaussian1d(3*scale, 0, pts[0,...], phasex) - gy = gaussian1d(scale, 0, pts[1,...], phasey) + gx = gaussian1d(3*scale, 0, pts[0, ...], phasex) + gy = gaussian1d(scale, 0, pts[1, ...], phasey) - image = gx*gy + f = gx*gy - image = np.reshape(image,(sup,sup)) - return image + f = np.reshape(f, (sup, sup)) + f = normalise(f) + return f def makeLMfilters(): sup = 49 - scalex = np.sqrt(2) * np.array([1,2,3]) + scalex = np.sqrt(2) ** np.array([1,2,3]) norient = 6 nrotinv = 12 @@ -70,9 +75,10 @@ def makeLMfilters(): hsup = (sup - 1)/2 x = [np.arange(-hsup,hsup+1)] - y = [np.arange(-hsup,hsup+1)] + # y = [np.arange(-hsup,hsup+1)] + y = [np.arange(hsup, -hsup - 1, -1)] - [x,y] = np.meshgrid(x,y) + [x, y] = np.meshgrid(x,y) orgpts = [x.flatten(), y.flatten()] orgpts = np.array(orgpts) @@ -83,15 +89,15 @@ def makeLMfilters(): angle = (np.pi * orient)/norient c = np.cos(angle) s = np.sin(angle) - rotpts = [[c+0,-s+0],[s+0,c+0]] + rotpts = [[c+0, -s+0], [s+0, c+0]] rotpts = np.array(rotpts) rotpts = np.dot(rotpts,orgpts) - F[:,:,count] = makefilter(scalex[scale], 0, 1, rotpts, sup) - F[:,:,count+nedge] = makefilter(scalex[scale], 0, 2, rotpts, sup) + F[:, :, count] = makefilter(scalex[scale], 0, 1, rotpts, sup) + F[:, :, count+nedge] = makefilter(scalex[scale], 0, 2, rotpts, sup) count = count + 1 count = nbar+nedge - scales = np.sqrt(2) * np.array([1,2,3,4]) + scales = np.sqrt(2) ** np.array([1,2,3,4]) for i in range(len(scales)): F[:,:,count] = gaussian2d(sup, scales[i]) @@ -109,4 +115,4 @@ def makeLMfilters(): # Call the make filter function F = makeLMfilters() -print F.shape +print(F.shape) From f4bb5ce2e5666b4a16018b2b8ae9bb55aab320b5 Mon Sep 17 00:00:00 2001 From: QiLin Date: Wed, 16 Nov 2022 17:31:56 +0800 Subject: [PATCH 2/3] format the code a bit --- lm.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/lm.py b/lm.py index a9b098e..a878858 100644 --- a/lm.py +++ b/lm.py @@ -89,30 +89,25 @@ def makeLMfilters(): angle = (np.pi * orient)/norient c = np.cos(angle) s = np.sin(angle) - rotpts = [[c+0, -s+0], [s+0, c+0]] - rotpts = np.array(rotpts) - rotpts = np.dot(rotpts,orgpts) + rotmtx = np.array([[c, -s], [s, c]]) + rotpts = np.dot(rotmtx, orgpts) F[:, :, count] = makefilter(scalex[scale], 0, 1, rotpts, sup) F[:, :, count+nedge] = makefilter(scalex[scale], 0, 2, rotpts, sup) count = count + 1 - count = nbar+nedge - scales = np.sqrt(2) ** np.array([1,2,3,4]) + count = nbar + nedge + scales = np.sqrt(2) ** np.array([1, 2, 3, 4]) for i in range(len(scales)): - F[:,:,count] = gaussian2d(sup, scales[i]) - count = count + 1 - - for i in range(len(scales)): - F[:,:,count] = log2d(sup, scales[i]) - count = count + 1 - - for i in range(len(scales)): - F[:,:,count] = log2d(sup, 3*scales[i]) - count = count + 1 + F[:, :, count] = gaussian2d(sup, scales[i]) + F[:, :, count] = log2d(sup, scales[i]) + F[:, :, count] = log2d(sup, 3 * scales[i]) + count = count + 3 return F + # Call the make filter function -F = makeLMfilters() -print(F.shape) +if __name__ == '__main__': + F = makeLMfilters() + print(F.shape) From f0a391b77c51de00d32a681d348f20c23de37a80 Mon Sep 17 00:00:00 2001 From: QiLin Date: Fri, 2 Dec 2022 08:45:54 +0800 Subject: [PATCH 3/3] correct the index for the LOG filters --- lm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lm.py b/lm.py index a878858..92875b6 100644 --- a/lm.py +++ b/lm.py @@ -100,8 +100,8 @@ def makeLMfilters(): for i in range(len(scales)): F[:, :, count] = gaussian2d(sup, scales[i]) - F[:, :, count] = log2d(sup, scales[i]) - F[:, :, count] = log2d(sup, 3 * scales[i]) + F[:, :, count+1] = log2d(sup, scales[i]) + F[:, :, count+2] = log2d(sup, 3 * scales[i]) count = count + 3 return F