Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIFT custom #1186

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions features2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,11 @@ SIFT SIFT_Create() {
return new cv::Ptr<cv::SIFT>(cv::SIFT::create());
}

SIFT SIFT_CreateWithParams(int nfeatures, int nOctaveLayers, double contrastThreshold, double edgeThreshold, double sigma) {
return new cv::Ptr<cv::SIFT>(cv::SIFT::create(nfeatures, nOctaveLayers, contrastThreshold, edgeThreshold, sigma));
}


void SIFT_Close(SIFT d) {
delete d;
}
Expand Down
33 changes: 33 additions & 0 deletions features2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,39 @@ func NewSIFT() SIFT {
return SIFT{p: unsafe.Pointer(C.SIFT_Create())}
}

func NewSIFTWithParams(nfeatures *int, nOctaveLayers *int, contrastThreshold *float64, edgeThreshold *float64, sigma *float64) SIFT {
numFeatures := 0
if nfeatures != nil {
numFeatures = *nfeatures
}

numOctaveLayers := 3

if nOctaveLayers != nil {
numOctaveLayers = *nOctaveLayers
}

var numContrastThreshold float64 = 0.04

if contrastThreshold != nil {
numContrastThreshold = *contrastThreshold
}

var numEdgeThreshold float64 = 10

if edgeThreshold != nil {
numEdgeThreshold = *edgeThreshold
}

var numSigma float64 = 1.6

if sigma != nil {
numSigma = *sigma
}

return SIFT{p: unsafe.Pointer(C.SIFT_CreateWithParams(C.int(numFeatures), C.int(numOctaveLayers), C.double(numContrastThreshold), C.double(numEdgeThreshold), C.double(numSigma)))}
}

// Close SIFT.
func (d *SIFT) Close() error {
C.SIFT_Close((C.SIFT)(d.p))
Expand Down
1 change: 1 addition & 0 deletions features2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ struct MultiDMatches FlannBasedMatcher_KnnMatch(FlannBasedMatcher f, Mat query,
void DrawKeyPoints(Mat src, struct KeyPoints kp, Mat dst, const Scalar s, int flags);

SIFT SIFT_Create();
SIFT SIFT_CreateWithParams(int nfeatures, int nOctaveLayers, double contrastThreshold, double edgeThreshold, double sigma);
void SIFT_Close(SIFT f);
struct KeyPoints SIFT_Detect(SIFT f, Mat src);
struct KeyPoints SIFT_Compute(SIFT f, Mat src, struct KeyPoints kp, Mat desc);
Expand Down
45 changes: 45 additions & 0 deletions features2d_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,48 @@ func TestSIFT(t *testing.T) {
t.Error("Invalid Mat desc in SIFT DetectAndCompute")
}
}

func TestSIFTWithParams(t *testing.T) {
img := IMRead("./images/face.jpg", IMReadGrayScale)
if img.Empty() {
t.Error("Invalid Mat in SIFT test")
}
defer img.Close()

dst := NewMat()
defer dst.Close()

nFeatures := 256
nOctaveLayers := 3
contrastThreshold := 0.039
var edgeThreshold float64 = 11
sigma := 1.55
si := NewSIFTWithParams(&nFeatures, &nOctaveLayers, &contrastThreshold, &edgeThreshold, &sigma)
defer si.Close()

kp := si.Detect(img)
if len(kp) != 256 {
t.Errorf("Invalid KeyPoint array in SIFT test: %d", len(kp))
}

mask := NewMat()
defer mask.Close()

kpc, desc := si.Compute(img, mask, kp)
defer desc.Close()
if len(kpc) != 256 {
t.Errorf("Invalid KeyPoint array in SIFT Compute: %d", len(kpc))
}
if desc.Empty() {
t.Error("Invalid Mat desc in SIFT Compute")
}

kpdc, desc2 := si.DetectAndCompute(img, mask)
defer desc2.Close()
if len(kpdc) != 256 {
t.Errorf("Invalid KeyPoint array in SIFT DetectAndCompute: %d", len(kpdc))
}
if desc2.Empty() {
t.Error("Invalid Mat desc in SIFT DetectAndCompute")
}
}
Loading