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

Add BeblidDescriptorExtractor and TeblidDescriptorExtractor to xfeatures2d #1143

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions contrib/xfeatures2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,49 @@ 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);
}

TeblidDescriptorExtractor TeblidDescriptorExtractor_Create(float scaleFactor, int size) {
return new cv::Ptr<cv::xfeatures2d::TEBLID>(cv::xfeatures2d::TEBLID::create(scaleFactor, size));
}

void TeblidDescriptorExtractor_Close(TeblidDescriptorExtractor b) {
delete b;
}

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

// BeblidDescriptorExtractor is a wrapper around the cv::xfeatures2d::BEBLID 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/4.9.0/d7/d99/classcv_1_1xfeatures2d_1_1BEBLID.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
}

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

type TeblidDescriptorExtractorSize = int

const (
TEBLID_SIZE_256_BITS TeblidDescriptorExtractorSize = 102
TEBLID_SIZE_512_BITS TeblidDescriptorExtractorSize = 103
)

// NewTeblidDescriptorExtractor returns a new TEBLID descriptor algorithm.
//
// For further details, please see:
// https://docs.opencv.org/4.x/dd/dc1/classcv_1_1xfeatures2d_1_1TEBLID.html
func NewTeblidDescriptorExtractor(scaleFactor float32, size TeblidDescriptorExtractorSize) TeblidDescriptorExtractor {
return TeblidDescriptorExtractor{p: unsafe.Pointer(C.TeblidDescriptorExtractor_Create(C.float(scaleFactor), C.int(size)))}
}

// Close TEBLID.
func (d *TeblidDescriptorExtractor) Close() error {
C.TeblidDescriptorExtractor_Close((C.TeblidDescriptorExtractor)(d.p))
d.p = nil
return nil
}

// Detect describes keypoints in an image using TEBLID
//
// For further details, please see:
// https://docs.opencv.org/4.x/dd/dc1/classcv_1_1xfeatures2d_1_1TEBLID.html
func (b *TeblidDescriptorExtractor) 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.TeblidDescriptorExtractor_Compute((C.TeblidDescriptorExtractor)(b.p), C.Mat(src.Ptr()), cKeyPoints, C.Mat(desc.Ptr()))
return desc
}
12 changes: 12 additions & 0 deletions contrib/xfeatures2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@ extern "C" {

#ifdef __cplusplus
typedef cv::Ptr<cv::xfeatures2d::SURF>* SURF;
typedef cv::Ptr<cv::xfeatures2d::BEBLID>* BeblidDescriptorExtractor;
typedef cv::Ptr<cv::xfeatures2d::TEBLID>* TeblidDescriptorExtractor;
#else
typedef void* SURF;
typedef void* BeblidDescriptorExtractor;
typedef void* TeblidDescriptorExtractor;
#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);

TeblidDescriptorExtractor TeblidDescriptorExtractor_Create(float scaleFactor, int size);
void TeblidDescriptorExtractor_Close(TeblidDescriptorExtractor b);
void TeblidDescriptorExtractor_Compute(TeblidDescriptorExtractor b, Mat src, struct KeyPoints kp, Mat desc);

#ifdef __cplusplus
}
#endif
Expand Down
60 changes: 60 additions & 0 deletions contrib/xfeatures2d_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,63 @@ 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")
}
}

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

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

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

b := NewTeblidDescriptorExtractor(1.00, TEBLID_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 TeblidDescriptorExtractor Compute")
}
}
Loading