1 #include <math.h>
2 #include <values.h>
3 #include <tina/sys.h>
4 #include <tina/sysfuncs.h>
5 #include <tina/math.h>
6 #include <tina/mathfuncs.h>
7
8 #define s2(a, b) {register int t; if ((t=b-a)< 0) {a += t; b -= t;}}
9 #define mn3(a, b, c) s2(a, b); s2(a, c);
10 #define mx3(a, b, c) s2(b, c); s2(a, c);
11 #define mnmx3(a, b, c) mx3(a, b, c); s2(a, b);
12 #define mnmx4(a, b, c, d) s2(a, b); s2(c, d); s2(a, c); s2(b, d);
13 #define mnmx5(a, b, c, d, e) s2(a, b); s2(c, d); mn3(a, c, e); mx3(b, d, e);
14 #define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); \
15 mn3(a, b, c); mx3(d, e, f);
16
17 int med3by3(int *b1, int *b2, int *b3)
18 {
19 register int r1, r2, r3, r4, r5, r6;
20 r1 = *b1--; r2 = *b1++; r3 = *b1++;
21 r4 = *b2--; r5 = *b2++; r6 = *b2++;
22 mnmx6(r1, r2, r3, r4, r5, r6);
23 r1 = *b3--;
24 mnmx5(r1, r2, r3, r4, r5);
25 r1 = *b3++;
26 mnmx4(r1, r2, r3, r4);
27 r1 = *b3++;
28 mnmx3(r1, r2, r3);
29 return(r2);
30 }
31
32 float med3by3f(float *b1, float *b2, float *b3)
33 {
34 float r[9];
35 int label[9];
36 int i,j;
37 float max=-MAXFLOAT,min=MAXFLOAT;
38 int max_label=0,min_label=0;
39
40 r[0] = b1[-1]; r[1] = b1[0]; r[2] = b1[1];
41 r[3] = b2[-1]; r[4] = b2[0]; r[5] = b2[1];
42 r[6] = b3[-1]; r[7] = b3[0]; r[8] = b3[1];
43 for (i=0;i<9;i++) label[i] = 1;
44 for(j=0;j<5;j++)
45 {
46 for(i=0;i<9;i++)
47 {
48 if (label[i] && (r[i]<=min))
49 {
50 min_label = i;
51 min = r[i];
52 }
53 if (label[i] && (r[i]>=max))
54 {
55 max_label = i;
56 max = r[i];
57 }
58 }
59 label[max_label] = 0;
60 label[min_label] = 0;
61 max = -MAXFLOAT;
62 min = MAXFLOAT;
63 }
64 return(r[min_label]);
65 }
66 /**
67 3x3 median filter
68 At edges adds e.g. new first row equal to old before filtering
69 **/
70
71 Imrect *imc_median(Imrect *im1)
72 {
73 Imrect *im2;
74 Imregion *roi;
75 int *row, *row1, *row2, *row3;
76 int lx, ux, ly, uy;
77 int i, j;
78
79 if (im1 == NULL)
80 return (NULL);
81
82 roi = im1->region;
83 if (roi == NULL)
84 return (NULL);
85 lx = roi->lx;
86 ux = roi->ux;
87 ly = roi->ly;
88 uy = roi->uy;
89
90 im2 = im_alloc(im1->height, im1->width, roi, uchar_v);
91 row = ivector_alloc(lx, ux);
92 row1 = ivector_alloc(lx-1, ux+1);
93 row2 = ivector_alloc(lx-1, ux+1);
94 row3 = ivector_alloc(lx-1, ux+1);
95 im_get_row(row2, im1, ly, lx, ux);
96 row2[lx-1] = row2[lx]; row2[ux] = row2[ux-1];
97 im_get_row(row3, im1, ly, lx, ux);
98 row3[lx-1] = row3[lx]; row3[ux] = row3[ux-1];
99 for (i = ly; i < uy; ++i)
100 {
101 int *temp = row1;
102 row1 = row2;
103 row2 = row3;
104 row3 = temp;
105
106 if(i != uy-1)
107 im_get_row(row3, im1, i+1, lx, ux);
108 else
109 im_get_row(row3, im1, i, lx, ux);
110 row3[lx-1] = row3[lx]; row3[ux] = row3[ux-1];
111
112 for (j = lx; j < ux; ++j)
113 row[j] = med3by3(&row1[j], &row2[j], &row3[j]);
114
115 im_put_row(row, im2, i, lx, ux);
116 }
117
118 ivector_free(row, lx);
119 ivector_free(row1, lx-1);
120 ivector_free(row2, lx-1);
121 ivector_free(row3, lx-1);
122 return (im2);
123 }
124
125 Imrect *imi_median(Imrect *im1)
126 {
127 Imrect *im2;
128 Imregion *roi;
129 int *row, *row1, *row2, *row3;
130 int lx, ux, ly, uy;
131 int i, j;
132
133 if (im1 == NULL)
134 return (NULL);
135
136 roi = im1->region;
137 if (roi == NULL)
138 return (NULL);
139 lx = roi->lx;
140 ux = roi->ux;
141 ly = roi->ly;
142 uy = roi->uy;
143
144 im2 = im_alloc(im1->height, im1->width, roi, int_v);
145 row = ivector_alloc(lx, ux);
146 row1 = ivector_alloc(lx-1, ux+1);
147 row2 = ivector_alloc(lx-1, ux+1);
148 row3 = ivector_alloc(lx-1, ux+1);
149 im_get_row(row2, im1, ly, lx, ux);
150 row2[lx-1] = row2[lx]; row2[ux] = row2[ux-1];
151 im_get_row(row3, im1, ly, lx, ux);
152 row3[lx-1] = row3[lx]; row3[ux] = row3[ux-1];
153 for (i = ly; i < uy; ++i)
154 {
155 int *temp = row1;
156 row1 = row2;
157 row2 = row3;
158 row3 = temp;
159
160 if(i != uy-1)
161 im_get_row(row3, im1, i+1, lx, ux);
162 else
163 im_get_row(row3, im1, i, lx, ux);
164 row3[lx-1] = row3[lx]; row3[ux] = row3[ux-1];
165
166 for (j = lx; j < ux; ++j)
167 row[j] = med3by3(&row1[j], &row2[j], &row3[j]);
168
169 im_put_row(row, im2, i, lx, ux);
170 }
171
172 ivector_free(row, lx);
173 ivector_free(row1, lx-1);
174 ivector_free(row2, lx-1);
175 ivector_free(row3, lx-1);
176 return (im2);
177 }
178
179 Imrect *imf_median(Imrect *im1)
180 {
181 Imrect *im2;
182 Imregion *roi;
183 float *row, *row1, *row2, *row3;
184 int lx, ux, ly, uy;
185 int i, j;
186
187 if (im1 == NULL)
188 return (NULL);
189
190 roi = im1->region;
191 if (roi == NULL)
192 return (NULL);
193 lx = roi->lx;
194 ux = roi->ux;
195 ly = roi->ly;
196 uy = roi->uy;
197
198 im2 = im_alloc(im1->height, im1->width, roi, float_v);
199 row = fvector_alloc(lx, ux);
200 row1 = fvector_alloc(lx-1, ux+1);
201 row2 = fvector_alloc(lx-1, ux+1);
202 row3 = fvector_alloc(lx-1, ux+1);
203 im_get_rowf(row2, im1, ly, lx, ux);
204 row2[lx-1] = row2[lx]; row2[ux] = row2[ux-1];
205 im_get_rowf(row3, im1, ly, lx, ux);
206 row3[lx-1] = row3[lx]; row3[ux] = row3[ux-1];
207 for (i = ly; i < uy; ++i)
208 {
209 float *temp = row1;
210 row1 = row2;
211 row2 = row3;
212 row3 = temp;
213
214 if(i != uy-1)
215 im_get_rowf(row3, im1, i+1, lx, ux);
216 else
217 im_get_rowf(row3, im1, i, lx, ux);
218 row3[lx-1] = row3[lx]; row3[ux] = row3[ux-1];
219
220 for (j = lx; j < ux; ++j)
221 row[j] = med3by3f(&row1[j], &row2[j], &row3[j]);
222
223 im_put_rowf(row, im2, i, lx, ux);
224 }
225
226 fvector_free(row, lx);
227 fvector_free(row1, lx-1);
228 fvector_free(row2, lx-1);
229 fvector_free(row3, lx-1);
230 return (im2);
231 }
232
233 Imrect *imz_median(Imrect *im)
234 {
235 return(NULL);
236 }
237
238 Imrect *im_median(Imrect *im)
239 {
240 if(im == NULL)
241 return(NULL);
242 switch(im->vtype)
243 {
244 case uchar_v:
245 return(imc_median(im));
246 case short_v:
247 case ushort_v:
248 case int_v:
249 return(imi_median(im));
250 case float_v:
251 return(imf_median(im));
252 case complex_v:
253 return(imz_median(im));
254 default:
255 return(NULL);
256 }
257 }
258
259
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.