[0.5 pt] In the previous question, replace cornerness measure with the following:
</br>Cornerness(p, σ, α) = λ 1 λ 2 − α(λ 1 + λ 2 )
and determine the efficiency of this system and the system in the previous question by measuring and reporting the
time. You are supposed to get the same results in accuracy but different results in efficiency
In [2]:
import math
from scipy import ndimage
from PIL import Image
from numpy import *
from matplotlib import pyplot as plt
from pylab import *
import time
In [3]:
# gaussian filter func
def gfilter (x,y,s):
gfilter = (1/(math.sqrt(2*(math.pi))*s))*exp(-((x**2) + (y**2))/2/s**2)
return gfilter
In [4]:
#gaussian filter first derivative func
def gfilter1 (x,y,s,z):
if(z =='x'):
gfilter1 = gfilter(x,y,s)*(-x/(s**2))
elif(z=='y'):
gfilter1 = gfilter(x,y,s)*(-y/(s**2))
return gfilter1
In [14]:
#file - name of input image file
#size - size of the kernal size = 2(input)+1
#a - aplha, the cornerness parameter.
#t - threshold
#s - sigma value
def Harris(file,size,a,t,s):
start = time.time()
inp = file # input image
I = array(Image.open(inp).convert('L')) # read the input image
G = []
for i in range(-2,2+1):
G.append(gfilter(i,0,s)) # equating y to 0 for a 1D matrix
Gx = [] #gaussian in x direction
for i in range(-size,size+1):
Gx.append(gfilter1(i,0,s,'x'))
Gy = [] #gaussian in y direction
for i in range(-size,size+1):
Gy.append([gfilter1(0,i,s,'y')])
I1 = []
for i in range(len(I[:,0])):
I1.extend([convolve(I[i,:],G)]) # I*G in x direction
I1 = array(matrix(I1))
I11 = I1*I1 #I11 = square from formula for harris(Lsquare X)
Ix = []
for i in range(len(I[:,0])):
Ix.extend([convolve(I1[i,:],Gx)]) # I*G in x direction
Ix = array(matrix(Ix))
I2 = []
for i in range(len(I[0,:])):
I2.extend([convolve(I[:,i],G)]) # I*G in y direction
I2 = array(matrix(transpose(I2)))
I22 = I2*I2 #I22 = square from formula for harris(Lsquare Y)
Iy = []
for i in range(len(I[0,:])):
Iy.extend([convolve(I2[:,i],Gx)]) # I*G in y direction
Iy = array(matrix(transpose(Iy)))
I12 = []
for i in range(len(I1[:,0])):
temp = []
for j in range(len(I2[0,:])):
temp.append(I1[i,j]*I2[i,j])
if (j == len(I2[0,:])-1):
I12.extend(array(matrix(temp)))
I12 = array(matrix(I12))
Ixy = []
for i in range(len(I12[:,0])):
Ixy.extend([convolve(I12[i,:],Gx)]) # I*G in x direction
Ixy = array(matrix(Ixy))
#store values in x,y to plot the corners
x = [] # array x[] stores x coordinates of the corner
y = [] # array y[] stores y coordinates of the corner
for i in range(len(I[:,0])):
for j in range(len(I[0,:])):
H1 = ([Ix[i,j]**2,Ix[i,j]*Iy[i,j]],[Ix[i,j]*Iy[i,j],Iy[i,j]**2]) # Harris Matrix
e,v = linalg.eig(H1) # changing cornerness measure ,Eigen Values
if((abs((e[0]*e[1])-(a*(e[0]+e[1])))) > t):
y.append(i-5)
x.append(j-5)
plt.figure()
plt.imshow(I,cmap = cm.gray)
plot(x,y,'r.')
plt.axis([5,len(I[0,:]),len(I[:,0]),5])
show()
return time.time() - start
In [10]:
inp1 = Harris('/home/srikar/CVPA1/CVV/input1.png',2,.055,41,1.5)
In [11]:
inp2 = Harris('/home/srikar/CVPA1/CVV/input2.png',2,.004,4.55,1.5)
In [12]:
inp3 = Harris('/home/srikar/CVPA1/CVV/input3.png',2,.004,1,1.5)
In [15]:
print ('The Accuracy/Time to Execute is:\nInput Image 1: %.2fseconds\nInput Image 2: %.2fseconds\nInput Image 3: %.2fseconds)'%(inp1,inp2,inp3))
In [ ]: