CornerSubpixel.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  KLTTracker.cpp
00037 // Author:    Pedram Azad
00038 // Date:      18.11.2009
00039 // ****************************************************************************
00040 
00041 
00042 // ****************************************************************************
00043 // Includes
00044 // ****************************************************************************
00045 
00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00047 
00048 #include "CornerSubpixel.h"
00049 #include "Image/ByteImage.h"
00050 #include "Math/Math2d.h"
00051 
00052 #include <stdio.h>
00053 #include <float.h>
00054 #include <math.h>
00055 
00056 
00057 // ****************************************************************************
00058 // Defines
00059 // ****************************************************************************
00060 
00061 #define MAX_CORNER_SUBPIXEL_ITERATIONS 100
00062 
00063 
00064 
00065 // ****************************************************************************
00066 // Methods
00067 // ****************************************************************************
00068 
00069 bool CornerSubpixel::Refine(const CByteImage *pImage, const Vec2d &point, Vec2d &resultPoint, int nHalfWindowSize, int nMaxIterations)
00070 {
00071         if (pImage->type != CByteImage::eGrayScale)
00072         {
00073                 printf("error: image must be of type eGrayScale for CCornerSubpixel::refine\n");
00074                 return false;
00075         }
00076         
00077         const int width = pImage->width;
00078         const int height = pImage->height;
00079         
00080         const int nWindowSize = 2 * nHalfWindowSize + 1;
00081         const int nWindowSizePlus2 = nWindowSize + 2;
00082         const int diff = width - nWindowSizePlus2;
00083         
00084         float grayValues[1024];
00085         float *pGrayValues = nWindowSizePlus2 <= 32 ? grayValues : new float[nWindowSizePlus2 * nWindowSizePlus2];
00086         
00087         // track each point
00088         const float ux = point.x;
00089         const float uy = point.y;
00090         
00091         const unsigned char *pixels = pImage->pixels;
00092         
00093         Vec2d v = { 0.0f, 0.0f };
00094         
00095         for (int k = 0; k < nMaxIterations; k++)
00096         {
00097                 const int xb = int(floorf(ux + v.x));
00098                 const int yb = int(floorf(uy + v.y));
00099                 
00100                 if (xb - nHalfWindowSize < 1 || xb + nHalfWindowSize + 3 >= width || yb - nHalfWindowSize < 1 || yb + nHalfWindowSize + 3 >= height)
00101                         break;
00102                 
00103                 const float dx = ux + v.x - float(xb);
00104                 const float dy = uy + v.y - float(yb);
00105                 
00106                 const float f00 = (1.0f - dx) * (1.0f - dy);
00107                 const float f10 = dx * (1.0f - dy);
00108                 const float f01 = (1.0f - dx) * dy;
00109                 const float f11 = dx * dy;
00110                 
00111                 int j, offset, offset2;
00112 
00113                 for (j = nWindowSizePlus2, offset = (yb - nHalfWindowSize - 1) * width + xb - nHalfWindowSize - 1, offset2 = 0; j; j--, offset += diff)
00114                         for (int jj = nWindowSizePlus2; jj; jj--, offset++, offset2++)
00115                                 pGrayValues[offset2] = f00 * pixels[offset] + f10 * pixels[offset + 1] + f01 * pixels[offset + width] + f11 * pixels[offset + width + 1];
00116                 
00117                 float gxx_sum = 0.0f, gxy_sum = 0.0f, gyy_sum = 0.0f;
00118                 float bx = 0.0f, by = 0.0f;
00119                 
00120                 for (j = 0, offset = nWindowSizePlus2 + 1; j < nWindowSize; j++, offset += 2)
00121                         for (int jj = 0; jj < nWindowSize; jj++, offset++)
00122                         {
00123                                 const float ix = pGrayValues[offset + 1] - pGrayValues[offset - 1];
00124                                 const float iy = pGrayValues[offset + nWindowSizePlus2] - pGrayValues[offset - nWindowSizePlus2];
00125                                 
00126                                 const float gxx = ix * ix;
00127                                 const float gyy = iy * iy;
00128                                 const float gxy = ix * iy;
00129                                 
00130                                 gxx_sum += gxx;
00131                                 gyy_sum += gyy;
00132                                 gxy_sum += gxy;
00133                                 
00134                                 bx += gxx * float(jj - nHalfWindowSize) + gxy * float(j - nHalfWindowSize);
00135                                 by += gxy * float(jj - nHalfWindowSize) + gyy * float(j - nHalfWindowSize);
00136                         }
00137                 
00138                 if (fabsf(gxx_sum * gyy_sum - gxy_sum * gxy_sum) <= FLT_EPSILON)
00139                 {
00140                         if (nWindowSizePlus2 > 32)
00141                                 delete [] pGrayValues;
00142                         
00143                         Math2d::SetVec(resultPoint, point);
00144                         
00145                         return false;
00146                 }
00147                 
00148                 const Mat2d G = { gxx_sum, gxy_sum, gxy_sum, gyy_sum };
00149                 Mat2d G_;
00150                 Math2d::Invert(G, G_);
00151                 
00152                 const Vec2d b = { bx, by };
00153                 
00154                 Vec2d n;
00155                 Math2d::MulMatVec(G_, b, n);
00156                 
00157                 Math2d::AddToVec(v, n);
00158                 
00159                 if (Math2d::SquaredLength(n) < 0.03f)
00160                         break;
00161         }
00162         
00163         if (nWindowSizePlus2 > 32)
00164                 delete [] pGrayValues;
00165         
00166         Math2d::SetVec(resultPoint, ux + v.x, uy + v.y);
00167                 
00168         return true;
00169 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57