83 int nWindowSize,
int d1,
int d2,
int d_step,
int nErrorThreshold)
90 printf(
"error: input images and/or output image do not match for CStereoVision::ProcessSSD\n");
96 printf(
"error: d_step must not be zero for CStereoVision::ProcessSSD\n");
100 if (d_step > 0 && d1 >= d2)
102 printf(
"error: if d_step > 0 then d2 must be greater than d1 for CStereoVision::ProcessSSD\n");
106 if (d_step < 0 && d1 <= d2)
108 printf(
"error: if d_step < 0 then d1 must be greater than d2 for CStereoVision::ProcessSSD\n");
114 nErrorThreshold *= nWindowSize * nWindowSize;
119 for (d = d1; d <= d2; d += d_step);
121 for (d = d1; d >= d2; d += d_step);
127 const unsigned char *pLeftImageData = pLeftImage->
pixels;
128 const unsigned char *pRightImageData = pRightImage->
pixels;
129 unsigned char *pDepthImageData = pDepthImage->
pixels + (nWindowSize / 2) * (width + 1);
131 const int max_i = height - nWindowSize + 1;
132 const int max_j = width - nWindowSize + 1;
134 const int start_j =
MY_MAX(nWindowSize, d2);
135 const int diff = width - (max_j - start_j);
136 const int diff2 = width - nWindowSize;
138 for (
int i = 0,
offset = start_j; i < max_i; i++,
offset += diff)
140 for (
int j = start_j; j < max_j; j++, offset++)
142 int best_error = INT_MAX;
146 for (
int d = d1; d != d2; d += d_step)
150 for (
int y = 0, offset2 = offset;
y < nWindowSize;
y++, offset2 += diff2)
151 for (
int x = 0;
x < nWindowSize;
x++, offset2++)
152 error += abs(pLeftImageData[offset2] - pRightImageData[offset2 - d]);
154 if (error < best_error)
161 pDepthImageData[i * width + j] = best_error < nErrorThreshold ? best_d : 0;
170 int nWindowSize,
int d1,
int d2,
int d_step,
int nErrorThreshold)
177 printf(
"error: intput images and output image do not match in CStereoVision::ProcessFast\n");
183 printf(
"error: d_step must not be zero for CStereoVision::ProcessFast\n");
187 if (d_step > 0 && d1 >= d2)
189 printf(
"error: if d_step > 0 then d2 must be greater than d1 for CStereoVision::ProcessFast\n");
193 if (d_step < 0 && d1 <= d2)
195 printf(
"error: if d_step < 0 then d1 must be greater than d2 for CStereoVision::ProcessFast\n");
201 _ProcessFast(pLeftImage, pRightImage, pDepthImage, nWindowSize, d1, d2, d_step, nErrorThreshold);
210 int nWindowSize,
int d1,
int d2,
int d_step,
int nErrorThreshold)
214 nErrorThreshold *= nWindowSize * nWindowSize;
219 for (d = d1; d <= d2; d += d_step);
221 for (d = d1; d >= d2; d += d_step);
226 const int nPixels = width *
height;
229 int *H_SUM =
new int[nWindowSize *
width];
230 int *V_SUM =
new int[
width];
231 int *SAD =
new int[
width];
232 int *MIN_SSAD =
new int[width *
height];
234 for (
int i = 0; i < nPixels; i++)
235 MIN_SSAD[i] = nErrorThreshold;
237 const int window_offset = - (nWindowSize / 2) * (width + 1);
238 const int start_c =
MY_MAX(nWindowSize, d2) + nWindowSize;
240 const unsigned char *pLeftImageData = pLeftImage->
pixels;
241 const unsigned char *pRightImageData = pRightImage->
pixels;
242 unsigned char *pDepthImageData = pDepthImage->
pixels + window_offset;
245 for (d = d1; d != d2; d += d_step)
250 for (r = 0; r < nWindowSize; r++)
252 const unsigned char *pLeftImageData_helper = pLeftImageData + r *
width;
253 const unsigned char *pRightImageData_helper = pRightImageData + r * width - d;
255 H_SUM[r * width + start_c - 1] = 0;
257 for (c = start_c - nWindowSize; c < start_c; c++)
259 SAD[
c] = abs(pLeftImageData_helper[c] - pRightImageData_helper[c]);
260 H_SUM[r * width + start_c - 1] += SAD[
c];
263 for (c = start_c; c <
width; c++)
265 SAD[
c] = abs(pLeftImageData_helper[c] - pRightImageData_helper[c]);
266 H_SUM[r * width +
c] = H_SUM[r * width + c - 1] + SAD[
c] - SAD[c - nWindowSize];
270 int *pMinHelper = MIN_SSAD + (nWindowSize - 1) * width;
271 unsigned char *pDepthHelper = pDepthImageData + (nWindowSize - 1) * width;
272 for (c = start_c - 1; c <
width; c++)
275 for (r = 0; r < nWindowSize; r++)
276 V_SUM[c] += H_SUM[r * width + c];
280 if (V_SUM[c] < pMinHelper[c])
282 pMinHelper[
c] = V_SUM[
c];
283 pDepthHelper[
c] = abs(d);
289 for (r = nWindowSize; r <
height; r++)
291 const unsigned char *pLeftImageData_helper = pLeftImageData + r *
width;
292 const unsigned char *pRightImageData_helper = pRightImageData + r * width - d;
294 int *hsum_helper = H_SUM + (r % nWindowSize) * width;
295 int *pMinHelper = MIN_SSAD + r *
width;
296 unsigned char *pDepthHelper = pDepthImageData + r *
width;
298 const int save = hsum_helper[start_c - 1];
299 hsum_helper[start_c - 1] = 0;
300 for (c = start_c - nWindowSize; c < start_c; c++)
302 SAD[
c] = abs(pLeftImageData_helper[c] - pRightImageData_helper[c]);
303 hsum_helper[start_c - 1] += SAD[
c];
308 const int result = V_SUM[start_c - 1] = V_SUM[start_c - 1] + hsum_helper[start_c - 1] - save;
309 if (result < pMinHelper[start_c - 1])
311 pMinHelper[start_c - 1] = result;
312 pDepthHelper[start_c - 1] = abs(d);
317 for (c = start_c; c <
width; c++)
319 SAD[
c] = abs(pLeftImageData_helper[c] - pRightImageData_helper[c]);
321 const int save = hsum_helper[
c];
322 hsum_helper[
c] = hsum_helper[c - 1] + SAD[
c] - SAD[c - nWindowSize];
323 const int result = V_SUM[
c] = V_SUM[
c] + hsum_helper[
c] - save;
325 if (result < pMinHelper[c])
327 pMinHelper[
c] = result;
328 pDepthHelper[
c] = abs(d);
bool Process(const CByteImage *pLeftImage, const CByteImage *pRightImage, CByteImage *pDepthImage, int nWindowSize, int d1, int d2, int d_step, int nErrorThreshold=50)
void _ProcessFast(const CByteImage *pLeftImage, const CByteImage *pRightImage, CByteImage *pDepthImage, int nWindowSize, int d1, int d2, int d_step, int nErrorThreshold)
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.
bool ProcessFast(const CByteImage *pLeftImage, const CByteImage *pRightImage, CByteImage *pDepthImage, int nWindowSize, int d1, int d2, int d_step, int nErrorThreshold=50)
int height
The height of the image in pixels.
GLenum GLsizei GLsizei height
ImageType type
The type of the image.
GLdouble GLdouble GLdouble r
void Zero(CByteImage *pImage, const MyRegion *pROI=0)
Sets all values in a CByteImage to zero.