image_f32.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 
4 This software was developed in the APRIL Robotics Lab under the
5 direction of Edwin Olson, ebolson@umich.edu. This software may be
6 available under alternative licensing terms; contact the address above.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 The views and conclusions contained in the software and documentation are those
29 of the authors and should not be interpreted as representing official policies,
30 either expressed or implied, of the Regents of The University of Michigan.
31 */
32 
33 #include <assert.h>
34 #include <math.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "math_util.h"
41 
42 #include "image_f32.h"
43 
44 
45 static inline float sqf(float v)
46 {
47  return v*v;
48 }
49 
50 image_f32_t *image_f32_create_stride(int width, int height, int stride)
51 {
52  float *buf = calloc(height * stride, sizeof(float));
53  // const initializer
54  image_f32_t tmp = { .width = width, .height = height, .stride = stride, .buf = buf };
55 
56  image_f32_t *fim = (image_f32_t*) calloc(1, sizeof(image_f32_t));
57 
58  memcpy(fim, &tmp, sizeof(image_f32_t));
59 
60  return fim;
61 }
62 
63 image_f32_t *image_f32_create(int width, int height)
64 {
65  int stride = width;
66  while (stride & 7)
67  stride++;
68  return image_f32_create_stride(width, height, stride);
69 }
70 
71 // scales by 1/255u
73 {
74  image_f32_t *fim = image_f32_create(im->width, im->height);
75 
76  for (int y = 0; y < fim->height; y++)
77  for (int x = 0; x < fim->width; x++)
78  fim->buf[y*fim->stride + x] = im->buf[y*im->stride + x] / 255.0f;
79 
80  return fim;
81 }
82 
84 {
85  free(im->buf);
86  free(im);
87 }
88 
89 static void convolve(const float *x, float *y, int sz, const float *k, int ksz)
90 {
91  assert((ksz&1)==1);
92 
93  for (int i = 0; i < ksz/2 && i < sz; i++)
94  y[i] = x[i];
95 
96  for (int i = 0; i < sz - ksz; i++) {
97  float acc = 0;
98 
99  for (int j = 0; j < ksz; j++)
100  acc += k[j]*x[i+j];
101 
102  y[ksz/2 + i] = acc;
103  }
104 
105  for (int i = sz - ksz + ksz/2; i < sz; i++)
106  y[i] = x[i];
107 }
108 
109 void image_f32_gaussian_blur(image_f32_t *im, double sigma, int ksz)
110 {
111  assert((ksz & 1) == 1); // ksz must be odd.
112 
113  // build the kernel.
114  float k[ksz];
115 
116  // for kernel of length 5:
117  // dk[0] = f(-2), dk[1] = f(-1), dk[2] = f(0), dk[3] = f(1), dk[4] = f(2)
118  for (int i = 0; i < ksz; i++) {
119  int x = -ksz/2 + i;
120  float v = exp(-.5*sqf(x / sigma));
121  k[i] = v;
122  }
123 
124  // normalize
125  float acc = 0;
126  for (int i = 0; i < ksz; i++)
127  acc += k[i];
128 
129  for (int i = 0; i < ksz; i++)
130  k[i] /= acc;
131 
132  for (int y = 0; y < im->height; y++) {
133  float x[im->stride];
134  memcpy(x, &im->buf[y*im->stride], im->stride * sizeof(float));
135  convolve(x, &im->buf[y*im->stride], im->width, k, ksz);
136  }
137 
138  for (int x = 0; x < im->width; x++) {
139  float xb[im->height];
140  float yb[im->height];
141 
142  for (int y = 0; y < im->height; y++)
143  xb[y] = im->buf[y*im->stride + x];
144 
145  convolve(xb, yb, im->height, k, ksz);
146 
147  for (int y = 0; y < im->height; y++)
148  im->buf[y*im->stride + x] = yb[y];
149  }
150 }
151 
152 // remap all values to [0, 1]
154 {
155  float min = HUGE_VALF, max = -HUGE_VALF;
156 
157  for (int y = 0; y < im->height; y++) {
158  for (int x = 0; x < im->width; x++) {
159  float v = im->buf[y*im->stride + x];
160  if (v < min)
161  min = v;
162  if (v > max)
163  max = v;
164  }
165  }
166 
167  if (min == max) {
168  for (int y = 0; y < im->height; y++) {
169  for (int x = 0; x < im->width; x++) {
170  im->buf[y*im->stride + x] = 0.5;
171  }
172  }
173  } else {
174 
175  for (int y = 0; y < im->height; y++) {
176  for (int x = 0; x < im->width; x++) {
177  float v = im->buf[y*im->stride + x];
178 
179  im->buf[y*im->stride + x] = (v - min) / (max - min);
180  }
181  }
182  }
183 }
184 
185 // image is assumed to be [0, 1]
186 int image_f32_write_pnm(const image_f32_t *im, const char *path)
187 {
188  FILE *f = fopen(path, "wb");
189  int res = 0;
190 
191  if (f == NULL) {
192  res = -1;
193  goto finish;
194  }
195 
196  // Only outputs to grayscale
197  fprintf(f, "P5\n%d %d\n255\n", im->width, im->height);
198 
199  for (int y = 0; y < im->height; y++) {
200  uint8_t line[im->width];
201  for (int x = 0; x < im->width; x++) {
202  float v = im->buf[y*im->stride + x];
203  if (v < 0)
204  v = 0;
205  if (v > 1)
206  v = 1;
207  line[x] = v * 255.0;
208  }
209 
210  if (im->width != fwrite(line, 1, im->width, f)) {
211  res = -2;
212  goto finish;
213  }
214  }
215 
216 finish:
217  if (f != NULL)
218  fclose(f);
219 
220  return res;
221 }
int image_f32_write_pnm(const image_f32_t *im, const char *path)
Definition: image_f32.c:186
static float sqf(float v)
Definition: image_f32.c:45
const int32_t height
Definition: image_types.h:44
static void convolve(const float *x, float *y, int sz, const float *k, int ksz)
Definition: image_f32.c:89
void image_f32_gaussian_blur(image_f32_t *im, double sigma, int ksz)
Definition: image_f32.c:109
const int32_t height
Definition: image_types.h:14
const int32_t width
Definition: image_types.h:13
const int32_t stride
Definition: image_types.h:15
uint8_t * buf
Definition: image_types.h:17
image_f32_t * image_f32_create(int width, int height)
Definition: image_f32.c:63
float * buf
Definition: image_types.h:47
#define min(A, B)
Definition: math_util.h:59
image_f32_t * image_f32_create_stride(int width, int height, int stride)
Definition: image_f32.c:50
void image_f32_destroy(image_f32_t *im)
Definition: image_f32.c:83
const int32_t stride
Definition: image_types.h:45
image_f32_t * image_f32_create_from_u8(const image_u8_t *im)
Definition: image_f32.c:72
#define max(A, B)
Definition: math_util.h:58
const int32_t width
Definition: image_types.h:43
void image_f32_normalize(image_f32_t *im)
Definition: image_f32.c:153


apriltags2
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:32