Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
image.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Freek van Tienen <freek.v.tienen@gmail.com>
3  *
4  * This file is part of Paparazzi.
5  *
6  * Paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #include "image.h"
28 #include <stdlib.h>
29 #include <string.h>
30 
38 void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
39 {
40  // Set the variables
41  img->type = type;
42  img->w = width;
43  img->h = height;
44 
45  // Depending on the type the size differs
46  if (type == IMAGE_YUV422) {
47  img->buf_size = sizeof(uint8_t) * 2 * width * height;
48  } else if (type == IMAGE_JPEG) {
49  img->buf_size = sizeof(uint8_t) * 2 * width * height; // At maximum quality this is enough
50  } else if (type == IMAGE_GRADIENT) {
51  img->buf_size = sizeof(int16_t) * width * height;
52  } else {
53  img->buf_size = sizeof(uint8_t) * width * height;
54  }
55 
56  img->buf = malloc(img->buf_size);
57 }
58 
63 void image_free(struct image_t *img)
64 {
65  if (img->buf != NULL) {
66  free(img->buf);
67  img->buf = NULL;
68  }
69 }
70 
77 void image_copy(struct image_t *input, struct image_t *output)
78 {
79  if (input->type != output->type) {
80  return;
81  }
82 
83  output->w = input->w;
84  output->h = input->h;
85  output->buf_size = input->buf_size;
86  memcpy(&output->ts, &input->ts, sizeof(struct timeval));
87  memcpy(output->buf, input->buf, input->buf_size);
88 }
89 
97 void image_switch(struct image_t *a, struct image_t *b)
98 {
99  /* Remember everything from image a */
100  struct image_t old_a;
101  memcpy(&old_a, a, sizeof(struct image_t));
102 
103  /* Copy everything from b to a */
104  memcpy(a, b, sizeof(struct image_t));
105 
106  /* Copy everything from the remembered a to b */
107  memcpy(b, &old_a, sizeof(struct image_t));
108 }
109 
116 void image_to_grayscale(struct image_t *input, struct image_t *output)
117 {
118  uint8_t *source = input->buf;
119  uint8_t *dest = output->buf;
120  source++;
121 
122  // Copy the creation timestamp (stays the same)
123  memcpy(&output->ts, &input->ts, sizeof(struct timeval));
124 
125  // Copy the pixels
126  for (int y = 0; y < output->h; y++) {
127  for (int x = 0; x < output->w; x++) {
128  if (output->type == IMAGE_YUV422) {
129  *dest++ = 127; // U / V
130  }
131  *dest++ = *source; // Y
132  source += 2;
133  }
134  }
135 }
136 
149 uint16_t image_yuv422_colorfilt(struct image_t *input, struct image_t *output, uint8_t y_m, uint8_t y_M, uint8_t u_m,
150  uint8_t u_M, uint8_t v_m, uint8_t v_M)
151 {
152  uint16_t cnt = 0;
153  uint8_t *source = input->buf;
154  uint8_t *dest = output->buf;
155 
156  // Copy the creation timestamp (stays the same)
157  memcpy(&output->ts, &input->ts, sizeof(struct timeval));
158 
159  // Go trough all the pixels
160  for (uint16_t y = 0; y < output->h; y++) {
161  for (uint16_t x = 0; x < output->w; x += 2) {
162  // Check if the color is inside the specified values
163  if (
164  (dest[1] >= y_m)
165  && (dest[1] <= y_M)
166  && (dest[0] >= u_m)
167  && (dest[0] <= u_M)
168  && (dest[2] >= v_m)
169  && (dest[2] <= v_M)
170  ) {
171  cnt ++;
172  // UYVY
173  dest[0] = 64; // U
174  dest[1] = source[1]; // Y
175  dest[2] = 255; // V
176  dest[3] = source[3]; // Y
177  } else {
178  // UYVY
179  char u = source[0] - 127;
180  u /= 4;
181  dest[0] = 127; // U
182  dest[1] = source[1]; // Y
183  u = source[2] - 127;
184  u /= 4;
185  dest[2] = 127; // V
186  dest[3] = source[3]; // Y
187  }
188 
189  // Go to the next 2 pixels
190  dest += 4;
191  source += 4;
192  }
193  }
194  return cnt;
195 }
196 
213 void image_yuv422_downsample(struct image_t *input, struct image_t *output, uint16_t downsample)
214 {
215  uint8_t *source = input->buf;
216  uint8_t *dest = output->buf;
217  uint16_t pixelskip = (downsample - 1) * 2;
218 
219  // Copy the creation timestamp (stays the same)
220  memcpy(&output->ts, &input->ts, sizeof(struct timeval));
221 
222  // Go trough all the pixels
223  for (uint16_t y = 0; y < output->h; y++) {
224  for (uint16_t x = 0; x < output->w; x += 2) {
225  // YUYV
226  *dest++ = *source++; // U
227  *dest++ = *source++; // Y
228  *dest++ = *source++; // V
229  source += pixelskip;
230  *dest++ = *source++; // Y
231  source += pixelskip;
232  }
233  // read 1 in every 'downsample' rows, so skip (downsample-1) rows after reading the first
234  source += (downsample - 1) * input->w * 2;
235  }
236 }
237 
247 void image_subpixel_window(struct image_t *input, struct image_t *output, struct point_t *center, uint16_t subpixel_factor)
248 {
249  uint8_t *input_buf = (uint8_t *)input->buf;
250  uint8_t *output_buf = (uint8_t *)output->buf;
251 
252  // Calculate the window size
253  uint16_t half_window = output->w / 2;
254  uint16_t subpixel_w = input->w * subpixel_factor;
255  uint16_t subpixel_h = input->h * subpixel_factor;
256 
257  // Go through the whole window size in normal coordinates
258  for (uint16_t i = 0; i < output->w; i++) {
259  for (uint16_t j = 0; j < output->h; j++) {
260  // Calculate the subpixel coordinate
261  uint16_t x = center->x + (i - half_window) * subpixel_factor;
262  uint16_t y = center->y + (j - half_window) * subpixel_factor;
263  BoundUpper(x, subpixel_w);
264  BoundUpper(y, subpixel_h);
265 
266  // Calculate the original pixel coordinate
267  uint16_t orig_x = x / subpixel_factor;
268  uint16_t orig_y = y / subpixel_factor;
269 
270  // Calculate top left (in subpixel coordinates)
271  uint16_t tl_x = orig_x * subpixel_factor;
272  uint16_t tl_y = orig_y * subpixel_factor;
273 
274  // Check if it is the top left pixel
275  if (tl_x == x && tl_y == y) {
276  output_buf[output->w * j + i] = input_buf[input->w * orig_y + orig_x];
277  } else {
278  // Calculate the difference from the top left
279  uint16_t alpha_x = (x - tl_x);
280  uint16_t alpha_y = (y - tl_y);
281 
282  // Blend from the 4 surrounding pixels
283  uint32_t blend = (subpixel_factor - alpha_x) * (subpixel_factor - alpha_y) * input_buf[input->w * orig_y + orig_x];
284  blend += alpha_x * (subpixel_factor - alpha_y) * input_buf[input->w * orig_y + (orig_x + 1)];
285  blend += (subpixel_factor - alpha_x) * alpha_y * input_buf[input->w * (orig_y + 1) + orig_x];
286  blend += alpha_x * alpha_y * input_buf[input->w * (orig_y + 1) + (orig_x + 1)];
287 
288  // Set the normalized pixel blend
289  output_buf[output->w * j + i] = blend / (subpixel_factor * subpixel_factor);
290  }
291  }
292  }
293 }
294 
302 void image_gradients(struct image_t *input, struct image_t *dx, struct image_t *dy)
303 {
304  // Fetch the buffers in the correct format
305  uint8_t *input_buf = (uint8_t *)input->buf;
306  int16_t *dx_buf = (int16_t *)dx->buf;
307  int16_t *dy_buf = (int16_t *)dy->buf;
308 
309  // Go trough all pixels except the borders
310  for (uint16_t x = 1; x < input->w - 1; x++) {
311  for (uint16_t y = 1; y < input->h - 1; y++) {
312  dx_buf[(y - 1)*dx->w + (x - 1)] = (int16_t)input_buf[y * input->w + x + 1] - (int16_t)input_buf[y * input->w + x - 1];
313  dy_buf[(y - 1)*dy->w + (x - 1)] = (int16_t)input_buf[(y + 1) * input->w + x] - (int16_t)input_buf[(y - 1) * input->w + x];
314  }
315  }
316 }
317 
325 void image_calculate_g(struct image_t *dx, struct image_t *dy, int32_t *g)
326 {
327  int32_t sum_dxx = 0, sum_dxy = 0, sum_dyy = 0;
328 
329  // Fetch the buffers in the correct format
330  int16_t *dx_buf = (int16_t *)dx->buf;
331  int16_t *dy_buf = (int16_t *)dy->buf;
332 
333  // Calculate the different sums
334  for (uint16_t x = 0; x < dx->w; x++) {
335  for (uint16_t y = 0; y < dy->h; y++) {
336  sum_dxx += ((int32_t)dx_buf[y * dx->w + x] * dx_buf[y * dx->w + x]);
337  sum_dxy += ((int32_t)dx_buf[y * dx->w + x] * dy_buf[y * dy->w + x]);
338  sum_dyy += ((int32_t)dy_buf[y * dy->w + x] * dy_buf[y * dy->w + x]);
339  }
340  }
341 
342  // ouput the G vector
343  g[0] = sum_dxx / 255;
344  g[1] = sum_dxy / 255;
345  g[2] = g[1];
346  g[3] = sum_dyy / 255;
347 }
348 
357 uint32_t image_difference(struct image_t *img_a, struct image_t *img_b, struct image_t *diff)
358 {
359  uint32_t sum_diff2 = 0;
360  int16_t *diff_buf = NULL;
361 
362  // Fetch the buffers in the correct format
363  uint8_t *img_a_buf = (uint8_t *)img_a->buf;
364  uint8_t *img_b_buf = (uint8_t *)img_b->buf;
365 
366  // If we want the difference image back
367  if (diff != NULL) {
368  diff_buf = (int16_t *)diff->buf;
369  }
370 
371  // Go trough the imagge pixels and calculate the difference
372  for (uint16_t x = 0; x < img_b->w; x++) {
373  for (uint16_t y = 0; y < img_b->h; y++) {
374  int16_t diff_c = img_a_buf[(y + 1) * img_a->w + (x + 1)] - img_b_buf[y * img_b->w + x];
375  sum_diff2 += diff_c * diff_c;
376 
377  // Set the difference image
378  if (diff_buf != NULL) {
379  diff_buf[y * diff->w + x] = diff_c;
380  }
381  }
382  }
383 
384  return sum_diff2;
385 }
386 
395 int32_t image_multiply(struct image_t *img_a, struct image_t *img_b, struct image_t *mult)
396 {
397  int32_t sum = 0;
398  int16_t *img_a_buf = (int16_t *)img_a->buf;
399  int16_t *img_b_buf = (int16_t *)img_b->buf;
400  int16_t *mult_buf = NULL;
401 
402  // When we want an output
403  if (mult != NULL) {
404  mult_buf = (int16_t *)mult->buf;
405  }
406 
407  // Calculate the multiplication
408  for (uint16_t x = 0; x < img_a->w; x++) {
409  for (uint16_t y = 0; y < img_a->h; y++) {
410  int16_t mult_c = img_a_buf[y * img_a->w + x] * img_b_buf[y * img_b->w + x];
411  sum += mult_c;
412 
413  // Set the difference image
414  if (mult_buf != NULL) {
415  mult_buf[y * mult->w + x] = mult_c;
416  }
417  }
418  }
419 
420  return sum;
421 }
422 
431 void image_show_points(struct image_t *img, struct point_t *points, uint16_t points_cnt)
432 {
433  uint8_t *img_buf = (uint8_t *)img->buf;
434  uint8_t pixel_width = (img->type == IMAGE_YUV422) ? 2 : 1;
435 
436  // Go trough all points and color them
437  for (int i = 0; i < points_cnt; i++) {
438  uint32_t idx = pixel_width * points[i].y * img->w + points[i].x * pixel_width;
439  img_buf[idx] = 255;
440 
441  // YUV422 consists of 2 pixels
442  if (img->type == IMAGE_YUV422) {
443  idx++;
444  img_buf[idx] = 255;
445  }
446  }
447 }
448 
456 void image_show_flow(struct image_t *img, struct flow_t *vectors, uint16_t points_cnt, uint8_t subpixel_factor)
457 {
458  // Go through all the points
459  for (uint16_t i = 0; i < points_cnt; i++) {
460  // Draw a line from the original position with the flow vector
461  struct point_t from = {
462  vectors[i].pos.x / subpixel_factor,
463  vectors[i].pos.y / subpixel_factor
464  };
465  struct point_t to = {
466  (vectors[i].pos.x + vectors[i].flow_x) / subpixel_factor,
467  (vectors[i].pos.y + vectors[i].flow_y) / subpixel_factor
468  };
469  image_draw_line(img, &from, &to);
470  }
471 }
472 
479 void image_draw_line(struct image_t *img, struct point_t *from, struct point_t *to)
480 {
481  int xerr = 0, yerr = 0;
482  uint8_t *img_buf = (uint8_t *)img->buf;
483  uint8_t pixel_width = (img->type == IMAGE_YUV422) ? 2 : 1;
484  uint16_t startx = from->x;
485  uint16_t starty = from->y;
486 
487  /* compute the distances in both directions */
488  int32_t delta_x = to->x - from->x;
489  int32_t delta_y = to->y - from->y;
490 
491  /* Compute the direction of the increment,
492  an increment of 0 means either a horizontal or vertical
493  line.
494  */
495  int8_t incx, incy;
496  if (delta_x > 0) { incx = 1; }
497  else if (delta_x == 0) { incx = 0; }
498  else { incx = -1; }
499 
500  if (delta_y > 0) { incy = 1; }
501  else if (delta_y == 0) { incy = 0; }
502  else { incy = -1; }
503 
504  /* determine which distance is greater */
505  uint16_t distance = 0;
506  delta_x = abs(delta_x);
507  delta_y = abs(delta_y);
508  if (delta_x > delta_y) { distance = delta_x * 20; }
509  else { distance = delta_y * 20; }
510 
511  /* draw the line */
512  for (uint16_t t = 0; /* starty >= 0 && */ starty < img->h && /* startx >= 0 && */ startx < img->w && t <= distance + 1; t++) {
513  img_buf[img->w * pixel_width * starty + startx * pixel_width] = (t <= 3) ? 0 : 255;
514 
515  if (img->type == IMAGE_YUV422) {
516  img_buf[img->w * pixel_width * starty + startx * pixel_width + 1] = 255;
517 
518  if (startx + 1 < img->w) {
519  img_buf[img->w * pixel_width * starty + startx * pixel_width + 2] = (t <= 3) ? 0 : 255;
520  img_buf[img->w * pixel_width * starty + startx * pixel_width + 3] = 255;
521  }
522  }
523 
524  xerr += delta_x;
525  yerr += delta_y;
526  if (xerr > distance) {
527  xerr -= distance;
528  startx += incx;
529  }
530  if (yerr > distance) {
531  yerr -= distance;
532  starty += incy;
533  }
534  }
535 }
unsigned short uint16_t
Definition: types.h:16
void image_gradients(struct image_t *input, struct image_t *dx, struct image_t *dy)
Calculate the gradients using the following matrix: [0 -1 0; -1 0 1; 0 1 0].
Definition: image.c:302
static uint8_t dest[]
Definition: w5100.c:98
uint16_t x
The x coordinate of the point.
Definition: image.h:55
uint32_t buf_size
The buffer size.
Definition: image.h:49
image_type
Definition: image.h:34
void image_yuv422_downsample(struct image_t *input, struct image_t *output, uint16_t downsample)
Simplified high-speed low CPU downsample function without averaging downsample factor must be 1...
Definition: image.c:213
uint32_t image_difference(struct image_t *img_a, struct image_t *img_b, struct image_t *diff)
Calculate the difference between two images and return the error This will only work with grayscale i...
Definition: image.c:357
void image_switch(struct image_t *a, struct image_t *b)
This will switch image *a and *b This is faster as image_copy because it doesn't copy the whole image...
Definition: image.c:97
void image_free(struct image_t *img)
Free the image.
Definition: image.c:63
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
Definition: image.c:38
Definition: image.h:42
Definition: image.h:60
void image_draw_line(struct image_t *img, struct point_t *from, struct point_t *to)
Draw a line on the image.
Definition: image.c:479
void image_copy(struct image_t *input, struct image_t *output)
Copy an image from inut to output This will only work if the formats are the same.
Definition: image.c:77
void image_show_points(struct image_t *img, struct point_t *points, uint16_t points_cnt)
Show points in an image by coloring them through giving the pixels the maximum value.
Definition: image.c:431
void image_show_flow(struct image_t *img, struct flow_t *vectors, uint16_t points_cnt, uint8_t subpixel_factor)
Shows the flow from a specific point to a new point This works on YUV422 and Grayscale images...
Definition: image.c:456
Image helper functions like resizing, color filter, converters...
int16_t flow_x
The x direction flow in subpixels.
Definition: image.h:62
uint16_t w
Image width.
Definition: image.h:44
unsigned long uint32_t
Definition: types.h:18
uint16_t h
Image height.
Definition: image.h:45
signed short int16_t
Definition: types.h:17
void * buf
Image buffer (depending on the image_type)
Definition: image.h:50
struct point_t pos
The original position the flow comes from.
Definition: image.h:61
void image_to_grayscale(struct image_t *input, struct image_t *output)
Convert an image to grayscale.
Definition: image.c:116
Definition: image.h:54
signed long int32_t
Definition: types.h:19
unsigned char uint8_t
Definition: types.h:14
struct timeval ts
The timestamp of creation.
Definition: image.h:46
UYVY format (uint16 per pixel)
Definition: image.h:35
uint16_t y
The y coordinate of the point.
Definition: image.h:56
void image_calculate_g(struct image_t *dx, struct image_t *dy, int32_t *g)
Calculate the G vector of an image gradient This is used for optical flow calculation.
Definition: image.c:325
void image_subpixel_window(struct image_t *input, struct image_t *output, struct point_t *center, uint16_t subpixel_factor)
This outputs a subpixel window image in grayscale Currently only works with Grayscale images as input...
Definition: image.c:247
signed char int8_t
Definition: types.h:15
An image gradient (int16 per pixel)
Definition: image.h:38
uint16_t image_yuv422_colorfilt(struct image_t *input, struct image_t *output, uint8_t y_m, uint8_t y_M, uint8_t u_m, uint8_t u_M, uint8_t v_m, uint8_t v_M)
Filter colors in an YUV422 image.
Definition: image.c:149
int16_t flow_y
The y direction flow in subpixels.
Definition: image.h:63
int32_t image_multiply(struct image_t *img_a, struct image_t *img_b, struct image_t *mult)
Calculate the multiplication between two images and return the error This will only work with image g...
Definition: image.c:395
An JPEG encoded image (not per pixel encoded)
Definition: image.h:37
enum image_type type
The image type.
Definition: image.h:43