annotate _me.pyx @ 0:4214d9245f8e

import
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Thu, 06 Sep 2007 13:45:48 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 cimport c_numpy
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 cimport c_python
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 import numpy, sys
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 c_numpy.import_array()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 cdef extern from 'math.h':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 cdef double fabs(double)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 cdef extern from 'sys/param.h':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 cdef int MIN(int, int)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 cdef int MAX(int, int)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 cdef double calc_sad(c_numpy.ndarray ablock, int rs, int cs, c_numpy.ndarray refblock):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 cdef double sad
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 cdef int i, j
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 cdef char *p_a, *p_ref
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 sad = 0.0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 for i from 0 <= i < refblock.dimensions[0]:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 p_a = ablock.data + (rs+i) * ablock.strides[0] + cs * ablock.strides[1]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 p_ref = refblock.data + i * refblock.strides[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 for j from 0 <= j < refblock.dimensions[1]:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 sad = sad + fabs((<double*>p_a)[0] - (<double*>p_ref)[0])
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 p_a = p_a + ablock.strides[1]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 p_ref = p_ref + refblock.strides[1]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 return sad
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 def me(c_numpy.ndarray a, c_numpy.ndarray refblock, int rc, int cc, int sr):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 cdef int rs, rs1, rs2, cs, cs1, cs2
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 cdef int min_r, min_c
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 cdef double sad, min_sad
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 cdef int bs
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 bs = refblock.dimensions[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 min_sad = sys.maxint
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 min_r, min_c = 0, 0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 rs1 = MAX(0,rc-sr)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 rs2 = MIN(a.dimensions[0]-bs,rc+sr)+1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 cs1 = MAX(0,cc-sr)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 cs2 = MIN(cc+sr,a.dimensions[1]-bs)+1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47 for rs from rs1 <= rs < rs2:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 for cs from cs1 <= cs < cs2:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 sad = calc_sad(a, rs, cs, refblock)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 if sad < min_sad:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 # found new local block SAD minimum, store motion vector
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 min_r, min_c, min_sad = rs, cs, sad
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 return min_r, min_c, min_sad
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.