Skip to content

Commit

Permalink
Added Python interface
Browse files Browse the repository at this point in the history
  • Loading branch information
BAILOOL committed Feb 23, 2018
1 parent ecfd531 commit 4d0ac8c
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 16 deletions.
16 changes: 8 additions & 8 deletions CmakeProject/source/anms.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ vector<cv::KeyPoint> Sdc(vector<cv::KeyPoint> keyPoints, int numRetPoints, float
else if (result.size()<Kmin) high = radius-1; //update binary search range
else low = radius+1;
}
// retrieve final keypoints
vector<cv::KeyPoint> kp;
for (unsigned int i = 0; i<ResultVec.size(); i++) kp.push_back(keyPoints[ResultVec[i]]);
// retrieve final keypoints
vector<cv::KeyPoint> kp;
for (unsigned int i = 0; i<ResultVec.size(); i++) kp.push_back(keyPoints[ResultVec[i]]);

return kp;
return kp;
}


Expand Down Expand Up @@ -347,11 +347,11 @@ vector<cv::KeyPoint> Ssc(vector<cv::KeyPoint> keyPoints, int numRetPoints,float
else low = width+1;
prevWidth = width;
}
// retrieve final keypoints
vector<cv::KeyPoint> kp;
for (unsigned int i = 0; i<ResultVec.size(); i++) kp.push_back(keyPoints[ResultVec[i]]);
// retrieve final keypoints
vector<cv::KeyPoint> kp;
for (unsigned int i = 0; i<ResultVec.size(); i++) kp.push_back(keyPoints[ResultVec[i]]);

return kp;
return kp;
}


Expand Down
5 changes: 5 additions & 0 deletions Python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Python

To run this code set parameters in run_demo.sh and execute `./run_demo.sh` from your terminal.

We have tested with Python 2.7.6 and OpenCV 2.4.8
Empty file added Python/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions Python/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python

import argparse
import cv2
import numpy
from random import shuffle
from ssc import *

def main():
parser = argparse.ArgumentParser()
parser.add_argument('--image_path', type=str, default="../Images/test.png")
parser.add_argument('--num_ret_points', type=int, default=10)
parser.add_argument('--tolerance', type=float, default=0.1)
args = parser.parse_args()

img = cv2.imread(args.image_path)
cv2.imshow('Input Image', img)
cv2.waitKey(0)

fast = cv2.FastFeatureDetector()
keypoints = fast.detect(img, None)
img2 = cv2.drawKeypoints(img, keypoints, color=(255,0,0))
cv2.imshow('Detected FAST keypoints', img2)
cv2.waitKey(0)

# keypoints should be sorted by strength in descending order before feeding to SSC to work correctly
shuffle(keypoints) # simulating sorting by score with random shuffle

selected_keypoints = SSC(keypoints, args.num_ret_points, args.tolerance, img.shape[1], img.shape[0])

img3 = cv2.drawKeypoints(img, selected_keypoints, color=(255,0,0))
cv2.imshow('Selected keypoints', img3)
cv2.waitKey(0)

if __name__ == '__main__':
main()
4 changes: 4 additions & 0 deletions Python/run_demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python demo.py \
--image_path='../Images/test.png' \
--num_ret_points=750 \
--tolerance=0.1 \
62 changes: 62 additions & 0 deletions Python/ssc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import math

def SSC(keypoints, num_ret_points, tolerance, cols, rows):
exp1 = rows + cols + 2*num_ret_points
exp2 = 4*cols + 4*num_ret_points + 4*rows*num_ret_points + rows*rows + cols*cols - 2*rows*cols + 4*rows*cols*num_ret_points
exp3 = math.sqrt(exp2)
exp4 = (2*(num_ret_points - 1))

sol1 = -round(float(exp1+exp3)/exp4) # first solution
sol2 = -round(float(exp1-exp3)/exp4) # second solution

high = sol1 if (sol1>sol2) else sol2 #binary search range initialization with positive solution
low = math.floor(math.sqrt(len(keypoints)/num_ret_points))

prevWidth = -1
selected_keypoints = []
ResultVec = []
result = []
complete = False
K = num_ret_points
Kmin = round(K-(K*tolerance))
Kmax = round(K+(K*tolerance))

while(~complete):
width = low+(high-low)/2
if (width == prevWidth or low>high): #needed to reassure the same radius is not repeated again
ResultVec = result #return the keypoints from the previous iteration
break

c = width/2; #initializing Grid
numCellCols = int(math.floor(cols/c));
numCellRows = int(math.floor(rows/c));
coveredVec = [ [False for i in range(numCellCols+1)] for j in range(numCellCols+1)]
result = []

for i in range(len(keypoints)):
row = int(math.floor(keypoints[i].pt[1]/c)) #get position of the cell current point is located at
col = int(math.floor(keypoints[i].pt[0]/c))
if (coveredVec[row][col]==False): # if the cell is not covered
result.append(i)
rowMin = int((row-math.floor(width/c)) if ((row-math.floor(width/c))>=0) else 0) #get range which current radius is covering
rowMax = int((row+math.floor(width/c)) if ((row+math.floor(width/c))<=numCellRows) else numCellRows)
colMin = int((col-math.floor(width/c)) if ((col-math.floor(width/c))>=0) else 0)
colMax = int((col+math.floor(width/c)) if ((col+math.floor(width/c))<=numCellCols) else numCellCols)
for rowToCov in range(rowMin, rowMax+1):
for colToCov in range(colMin, colMax+1):
if (~coveredVec[rowToCov][colToCov]):
coveredVec[rowToCov][colToCov] = True #cover cells within the square bounding box with width w

if (len(result)>=Kmin and len(result)<=Kmax): #solution found
ResultVec = result
complete = True
elif (len(result)<Kmin):
high = width-1 #update binary search range
else:
low = width+1
prevWidth = width

for i in range(len(ResultVec)):
selected_keypoints.append(keypoints[ResultVec[i]])

return selected_keypoints
16 changes: 8 additions & 8 deletions QtProject/anms.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ vector<cv::KeyPoint> Sdc(vector<cv::KeyPoint> keyPoints, int numRetPoints, float
else if (result.size()<Kmin) high = radius-1; //update binary search range
else low = radius+1;
}
// retrieve final keypoints
vector<cv::KeyPoint> kp;
for (unsigned int i = 0; i<ResultVec.size(); i++) kp.push_back(keyPoints[ResultVec[i]]);
// retrieve final keypoints
vector<cv::KeyPoint> kp;
for (unsigned int i = 0; i<ResultVec.size(); i++) kp.push_back(keyPoints[ResultVec[i]]);

return kp;
return kp;
}


Expand Down Expand Up @@ -348,11 +348,11 @@ vector<cv::KeyPoint> Ssc(vector<cv::KeyPoint> keyPoints, int numRetPoints,float
else low = width+1;
prevWidth = width;
}
// retrieve final keypoints
vector<cv::KeyPoint> kp;
for (unsigned int i = 0; i<ResultVec.size(); i++) kp.push_back(keyPoints[ResultVec[i]]);
// retrieve final keypoints
vector<cv::KeyPoint> kp;
for (unsigned int i = 0; i<ResultVec.size(); i++) kp.push_back(keyPoints[ResultVec[i]]);

return kp;
return kp;
}


Expand Down

0 comments on commit 4d0ac8c

Please sign in to comment.