-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenCV.h
218 lines (172 loc) · 6.4 KB
/
OpenCV.h
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//vinjn's wrapper for OpenCV
#pragma once
#pragma warning( disable: 4244 )
#pragma warning( disable: 4996 )
#pragma warning( disable: 4305 )
#pragma warning( disable: 4018 )
#pragma warning( disable: 4099 )
#pragma warning( disable: 4819 )
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <vector>
#include "point2d.h"
#ifdef VIDEOINPUT_LIB
class videoInput;
#endif
#ifdef KINECT
class ofxKinectCLNUI;
#endif
using std::vector;
// enum T_VideoCodec
// {
// T_MPEG = CV_FOURCC('P','I','M','1'), // = MPEG-1 codec
// T_MJPG = CV_FOURCC('M','J','P','G') , // = motion-jpeg codec (does not work well)
// T_MP42 = CV_FOURCC('M', 'P', '4', '2'), // = MPEG-4.2 codec
// T_DIV3 = CV_FOURCC('D', 'I', 'V', '3'), // = MPEG-4.3 codec
// T_DIVX = CV_FOURCC('D', 'I', 'V', 'X'), // = MPEG-4 codec
// T_H263 = CV_FOURCC('U', '2', '6', '3'), // = H263 codec
// T_H263I = CV_FOURCC('I', '2', '6', '3'), // = H263I codec
// T_FLV = CV_FOURCC('F', 'L', 'V', '1'), // = FLV1 codec
// };
template<class T> class Image
{
private:
IplImage* imgp;
public:
Image(IplImage* img=0) {imgp=img;}
~Image(){imgp=0;}
void operator=(IplImage* img) {imgp=img;}
inline T* operator[](const int rowIndx) {
return ((T *)(imgp->imageData + rowIndx*imgp->widthStep));}
};
typedef struct{
unsigned char b,g,r;
} RgbPixel;
typedef struct{
float b,g,r;
} RgbPixelFloat;
typedef Image<RgbPixel> RgbImage;
typedef Image<RgbPixelFloat> RgbImageFloat;
typedef Image<unsigned char> BwImage;
typedef Image<float> BwImageFloat;
/*
For a single-channel byte image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
BwImage imgA(img);
imgA[i][j] = 111;
For a multi-channel byte image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
RgbImage imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111;
For a multi-channel float image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
RgbImageFloat imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111;
*/
void vFastCopyImageTo(const cv::Mat& src, cv::Mat& dst, const cv::Rect& roi);
void vCopyImageTo(const cv::Mat& src, cv::Mat& dst, const cv::Rect& roi);
void vFlip(cv::Mat& src, int flipX, int flipY);
void vDrawText(cv::Mat& img, int x,int y,char* str, CvScalar clr=CV_RGB(255,255,255));
void vPolyLine(cv::Mat& dst, vector<cv::Point>& pts, CvScalar clr=CV_RGB(255,255,255), int thick = 1);
CvScalar vDefaultColor(int idx);
#define show_image(img_name) do{\
cvNamedWindow(#img_name);\
cvShowImage(#img_name, img_name);}\
while(0);
#define show_mat(img_name) do{\
cv::namedWindow(#img_name);\
cv::imshow(#img_name, img_name);}\
while(0);
const CvScalar CV_RED = CV_RGB(255,0,0);
const CvScalar CV_GREEN = CV_RGB(0,255,0);
const CvScalar CV_BLUE = CV_RGB(0,0,255);
const CvScalar CV_BLACK = CV_RGB(0,0,0);
const CvScalar CV_WHITE = CV_RGB(255,255,255);
const CvScalar CV_GRAY = CV_RGB(122,122,122);
inline CvScalar vRandomColor()
{
static CvRNG rng = cvRNG((unsigned)-1);
int icolor = cvRandInt(&rng);
return CV_RGB(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
}
#define vDrawRect(image, rc, clr) cv::rectangle(image, cv::Point(rc.x,rc.y), cv::Point(rc.x+rc.width,rc.y+rc.height), clr)
#define WRITE_(key, var) fs<<key<<var
#define WRITE_FS(var) fs<<(#var)<<(var)
#define READ_(key, var) fs[key]>>var
#define READ_FS(var) fs[#var]>>(var)
#define vGrayScale(clr, gray) cv::cvtColor(clr, gray, CV_BGR2GRAY)
#define vColorFul(gray, clr) cv::cvtColor(gray, clr , CV_GRAY2BGR)
#define vThresh(gray, thresh) cv::threshold( gray, gray, thresh, 255, CV_THRESH_BINARY )//if > thresh -> white
#define vThreshInv(gray, thresh) cv::threshold( gray, gray, thresh, 255, CV_THRESH_BINARY_INV )//if < thresh -> white
#define vOpen(img, times) cv::morphologyEx( img, img, NULL, NULL, CV_MOP_OPEN, times );//去除白色小区域
#define vClose(img, times) cv::morphologyEx( img, img, NULL, NULL, CV_MOP_CLOSE, times );//去除黑色小区域
#define vDilate(img, times) cv::morphologyEx( img, img, NULL, NULL, CV_MOP_DILATE, times );
#define vErode(img, times) cv::morphologyEx( img, img, NULL, NULL, CV_MOP_ERODE, times );
#define vFullScreen(win_name) cvSetWindowProperty(win_name, CV_WND_PROP_FULLSCREEN, 1);
#define vCreateGray(clr) cvCreateImage(cvGetSize(clr), 8, 1);
#define vCreateColor(clr) cvCreateImage(cvGetSize(clr), 8, 3);
struct VideoInput
{
int _fps;
int device_id;
enum e_InputType
{
From_Image = 0,
From_Video,
From_Camera,
#ifdef KINECT
From_Kinect,
#endif
From_Count,
}_InputType;
int _argc;
char** _argv;
cv::VideoCapture _capture;
void showSettingsDialog();
cv::Mat _frame;
int _cam_idx;
cv::Size _size;
cv::Size _half_size;
int _channel;
int _codec;
int _frame_num;
VideoInput();
void resize(int w, int h);
bool init(int cam_idx);
bool init(const std::string& video_file);
bool init(int argc, char** argv);
void wait(int t);
cv::Mat get_frame();
private:
#ifdef KINECT
cv::Ptr<ofxKinectCLNUI> _kinect;
bool init_kinect();
#endif
#ifdef VIDEOINPUT_LIB
cv::Ptr<videoInput> VI;
#endif
void _post_init();
char buffer[256];
};
void vRotateImage(IplImage* image, float angle, float centreX, float centreY);
void vHighPass(const cv::Mat& src, cv::Mat& dst, int blurLevel = 10, int noiseLevel = 3);
void vPerspectiveTransform(const CvArr* src, CvArr* dst, cv::Point srcQuad[4], cv::Point dstQuad[4]);
void vGetPerspectiveMatrix(CvMat*& warp_matrix, cv::Point2f xsrcQuad[4], cv::Point2f xdstQuad[4]);
#define vAddWeighted(src, alpha, dst) cvAddWeighted(src, alpha, dst, 1-alpha, 0, dst);
void cvSkinSegment(IplImage* img, IplImage* mask);
void vFillPoly(IplImage* img, const vector<cv::Point>& pt_list, const cv::Scalar& clr = cv::Scalar(255,255,255));
void vLinePoly(IplImage* img, const vector<cv::Point>& pt_list, const cv::Scalar& clr = cv::Scalar(255,255,255), int thick = 1);
void vLinePoly(IplImage* img, const vector<cv::Point2f>& pt_list, const cv::Scalar& clr = cv::Scalar(255,255,255), int thick = 1);
inline bool isPointInsideRect(int x, int y, const cv::Rect& rect)
{
return (x >= rect.x && x <= rect.x+rect.width &&
y >= rect.y && y <= rect.height);
}
// Object-to-object bounding-box collision detector:
bool vTestRectHitRect(const cv::Rect& object1, const cv::Rect& object2);