Example #1
0
def findSurfPoints(filename):
  """
  Input : name of file
  Output: interest points
  """
  clear = " " * 50
  I_bw = cv2.imread(filename, 0).astype(float)/255.0
  I = cv2.imread(filename)
  
  # Initialize gaussian kernel holders
  filter9 =  numpy.zeros((6,9,9))
  filter15 = numpy.zeros((6,15,15))
  filter21 = numpy.zeros((6,21,21))
  filter27 = numpy.zeros((6,27,27))
  filter39 = numpy.zeros((6,39,39))
  filter51 = numpy.zeros((6,51,51))
  filter75 = numpy.zeros((6,75,75))
  filter99 = numpy.zeros((6,99,99))

  #print("Process: Calculating Gaussian kernels","\r", end="")
  # Get gaussian kernels [dxx,dyy,dxy,dx,dy,gauss]
  #numpy.set_printoptions(precision=0)
  for i in range(0,6):
    filter9[i] =  getGauss(9,i)
    filter15[i] = getGauss(15,i)
    filter21[i] = getGauss(21,i)
    filter27[i] = getGauss(27,i)
    filter39[i] = getGauss(39,i)
    filter75[i] = getGauss(75,i)
    filter99[i] = getGauss(99,i)
  #print ("\n")
  #print (numpy.sum(filter9[:,:,1]))
  #print (numpy.sum(filter9[:,:,0]))

  # Intitialize convolved image holder
  conv9  = numpy.zeros((6, I_bw.shape[0],I_bw.shape[1]))
  conv15 = numpy.zeros((6, I_bw.shape[0],I_bw.shape[1]))
  conv21 = numpy.zeros((6, I_bw.shape[0],I_bw.shape[1]))
  conv27 = numpy.zeros((6, I_bw.shape[0],I_bw.shape[1]))
  conv39 = numpy.zeros((6, I_bw.shape[0],I_bw.shape[1]))
  conv51 = numpy.zeros((6, I_bw.shape[0],I_bw.shape[1]))
  conv75 = numpy.zeros((6, I_bw.shape[0],I_bw.shape[1]))
  conv99 = numpy.zeros((6, I_bw.shape[0],I_bw.shape[1]))

  # Each holder has the image corresponding to dxx, dyy, dxy, dx, dy, gauss
  #print(clear,"\r", end="")
  #print("Process: Convolving image for all derivates","\r", end="")
  for i in range(0,6):
    conv9[i]  = cv2.filter2D(I_bw,-1, filter9[i])
    conv15[i] = cv2.filter2D(I_bw,-1, filter15[i])
    conv21[i] = cv2.filter2D(I_bw,-1, filter21[i])
    conv27[i] = cv2.filter2D(I_bw,-1, filter27[i])
    conv39[i] = cv2.filter2D(I_bw,-1, filter39[i])
    conv51[i] = cv2.filter2D(I_bw,-1, filter51[i])
    conv75[i] = cv2.filter2D(I_bw,-1, filter75[i])
    conv99[i] = cv2.filter2D(I_bw,-1, filter99[i])
    #cv2.imshow('image', conv9[i])
    #cv2.waitKey(0)
  #print (conv9[:,:,2])
  
  # Initialize holders for determinants of hessian
  o1 = numpy.zeros((4, I_bw.shape[0],I_bw.shape[1]))
  o2 = numpy.zeros((4, I_bw.shape[0],I_bw.shape[1]))
  o3 = numpy.zeros((4, I_bw.shape[0],I_bw.shape[1]))

  # Calculate determinant for each octave
  for y in range(3,I_bw.shape[0]-3):
    for x in range(3,I_bw.shape[1]-3):
      o1[0, y,x] =  conv9[0,y,x]* conv9[1,y,x]-((conv9[2,y,x])**2)
      o1[1, y,x] = conv15[0,y,x]*conv15[1,y,x]-((conv15[2,y,x])**2)
      o1[2, y,x] = conv21[0,y,x]*conv21[1,y,x]-((conv21[2,y,x])**2)
      o1[3, y,x] = conv27[0,y,x]*conv27[1,y,x]-((conv27[2,y,x])**2)

      o2[0, y,x] = conv15[0,y,x]* conv9[1,y,x]-((conv9[2,y,x])**2)
      o2[1, y,x] = conv27[0,y,x]*conv15[1,y,x]-((conv15[2,y,x])**2)
      o2[2, y,x] = conv39[0,y,x]*conv21[1,y,x]-((conv21[2,y,x])**2)
      o2[3, y,x] = conv51[0,y,x]*conv27[1,y,x]-((conv27[2,y,x])**2)

      o3[0, y,x] = conv27[0,y,x]* conv9[1,y,x]-((conv9[2,y,x])**2)
      o3[1, y,x] = conv51[0,y,x]*conv15[1,y,x]-((conv15[2,y,x])**2)
      o3[2, y,x] = conv75[0,y,x]*conv21[1,y,x]-((conv21[2,y,x])**2)
      o3[3, y,x] = conv99[0,y,x]*conv27[1,y,x]-((conv27[2,y,x])**2)

  extrema_points_1_1 = []
  extrema_points_1_2 = []
  extrema_points_2_1 = []
  extrema_points_2_2 = []
  extrema_points_3_1 = []
  extrema_points_3_2 = []
  #extrema_points_4 = [] 
  #print(clear,"\r", end="")

  #print("Process: Finding points for first octave","\r", end="")

  # Perform non maximal supression on determinant of Hessian.
  passedmax, passeddxhat, rejectedxhat = 0,0,0
  passedmax1, passeddxhat1, rejectedxhat1 = 0,0,0

  for y in range(0,I_bw.shape[0]):
    for x in range(0,I_bw.shape[1]):
      Flag = False
      if find_max_new(o1,1,y,x) == 1:
        if (accurate_keypoint(I_bw[y,x],conv9[:,y,x],conv15[:,y,x],conv21[:,y,x], 0.5) == 1):
          extrema_points_1_1.append([y,x,(2.0)])
          #I[y,x] = (0,0,255)
          cv2.circle(I,(x,y), 3, (0,0,255), -1)
        rejectedxhat += 1
        Flag = True
      if Flag == False and find_max_new(o1,2,y,x) == 1:
        if (accurate_keypoint(I_bw[y,x],conv15[:,y,x],conv21[:,y,x],conv27[:,y,x], 0.5) == 1):
          extrema_points_1_2.append([y,x,(2.8)])
          #I[y,x] = (0,0,255)
          cv2.circle(I,(x,y), 5, (0,0,155), -1)

  dogn1 = numpy.array(extrema_points_1_1)
  dogn2 = numpy.array(extrema_points_1_2)
  if (len(dogn1) > 0) and (len(dogn2)>0):
    result = numpy.vstack([dogn1, dogn2])
  #print("dog1")
  #print(dogn1)
  #print("\ndogn2")
  #print(dogn2)
  print ("Number of points in first octave: %d" % len(result))
  
  for y in range(0,I_bw.shape[0]):
    for x in range(0,I_bw.shape[1]):
      Flag = False
      if find_max_new(o2,1,y,x) == 1:
        if (accurate_keypoint(I_bw[y,x],conv15[:,y,x],conv27[:,y,x],conv39[:,y,x], 0.5) == 1):
          extrema_points_2_1.append([y,x,(3.6)])
          #I[y,x] = (0,0,255)
          cv2.circle(I,(x,y), 5, (0,255,0), 2)

        Flag = True
      if Flag == False and find_max_new(o2,2,y,x) == 1:
        if (accurate_keypoint(I_bw[y,x],conv27[:,y,x],conv39[:,y,x],conv51[:,y,x], 0.5) == 1):
          extrema_points_2_2.append([y,x,(5.2)])
          #I[y,x] = (0,0,255)
          cv2.circle(I,(x,y), 5, (0,155,0), 2)
  dogn3 =  numpy.array(extrema_points_2_1)
  dogn4 = numpy.array(extrema_points_2_2)
  if (len(dogn3) > 1) and (len(dogn4)>1):
    result1 = numpy.vstack([dogn3, dogn4])
    print ("Number of points in second octave: %d" % len(result1))

  for y in range(0,I_bw.shape[0]):
    for x in range(0,I_bw.shape[1]):
      Flag = False
      if find_max_new(o3,1,y,x) == 1:
        if (accurate_keypoint(I_bw[y,x],conv27[:,y,x],conv51[:,y,x],conv75[:,y,x], 0.5) == 1):
          #I[y,x] = (255,0,0)
          cv2.circle(I,(x,y), 7, (255,0,0), 2)
          extrema_points_3_1.append([y,x,(6.8)])
        Flag = True
      if Flag == False and find_max_new(o3,2,y,x) == 1:
        if (accurate_keypoint(I_bw[y,x],conv51[:,y,x],conv75[:,y,x],conv99[:,y,x], 0.5) == 1):
          #I[y,x] = (0,0,255)
          cv2.circle(I,(x,y), 7, (155,0,0), 2)
          extrema_points_3_2.append([y,x,(10.0)])

  dogn5 =  numpy.array(extrema_points_3_1)
  dogn6 = numpy.array(extrema_points_3_2)
  if (len(dogn5) == 0):
    dogn5 =[0,0,0]
  if (len(dogn6) == 0):
    dogn6 = [0,0,0]
  result2 = numpy.vstack([dogn5, dogn6])
  print ("Number of points in thrid octave: %d" % len(result2))
  alloctaves = numpy.vstack([result,result1,result2]) 
  #III = cv2.imread(filename)
  #coordin = (numpy.array([alloctaves[:,1], alloctaves[:,0]]).astype(int)).T
  #III[coordin] = (0,0,255)
  #cv2.imwrite(str(filename)[:-4] + "-surfdet" + ".jpg", III)
  cv2.imwrite("xx" + str(filename)[:-4] + ".jpg", I)
  h.points_to_txt_3_points(alloctaves, "surfallpoints.txt", "\n")
  print("pic written")
  return alloctaves 
Example #2
0
def SIFT(filename, r_mag):
  """
  Returns the interest points found
  """
  s = 3
  k = 2 ** (1.0 / s)
  I = cv2.imread(filename)
  I_bw = cv2.imread(filename, 0)
  I1 = misc.imresize(I_bw, 50, 'bilinear').astype(int)
  I2 = misc.imresize(I1, 50, 'bilinear').astype(int)
  I3 = misc.imresize(I2, 50, 'bilinear').astype(int)
  dim = I_bw.shape
  # Sigma value we defined, yields more responses than the one below;
  #sigma1 = [math.sqrt(0.5), math.sqrt(1), math.sqrt(2), math.sqrt(4),
           #math.sqrt(8), math.sqrt(16), math.sqrt(32), math.sqrt(64),
           #math.sqrt(128), math.sqrt(256), math.sqrt(512)]
  # Sigma values for FIRST SCALE ONLY proposed by Lowe, yields less responses:
  sigma1 = numpy.array([1.3, 1.6, 1.6 * k, 1.6 * (k ** 2), 1.6 * (k ** 3), 1.6 * (k ** 4)])

  print "creating gaussian pyramids.."
  o1sc = numpy.zeros((I_bw.shape[0],I_bw.shape[1],5))
  o2sc = numpy.zeros((I1.shape[0],I1.shape[1],5))
  o3sc = numpy.zeros((I2.shape[0],I2.shape[1],5))
  o4sc = numpy.zeros((I3.shape[0],I3.shape[1],5))

  for i in range(0,5):
    o1sc[:,:,i] = scipy.ndimage.filters.gaussian_filter(I_bw,sigma = sigma1[i])
    o2sc[:,:,i] = scipy.ndimage.filters.gaussian_filter(I1,sigma = sigma1[i]) #+2
    o3sc[:,:,i] = scipy.ndimage.filters.gaussian_filter(I2,sigma = sigma1[i]) #+4 
    o4sc[:,:,i] = scipy.ndimage.filters.gaussian_filter(I3,sigma = sigma1[i]) # +6
  # Calculate difference of gaussian images.
  print "creating difference of gaussian pyramids.."
  DoG_pictures_scale_1 = numpy.zeros((I_bw.shape[0],I_bw.shape[1],4))
  DoG_pictures_scale_2 = numpy.zeros((I1.shape[0],I1.shape[1],4))
  DoG_pictures_scale_3 = numpy.zeros((I2.shape[0],I2.shape[1],4))
  DoG_pictures_scale_4 = numpy.zeros((I3.shape[0],I3.shape[1],4))

  for i in range(0,4):
    # CT: TRY WITH HELPERFUNCTION MINUS
    DoG_pictures_scale_1[:,:,i] = o1sc[:,:,i+1] - o1sc[:,:,i]
    #DoG_pictures_scale_1[:,:,i] = h.matrix_substraction(o1sc[:,:,i+1],o1sc[:,:,i]) # ct
    DoG_pictures_scale_2[:,:,i] = o2sc[:,:,i+1] - o2sc[:,:,i]
    DoG_pictures_scale_3[:,:,i] = o3sc[:,:,i+1] - o3sc[:,:,i]
    DoG_pictures_scale_4[:,:,i] = o4sc[:,:,i+1] - o4sc[:,:,i]
  #print DoG_pictures_scale_1[:,:,1]
  #cv2.imshow('image',DoG_pictures_scale_1[:,:,0])
  #cv2.waitKey(0)
  
  # Initialize arrays for keypoints
  DoG_extrema_points_1_1 = []
  DoG_extrema_points_1_2 = []
  DoG_extrema_points_2 = []
  DoG_extrema_points_3 = []
  DoG_extrema_points_4 = []
   
  print("Finding points for scale 1")
  for y in range(3, I_bw.shape[0] - 3):
    for x in range(3, I_bw.shape[1] - 3):
      if (find_max_new(DoG_pictures_scale_1,1,y,x, r_mag) == 1):
        DoG_extrema_points_1_1.append([y,x,0])
      if (find_max_new(DoG_pictures_scale_1,2,y,x, r_mag) == 1):
        DoG_extrema_points_1_2.append([y,x,1])
  dogn1 =  numpy.array(DoG_extrema_points_1_1)
  dogn2 = numpy.array(DoG_extrema_points_1_2)
  if (len(dogn1) > 1) and (len(dogn2)>1):
    result = numpy.vstack([dogn1, dogn2])
    #print result
    print "Number of points in first octave: %d" % len(result)
    #h.points_to_txt(result, "interest_points_sc1.txt", "\n")
    h.points_to_txt_3_points(result, "interest_points_sc1.txt", "\n")
    h.color_pic(I, result, filename[:-4] + "-sift_sc1-"+ "r-" + str(r_mag) + ".jpg")
  
  # TODO add scales
  """
  print "Finding points for scale 2"
  for y in range(3, I1.shape[0] - 3):
    for x in range(3,  I1.shape[1] - 3):
      if (find_max_new(DoG_pictures_scale_2,y,x, r_mag) == 1):
        DoG_extrema_points_2.append([y,x,0])
  print len(DoG_extrema_points_2)
  dogn2 =  numpy.array(DoG_extrema_points_2)
  if (len(dogn2) > 1):
    result1 = numpy.vstack([dogn2])
    print "Number of points in second octave: %d" % len(result1)
    h.points_to_txt(result1, "interest_points_sc2.txt", "\n")
    h.color_pic(I1, result1, filename[:-4] + "-sift_sc2-"+ "r-" + str(r_mag) + ".jpg")

  print "Finding points for scale 3"
  for y in range(3, I2.shape[0] - 3):
    for x in range(3,  I2.shape[1] - 3):
      if (find_max_new(DoG_pictures_scale_3,y,x, r_mag) == 1):
        DoG_extrema_points_3.append([y,x,0])
  dogn3 =  numpy.array(DoG_extrema_points_3)
  if (len(dogn3) > 1):
    result2 = numpy.vstack([dogn3])
    print "LENGTH", len(result2)
    print "Number of points in third octave: %d" % len(result2)
    h.points_to_txt(result2, "interest_points_sc3.txt", "\n")
    h.color_pic(I2, result2, filename[:-4] + "-sift_sc3-"+ "r-" + str(r_mag) + ".jpg")  

  print "Finding points for scale 4"
  for y in range(3, I3.shape[0] - 3):
    for x in range(3,  I3.shape[1] - 3):
      if (find_max_new(DoG_pictures_scale_4,y,x, r_mag) == 1):
        DoG_extrema_points_4.append([y,x,0])
  dogn4 =  numpy.array(DoG_extrema_points_4)
  if (len(dogn4) > 1):
    result3 = numpy.vstack([dogn4])
    print "Number of points in fourth octave: %d" % len(result3)
    h.points_to_txt(result3, "interest_points_sc4.txt", "\n")
    h.color_pic(I3, result3, filename[:-4] + "-sift_sc4-"+ "r-" + str(r_mag) + ".jpg")
  """

  return(result)
Example #3
0
def findSurfPoints(filename):
  clear = " " * 50
  I_bw = cv2.imread(filename, 0).astype(float)
  I = cv2.imread(filename)
  sigma = 1.2

  gausspictures = numpy.zeros((I_bw.shape[0],I_bw.shape[1],8))
  
  for i in range (0,7):
    gausspictures[:,:,i] = cv2.filter2D(I_bw,-1, getGauss(1.2,i))

  deriv9 = numpy.zeros((I_bw.shape[0],I_bw.shape[1],5))
  deriv15 = numpy.zeros((I_bw.shape[0],I_bw.shape[1],5))
  deriv21 = numpy.zeros((I_bw.shape[0],I_bw.shape[1],5))
  deriv27 = numpy.zeros((I_bw.shape[0],I_bw.shape[1],5))

  for y in range (10, I_bw.shape[0]-10):
    for x in range (10, I_bw.shape[1]-10):
      deriv9[y,x,0] = gausspictures[y,x+1,0] + gausspictures[y,x-1,0] - 2 * gausspictures[y,x,0] # DXX
      deriv9[y,x,1] = gausspictures[y+1,x,0] + gausspictures[y-1,x,0] - 2 * gausspictures[y,x,0] # DYY
      deriv9[y,x,2] = gausspictures[y+1,x+1,0] - gausspictures[y+1,x-1,0] - gausspictures[y-1,x+1,0] + gausspictures[y-1,x-1,0] # DXY
      deriv9[y,x,3] = gausspictures[y,x+1,0] - gausspictures[y,x-1,0] # DX
      deriv9[y,x,4] = gausspictures[y+1,x,0] - gausspictures[y+1,x,0] # DY
      # TODO Also calculate ds
      
      deriv15[y,x,0] = gausspictures[y,x+1,1] + gausspictures[y,x-1,1] - 2 * gausspictures[y,x,1] # DXX
      deriv15[y,x,1] = gausspictures[y+1,x,1] + gausspictures[y-1,x,1] - 2 * gausspictures[y,x,1] # DYY
      deriv15[y,x,2] = gausspictures[y+1,x+1,1] - gausspictures[y+1,x-1,1] - gausspictures[y-1,x+1,1] + gausspictures[y-1,x-1,1] # DXY
      deriv15[y,x,3] = gausspictures[y,x+1,1] - gausspictures[y,x-1,1]
      deriv15[y,x,4] = gausspictures[y+1,x,1] - gausspictures[y+1,x,1]


      deriv21[y,x,0] = gausspictures[y,x+1,2] + gausspictures[y,x-1,2] - 2 * gausspictures[y,x,2] # DXX
      deriv21[y,x,1] = gausspictures[y+1,x,2] + gausspictures[y-1,x,2] - 2 * gausspictures[y,x,2] # DYY
      deriv21[y,x,2] = gausspictures[y+1,x+1,2] - gausspictures[y+1,x-1,2] - gausspictures[y-1,x+1,2] + gausspictures[y-1,x-1,2] # DXY
      deriv21[y,x,3] = gausspictures[y,x+1,2] - gausspictures[y,x-1,2]
      deriv21[y,x,4] = gausspictures[y+1,x,2] - gausspictures[y+1,x,2]

      deriv27[y,x,0] = gausspictures[y,x+1,3] + gausspictures[y,x-1,3] - 2 * gausspictures[y,x,3] # DXX
      deriv27[y,x,1] = gausspictures[y+1,x,3] + gausspictures[y-1,x,3] - 2 * gausspictures[y,x,3] # DYY
      deriv27[y,x,2] = gausspictures[y+1,x+1,3] - gausspictures[y+1,x-1,3] - gausspictures[y-1,x+1,3] + gausspictures[y-1,x-1,3] # DXY
      deriv27[y,x,3] = gausspictures[y,x+1,3] - gausspictures[y,x-1,3]
      deriv27[y,x,4] = gausspictures[y+1,x,3] - gausspictures[y+1,x,3]

  hessian9  = numpy.zeros((I_bw.shape[0],I_bw.shape[1]))
  hessian15 = numpy.zeros((I_bw.shape[0],I_bw.shape[1]))
  hessian21 = numpy.zeros((I_bw.shape[0],I_bw.shape[1]))
  hessian27 = numpy.zeros((I_bw.shape[0],I_bw.shape[1]))

  for y in range (10, I_bw.shape[0]-10):
    for x in range (10, I_bw.shape[1]-10):
      hessian9[y,x] = (deriv9[y,x,0] * deriv9[y,x,1]) - (0.9*deriv9[y,x,2])**2
      hessian15[y,x] = (deriv15[y,x,0] * deriv15[y,x,1]) - (0.9*deriv15[y,x,2])**2
      hessian21[y,x] = (deriv21[y,x,0] * deriv21[y,x,1]) - (0.9*deriv21[y,x,2])**2
      hessian27[y,x] = (deriv27[y,x,0] * deriv27[y,x,1]) - (0.9*deriv27[y,x,2])**2
  
  scale1hessian = numpy.zeros((I_bw.shape[0],I_bw.shape[1],4))
  scale1hessian[:,:,0] = hessian9
  scale1hessian[:,:,1] = hessian15
  scale1hessian[:,:,2] = hessian21
  scale1hessian[:,:,3] = hessian27


  extrema_points_1_1 = []
  extrema_points_1_2 = []
  
  for y in range(0,I_bw.shape[0]):
    for x in range(0,I_bw.shape[1]):
      Flag = False
      if find_max_new(scale1hessian,1,y,x) == 1 and (accurate_keypoint(deriv15[y,x,:]) == 1):
        extrema_points_1_1.append([y,x,(9/9*1.2)])
        Flag = True
      if Flag == False and find_max_new(scale1hessian,2,y,x) == 1  and (accurate_keypoint(deriv21[y,x,:]) == 1):
        extrema_points_1_2.append([y,x,(15/9*1.2)])
  dogn1 =  numpy.array(extrema_points_1_1)
  dogn2 = numpy.array(extrema_points_1_2)
  if (len(dogn1) > 1) and (len(dogn2)>1):
    result = numpy.vstack([dogn1, dogn2])
    print ("Number of points in first octave: %d" % len(result))
    h.points_to_txt_3_points(result, "SURF_interest_points_o1.txt", "\n")
    h.color_pic(I, result, filename[:-4] + "Surfo1" + ".jpg")