Skip to content

Commit

Permalink
Add BriefDescriptorExtractor to xfeatures2d (#1114)
Browse files Browse the repository at this point in the history
xfeatures2d: add briefdescriptorextractor to xfeatures2d
  • Loading branch information
Cartermel authored Oct 17, 2023
1 parent 532682d commit 038c158
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
27 changes: 27 additions & 0 deletions contrib/xfeatures2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,30 @@ struct KeyPoints SURF_DetectAndCompute(SURF d, Mat src, Mat mask, Mat desc) {
KeyPoints ret = {kps, (int)detected.size()};
return ret;
}

BriefDescriptorExtractor BriefDescriptorExtractor_Create() {
return new cv::Ptr<cv::xfeatures2d::BriefDescriptorExtractor>(cv::xfeatures2d::BriefDescriptorExtractor::create());
}

BriefDescriptorExtractor BriefDescriptorExtractor_CreateWithParams(int bytes, bool useOrientation) {
return new cv::Ptr<cv::xfeatures2d::BriefDescriptorExtractor>(cv::xfeatures2d::BriefDescriptorExtractor::create(bytes, useOrientation));
}

void BriefDescriptorExtractor_Close(BriefDescriptorExtractor b) {
delete b;
}

void BriefDescriptorExtractor_Compute(BriefDescriptorExtractor b, Mat src, struct KeyPoints kp, Mat desc) {
std::vector<cv::KeyPoint> keypts;
keypts.reserve(kp.length);
cv::KeyPoint keypt;

for (int i = 0; i < kp.length; ++i) {
keypt = cv::KeyPoint(kp.keypoints[i].x, kp.keypoints[i].y,
kp.keypoints[i].size, kp.keypoints[i].angle, kp.keypoints[i].response,
kp.keypoints[i].octave, kp.keypoints[i].classID);
keypts.push_back(keypt);
}

(*b)->compute(*src, keypts, *desc);
}
56 changes: 56 additions & 0 deletions contrib/xfeatures2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,59 @@ func getKeyPoints(ret C.KeyPoints) []gocv.KeyPoint {
}
return keys
}

// BriefDescriptorExtractor is a wrapper around the cv::BriefDescriptorExtractor algorithm.
type BriefDescriptorExtractor struct {
// C.BriefDescriptorExtractor
p unsafe.Pointer
}

// NewBriefDescriptorExtractor returns a new BriefDescriptorExtractor algorithm.
//
// For further details, please see:
// https://docs.opencv.org/master/d1/d93/classcv_1_1xfeatures2d_1_1BriefDescriptorExtractor.html
func NewBriefDescriptorExtractor() BriefDescriptorExtractor {
return BriefDescriptorExtractor{p: unsafe.Pointer(C.BriefDescriptorExtractor_Create())}
}

// NewBriefDescriptorExtractorWithParams returns a new BriefDescriptorExtractor algorithm algorithm with parameters
//
// For further details, please see:
// https://docs.opencv.org/master/d1/d93/classcv_1_1xfeatures2d_1_1BriefDescriptorExtractor.html#ae3bc52666010fb137ab6f0d32de51f60
func NewBriefDescriptorExtractorWithParams(bytes int, useOrientation bool) BriefDescriptorExtractor {
return BriefDescriptorExtractor{p: unsafe.Pointer(C.BriefDescriptorExtractor_CreateWithParams(C.int(bytes), C.bool(useOrientation)))}
}

// Close BriefDescriptorExtractor.
func (d *BriefDescriptorExtractor) Close() error {
C.BriefDescriptorExtractor_Close((C.BriefDescriptorExtractor)(d.p))
d.p = nil
return nil
}

// Compute descriptors with given keypoints using BriefDescriptorExtractor
//
// For further details, please see:
// https://docs.opencv.org/master/d0/d13/classcv_1_1Feature2D.html#ab3cce8d56f4fc5e1d530b5931e1e8dc0
func (b *BriefDescriptorExtractor) Compute(keyPoints []gocv.KeyPoint, src gocv.Mat) gocv.Mat {
desc := gocv.NewMat()
cKeyPointArray := make([]C.struct_KeyPoint, len(keyPoints))

for i, kp := range keyPoints {
cKeyPointArray[i].x = C.double(kp.X)
cKeyPointArray[i].y = C.double(kp.Y)
cKeyPointArray[i].size = C.double(kp.Size)
cKeyPointArray[i].angle = C.double(kp.Angle)
cKeyPointArray[i].response = C.double(kp.Response)
cKeyPointArray[i].octave = C.int(kp.Octave)
cKeyPointArray[i].classID = C.int(kp.ClassID)
}

cKeyPoints := C.struct_KeyPoints{
keypoints: (*C.struct_KeyPoint)(&cKeyPointArray[0]),
length: (C.int)(len(keyPoints)),
}

C.BriefDescriptorExtractor_Compute((C.BriefDescriptorExtractor)(b.p), C.Mat(src.Ptr()), cKeyPoints, C.Mat(desc.Ptr()))
return desc
}
6 changes: 6 additions & 0 deletions contrib/xfeatures2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ extern "C" {

#ifdef __cplusplus
typedef cv::Ptr<cv::xfeatures2d::SURF>* SURF;
typedef cv::Ptr<cv::xfeatures2d::BriefDescriptorExtractor>* BriefDescriptorExtractor;
#else
typedef void* SURF;
typedef void* BriefDescriptorExtractor;
#endif

SURF SURF_Create();
SURF SURF_CreateWithParams(double hessianThreshold, int nOctaves, int nOctaveLayers, bool extended, bool upright);
void SURF_Close(SURF f);
struct KeyPoints SURF_Detect(SURF f, Mat src);
struct KeyPoints SURF_DetectAndCompute(SURF f, Mat src, Mat mask, Mat desc);
BriefDescriptorExtractor BriefDescriptorExtractor_Create();
BriefDescriptorExtractor BriefDescriptorExtractor_CreateWithParams(int bytes, bool useOrientation);
void BriefDescriptorExtractor_Close(BriefDescriptorExtractor b);
void BriefDescriptorExtractor_Compute(BriefDescriptorExtractor b, Mat src, struct KeyPoints kp, Mat desc);

#ifdef __cplusplus
}
Expand Down
54 changes: 54 additions & 0 deletions contrib/xfeatures2d_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,57 @@ func TestSURFWithParams(t *testing.T) {
t.Error("Invalid Mat desc in SURF DetectAndCompute")
}
}

func TestBriefDescriptorExtractor(t *testing.T) {
testNonFree := os.Getenv("OPENCV_ENABLE_NONFREE")
if testNonFree == "" {
t.Skip("Skipping BriefDescriptorExtractor test since OPENCV_ENABLE_NONFREE was not set")
}

img := gocv.IMRead("../images/face.jpg", gocv.IMReadGrayScale)
if img.Empty() {
t.Error("Invalid Mat in BriefDescriptorExtractor test")
}
defer img.Close()

fast := gocv.NewFastFeatureDetector()
defer fast.Close()

b := NewBriefDescriptorExtractor()
defer b.Close()

kp := fast.Detect(img)

desc := b.Compute(kp, img)

if desc.Empty() {
t.Error("Invalid Mat desc in BriefDescriptorExtractor Compute")
}
}

func TestBriefDescriptorExtractorWithParams(t *testing.T) {
testNonFree := os.Getenv("OPENCV_ENABLE_NONFREE")
if testNonFree == "" {
t.Skip("Skipping BriefDescriptorExtractorWithParams test since OPENCV_ENABLE_NONFREE was not set")
}

img := gocv.IMRead("../images/face.jpg", gocv.IMReadGrayScale)
if img.Empty() {
t.Error("Invalid Mat in BriefDescriptorExtractorWithParams test")
}
defer img.Close()

fast := gocv.NewFastFeatureDetector()
defer fast.Close()

b := NewBriefDescriptorExtractorWithParams(32, false)
defer b.Close()

kp := fast.Detect(img)

desc := b.Compute(kp, img)

if desc.Empty() {
t.Error("Invalid Mat desc in BriefDescriptorExtractorWithParams Compute")
}
}

0 comments on commit 038c158

Please sign in to comment.