diff --git a/features2d.cpp b/features2d.cpp index e27bd1d3..e2a43b01 100644 --- a/features2d.cpp +++ b/features2d.cpp @@ -590,6 +590,11 @@ SIFT SIFT_Create() { return new cv::Ptr(cv::SIFT::create()); } +SIFT SIFT_CreateWithParams(int nfeatures, int nOctaveLayers, double contrastThreshold, double edgeThreshold, double sigma) { + return new cv::Ptr(cv::SIFT::create(nfeatures, nOctaveLayers, contrastThreshold, edgeThreshold, sigma)); +} + + void SIFT_Close(SIFT d) { delete d; } diff --git a/features2d.go b/features2d.go index 02bb67ad..3140a1bc 100644 --- a/features2d.go +++ b/features2d.go @@ -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)) diff --git a/features2d.h b/features2d.h index c941f505..02fd6107 100644 --- a/features2d.h +++ b/features2d.h @@ -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); diff --git a/features2d_test.go b/features2d_test.go index 9b07e9fe..2ee5ff2c 100644 --- a/features2d_test.go +++ b/features2d_test.go @@ -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") + } +}