forked from jingweizhanghuai/image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threshold.c
99 lines (79 loc) · 1.76 KB
/
threshold.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 <stdio.h>
#include "type.h"
#include "err.h"
void imgThreshold(ImgMat *src,ImgMat *dst,int cn,ImgThreshold *threshold)//int *thresh,int *value)
{
#ifdef DEBUG
SOURCE_ERROR_CHECK(imgThreshold,src);
DESTINATION_ERROR_CHECK(imgThreshold,dst);
if(threshold==NULL)
{
printf("IMG Error:\n\tin imgThreshold:\n");
exit(0);
}
#endif
int thresh_num = threshold->thresh_num;
int i,j,k;
int img_size;
img_size = src->width*src->height;
int type_cn;
type_cn = ((src->type)>>3)+1;
unsigned char *p_src;
p_src = src->data.ptr+cn;
unsigned char *p_dst;
p_dst = dst->data.ptr+cn;
int thresh[11];
int value[10];
thresh[0] = 0;
for(k=0;k<thresh_num;k++)
{
value[k] = threshold->value[k];
thresh[k+1] = threshold->thresh[k];
}
value[k] = threshold->value[k];
thresh[k+1] = 256;
int data;
printf("thresh[0] is %d\n",thresh[0]);
printf("value is %d,%d\n",value[0],value[1]);
for(j=0;j<type_cn;j++)
{
if((j+1)!=cn)
copy_mat_data(p_src,p_dst,img_size);
else
{
for(i=0;i<img_size;i++)
{
data = p_src[i];
for(k=0;k<thresh_num+1;k++)
if((data>=thresh[k])&&(data<thresh[k+1]))
{
if(value[k] == NOT_CHANGE_WITH_THRESHOLD)
p_dst[i] = data;
else
p_dst[i] = value[k];
break;
}
}
}
p_src = p_src+img_size;
p_dst = p_dst+img_size;
}
}
ImgMat *imgCreateMat(int height,int width,char type);
void Threshold(ImgMat *src,ImgMat *dst,int cn,ImgThreshold *threshold)
{
#ifdef DEBUG
SOURCE_ERROR_CHECK(imgThreshold,src);
#endif
if(dst==NULL)
{
dst = imgCreateMat(src->height,src->width,src->type);
imgThreshold(src,dst,cn,threshold);
free(src->data.ptr);
free(src->hidinfo);
*src = *dst;
free(dst);
}
else
imgThreshold(src,dst,cn,threshold);
}