forked from parallella/pal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p_median3x3.c
99 lines (85 loc) · 2.95 KB
/
p_median3x3.c
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
#include <pal.h>
void *memcpy(void *dest, const void *src, size_t n);
/*
* The following routines have been built from knowledge gathered
* around the Web. I am not aware of any copyright problem with
* them, so use it as you want.
* N. Devillard - 1998
*/
typedef float pixelvalue ;
#define PIX_SORT(a,b) { if ((a)>(b)) PIX_SWAP((a),(b)); }
#define PIX_SWAP(a,b) { pixelvalue temp=(a);(a)=(b);(b)=temp; }
/*----------------------------------------------------------------------------
Function : opt_med9()
In : pointer to an array of 9 pixelvalues
Out : a pixelvalue
Job : optimized search of the median of 9 pixelvalues
Notice : in theory, cannot go faster without assumptions on the
signal.
Formula from:
XILINX XCELL magazine, vol. 23 by John L. Smith
The input array is *NOT* modified in the process
The result array is guaranteed to contain the median
value
---------------------------------------------------------------------------*/
pixelvalue opt_med9(pixelvalue * pointer)
{
pixelvalue p[9];
memcpy(p, pointer, 9*sizeof(pixelvalue) );
PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
PIX_SORT(p[0], p[1]) ; PIX_SORT(p[3], p[4]) ; PIX_SORT(p[6], p[7]) ;
PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
PIX_SORT(p[0], p[3]) ; PIX_SORT(p[5], p[8]) ; PIX_SORT(p[4], p[7]) ;
PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ;
PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ;
PIX_SORT(p[4], p[2]) ; return(p[4]) ;
}
/*
* A median 3x3 filter.
*
* @param x Pointer to input image, a 2D array of size 'rows' x 'cols'
*
* @param r Pointer to output image
*
* @param rows Number of rows in input image
*
* @param cols Number of columns in input image
*
* @return None
*
*/
void p_median3x3_f32(const float *x, float *r, int rows, int cols)
{
float buffer[9];
const float *px;
float *pr;
int i, j, buffer_col;
px = x;
pr = r;
for (i = 0; i < rows - 2; i++) {
// fully filling first window
buffer[0] = *px;
buffer[1] = *(px + 1);
buffer[2] = *(px + 2);
buffer[3] = *(px + cols);
buffer[4] = *(px + cols + 1);
buffer[5] = *(px + cols + 2);
buffer[6] = *(px + cols + cols);
buffer[7] = *(px + cols + cols + 1);
buffer[8] = *(px + cols + cols + 2);
p_median_f32(buffer, pr, 9);
pr++;
px += 3;
// other windows differ only by one column
// so only one is exchanged
for (j = 0; j < cols - 3; j++) {
buffer_col = j % 3;
buffer[buffer_col] = *px;
buffer[buffer_col + 3] = *(px + cols);
buffer[buffer_col + 6] = *(px + cols + cols);
*pr = opt_med9(buffer);
pr++;
px++;
}
}
}