From c05a57c43283cb638352e1ec577b5b4283c01652 Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Tue, 19 Oct 2021 00:18:41 +0200 Subject: [PATCH] adding second version of InputDetectCrossC - seems to work much better --- detect.cc | 127 ++++++++++++++++++++++++++++++++++++++++++++++-- detect.h | 2 + simpleskycam.ui | 2 +- 3 files changed, 126 insertions(+), 5 deletions(-) diff --git a/detect.cc b/detect.cc index c98228c..fa0e3d4 100644 --- a/detect.cc +++ b/detect.cc @@ -38,6 +38,7 @@ Detect::Detect() { // @suppress("Class members should be properly initialized") maxx = NULL; maxy = NULL; detmatrix = NULL; + detmatrixv2 = NULL; inputtype = 0; }; @@ -91,11 +92,24 @@ void Detect::Thread() { case 1: if (posx < objectW/2 || posy < objectH/2 || - posx > (inFrame.w-objectW/2) || posy > (inFrame.h-objectH/2)) + posx > (inFrame.w-objectW/2) || posy > (inFrame.h-objectH/2)) { + printf ("%s:%d %s find new position (last known:%d,%d)\n", + __FILE__, __LINE__, __FUNCTION__, posx, posy); InputDetect(&posx, &posy); + } else InputDetectCrossC(&posx, &posy); break; + case 2: + if (posx < objectW/2 || posy < objectH/2 || + posx > (inFrame.w-objectW/2) || posy > (inFrame.h-objectH/2)) { + printf ("%s:%d %s find new position (last known:%d,%d)\n", + __FILE__, __LINE__, __FUNCTION__, posx, posy); + InputDetect(&posx, &posy); + } + else InputDetectCrossCv2(&posx, &posy); + break; + default: if (errorinputtype != inputtype) printf ("%s:%d %s error: inputtype (%d) not found\n", __FILE__, __LINE__, __FUNCTION__, inputtype); @@ -138,7 +152,9 @@ void Detect::SetInputSize (int nw, int nh) { if ((posmaxx * posmaxy) < (nw * nh) || detmatrix != NULL) { free (detmatrix); + free (detmatrixv2); detmatrix = (float*) malloc (sizeof(float) * nw * nh); + detmatrixv2 = (uint16_t*) malloc (sizeof(uint16_t) * nw * nh); } posmaxx = nw; @@ -219,7 +235,7 @@ void Detect::InputDetect(int *posx, int *posy) { } - +#define OBJSIZE 50 #define MAXSHIFT 20 // #define DEBUGTIMES 1 void Detect::InputDetectCrossC(int *posx, int *posy) { @@ -297,8 +313,110 @@ void Detect::InputDetectCrossC(int *posx, int *posy) { // printf ("%s:%d %s pos (%d,%d) dpos (%d,%d) newpos (%d,%d)\n", __FILE__, __LINE__, __FUNCTION__, // *posx, *posy, (mxx-MAXSHIFT/2), (mxy-MAXSHIFT/2), *posx - (mxx-MAXSHIFT/2), *posy - (mxy-MAXSHIFT/2)); - *posx -= (mxx-MAXSHIFT/2); - *posy -= (mxy-MAXSHIFT/2); + *posx += (mxx-MAXSHIFT/2); + *posy += (mxy-MAXSHIFT/2); + + // + // copy image data to destination + for (x = 0; x < objectW; x++) for (y = 0; y < objectH; y++) { + desti = 3*(x + y * objectW); + ini = 3*((x + *posx - objectW/2) + (y + *posy - objectH/2) * inFrame.w); + if (desti < 0 || desti > objectW*objectH*3) continue; + if (ini < 0 || ini > inFrame.w*inFrame.h*3) continue; + pxd[desti+0] = pxi[ini+0]; + pxd[desti+1] = pxi[ini+1]; + pxd[desti+2] = pxi[ini+2]; + } + +#ifdef DEBUGTIMES + f = get_cycletime(&t1); + printf ("%s:%d copy output:%f\n", __FILE__, __LINE__, f); + f = get_cycletime(&t2); + printf ("%s:%d time needed:%f\n", __FILE__, __LINE__, f); +#endif +} + + +#define OBJSIZE 50 +#define MAXSHIFT 20 +// #define DEBUGTIMES 1 +void Detect::InputDetectCrossCv2(int *posx, int *posy) { + unsigned char *pxi; // input image + unsigned char *pxd; // destination image + unsigned char *pxo; // old image + int inx, iny, oldx, oldy; + + struct timeval t1, t2; + int shiftx, shifty, x, y, ini, oldi, desti, mxi, mxx, mxy; + float f; + + if (oldFrame.h != inFrame.h || oldFrame.w != inFrame.w || *posx == -1 || *posy == -1) { + *posx = -1; + *posy = -1; + return; + } + +#ifdef DEBUGTIMES + f = get_cycletime(&t1); + t2 = t1; +#endif + + if (objectW > inFrame.w || objectH > inFrame.h) { + printf ("%s:%d %s objectW,H (%d,%d) > inFrame.W,H (%d, %d)\n", __FILE__, __LINE__, __FUNCTION__, + objectW, objectH, inFrame.w, inFrame.h); + *posx = -1; + *posy = -1; + return; + } + + image.SetSize (objectW, objectH); + SetInputSize(inFrame.w, inFrame.h); + pxi = inFrame.data; + pxo = oldFrame.data; + pxd = image.data; + +#ifdef DEBUGTIMES + f = get_cycletime(&t1); + printf ("%s:%d setup memory time needed:%f\n", __FILE__, __LINE__, f); +#endif + + mxx = mxy = 0; + for (shifty = 0; shifty < MAXSHIFT; shifty++) { + // printf ("%s:%d %s shift (%d)\n", __FILE__, __LINE__, __FUNCTION__, shifty); + mxi = shifty * objectW; + for (shiftx = 0; shiftx < MAXSHIFT; shiftx++, mxi++) { + // fixme: help help + f = 0.0; + + for (y = 0; y < OBJSIZE; y++) { + oldx = (*posx)- OBJSIZE/2; + oldy = (*posy)- OBJSIZE/2 + y; + oldi = 3 * (oldx + oldFrame.w * oldy); + + inx = oldx + shiftx - MAXSHIFT/2; + iny = oldy + shifty - MAXSHIFT/2; + ini = 3* (inx + inFrame.w * iny); + + for (x = 0; x < OBJSIZE; x++, oldi += 3, ini += 3, oldx++, inx++) { + if (oldx >= 0 && oldy >= 0 && oldx < oldFrame.w && oldy <= oldFrame.h && + inx >= 0 && inx < inFrame.w && iny >= 0 && iny < inFrame.h) { + f += (float)(pxo[oldi+0])*(float)(pxi[ini+0]); + } + } + } + detmatrix[mxi] = f; + if (detmatrix[mxx + (mxy * objectW)] < f) { + mxx = shiftx; + mxy = shifty; + } + } + } + +// printf ("%s:%d %s pos (%d,%d) dpos (%d,%d) newpos (%d,%d)\n", __FILE__, __LINE__, __FUNCTION__, +// *posx, *posy, (mxx-MAXSHIFT/2), (mxy-MAXSHIFT/2), *posx - (mxx-MAXSHIFT/2), *posy - (mxy-MAXSHIFT/2)); + + *posx += (mxx-MAXSHIFT/2); + *posy += (mxy-MAXSHIFT/2); // // copy image data to destination @@ -322,6 +440,7 @@ void Detect::InputDetectCrossC(int *posx, int *posy) { + void Detect::SetInputType(int intype) { LockMutex(); inputtype = intype; diff --git a/detect.h b/detect.h index 7b2d27c..6e26005 100644 --- a/detect.h +++ b/detect.h @@ -40,6 +40,7 @@ private: int posmaxx; int posmaxy; float *detmatrix; + uint16_t *detmatrixv2; int objectW; // object Image Size Width int objectH; // object Image Size Height @@ -49,6 +50,7 @@ private: void InputDetect (int *posx, int *posy); void InputDetectCrossC (int *posx, int *posy); + void InputDetectCrossCv2 (int *posx, int *posy); // MMX optimiert void SetInputSize (int nw, int nh); public: diff --git a/simpleskycam.ui b/simpleskycam.ui index dd0fd4c..608570f 100644 --- a/simpleskycam.ui +++ b/simpleskycam.ui @@ -542,7 +542,7 @@ - unknown + neu InputDetect + InputDetectCrossCv2 True True False