00001
00002 #ifndef __JBXL_CPP_OPENCV_TOOl_H_
00003 #define __JBXL_CPP_OPENCV_TOOl_H_
00004
00005
00006 #ifndef ENABLE_OPENCV
00007 #error "OpenCVTool.h is called, but ENABLE_OPENCV is not defined."
00008 #endif
00009
00010
00023 #include "common++.h"
00024 #include "opencv2/opencv.hpp"
00025
00026 #include "Gdata.h"
00027 #include "xtools.h"
00028
00029
00030 #ifdef WIN32
00031 #ifdef _DEBUG
00032 #pragma comment(lib, "opencv_core242d.lib")
00033 #pragma comment(lib, "opencv_imgproc242d.lib")
00034 #pragma comment(lib, "opencv_objdetect242d.lib")
00035 #pragma comment(lib, "zlibd.lib")
00036 #else
00037 #pragma comment(lib, "opencv_core242.lib")
00038 #pragma comment(lib, "opencv_imgproc242.lib")
00039 #pragma comment(lib, "opencv_objdetect242.lib")
00040 #pragma comment(lib, "zlib.lib")
00041 #endif
00042 #endif
00043
00044
00045
00046 namespace jbxl {
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00067 template <typename T> rectangle* cvDetectObjects(cv::CascadeClassifier cascade, MSGraph<T>* vp, int& num, int sz=0, double scale=1.0)
00068 {
00069 num = 0;
00070
00071 cv::Mat imag = copyMSGraph2CvMat<uByte>(vp);
00072 cv::equalizeHist(imag, imag);
00073
00074 cv::Mat simg = imag;
00075 if (scale>1.0) {
00076 simg = cv::Mat_<uByte>((int)(imag.rows/scale), (int)(imag.cols/scale));
00077 cv::resize(imag, simg, simg.size(), 0, 0, cv::INTER_LINEAR);
00078 }
00079 else scale = 1.0;
00080
00081 std::vector<cv::Rect> faces;
00082 if (sz>1) cascade.detectMultiScale(simg, faces, 1.1, 3, 0, cv::Size(sz, sz));
00083 else cascade.detectMultiScale(simg, faces);
00084
00085 num = (int)faces.size();
00086 if (num==0) return NULL;
00087
00088 int len = sizeof(rectangle)*num;
00089 rectangle* data = (rectangle*)malloc(len);
00090 if (data==NULL) return NULL;
00091 memset(data, 0, len);
00092
00093 int n = 0;
00094 std::vector<cv::Rect>::const_iterator r;
00095 for (r=faces.begin(); r!=faces.end(); r++) {
00096 data[n].x = (int)(r->x*scale);
00097 data[n].y = (int)(r->y*scale);
00098 data[n].xsize = (int)(r->width *scale);
00099 data[n].ysize = (int)(r->height*scale);
00100 n++;
00101 }
00102
00103 return data;
00104 }
00105
00106
00107
00124 template <typename R, typename T> cv::Mat copyMSGraph2CvMat(MSGraph<T>* vp)
00125 {
00126 cv::Mat mat;
00127
00128 if (vp->zs<=1) {
00129 vp->zs = 1;
00130 mat = cv::Mat_<R>(vp->ys, vp->xs);
00131
00132
00133 if (mat.isContinuous()) {
00134 R* dst = mat.ptr<R>(0);
00135 for (int i=0; i<vp->xs*vp->ys; i++) {
00136 dst[i] = (R)vp->gp[i];
00137 }
00138 }
00139 else {
00140 for (int j=0; j<vp->ys; j++) {
00141 R* dst = mat.ptr<R>(j);
00142 T* src = &(vp->gp[j*vp->xs]);
00143 for (int i=0; i<vp->xs; i++) {
00144 dst[i] = (R)src[i];
00145 }
00146 }
00147 }
00148 }
00149
00150
00151 else {
00152 int size[3];
00153 size[0] = vp->zs;
00154 size[1] = vp->ys;
00155 size[2] = vp->xs;
00156 mat = cv::Mat_<R>(3, size);
00157
00158
00159 if (mat.isContinuous()) {
00160 R* dst = (R*)mat.data;
00161 for (int i=0; i<vp->xs*vp->ys*vp->zs; i++) {
00162 dst[i] = (R)vp->gp[i];
00163 }
00164 }
00165 else {
00166 for (int k=0; k<vp->zs; k++) {
00167 int kk = k*vp->ys*vp->xs;
00168 for (int j=0; j<vp->ys; j++) {
00169 R* dst = mat.ptr<R>(k, j);
00170 T* src = &(vp->gp[j*vp->xs + kk]);
00171 for (int i=0; i<vp->xs; i++) {
00172 dst[i] = (R)src[i];
00173 }
00174 }
00175 }
00176 }
00177 }
00178
00179 return mat;
00180 }
00181
00182
00183
00215 template <typename T> MSGraph<T>* getMSGraphFromCvMat(cv::Mat mat)
00216 {
00217 MSGraph<T>* vp = NULL;
00218
00219 if (mat.channels()==1) {
00220 if (mat.depth()==CV_8U) vp = _getMSGraph_CvMat_C1<T, uByte>(mat);
00221 else if (mat.depth()==CV_8S) vp = _getMSGraph_CvMat_C1<T, sByte>(mat);
00222 else if (mat.depth()==CV_16U) vp = _getMSGraph_CvMat_C1<T, uWord>(mat);
00223 else if (mat.depth()==CV_16S) vp = _getMSGraph_CvMat_C1<T, sWord>(mat);
00224 else if (mat.depth()==CV_32S) vp = _getMSGraph_CvMat_C1<T, int>(mat);
00225
00226
00227 }
00228
00229 else if (mat.channels()==3) {
00230 if (mat.depth()==CV_8U) vp = _getMSGraph_CvMat_C3<T, uByte>(mat);
00231 else if (mat.depth()==CV_8S) vp = _getMSGraph_CvMat_C3<T, sByte>(mat);
00232 else if (mat.depth()==CV_16U) vp = _getMSGraph_CvMat_C3<T, uWord>(mat);
00233 else if (mat.depth()==CV_16S) vp = _getMSGraph_CvMat_C3<T, sWord>(mat);
00234 else if (mat.depth()==CV_32S) vp = _getMSGraph_CvMat_C3<T, int>(mat);
00235
00236
00237 }
00238
00239 return vp;
00240 }
00241
00242
00243
00244
00245
00246
00247 template <typename T, typename R> MSGraph<T>* _getMSGraph_CvMat_C1(cv::Mat mat)
00248 {
00249 MSGraph<T>* vp = NULL;
00250
00251 if (mat.channels()!=1) return NULL;
00252
00253 if (mat.dims==2) {
00254 vp = new MSGraph<T>(mat.cols, mat.rows);
00255 if (vp==NULL || vp->gp==NULL) return vp;
00256
00257
00258 if (mat.isContinuous()) {
00259 R* ptr = (R*)mat.data;
00260 T* dst = vp->gp;
00261 for (int i=0; i<vp->xs*vp->ys; i++) {
00262 dst[i] = (T)ptr[i];
00263 }
00264 }
00265 else {
00266 for (int j=0; j<vp->ys; j++) {
00267 R* ptr = mat.ptr<R>(j);
00268 T* dst = &(vp->gp[j*vp->xs]);
00269 for (int i=0; i<vp->xs; i++) {
00270 dst[i] = (T)ptr[i];
00271 }
00272 }
00273 }
00274
00275 vp->color = GRAPH_COLOR_MONO;
00276 }
00277
00278
00279 else if (mat.dims==3) {
00280 vp = new MSGraph<T>((int)mat.size[2], (int)mat.size[1], (int)mat.size[0]);
00281 if (vp==NULL || vp->gp==NULL) return vp;
00282
00283
00284 if (mat.isContinuous()) {
00285 R* ptr = (R*)mat.data;
00286 T* dst = vp->gp;
00287 for (int i=0; i<vp->xs*vp->ys*vp->zs; i++) {
00288 dst[i] = (T)ptr[i];
00289 }
00290 }
00291 else {
00292 for (int k=0; k<vp->zs; k++) {
00293 int kk = k*vp->ys*vp->xs;
00294 for (int j=0; j<vp->ys; j++) {
00295 R* ptr = mat.ptr<R>(k, j);
00296 T* dst = &(vp->gp[j*vp->xs + kk]);
00297 for (int i=0; i<vp->xs; i++) {
00298 dst[i] = (T)ptr[i];
00299 }
00300 }
00301 }
00302 }
00303
00304 vp->color = GRAPH_COLOR_MONO;
00305 }
00306
00307 return vp;
00308 }
00309
00310
00311
00312
00313
00314
00315 template <typename T, typename R> MSGraph<T>* _getMSGraph_CvMat_C3(cv::Mat mat)
00316 {
00317 MSGraph<T>* vp = NULL;
00318
00319 if (mat.channels()!=3) return NULL;
00320 int tsz = sizeof(T);
00321
00322 if (mat.dims==2) {
00323 vp = new MSGraph<T>(mat.cols, mat.rows);
00324 if (vp==NULL || vp->gp==NULL) return vp;
00325
00326
00327 if (mat.isContinuous()) {
00328 R* src = (R*)mat.data;
00329 T* dst = vp->gp;
00330 vp->color = _linecopy_MAT2MSG_C3(src, dst, vp->xs*vp->ys, tsz);
00331 }
00332
00333 else {
00334 for (int j=0; j<vp->ys; j++) {
00335 R* src = mat.ptr<R>(j);
00336 T* dst = &(vp->gp[j*vp->xs]);
00337 _linecopy_MAT2MSG_C3(src, dst, vp->xs, tsz);
00338 }
00339 vp->color = _linecopy_MAT2MSG_C3((R*)NULL, (T*)NULL, 0, tsz);
00340 }
00341 }
00342
00343
00344 else if (mat.dims==3) {
00345 vp = new MSGraph<T>((int)mat.size[2], (int)mat.size[1], (int)mat.size[0]);
00346 if (vp==NULL || vp->gp==NULL) return vp;
00347
00348
00349 if (mat.isContinuous()) {
00350 R* src = (R*)mat.data;
00351 T* dst = vp->gp;
00352 vp->color = _linecopy_MAT2MSG_C3(src, dst, vp->xs*vp->ys*vp->zs, tsz);
00353 }
00354 else {
00355 for (int k=0; k<vp->zs; k++) {
00356 int kk = k*vp->ys*vp->xs;
00357 for (int j=0; j<vp->ys; j++) {
00358 R* src = mat.ptr<R>(k, j);
00359 T* dst = &(vp->gp[j*vp->xs + kk]);
00360 for (int i=0; i<vp->xs; i++) {
00361 _linecopy_MAT2MSG_C3(src, dst, vp->xs, tsz);
00362 }
00363 }
00364 }
00365 vp->color = _linecopy_MAT2MSG_C3((R*)NULL, (T*)NULL, 0, tsz);
00366 }
00367 }
00368
00369 return vp;
00370 }
00371
00372
00373
00374
00375
00376 template <typename R, typename T> int _linecopy_MAT2MSG_C3(R* src, T* dst, int len, int sz)
00377 {
00378 int i3 = 0;
00379 int color = GRAPH_COLOR_MONO;
00380
00381 if (sz==1) {
00382 for (int i=0; i<len; i++) {
00383 dst[i] = (T)(((unsigned int)src[i3] + (unsigned int)src[i3+1] + (unsigned int)src[i3+2])/3);
00384 i3 += 3;
00385 }
00386 }
00387
00388 else if (sz==2) {
00389 for (int i=0; i<len; i++) {
00390 dst[i] = (T)RGB2Word((unsigned int)src[i3+2], (unsigned int)src[i3+1], (unsigned int)src[i3]);
00391 i3 += 3;
00392 }
00393 color = GRAPH_COLOR_RGB16;
00394 }
00395
00396 else {
00397 for (int i=0; i<len; i++) {
00398 dst[i] = (T)ABGR2Int(0, (unsigned int)src[i3], (unsigned int)src[i3+1], (unsigned int)src[i3+2]);
00399 i3 += 3;
00400 }
00401 color = GRAPH_COLOR_ABGR;
00402 }
00403
00404 return color;
00405 }
00406
00407
00408 }
00409
00410
00411 #endif
00412