-
Notifications
You must be signed in to change notification settings - Fork 1
/
haar.c
79 lines (62 loc) · 1.97 KB
/
haar.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
#include "haar.h"
float accessPixel24(Matrix* m, float x, float y)
{
//printf("(%d,%d) ",x,y);
float newx=((float)(x*(m->xmax-m->xmin)))/24.0 -1 + m->xmin;
float newy=((float)(y*(m->ymax-m->ymin)))/24.0 -1 + m->ymin;
//printf("%f,%f ",newx,newy);
int intx=newx;
int inty=newy;
int decimalx=newx-intx;
int decimaly=newy-inty;
if(intx<0||inty<0||intx>=m->x||inty>=m->y)
return 0;
if(intx>=m->x-1||inty>=m->y-1)
return ((float)m->array[intx][inty])/((m->xmax-m->xmin)*(m->ymax-m->ymin));
float r = m->array[intx][inty] +
decimalx*(m->array[intx+1][inty]) +
decimaly*(m->array[intx][inty+1]);
//printf("%f ",((float)r)/(m->x*m->y));
return r/((m->xmax-m->xmin)*(m->ymax-m->ymin));
}
float rectSum(Matrix* m, float xmin, float ymin, float xmax, float ymax)
{
float s=accessPixel24(m, xmax, ymax)
-accessPixel24(m, xmax, ymin)
-accessPixel24(m, xmin, ymax)
+accessPixel24(m, xmin, ymin);
return s;
}
float haar(Matrix* m, int type, int xmin, int ymin, int xmax, int ymax)
{
float ymed=(ymin+ymax)/2;
float xmed=(xmin+xmax)/2;
float xthird=(xmin+xmax)/3;
switch(type)
{
case 1:
return rectSum(m, xmed, ymin, xmax, ymax)
-rectSum(m, xmin, ymin, xmed, ymax);
case 2:
return rectSum(m, xmin, ymin, xmax, ymed)
-rectSum(m, xmin, ymed, xmax, ymax);
case 3:
return (2*rectSum(m, xmin+xthird, ymin, xmin+2*xthird, ymax))
-rectSum(m, xmin, ymin, xmax, ymax);
case 4:
return rectSum(m, xmin, ymin, xmax, ymax)
-(2*rectSum(m, xmed, ymin, xmax, ymed))
-(2*rectSum(m, xmin, ymed, xmed, ymax));
}
return 0;
}
float relativeHaar(Matrix *m, int type, int xmin, int ymin, int xmax, int ymax)
{
double intVal = haar(m, type, xmin, ymin, xmax, ymax);
//double max = (((long)(xmax-xmin)*(ymax-ymin)*255) *
// ((long)(m->xmax-m->xmin)*(m->ymax-m->ymin)))/576;
//double max=(m->xmax-m->xmin)*(m->ymax-m->ymin);
double max=1;
//printf("(%d %d)(%d %d) %d =%f/%f=%f\n",xmin, ymin, xmax, ymax, type, intVal, max, intVal/max);
return intVal / max;
}