-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathmain.cpp
96 lines (76 loc) · 1.89 KB
/
main.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#define IMAGE_WIDTH 640
#define IMAGE_HEIGHT 480
#define S (IMAGE_WIDTH/8)
#define T (0.15f)
void adaptiveThreshold(unsigned char* input, unsigned char* bin)
{
unsigned long* integralImg = 0;
int i, j;
long sum=0;
int count=0;
int index;
int x1, y1, x2, y2;
int s2 = S/2;
// create the integral image
integralImg = (unsigned long*)malloc(IMAGE_WIDTH*IMAGE_HEIGHT*sizeof(unsigned long*));
for (i=0; i<IMAGE_WIDTH; i++)
{
// reset this column sum
sum = 0;
for (j=0; j<IMAGE_HEIGHT; j++)
{
index = j*IMAGE_WIDTH+i;
sum += input[index];
if (i==0)
integralImg[index] = sum;
else
integralImg[index] = integralImg[index-1] + sum;
}
}
// perform thresholding
for (i=0; i<IMAGE_WIDTH; i++)
{
for (j=0; j<IMAGE_HEIGHT; j++)
{
index = j*IMAGE_WIDTH+i;
// set the SxS region
x1=i-s2; x2=i+s2;
y1=j-s2; y2=j+s2;
// check the border
if (x1 < 0) x1 = 0;
if (x2 >= IMAGE_WIDTH) x2 = IMAGE_WIDTH-1;
if (y1 < 0) y1 = 0;
if (y2 >= IMAGE_HEIGHT) y2 = IMAGE_HEIGHT-1;
count = (x2-x1)*(y2-y1);
// I(x,y)=s(x2,y2)-s(x1,y2)-s(x2,y1)+s(x1,x1)
sum = integralImg[y2*IMAGE_WIDTH+x2] -
integralImg[y1*IMAGE_WIDTH+x2] -
integralImg[y2*IMAGE_WIDTH+x1] +
integralImg[y1*IMAGE_WIDTH+x1];
if ((long)(input[index]*count) < (long)(sum*(1.0-T)))
bin[index] = 0;
else
bin[index] = 255;
}
}
free (integralImg);
}
int main (int argc, char** argv)
{
IplImage* cvFrame;
IplImage* binImg;
int key;
cvFrame = cvLoadImage("image.bmp", -1);
binImg = cvCreateImage(cvSize(IMAGE_WIDTH, IMAGE_HEIGHT), 8, 1);
cvNamedWindow("Input", 1);
cvNamedWindow("Output", 1);
adaptiveThreshold((unsigned char*)cvFrame->imageData, (unsigned char*)binImg->imageData);
cvShowImage("Input", cvFrame);
cvShowImage("Output", binImg);
key = cvWaitKey(0);
return 0;
}