70 if (sum) cvReleaseImage(&sum);
74 (sum->height != gray->width+1) ||
75 (sum->width != gray->height+1))
77 if (sum) cvReleaseImage(&sum);
79 sum = cvCreateImage(cvSize(gray->width+1, gray->height+1), IPL_DEPTH_64F, 1);
81 cvIntegral(gray, sum);
85 int x2 = rect.x + rect.width;
87 int y2 = rect.y + rect.height;
95 double v = +((
double *)sum->imageData)[y2*sum->width+x2]
96 -((
double *)sum->imageData)[y2*sum->width+x1]
97 -((
double *)sum->imageData)[y1*sum->width+x2]
98 +((
double *)sum->imageData)[y1*sum->width+x1];
100 if (count) *count = rect.width*rect.height;
105 return GetSum(rect, &count)/count;
109 for (
IntIndex yy(rect.height, sub->height); yy.
get() != yy.end(); yy.next(),yi++) {
111 for (
IntIndex xx(rect.width, sub->width); xx.
get() != xx.end(); xx.next(),xi++) {
120 double ave = GetAve(r);
124 sub->imageData[yi*sub->widthStep+xi] = (char)ave;
129 int width = gray->width-1;
130 int height = gray->height-1;
131 if ((normalx == 0) ||
132 (normalx->width != width) ||
133 (normalx->height != height))
135 if (normalx) cvReleaseImage(&normalx);
136 if (normaly) cvReleaseImage(&normaly);
137 normalx = cvCreateImage(cvSize(width, height), IPL_DEPTH_64F, 1);
138 normaly = cvCreateImage(cvSize(width, height), IPL_DEPTH_64F, 1);
140 for (
int j=0; j<
height; j++) {
141 for (
int i=0; i<
width; i++) {
156 double a4 = (
unsigned char)gray->imageData[(j)*gray->widthStep+(i+1)];
157 double a3 = (
unsigned char)gray->imageData[(j)*gray->widthStep+(i)];
158 double a2 = (
unsigned char)gray->imageData[(j+1)*gray->widthStep+(i)];
159 double a1 = (
unsigned char)gray->imageData[(j+1)*gray->widthStep+(i+1)];
161 double nx = (-a1+a2+a3-a4)/4;
162 double ny = (-a1-a2+a3+a4)/4;
163 ((
double *)normalx->imageData)[j*normalx->width+i] = nx;
164 ((
double *)normaly->imageData)[j*normaly->width+i] = ny;
173 if (normalx) cvReleaseImage(&normalx);
174 if (normaly) cvReleaseImage(&normaly);
177 CalculatePointNormals(gray);
178 integx.Update(normalx);
179 integy.Update(normaly);
182 CvRect
r = {rect.x, rect.y, rect.width-1, rect.height-1};
183 if (count) *dirx = integx.GetSum(r, count);
184 else *dirx = integx.GetSum(r);
185 *diry = integy.GetSum(r);
189 GetGradient(rect, dirx, diry, &count);
void Update(IplImage *gray)
Update integral image for the given image.
This file implements integral image and integral gradient computations.
double GetAve(CvRect &rect)
Calculate the average for the given rectangular area in the image.
Class for calculating "evenly spaced" integer indices for data sequence.
int end() const
For testing have we reached the end.
void CalculatePointNormals(IplImage *gray)
void GetAveGradient(CvRect &rect, double *dirx, double *diry)
Calculate the average gradient for the given rectangular area in the image.
void Update(IplImage *gray)
Update intermediate images for calculating the gradients to the given image.
int get_next_step() const
How much the index will be increased with the next next()
int get() const
Get the index value.
IntIndex(int _res, int _steps)
Create IntIndex for indexing _res elements in predefined amount of _steps .
double GetSum(CvRect &rect, int *count=0)
Calculate the sum for the given rectangular area in the image.
void GetGradient(CvRect &rect, double *dirx, double *diry, int *count=0)
Calculate the gradient for the given rectangular area in the image.
int next()
Take the next integer index step.
int operator=(int v)
Set the integer index to the "grid" value nearest to v .
void GetSubimage(const CvRect &rect, IplImage *sub)
Get a sub-image using integral image representation.