Skip to content

Commit

Permalink
Merge pull request #193 from jmichel-otb/refac_remove_sift_binary
Browse files Browse the repository at this point in the history
[refactor] Replace sift_roi binary by shared library and ctypes
  • Loading branch information
dyoussef authored Mar 13, 2019
2 parents 7b12beb + c9e8ec0 commit bb7d3f3
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 157 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Required dependencies (Ubuntu 16.04):

and

pip install utm bs4 lxml requests
pip install utm bs4 lxml requests rasterio

`gdal` version must be 2.1.0 or newer.

Expand Down
20 changes: 11 additions & 9 deletions c/sift/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
CXXFLAGS ?= -march=native -O3
override CXXFLAGS := $(CXXFLAGS) `gdal-config --cflags`
override CXXLIBS := $(CXXLIBS) `gdal-config --libs`
override CXXFLAGS := $(CXXFLAGS) -fPIC
override CXXLIBS := $(CXXLIBS)
override LDLIBS := $(LDLIBS) -lm

OBJ = sift_roi.o \
LibImages/LibImages.o \
OBJ = LibImages/LibImages.o \
LibSSE/LibSSE.o \
LibSift/KeyPoint.o \
LibSift/LibSift.o \
Expand All @@ -13,12 +12,15 @@ OBJ = sift_roi.o \
Utilities/Memory.o \
Utilities/Parameters.o \
Utilities/Time.o \
Utilities/Utilities.o \
Utilities/Utilities.o\
sift4ctypes.o \

all: sift_roi matching
LIB = libsift4ctypes.so

sift_roi: $(OBJ)
$(CXX) -o $@ $^ $(CXXLIBS)
all: $(LIB) matching

$(LIB): $(OBJ)
$(CXX) -fPIC -shared -o $@ $^

clean:
rm -f sift_roi matching $(OBJ)
rm -f $(LIB) matching $(OBJ)
80 changes: 80 additions & 0 deletions c/sift/sift4ctypes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (C) 2019, Julien Michel (CNES) <[email protected]>

#include <stdlib.h>

#include "Utilities/Parameters.h"
#include "LibImages/LibImages.h"
#include "LibSift/LibSift.h"

extern "C"{
/**
* This function is meant to be mapped to python using ctypes.
*
* It computes sifts points of input_buffer which is interpreted as a w x h image.
* Keypoints are returned as a linear buffer of float of size recordSize * nbRecords.
*
* This buffer is the responsibiliy of the caller and should be freed by her.
*/
float * sift(const float * input_buffer, const size_t w, const size_t h,
const float thresh_dog,
const unsigned int ss_noct,
const unsigned int ss_nspo,
unsigned int & recordSize,
unsigned int & nbRecords) {

// Derive Image from buffer
Image im(input_buffer, (const size_t) w, (const size_t) h, 1);

// prepare params object
Parameters params;
params.setDefaultValues();
params.set_thresh_dog(thresh_dog);
params.set_noct(ss_noct);
params.set_nspo(ss_nspo);

// run sift
Sift sift(params);
sift.computeKeyPoints(im);

// Compute the number of records
nbRecords = sift.m_keyPoints->size();

// Compute the record length
size_t descriptorSize = 0;
if(nbRecords > 0)
{
const KeyPoint * firstPoint = sift.m_keyPoints->front();
descriptorSize = firstPoint->getNbOri() * firstPoint->getNbHist() * firstPoint->getNbHist();
}
recordSize = descriptorSize + 4;

// Allocate output buffer
float * out = new float[recordSize*nbRecords];

// Fill output buffer with keypoints
std::list<KeyPoint*>::iterator key = sift.m_keyPoints->begin();
size_t currentPoint = 0;
for(;key != sift.m_keyPoints->end();++key,++currentPoint)
{
size_t currentIndex = recordSize*currentPoint;
out[currentIndex] = (*key)->getY();
out[currentIndex+1] = (*key)->getX();
out[currentIndex+2] = (*key)->getSigma();
out[currentIndex+3] = (*key)->getTheta();
for(unsigned int i = 0; i < descriptorSize;++i)
{
out[currentIndex+4+i] = (*key)->getPtrDescr()[i];
}
}
return out;
}
/**
* This function allows to free the float buffer from python.
*/
void delete_buffer(float * buffer)
{
if (buffer != NULL)
delete [] buffer;
}

}
120 changes: 0 additions & 120 deletions c/sift/sift_roi.cpp

This file was deleted.

12 changes: 7 additions & 5 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ endif
# names of source and destination directories
SRCDIR = c
BINDIR = bin
LIBDIR = lib

# default rule builds only the programs necessary for the test
default: $(BINDIR) homography sift imscript mgm mgm_multi tvl1 lsd
default: $(BINDIR) $(LIBDIR) homography sift imscript mgm mgm_multi tvl1 lsd

# the "all" rule builds four further correlators
all: default msmw3 sgbm mgm_multi
Expand All @@ -35,6 +36,8 @@ test: default
# make sure that the destination directory is built
$(BINDIR):
mkdir -p $(BINDIR)
$(LIBDIR):
mkdir -p $(LIBDIR)

#
# four standard "modules": homography, sift, mgm, and mgm_multi
Expand All @@ -46,9 +49,8 @@ homography: $(BINDIR)

sift: $(BINDIR)
$(MAKE) -j -C c/sift
cp c/sift/sift_roi $(BINDIR)
cp c/sift/matching $(BINDIR)

cp c/sift/libsift4ctypes.so $(LIBDIR)
cp c/sift/matching ${BINDIR}
mgm:
$(MAKE) -C 3rdparty/mgm
#cp 3rdparty/mgm/mgm $(BINDIR)
Expand Down Expand Up @@ -188,7 +190,7 @@ clean_homography:

clean_sift:
$(MAKE) -C c/sift clean
$(RM) $(BINDIR)/sift_roi
$(RM) $(LIBDIR)/libsift4ctypes.so
$(RM) $(BINDIR)/matching

clean_asift:
Expand Down
12 changes: 6 additions & 6 deletions s2p_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ def unit_image_keypoints():
for i in range(test_kpts.shape[0]):
found = False
for j in range(ref_kpts.shape[0]):
dist = np.linalg.norm(test_kpts[i,0:1]-ref_kpts[j,0:1])
dist = np.linalg.norm(test_kpts[i,0:2]-ref_kpts[j,0:2])
if dist<dist_tol:
found = True
if not found:
print("KeyPoint not found: "+str((test_kpts[i,0:1])))
print("KeyPoint not found: "+str((test_kpts[i,0:2])))
nb_test_not_in_ref+=1

print(str(nb_test_not_in_ref)+" test kpts have no spatially close match in ref")

nb_ref_not_in_test = 0
for i in range(test_kpts.shape[0]):
for i in range(ref_kpts.shape[0]):
found = False
for j in range(ref_kpts.shape[0]):
dist = np.linalg.norm(test_kpts[i,0:1]-ref_kpts[j,0:1])
for j in range(test_kpts.shape[0]):
dist = np.linalg.norm(ref_kpts[i,0:2]-test_kpts[j,0:2])
if dist<dist_tol:
found = True
if not found:
print("KeyPoint not found: "+str((test_kpts[i,0:1])))
print("KeyPoint not found: "+str((test_kpts[i,0:2])))
nb_ref_not_in_test+=1

print(str(nb_ref_not_in_test)+" ref kpts have no spatially close match in test")
Expand Down
Loading

0 comments on commit bb7d3f3

Please sign in to comment.