1 /**********
2 *
3 * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
4 * University of Manchester, UK. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * . Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * . Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * . Neither the name of the University of Manchester nor the names of its
17 * contributors may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 *
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 **********
34 *
35 * Program : TINA
36 * File : $Source: /home/tina/cvs/tina-libs/tina/image/imgPrc_deriv.c,v $
37 * Date : $Date: 2003/09/22 16:09:02 $
38 * Version : $Revision: 1.4 $
39 * CVS Id : $Id: imgPrc_deriv.c,v 1.4 2003/09/22 16:09:02 tony Exp $
40 *
41 * Author : Legacy TINA
42 */
43
44 /**
45 * @file
46 * @brief Derivative operations on images - 1st and second partial derivatives, Laplacian etc.
47 *
48 */
49
50 #include "imgPrc_deriv.h"
51
52 #if HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56 #include <math.h>
57 #include <tina/sys/sysDef.h>
58 #include <tina/sys/sysPro.h>
59 #include <tina/math/mathDef.h>
60 #include <tina/math/mathPro.h>
61 #include <tina/image/img_GenDef.h>
62 #include <tina/image/img_GenPro.h>
63 #include <tina/image/imgPrc_combine.h>
64 #include <tina/image/imgPrc_apply.h>
65
66 Imrect *imf_diffx(Imrect * im1)
67 {
68 Imrect *im2;
69 Imregion *roi;
70 float *row1, *row2;
71 int lx, ux, ly, uy;
72 int i, j;
73
74 if (im1 == NULL)
75 return (NULL);
76
77 roi = im1->region;
78
79 if (roi == NULL)
80 return (NULL);
81
82 lx = roi->lx;
83 ux = roi->ux;
84 ly = roi->ly;
85 uy = roi->uy;
86
87 if (lx == ux)
88 return (NULL);
89
90 im2 = im_alloc(im1->height, im1->width, roi, float_v);
91 row1 = fvector_alloc(lx, ux);
92 row2 = fvector_alloc(lx, ux);
93
94 for (i = ly; i < uy; ++i)
95 {
96 im_get_rowf(row1, im1, i, lx, ux);
97 row2[lx] = row1[lx + 1] - row1[lx];
98 for (j = lx + 1; j < ux - 1; ++j)
99 row2[j] = (float) (0.5 * (row1[j + 1] - row1[j - 1]));
100 row2[ux - 1] = row1[ux - 1] - row1[ux - 2];
101 im_put_rowf(row2, im2, i, lx, ux);
102 }
103
104 fvector_free(row1, lx);
105 fvector_free(row2, lx);
106 return (im2);
107 }
108
109 Imrect *imf_diffy(Imrect * im1)
110 {
111 Imrect *im2;
112 Imregion *roi;
113 float *col1, *col2;
114 int lx, ux, ly, uy;
115 int i, j;
116
117 if (im1 == NULL)
118 return (NULL);
119
120 roi = im1->region;
121
122 if (roi == NULL)
123 return (NULL);
124
125 lx = roi->lx;
126 ux = roi->ux;
127 ly = roi->ly;
128 uy = roi->uy;
129
130 if (ly == uy)
131 return (NULL);
132
133 im2 = im_alloc(im1->height, im1->width, roi, float_v);
134 col1 = fvector_alloc(ly, uy);
135 col2 = fvector_alloc(ly, uy);
136
137 for (i = lx; i < ux; ++i)
138 {
139 im_get_colf(col1, im1, i, ly, uy);
140 col2[ly] = col1[ly + 1] - col1[ly];
141 for (j = ly + 1; j < uy - 1; ++j)
142 col2[j] = (float) (0.5 * (col1[j + 1] - col1[j - 1]));
143 col2[uy - 1] = col1[uy - 1] - col1[uy - 2];
144 im_put_colf(col2, im2, i, ly, uy);
145 }
146
147 fvector_free(col1, ly);
148 fvector_free(col2, ly);
149 return (im2);
150 }
151
152 /* set both x and y derivatives */
153 void im_grad(Imrect * im, Imrect ** imx, Imrect ** imy)
154 {
155 *imx = imf_diffx(im);
156 *imy = imf_diffy(im);
157 }
158
159 /*
160 * set both x and y derivatives, and second derivatives (if first derivs
161 * un-needed, pass null pointer)
162 */
163
164 void im_hessian(Imrect * im, Imrect ** imx, Imrect ** imy, Imrect ** imxx, Imrect ** imxy, Imrect ** imyy)
165 {
166 Imrect *imx1;
167 Imrect *imy1;
168
169 imx1 = imf_diffx(im);
170 imy1 = imf_diffy(im);
171 *imxx = imf_diffx(imx1);
172
173 if (imx == NULL)
174 im_free(imx1);
175 else
176 *imx = imx1;
177
178 *imxy = imf_diffx(imy1);
179 *imyy = imf_diffy(imy1);
180
181 if (imy == NULL)
182 im_free(imy1);
183 else
184 *imy = imy1;
185 }
186
187 /** image differential invariants **/
188
189 /* Laplacian */
190
191 Imrect *imf_laplacian(Imrect * im)
192 {
193 Imrect *imx;
194 Imrect *imxx;
195 Imrect *imy;
196 Imrect *imyy;
197
198 imx = imf_diffx(im);
199 imxx = imf_diffx(imx);
200 im_free(imx);
201 imy = imf_diffy(im);
202 imyy = imf_diffy(imy);
203 im_free(imy);
204 im = imf_sum(imxx, imyy);
205 im_free(imxx);
206 im_free(imyy);
207 return (im);
208 }
209
210 /* Squared gradient */
211
212 Imrect *imf_sqrgrad(Imrect * im)
213 {
214 Imrect *imx;
215 Imrect *imxx;
216 Imrect *imy;
217 Imrect *imyy;
218
219 imx = imf_diffx(im);
220 imxx = imf_sqr(imx);
221 im_free(imx);
222 imy = imf_diffy(im);
223 imyy = imf_sqr(imy);
224 im_free(imy);
225 im = imf_sum(imxx, imyy);
226 im_free(imxx);
227 im_free(imyy);
228 return (im);
229 }
230
231 /* Matrix op */
232
233 Imrect *imf_matop(Imrect * ax, Imrect * ay, Imrect * mxx, Imrect * mxy, Imrect * myx, Imrect * myy, Imrect * bx, Imrect * by)
234 {
235 Imrect *im1;
236 Imrect *im2;
237 Imrect *mx;
238 Imrect *my;
239 Imrect *m;
240
241 im1 = imf_prod(mxx, bx);
242 im2 = imf_prod(mxy, by);
243 mx = imf_sum(im1, im2);
244 im_free(im1);
245 im_free(im2);
246
247 im1 = imf_prod(myx, bx);
248 im2 = imf_prod(myy, by);
249 my = imf_sum(im1, im2);
250 im_free(im1);
251 im_free(im2);
252
253 im1 = imf_prod(ax, mx);
254 im2 = imf_prod(ay, my);
255 m = imf_sum(im1, im2);
256 im_free(im1);
257 im_free(im2);
258
259 im_free(mx);
260 im_free(my);
261
262 return (m);
263 }
264
265 /* Second order image invariants */
266
267 Imrect *imf_ddn(Imrect * im)
268 {
269 Imrect *imx;
270 Imrect *imy;
271 Imrect *imxx;
272 Imrect *imxy;
273 Imrect *imyy;
274
275 im_hessian(im, &imx, &imy, &imxx, &imxy, &imyy);
276 im = imf_matop(imx, imy, imxx, imxy, imxy, imyy, imx, imy);
277
278 im_free(imx);
279 im_free(imy);
280 im_free(imxx);
281 im_free(imxy);
282 im_free(imyy);
283
284 return (im);
285 }
286
287 Imrect *imf_ddt(Imrect * im)
288 {
289 Imrect *tx;
290 Imrect *ty;
291 Imrect *imx;
292 Imrect *imy;
293 Imrect *imxx;
294 Imrect *imxy;
295 Imrect *imyy;
296
297 im_hessian(im, &imx, &imy, &imxx, &imxy, &imyy);
298
299 tx = imf_minus(imy);
300 im_free(imy);
301 ty = imx;
302
303 im = imf_matop(tx, ty, imxx, imxy, imxy, imyy, tx, ty);
304
305 im_free(tx);
306 im_free(ty);
307 im_free(imxx);
308 im_free(imxy);
309 im_free(imyy);
310
311 return (im);
312 }
313
314 /**
315 curvature of contour lines
316 **/
317
318 Imrect *imf_curv(Imrect * im, double thresh, double val)
319 {
320 Imrect *num;
321 Imrect *den;
322
323 num = imf_ddt(im);
324 im = imf_sqrgrad(im);
325 den = imf_power(1.5, im);
326 im_free(im);
327 im = imf_div(num, den, thresh, val);
328 im_free(num);
329 im_free(den);
330 return (im);
331 }
332
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.