This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
/
calc_depth_optimized.c
76 lines (72 loc) · 2.94 KB
/
calc_depth_optimized.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Project 2: Performance Optimization
*/
#if defined(_MSC_VER)
#include <intrin.h>
#elif defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
#include <x86intrin.h>
#endif
#include <math.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include "calc_depth_naive.h"
#include "calc_depth_optimized.h"
#include "utils.h"
void calc_depth_optimized(float *depth, float *left, float *right,
int image_width, int image_height, int feature_width,
int feature_height, int maximum_displacement) {
// Naive implementation
for (int y = 0; y < image_height; y++) {
for (int x = 0; x < image_width; x++) {
if (y < feature_height || y >= image_height - feature_height
|| x < feature_width || x >= image_width - feature_width) {
depth[y * image_width + x] = 0;
continue;
}
float min_diff = -1;
int min_dy = 0;
int min_dx = 0;
for (int dy = -maximum_displacement; dy <= maximum_displacement; dy++) {
for (int dx = -maximum_displacement; dx <= maximum_displacement; dx++) {
if (y + dy - feature_height < 0
|| y + dy + feature_height >= image_height
|| x + dx - feature_width < 0
|| x + dx + feature_width >= image_width) {
continue;
}
float squared_diff = 0;
for (int box_y = -feature_height; box_y <= feature_height; box_y++) {
for (int box_x = -feature_width; box_x <= feature_width; box_x++) {
int left_x = x + box_x;
int left_y = y + box_y;
int right_x = x + dx + box_x;
int right_y = y + dy + box_y;
squared_diff += square_euclidean_distance(
left[left_y * image_width + left_x],
right[right_y * image_width + right_x]
);
}
}
if (min_diff == -1 || min_diff > squared_diff
|| (min_diff == squared_diff
&& displacement_naive(dx, dy) < displacement_naive(min_dx, min_dy))) {
min_diff = squared_diff;
min_dx = dx;
min_dy = dy;
}
}
}
if (min_diff != -1) {
if (maximum_displacement == 0) {
depth[y * image_width + x] = 0;
} else {
depth[y * image_width + x] = displacement_naive(min_dx, min_dy);
}
} else {
depth[y * image_width + x] = 0;
}
}
}
}