61 #define MAX_CORNER_SUBPIXEL_ITERATIONS 100 73 printf(
"error: image must be of type eGrayScale for CCornerSubpixel::refine\n");
80 const int nWindowSize = 2 * nHalfWindowSize + 1;
81 const int nWindowSizePlus2 = nWindowSize + 2;
82 const int diff = width - nWindowSizePlus2;
84 float grayValues[1024];
85 float *pGrayValues = nWindowSizePlus2 <= 32 ? grayValues :
new float[nWindowSizePlus2 * nWindowSizePlus2];
88 const float ux = point.
x;
89 const float uy = point.
y;
95 for (
int k = 0; k < nMaxIterations; k++)
97 const int xb = int(floorf(ux + v.
x));
98 const int yb = int(floorf(uy + v.
y));
100 if (xb - nHalfWindowSize < 1 || xb + nHalfWindowSize + 3 >= width || yb - nHalfWindowSize < 1 || yb + nHalfWindowSize + 3 >= height)
103 const float dx = ux + v.
x - float(xb);
104 const float dy = uy + v.
y - float(yb);
106 const float f00 = (1.0f - dx) * (1.0f - dy);
107 const float f10 = dx * (1.0f - dy);
108 const float f01 = (1.0f - dx) * dy;
109 const float f11 = dx * dy;
113 for (j = nWindowSizePlus2, offset = (yb - nHalfWindowSize - 1) * width + xb - nHalfWindowSize - 1, offset2 = 0; j; j--, offset += diff)
114 for (
int jj = nWindowSizePlus2; jj; jj--, offset++, offset2++)
115 pGrayValues[offset2] = f00 * pixels[offset] + f10 * pixels[offset + 1] + f01 * pixels[offset + width] + f11 * pixels[offset + width + 1];
117 float gxx_sum = 0.0f, gxy_sum = 0.0f, gyy_sum = 0.0f;
118 float bx = 0.0f,
by = 0.0f;
120 for (j = 0, offset = nWindowSizePlus2 + 1; j < nWindowSize; j++, offset += 2)
121 for (
int jj = 0; jj < nWindowSize; jj++, offset++)
123 const float ix = pGrayValues[offset + 1] - pGrayValues[offset - 1];
124 const float iy = pGrayValues[offset + nWindowSizePlus2] - pGrayValues[offset - nWindowSizePlus2];
126 const float gxx = ix * ix;
127 const float gyy = iy * iy;
128 const float gxy = ix * iy;
134 bx += gxx * float(jj - nHalfWindowSize) + gxy * float(j - nHalfWindowSize);
135 by += gxy * float(jj - nHalfWindowSize) + gyy * float(j - nHalfWindowSize);
138 if (fabsf(gxx_sum * gyy_sum - gxy_sum * gxy_sum) <= FLT_EPSILON)
140 if (nWindowSizePlus2 > 32)
141 delete [] pGrayValues;
148 const Mat2d G = { gxx_sum, gxy_sum, gxy_sum, gyy_sum };
163 if (nWindowSizePlus2 > 32)
164 delete [] pGrayValues;
bool Refine(const CByteImage *pImage, const Vec2d &point, Vec2d &resultPoint, int nHalfWindowSize=2, int nMaxIterations=100)
int width
The width of the image in pixels.
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
unsigned char * pixels
The pointer to the the pixels.
Data structure for the representation of a 2x2 matrix.
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
int height
The height of the image in pixels.
void Invert(const Mat2d &matrix, Mat2d &result)
GLenum GLsizei GLsizei height
void AddToVec(Vec2d &vec, const Vec2d &vectorToAdd)
ImageType type
The type of the image.
Data structure for the representation of a 2D vector.
void SetVec(Vec2d &vec, float x, float y)
float SquaredLength(const Vec2d &vec)
void MulMatVec(const Mat2d &matrix, const Vec2d &vec, Vec2d &result)