28 #define N_WINDOW_SIZES 1
30 #ifndef DETECT_WINDOW_FPS
31 #define DETECT_WINDOW_FPS 0
48 coordinate[0] = 0; coordinate[1] = 0;
57 printf(
"Coordinate: %d, %d\n", coordinate[0], coordinate[1]);
58 printf(
"Response = %d\n", response);
69 uint8_t calculate_integral_image = 1;
81 calculate_integral_image = (s == 0);
83 calculate_integral_image, integral_image, MODE);
84 if (s == 0 || best_response[s] < best_response[best_index]) {
86 best_xc = coordinate[0];
87 best_yc = coordinate[1];
91 coordinate[0] = best_xc;
92 coordinate[1] = best_yc;
93 return best_response[best_index];
111 uint16_t window_size, border_size, feature_size, px_whole, px_inner, px_border, px_outer;
120 if (calculate_integral_image) {
125 window_size = (*size);
126 border_size = (relative_border * window_size) / 100;
127 feature_size = window_size + 2 * border_size;
128 px_inner = feature_size - 2 * border_size;
129 px_inner = px_inner * px_inner;
130 px_whole = feature_size * feature_size;
131 px_border = px_whole - px_inner;
132 px_outer = border_size * window_size;
135 for (x = 0; x < image_width - feature_size; x++) {
136 for (y = 0; y < image_height - feature_size; y++) {
137 response =
get_window_response(x, y, feature_size, border_size, integral_image, image_width, image_height, px_inner,
140 if (response <
RES) {
143 response =
get_border_response(x, y, feature_size, window_size, border_size, integral_image, image_width, image_height,
147 if (response < min_response) {
150 min_response = response;
161 coordinate[0] += feature_size / 2;
162 coordinate[1] += feature_size / 2;
176 cell_width = (image_width - 2 * border) / n_cells;
177 cell_height = (image_height - 2 * border) / n_cells;
179 for (c = 0; c < n_cells; c++) {
180 for (r = 0; r < n_cells; r++) {
181 avg =
get_avg_disparity(c * cell_width + border, r * cell_height + border, (c + 1) * cell_width + border,
182 (r + 1) * cell_height + border, integral_image, image_width, image_height);
191 if (min_avg == 10000) {
192 escape_coordinate[0] = image_width / 2;
193 escape_coordinate[1] = image_height / 2;
195 escape_coordinate[0] = min_c * cell_width + border + cell_width / 2;
196 escape_coordinate[1] = min_r * cell_height + border + cell_height / 2;
205 for (x = 0; x < image_width; x++) {
206 for (y = 0; y < image_height; y++) {
207 if (x >= 1 && y >= 1) {
208 integral_image[x + y * image_width] = (
uint32_t) in[x + y * image_width] + integral_image[x - 1 + y * image_width] +
209 integral_image[x + (y - 1) * image_width] - integral_image[x - 1 + (y - 1) * image_width];
211 integral_image[x + y * image_width] = (
uint32_t) in[x + y * image_width] + integral_image[x - 1 + y * image_width];
213 integral_image[x + y * image_width] = (
uint32_t) in[x + y * image_width] + integral_image[x + (y - 1) * image_width];
215 integral_image[x + y * image_width] = (
uint32_t) in[x + y * image_width];
227 if (max_x + max_y * image_width >= image_width * image_height) {
return 0; }
228 sum = integral_image[min_x + min_y * image_width] + integral_image[max_x + max_y * image_width] -
229 integral_image[max_x + min_y * image_width] - integral_image[min_x + max_y * image_width];
240 w = max_x - min_x + 1;
241 h = max_y - min_y + 1;
244 sum = integral_image[min_x + min_y * image_width] + integral_image[max_x + max_y * image_width] -
245 integral_image[max_x + min_y * image_width] - integral_image[min_x + max_y * image_width];
247 avg = (sum *
RES) / n_pix;
255 uint32_t whole_area, inner_area, resp;
257 whole_area =
get_sum_disparities(x, y, x + feature_size, y + feature_size, integral_image, image_width, image_height);
259 inner_area =
get_sum_disparities(x + border, y + border, x + feature_size - border, y + feature_size - border,
260 integral_image, image_width, image_height);
263 if (whole_area - inner_area > 0) {
264 resp = (inner_area *
RES * px_border) / ((whole_area - inner_area) * px_inner);
269 if (inner_area > 0 && (inner_area / px_inner) > 0) {
270 resp = (
RES * (whole_area - inner_area) / px_border) / (inner_area / px_inner);
284 uint32_t inner_area, avg_inner, left_area, right_area, up_area, down_area, darkest, avg_dark, resp;
286 inner_area =
get_sum_disparities(x + border, y + border, x + feature_size - border, y + feature_size - border,
287 integral_image, image_width, image_height);
288 avg_inner = (
RES * inner_area) / px_inner;
290 left_area =
get_sum_disparities(x, y + border, x + border, y + border + window_size, integral_image, image_width,
292 right_area =
get_sum_disparities(x + border + window_size, y + border, x + 2 * border + window_size,
293 y + border + window_size, integral_image, image_width, image_height);
294 up_area =
get_sum_disparities(x + border, y, x + border + window_size, y + border, integral_image, image_width,
296 down_area =
get_sum_disparities(x + border, y + border + window_size, x + border + window_size,
297 y + 2 * border + window_size, integral_image, image_width, image_height);
299 darkest = (left_area < right_area) ? left_area : right_area;
300 darkest = (darkest < up_area) ? darkest : up_area;
301 darkest = (darkest < down_area) ? darkest : down_area;
302 avg_dark =
RES * darkest / px_outer;
303 if (avg_dark < avg_inner) {
309 resp =
RES * avg_inner / avg_dark;
319 for (x = 0; x < image_width; x++) {
320 for (y = 0; y < image_height; y++) {
321 if (in[x + y * image_width] < 4) {
322 in[x + y * image_width] = 6;
333 for (x = 0; x < image_width; x++) {
334 for (y = 0; y < image_height; y++) {
337 out[x + y * image_width] = in[2 * (x + y * image_width)] >> n_bits;
339 out[x + y * image_width] = (255 - in[2 * (x + y * image_width)]) >> n_bits;
void filter_bad_pixels(uint8_t *in, uint32_t image_width, uint32_t image_height)
struct image_t * detect_window(struct image_t *img)
uint16_t detect_window_sizes(uint8_t *in, uint32_t image_width, uint32_t image_height, uint16_t *coordinate, uint32_t *integral_image, uint8_t MODE)
void image_free(struct image_t *img)
Free the image.
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
uint16_t detect_escape(uint8_t *in, uint32_t image_width, uint32_t image_height, uint16_t *escape_coordinate, uint32_t *integral_image, uint8_t n_cells)
struct video_listener * cv_add_to_device(struct video_config_t *device, cv_function func, uint16_t fps)
uint16_t get_border_response(uint16_t x, uint16_t y, uint16_t feature_size, uint16_t window_size, uint16_t border, uint32_t *integral_image, uint16_t image_width, uint16_t image_height, uint16_t px_inner, uint16_t px_outer)
uint16_t get_window_response(uint16_t x, uint16_t y, uint16_t feature_size, uint16_t border, uint32_t *integral_image, uint16_t image_width, uint16_t image_height, uint16_t px_inner, uint16_t px_border, uint8_t MODE)
uint16_t detect_window_one_size(uint8_t *in, uint32_t image_width, uint32_t image_height, uint16_t *coordinate, uint16_t *size, uint8_t calculate_integral_image, uint32_t *integral_image, uint8_t MODE)
void * buf
Image buffer (depending on the image_type)
void transform_illuminance_image(uint8_t *in, uint8_t *out, uint32_t image_width, uint32_t image_height, uint8_t n_bits, uint8_t bright_win)
void get_integral_image(uint8_t *in, uint32_t image_width, uint32_t image_height, uint32_t *integral_image)
void image_to_grayscale(struct image_t *input, struct image_t *output)
Convert an image to grayscale.
static void h(const real32_T x[7], const real32_T q[4], real32_T y[6])
#define DETECT_WINDOW_FPS
Default FPS (zero means run at camera fps)
uint32_t get_sum_disparities(uint16_t min_x, uint16_t min_y, uint16_t max_x, uint16_t max_y, uint32_t *integral_image, uint32_t image_width, uint32_t image_height)
void detect_window_init(void)
Grayscale image with only the Y part (uint8 per pixel)
Detect a bright region surrounded by dark or viceversa - sometimes this corresponds to a window...
uint32_t get_avg_disparity(uint16_t min_x, uint16_t min_y, uint16_t max_x, uint16_t max_y, uint32_t *integral_image, uint32_t image_width, uint32_t image_height)