Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <new>
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
00059
00060
00061 #define MAX_CORNER_SUBPIXEL_ITERATIONS 100
00062
00063
00064
00065
00066
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
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