File: im_maxpos_subpel.c

package info (click to toggle)
vips 8.14.1-3%2Bdeb12u2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 35,912 kB
  • sloc: ansic: 165,449; cpp: 10,987; python: 4,462; xml: 4,212; sh: 471; perl: 40; makefile: 23
file content (169 lines) | stat: -rw-r--r-- 6,415 bytes parent folder | download
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* find position of maximum, subpixel estimation
 *
 * Copyright: 2008, Nottingham Trent University
 * Author: Tom Vajzovic
 * Written on: 2008-02-07
 *
 * 25/1/11
 * 	- gtk-doc
 */

/*

    This file is part of VIPS.

    VIPS is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H */
#include <glib/gi18n-lib.h>

#include <stdlib.h>
#include <vips/vips.h>
#include <vips/vips7compat.h>


#define MOST_OF( A, B )   ( (A) > 0.9 * (B) )
#define LITTLE_OF( A, B )   ( (B) < 0.1 * (B) )

/**
 * im_maxpos_subpel:
 * @in: input image
 * @x: output position of maximum 
 * @y: output position of maximum 
 *
 * This function implements:
 *
 *   "Extension of Phase Correlation to Subpixel Registration"
 *   by H. Foroosh, from IEEE trans. Im. Proc. 11(3), 2002.
 *
 * If the best three matches in the correlation are aranged:
 *
 *   02   or   01
 *   1         2
 *
 * then we return a subpixel match using the ratio of correlations in the
 * vertical and horizontal dimension.
 *
 * ( xs[0], ys[0] ) is the best integer alignment
 * ( xs[ use_x ], ys[ use_x ] ) is equal in y and (+/-)1 off in x
 * ( xs[ use_y ], ys[ use_y ] ) is equal in x and (+/-)1 off in y
 *
 * Alternatively if the best four matches in the correlation are aranged in
 * a square:
 *
 *   01  or  03  or  02  or  03
 *   32      12      31      21
 *
 * then we return a subpixel match weighting with the sum the two on each
 * side over the sum of all four, but only if all four of them are very
 * close to the best, and the fifth is nowhere near.
 *
 * This alternative method is not described by Foroosh, but is often the
 * case where the match is close to n-and-a-half pixels in both dimensions.
 *
 * See also: im_maxpos(), im_min(), im_stats().
 *
 * Returns: 0 on success, -1 on error
 */

int im_maxpos_subpel( IMAGE *in, double *x, double *y ){
#define FUNCTION_NAME "im_maxpos_subpel"

  int xs[5];
  int ys[5];
  double vals[5];
  int xa, ya, xb, yb;
  double vxa, vya, vxb, vyb;

  if( im_maxpos_vec( in, xs, ys, vals, 5 ))
    return -1;

#define WRAP_TEST_RETURN()                                                \
                                                                          \
    /* wrap around if we have alignment -1 < d <= 0 */                    \
    /* (change it to: size - 1 <= d < size ) */                           \
                                                                          \
    if( ! xa && in-> Xsize - 1 == xb )                                    \
      xa= in-> Xsize;                                                     \
                                                                          \
    else if( ! xb && in-> Xsize - 1 == xa )                               \
      xb= in-> Xsize;                                                     \
                                                                          \
    if( ! ya && in-> Ysize - 1 == yb )                                    \
      ya= in-> Ysize;                                                     \
                                                                          \
    else if( ! yb && in-> Ysize - 1 == ya )                               \
      yb= in-> Ysize;                                                     \
                                                                          \
    if( 1 == abs( xb - xa ) && 1 == abs( yb - ya )){                      \
      *x= ((double)xa) + ((double)( xb - xa )) * ( vxb / ( vxa + vxb ));  \
      *y= ((double)ya) + ((double)( yb - ya )) * ( vyb / ( vya + vyb ));  \
      return 0;                                                           \
    }

#define TEST3( A, B )                       \
  if( xs[0] == xs[A] && ys[0] == ys[B] ){   \
    xa= xs[0];                              \
    ya= ys[0];                              \
    xb= xs[B];                              \
    yb= ys[A];                              \
    vxa= vals[0];                           \
    vya= vals[0];                           \
    vxb= vals[B];                           \
    vyb= vals[A];                           \
    WRAP_TEST_RETURN()                      \
  }

  TEST3( 1, 2 )
  TEST3( 2, 1 )

  if( MOST_OF( vals[1], vals[0] ) && MOST_OF( vals[2], vals[0] )
      && MOST_OF( vals[3], vals[0] ) && LITTLE_OF( vals[4], vals[0] )){

#define TEST4( A, B, C, D, E, F, G, H )                                         \
    if( xs[A] == xs[B] && xs[C] == xs[D] && ys[E] == ys[F] && ys[G] == ys[H] ){ \
      xa= xs[A];                                                                \
      xb= xs[C];                                                                \
      ya= ys[E];                                                                \
      yb= ys[G];                                                                \
      vxa= vals[A] + vals[B];                                                   \
      vxb= vals[C] + vals[D];                                                   \
      vya= vals[E] + vals[F];                                                   \
      vyb= vals[G] + vals[H];                                                   \
      WRAP_TEST_RETURN()                                                        \
    }

    TEST4( 0, 3, 1, 2, 0, 1, 2, 3 )
    TEST4( 0, 1, 2, 3, 0, 3, 1, 2 )
    TEST4( 0, 3, 1, 2, 0, 2, 1, 3 )
    TEST4( 0, 2, 1, 3, 0, 3, 1, 2 )
  }

  im_warn( FUNCTION_NAME, "registration performed to nearest pixel only: correlation does not have the expected distribution for sub-pixel registration" );
  *x= (double) xs[0];
  *y= (double) ys[0];
  return 0;
}