-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhook_opencv.hpp
118 lines (97 loc) · 3.34 KB
/
hook_opencv.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once
#include <cstdio>
#include <stdexcept>
#include <dlfcn.h>
#include <opencv2/core/core_c.h>
#include <opencv2/imgproc/imgproc_c.h>
struct OpencvDLFCNSingleton{
using THIS_CLASS = OpencvDLFCNSingleton;
private:
OpencvDLFCNSingleton(){
libopencv_core_handle = dlopen("libopencv_core.so", RTLD_LOCAL|RTLD_LAZY);
if(libopencv_core_handle == nullptr){
throw std::runtime_error("Failed to open library libopencv_core.so");
}
libopencv_imgproc_handle = dlopen("libopencv_imgproc.so", RTLD_LOCAL|RTLD_LAZY);
if(libopencv_imgproc_handle == nullptr){
throw std::runtime_error("Failed to open library libopencv_imgproc.so");
}
}
public:
inline static OpencvDLFCNSingleton& getSingleton(){
static OpencvDLFCNSingleton singleton;
return singleton;
}
~OpencvDLFCNSingleton(){
if (libopencv_core_handle != nullptr){
dlclose(libopencv_core_handle);
}
if (libopencv_imgproc_handle != nullptr){
dlclose(libopencv_imgproc_handle);
}
}
static inline CvMat* cvInitMatHeader(
CvMat* mat, int rows, int cols,
int type, void* data = NULL,
int step = CV_AUTOSTEP
){
using FType = CvMat*(CvMat*, int, int, int, void*, int);
auto& singleton = getSingleton();
static auto func = (FType*)dlsym(singleton.libopencv_core_handle, "cvInitMatHeader");
return func(mat, rows, cols, type, data, step);
}
static inline CvMat* cvCreateMat(
int rows, int cols, int type
){
using FType = CvMat*(int, int, int);
auto& singleton = getSingleton();
static auto func = (FType*)dlsym(singleton.libopencv_core_handle, "cvCreateMat");
return func(rows, cols, type);
}
static inline void cvReleaseMat(CvMat** mat){
using FType = void(CvMat**);
auto& singleton = getSingleton();
static auto func = (FType*)dlsym(singleton.libopencv_core_handle, "cvReleaseMat");
return func(mat);
}
static inline CvMat* cvGetSubRect(
const CvArr* arr, CvMat* submat, CvRect rect
){
using FType = CvMat*(const CvArr*, CvMat*, CvRect);
auto& singleton = getSingleton();
static auto func = (FType*)dlsym(singleton.libopencv_core_handle, "cvGetSubRect");
return func(arr, submat, rect);
}
static inline void cvResize(
const CvArr* src, CvArr* dst,
int interpolation
){
using FType = void(const CvArr*, CvArr*, int);
auto& singleton = getSingleton();
static auto func = (FType*)dlsym(singleton.libopencv_imgproc_handle, "cvResize");
return func(src, dst, interpolation);
}
static inline void cvSetZero(CvArr* arr){
using FType = void(CvArr*);
auto& singleton = getSingleton();
static auto func = (FType*)dlsym(singleton.libopencv_core_handle, "cvSetZero");
return func(arr);
}
static inline void cvCopy(const CvArr* src, CvArr* dst){
using FType = void(const CvArr*, CvArr*);
auto& singleton = getSingleton();
static auto func = (FType*)dlsym(singleton.libopencv_core_handle, "cvCopy");
return func(src, dst);
}
static inline void cvCvtColor(
const CvArr* src, CvArr* dst,
int code
){
using FType = void(const CvArr*, CvArr*, int);
auto& singleton = getSingleton();
static auto func = (FType*)dlsym(singleton.libopencv_imgproc_handle, "cvCvtColor");
return func(src, dst, code);
}
void* libopencv_core_handle{nullptr};
void* libopencv_imgproc_handle{nullptr};
};