Skip to content

Commit

Permalink
add beblid descriptor extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
John Brandt committed Jan 14, 2024
1 parent e2db8f8 commit 95d643c
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
23 changes: 23 additions & 0 deletions contrib/xfeatures2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,26 @@ struct KeyPoints SURF_DetectAndCompute(SURF d, Mat src, Mat mask, Mat desc) {
KeyPoints ret = {kps, (int)detected.size()};
return ret;
}

BeblidDescriptorExtractor BeblidDescriptorExtractor_Create(float scaleFactor, int size) {
return new cv::Ptr<cv::xfeatures2d::BEBLID>(cv::xfeatures2d::BEBLID::create(scaleFactor, size));
}

void BeblidDescriptorExtractor_Close(BeblidDescriptorExtractor b) {
delete b;
}

void BeblidDescriptorExtractor_Compute(BeblidDescriptorExtractor 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);
}
55 changes: 55 additions & 0 deletions contrib/xfeatures2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,58 @@ func getKeyPoints(ret C.KeyPoints) []gocv.KeyPoint {
}
return keys
}

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

type BeblidDescriptorExtractorSize = int

const (
BEBLID_SIZE_256_BITS BeblidDescriptorExtractorSize = 101
BEBLID_SIZE_512_BITS BeblidDescriptorExtractorSize = 100
)

// NewBeblidDescriptorExtractor returns a new BEBLID descriptor algorithm.
//
// For further details, please see:
// https://docs.opencv.org/master/d5/df7/classcv_1_1xfeatures2d_1_1SURF.html
func NewBeblidDescriptorExtractor(scaleFactor float32, size BeblidDescriptorExtractorSize) BeblidDescriptorExtractor {
return BeblidDescriptorExtractor{p: unsafe.Pointer(C.BeblidDescriptorExtractor_Create(C.float(scaleFactor), C.int(size)))}
}

// Close BEBLID.
func (d *BeblidDescriptorExtractor) Close() error {
C.BeblidDescriptorExtractor_Close((C.BeblidDescriptorExtractor)(d.p))
d.p = nil
return nil
}

// Detect describes keypoints in an image using BEBLID
//
// For further details, please see:
// https://docs.opencv.org/4.9.0/d7/d99/classcv_1_1xfeatures2d_1_1BEBLID.html
func (b *BeblidDescriptorExtractor) 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.BeblidDescriptorExtractor_Compute((C.BeblidDescriptorExtractor)(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::BEBLID>* BeblidDescriptorExtractor;
#else
typedef void* SURF;
typedef void* BeblidDescriptorExtractor;
#endif

SURF SURF_Create();
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);

BeblidDescriptorExtractor BeblidDescriptorExtractor_Create(float scaleFactor, int size);
void BeblidDescriptorExtractor_Close(BeblidDescriptorExtractor b);
void BeblidDescriptorExtractor_Compute(BeblidDescriptorExtractor b, Mat src, struct KeyPoints kp, Mat desc);

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

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

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

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

b := NewBeblidDescriptorExtractor(1.00, BEBLID_SIZE_512_BITS)
defer b.Close()

kp := fast.Detect(img)

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

desc := b.Compute(kp, img)

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

0 comments on commit 95d643c

Please sign in to comment.