def test__Circular_Gabor__fit_Sf(self): self.pattern = np.zeros((256, 256)) self.pattern = cv2.circle(img=self.pattern, center=(100, 100), radius=75, color=255) cosfire = Cosfire(strategy_name='Circular Gabor', center_x=100, center_y=100, rho_list=(0, 75, 100), t1=0.99, t2=0.75, t3=0.9, filter_parameters=GaborParameters(ksize=(10, 10), σ=5, θ=np.linspace(start=0, stop=π, num=5, endpoint=True), λ=[12], # np.linspace(start=1, stop=100, num=100, endpoint=True), γ=0.25, ψ=0, ktype=cv2.CV_32F), sigma0=0.67, alpha=0.04, reflection_invariant=0, scale_invariant=[0], rotation_invariant=[0] ) cosfire._prototype_bank = _Circular_Gabor__compute_bank_of_responses(cosfire, self.pattern) cosfire.threshold_prototype_bank_of_responses(threshold=0.85) operator = _Circular_Gabor__fit_Sf(cosfire) self.assertEqual(operator,[CosfireCircularGaborTuple(λ=12, θ=0.0, ρ=75, φ=0.06981317007977318), CosfireCircularGaborTuple(λ=12, θ=3.141592653589793, ρ=75, φ=0.06981317007977318), CosfireCircularGaborTuple(λ=12, θ=2.356194490192345, ρ=75, φ=0.6457718232379019 ), CosfireCircularGaborTuple(λ=12, θ=2.356194490192345, ρ=75, φ=4.066617157146788 ), CosfireCircularGaborTuple(λ=12, θ=1.5707963267948966, ρ=75, φ=4.625122517784973 ), CosfireCircularGaborTuple(λ=12, θ=0.7853981633974483, ρ=75, φ=5.480333851262195 ), CosfireCircularGaborTuple(λ=12, θ=0.0, ρ=75, φ=6.178465552059927 ), CosfireCircularGaborTuple(λ=12, θ=3.141592653589793, ρ=75, φ=6.178465552059927 )])
def test_cosfire__preset_1(self): im1 = cv2.imread("media/patron1.jpg", 0) im2 = cv2.imread("media/prueba1.jpg", 0) for iter in range(1): a = Cosfire(**preset_1) a.fit(im1) r = a.transform(im2) # viewPattern(r, im2, 20) # cv2.imshow("Response", r) expected = np.load('media/test_cosfire__preset_1_expected.npy') assert_almost_equal(r, expected)
def test_shift_responses(self): specific_image = np.array([[3, 3, 3, 3, 3], [3, 2, 2, 2, 3], [3, 2, 1, 2, 3], [3, 2, 2, 2, 3], [3, 3, 3, 3, 3]], dtype=np.float32) tupla_collection = [CosfireCircularGaborTuple(ρ=1, ϕ=0, λ=0.1, θ=0), CosfireCircularGaborTuple(ρ=1, ϕ=π * 0.5, λ=0.1, θ=0), CosfireCircularGaborTuple(ρ=1, ϕ=π, λ=0.1, θ=0), CosfireCircularGaborTuple(ρ=1, ϕ=π * 1.5, λ=0.1, θ=0), ] response_collection = {tupla: specific_image for tupla in tupla_collection} cosfire_filter = Cosfire(**preset_1) result_collection = {key: cosfire_filter.shift_responses({key: response})[key] for key, response in response_collection.items()} expected_collection = { CosfireCircularGaborTuple(ρ=1, ϕ=0, λ=0.1, θ=0): np.array([[3, 3, 3, 3, 0], [2, 2, 2, 3, 0], [2, 1, 2, 3, 0], [2, 2, 2, 3, 0], [3, 3, 3, 3, 0]], dtype=np.float32), CosfireCircularGaborTuple(ρ=1, ϕ=π * 0.5, λ=0.1, θ=0): np.array([[0., 0., 0., 0., 0.], [3., 3., 3., 3., 3.], [3., 2., 2., 2., 3.], [3., 2., 1., 2., 3.], [3., 2., 2., 2., 3.]], dtype=np.float32), CosfireCircularGaborTuple(ρ=1, ϕ=π, λ=0.1, θ=0): np.array([[0., 3., 3., 3., 3.], [0., 3., 2., 2., 2.], [0., 3., 2., 1., 2.], [0., 3., 2., 2., 2.], [0., 3., 3., 3., 3.]], dtype=np.float32), CosfireCircularGaborTuple(ρ=1, ϕ=π * 1.5, λ=0.1, θ=0): np.array([[3., 2., 2., 2., 3.], [3., 2., 1., 2., 3.], [3., 2., 2., 2., 3.], [3., 3., 3., 3., 3.], [0., 0., 0., 0., 0.]], dtype=np.float32), } assert_almost_equal(result_collection[CosfireCircularGaborTuple(ρ=1, ϕ=π, λ=0.1, θ=0)], expected_collection[CosfireCircularGaborTuple(ρ=1, ϕ=π, λ=0.1, θ=0)]) assert_almost_equal(result_collection[CosfireCircularGaborTuple(ρ=1, ϕ=0, λ=0.1, θ=0)], expected_collection[CosfireCircularGaborTuple(ρ=1, ϕ=0, λ=0.1, θ=0)]) assert_almost_equal(result_collection[CosfireCircularGaborTuple(ρ=1, ϕ=π * 0.5, λ=0.1, θ=0)], expected_collection[CosfireCircularGaborTuple(ρ=1, ϕ=π * 0.5, λ=0.1, θ=0)]) assert_almost_equal(result_collection[CosfireCircularGaborTuple(ρ=1, ϕ=π * 1.5, λ=0.1, θ=0)], expected_collection[CosfireCircularGaborTuple(ρ=1, ϕ=π * 1.5, λ=0.1, θ=0)])
def test__threshold_prototype_bank_of_responses(self): self.pattern = np.zeros((256, 256)) self.pattern = cv2.circle(img=self.pattern, center=(100, 100), radius=75, color=255) cosfire = Cosfire(strategy_name='Circular Gabor', center_x=100, center_y=100, rho_list=range(0, 50, 10), t1=0.99, t2=0.75, t3=0.9, filter_parameters=GaborParameters(ksize=(10, 10), σ=5, θ=np.linspace(start=0, stop=π, num=5, endpoint=True), λ=[12], # np.linspace(start=1, stop=100, num=100, endpoint=True), γ=0.25, ψ=0, ktype=cv2.CV_32F), sigma0=0.67, alpha=0.04, reflection_invariant=0, scale_invariant=[1], rotation_invariant=[0] ) cosfire._prototype_bank = _Circular_Gabor__compute_bank_of_responses(cosfire, self.pattern) cosfire.threshold_prototype_bank_of_responses(threshold=0.75) generating_results = False if generating_results: # The first time I used this to generate expected results np.save('media/test__threshold_prototype_bank_of_responses__pattern.npy', self.pattern) with PdfPages('test__threshold_prototype_bank_of_responses.pdf') as pdf: for key, value in cosfire._prototype_bank.items(): file_name = 'media/test__threshold_prototype_bank_of_responses_' + str(key) + '.npy' np.save(file_name, value) fig = plt.figure() plt.imshow(value, cmap='cool') plt.title(key) pdf.savefig(fig) for key in cosfire._prototype_bank: file_name = 'media/test__threshold_prototype_bank_of_responses_' + str(key) + '.npy' expected = np.load(file_name) result = cosfire._prototype_bank[key] assert_almost_equal(result, expected)
def setUp(self): self.pattern = np.zeros((256, 256)) cv2.rectangle(img=self.pattern, pt1=(50, 100), pt2=(100, 106), color=255, thickness=-1) cv2.rectangle(img=self.pattern, pt1=(100, 100), pt2=(97, 50), color=255, thickness=-1) self.cosfire = Cosfire(strategy_name='Circular Gabor', center_x=100, center_y=100, rho_list=range(0, 100, 10), t1=0.99, t2=0.75, t3=0.9, filter_parameters=GaborParameters(ksize=(10, 10), σ=5, θ=np.linspace(start=0, stop=π, num=30, endpoint=False), λ=np.linspace(start=7, stop=8, num=10, endpoint=False), γ=0.5, ψ=π, ktype=cv2.CV_32F), sigma0=0.67, alpha=0.04, reflection_invariant=0, scale_invariant=[1], rotation_invariant=[0] )
# Now let's fit a COSFIRE filter using the demo pattern as prototype: ############################################################################## some_cosfire = Cosfire(strategy_name='Circular Gabor', center_x=100, center_y=100, rho_list=range(0, 100, 10), t1=0.99, t2=0.75, t3=0.9, filter_parameters=GaborParameters( ksize=(10, 10), σ=5, θ=np.linspace(start=0, stop=π, num=30, endpoint=False), λ=np.linspace(start=7, stop=8, num=10, endpoint=False), γ=0.5, ψ=π, ktype=cv2.CV_32F), sigma0=0.67, alpha=0.04, reflection_invariant=True, scale_invariant=[0.5, 1, 2], rotation_invariant=[0, 0.5 * π, 1.5 * π]) some_cosfire.fit(pattern) for parameters in some_cosfire._Sf:
class TestCosfireCircularGabor(unittest.TestCase): def setUp(self): self.pattern = np.zeros((256, 256)) cv2.rectangle(img=self.pattern, pt1=(50, 100), pt2=(100, 106), color=255, thickness=-1) cv2.rectangle(img=self.pattern, pt1=(100, 100), pt2=(97, 50), color=255, thickness=-1) self.cosfire = Cosfire(strategy_name='Circular Gabor', center_x=100, center_y=100, rho_list=range(0, 100, 10), t1=0.99, t2=0.75, t3=0.9, filter_parameters=GaborParameters(ksize=(10, 10), σ=5, θ=np.linspace(start=0, stop=π, num=30, endpoint=False), λ=np.linspace(start=7, stop=8, num=10, endpoint=False), γ=0.5, ψ=π, ktype=cv2.CV_32F), sigma0=0.67, alpha=0.04, reflection_invariant=0, scale_invariant=[1], rotation_invariant=[0] ) def test__cosfire_process(self): self.cosfire.fit(self.pattern) expected = [CosfireCircularGaborTuple(λ=7.5, θ=0.0, ρ=10, φ=1.3089969389957472), CosfireCircularGaborTuple(λ=7.5, θ=0.0, ρ=10, φ=2.0420352248333655), CosfireCircularGaborTuple(λ=7.5, θ=1.5707963267948966, ρ=10, φ=2.9845130209103035), CosfireCircularGaborTuple(λ=7.5, θ=1.5707963267948966, ρ=10, φ=4.171336912266447), CosfireCircularGaborTuple(λ=7.5, θ=0.0, ρ=20, φ=1.7976891295541595), CosfireCircularGaborTuple(λ=7.5, θ=1.5707963267948966, ρ=20, φ=3.07177948351002), CosfireCircularGaborTuple(λ=7.5, θ=1.5707963267948966, ρ=20, φ=3.5779249665883754), CosfireCircularGaborTuple(λ=7.5, θ=0.0, ρ=30, φ=1.710422666954443), CosfireCircularGaborTuple(λ=7.5, θ=1.5707963267948966, ρ=30, φ=3.420845333908886), CosfireCircularGaborTuple(λ=7.5, θ=0.0, ρ=40, φ=1.6755160819145565), CosfireCircularGaborTuple(λ=7.5, θ=1.5707963267948966, ρ=40, φ=3.351032163829113)] result = self.cosfire._Sf self.assertEqual(result, expected) def test__Circular_Gabor__compute_bank_of_responses__no_kwargs(self): self.pattern = np.zeros((256, 256)) self.pattern = cv2.circle(img=self.pattern, center=(100,100), radius=75, color=255) cosfire = Cosfire(strategy_name='Circular Gabor', center_x=100, center_y=100, rho_list=range(0, 50, 10), t1=0.99, t2=0.75, t3=0.9, filter_parameters=GaborParameters(ksize=(10, 10), σ=5, θ=np.linspace(start=0, stop=π, num=5, endpoint=True), λ=[12],#np.linspace(start=1, stop=100, num=100, endpoint=True), γ=0.25, ψ=0, ktype=cv2.CV_32F), sigma0=0.67, alpha=0.04, reflection_invariant=0, scale_invariant=[1], rotation_invariant=[0] ) results_bank = _Circular_Gabor__compute_bank_of_responses(cosfire, self.pattern) generating_results = False if generating_results: #The first time I used this to generate expected results np.save('media/test__Circular_Gabor__compute_bank_of_responses__no_kwargs__pattern.npy', self.pattern) with PdfPages('test__Circular_Gabor__compute_bank_of_responses.pdf') as pdf: for key, value in results_bank.items(): file_name = 'media/test__Circular_Gabor__compute_bank_of_responses__no_kwargs__' + str(key) + '.npy' np.save(file_name, value) fig = plt.figure() plt.imshow(value, cmap='cool') plt.title(key) pdf.savefig(fig) for key in results_bank: file_name = 'media/test__Circular_Gabor__compute_bank_of_responses__no_kwargs__' + str(key) + '.npy' expected = np.load(file_name) result = results_bank[key] assert_almost_equal(result, expected) def test__threshold_prototype_bank_of_responses(self): self.pattern = np.zeros((256, 256)) self.pattern = cv2.circle(img=self.pattern, center=(100, 100), radius=75, color=255) cosfire = Cosfire(strategy_name='Circular Gabor', center_x=100, center_y=100, rho_list=range(0, 50, 10), t1=0.99, t2=0.75, t3=0.9, filter_parameters=GaborParameters(ksize=(10, 10), σ=5, θ=np.linspace(start=0, stop=π, num=5, endpoint=True), λ=[12], # np.linspace(start=1, stop=100, num=100, endpoint=True), γ=0.25, ψ=0, ktype=cv2.CV_32F), sigma0=0.67, alpha=0.04, reflection_invariant=0, scale_invariant=[1], rotation_invariant=[0] ) cosfire._prototype_bank = _Circular_Gabor__compute_bank_of_responses(cosfire, self.pattern) cosfire.threshold_prototype_bank_of_responses(threshold=0.75) generating_results = False if generating_results: # The first time I used this to generate expected results np.save('media/test__threshold_prototype_bank_of_responses__pattern.npy', self.pattern) with PdfPages('test__threshold_prototype_bank_of_responses.pdf') as pdf: for key, value in cosfire._prototype_bank.items(): file_name = 'media/test__threshold_prototype_bank_of_responses_' + str(key) + '.npy' np.save(file_name, value) fig = plt.figure() plt.imshow(value, cmap='cool') plt.title(key) pdf.savefig(fig) for key in cosfire._prototype_bank: file_name = 'media/test__threshold_prototype_bank_of_responses_' + str(key) + '.npy' expected = np.load(file_name) result = cosfire._prototype_bank[key] assert_almost_equal(result, expected) def test__Circular_Gabor__fit_Sf(self): self.pattern = np.zeros((256, 256)) self.pattern = cv2.circle(img=self.pattern, center=(100, 100), radius=75, color=255) cosfire = Cosfire(strategy_name='Circular Gabor', center_x=100, center_y=100, rho_list=(0, 75, 100), t1=0.99, t2=0.75, t3=0.9, filter_parameters=GaborParameters(ksize=(10, 10), σ=5, θ=np.linspace(start=0, stop=π, num=5, endpoint=True), λ=[12], # np.linspace(start=1, stop=100, num=100, endpoint=True), γ=0.25, ψ=0, ktype=cv2.CV_32F), sigma0=0.67, alpha=0.04, reflection_invariant=0, scale_invariant=[0], rotation_invariant=[0] ) cosfire._prototype_bank = _Circular_Gabor__compute_bank_of_responses(cosfire, self.pattern) cosfire.threshold_prototype_bank_of_responses(threshold=0.85) operator = _Circular_Gabor__fit_Sf(cosfire) self.assertEqual(operator,[CosfireCircularGaborTuple(λ=12, θ=0.0, ρ=75, φ=0.06981317007977318), CosfireCircularGaborTuple(λ=12, θ=3.141592653589793, ρ=75, φ=0.06981317007977318), CosfireCircularGaborTuple(λ=12, θ=2.356194490192345, ρ=75, φ=0.6457718232379019 ), CosfireCircularGaborTuple(λ=12, θ=2.356194490192345, ρ=75, φ=4.066617157146788 ), CosfireCircularGaborTuple(λ=12, θ=1.5707963267948966, ρ=75, φ=4.625122517784973 ), CosfireCircularGaborTuple(λ=12, θ=0.7853981633974483, ρ=75, φ=5.480333851262195 ), CosfireCircularGaborTuple(λ=12, θ=0.0, ρ=75, φ=6.178465552059927 ), CosfireCircularGaborTuple(λ=12, θ=3.141592653589793, ρ=75, φ=6.178465552059927 )])