-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment.cpp
More file actions
executable file
·306 lines (281 loc) · 8.21 KB
/
segment.cpp
File metadata and controls
executable file
·306 lines (281 loc) · 8.21 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "stdafx.h"
#include "segment.h"
#include "math.h"
//ア゚ヤオシ・・
void sobel(unsigned char *imgBuf, int width, int height, int biBitCount)
{
unsigned char *buf=new unsigned char[width*height*biBitCount];
int i, j, k, x, y, lineByte=width*biBitCount;
for(i=1; i<height-1;i++){
for(j=1; j<width-1;j++){
for(k=0;k<biBitCount; k++){
x=*(imgBuf+(i+1)*lineByte+(j-1)*biBitCount +k)
+2**(imgBuf+(i)*lineByte+(j-1)*biBitCount+ k)
+*(imgBuf+(i-1)*lineByte+(j-1)*biBitCount+ k)
-*(imgBuf+(i+1)*lineByte+(j+1)*biBitCount+ k)
-2**(imgBuf+(i)*lineByte+(j+1)*biBitCount+ k)
-*(imgBuf+(i-1)*lineByte+(j+1)*biBitCount+ k);
y=*(imgBuf+(i+1)*lineByte+(j-1)*biBitCount +k)
+2**(imgBuf+(i+1)*lineByte+j*biBitCount +k)
+*(imgBuf+(i+1)*lineByte+(j+1)*biBitCount +k)
-*(imgBuf+(i-1)*lineByte+(j-1)*biBitCount +k)
-2**(imgBuf+(i-1)*lineByte+j*biBitCount +k)
-*(imgBuf+(i-1)*lineByte+(j+1)*biBitCount +k);
if(sqrt(double(x*x+y*y))<=255)
*(buf+i*lineByte+j*biBitCount+k)=sqrt(double(x*x+y*y));
else
*(buf+i*lineByte+j*biBitCount+k)=255;
}
}
}
for(i=0;i<lineByte*height;i++)
*(imgBuf+i)=*(buf+i);
delete []buf;
}
//robot
void robot(unsigned char *imgBuf, int width, int height, int biBitCount)
{
unsigned char *buf=new unsigned char[width*height*biBitCount];
int i, j, k, x, y, lineByte=width*biBitCount;
int max=0;
for(i=1; i<height-1;i++){
for(j=1; j<width-1;j++){
for(k=0;k<biBitCount; k++){
x=*(imgBuf+i*lineByte+j*biBitCount +k)
-*(imgBuf+(i-1)*lineByte+(j+1)*biBitCount+ k);
y=*(imgBuf+i*lineByte+(j+1)*biBitCount +k)
-*(imgBuf+(i-1)*lineByte+j*biBitCount +k);
if(sqrt(double(x*x+y*y))<=255)
*(buf+i*lineByte+j*biBitCount+k)=sqrt(double(x*x+y*y));
else
*(buf+i*lineByte+j*biBitCount+k)=255;
if(*(buf+i*lineByte+j*biBitCount+k)>max)
max=*(buf+i*lineByte+j*biBitCount+k);
}
}
}
for(i=0;i<lineByte*height;i++)
*(imgBuf+i)=*(buf+i)*255/max;
delete []buf;
}
//prewitt
void prewitt(unsigned char *imgBuf, int width, int height, int biBitCount)
{
unsigned char *buf=new unsigned char[width*height*biBitCount];
int i, j, k, x, y, lineByte=width*biBitCount;
for(i=1; i<height-1;i++){
for(j=1; j<width-1;j++){
for(k=0;k<biBitCount; k++){
x=*(imgBuf+(i+1)*lineByte+(j-1)*biBitCount +k)
+*(imgBuf+(i)*lineByte+(j-1)*biBitCount+ k)
+*(imgBuf+(i-1)*lineByte+(j-1)*biBitCount+ k)
-*(imgBuf+(i+1)*lineByte+(j+1)*biBitCount+ k)
-*(imgBuf+(i)*lineByte+(j+1)*biBitCount+ k)
-*(imgBuf+(i-1)*lineByte+(j+1)*biBitCount+ k);
y=*(imgBuf+(i+1)*lineByte+(j-1)*biBitCount +k)
+*(imgBuf+(i+1)*lineByte+j*biBitCount +k)
+*(imgBuf+(i+1)*lineByte+(j+1)*biBitCount +k)
-*(imgBuf+(i-1)*lineByte+(j-1)*biBitCount +k)
-*(imgBuf+(i-1)*lineByte+j*biBitCount +k)
-*(imgBuf+(i-1)*lineByte+(j+1)*biBitCount +k);
if(sqrt(double(x*x+y*y))<=255)
*(buf+i*lineByte+j*biBitCount+k)=sqrt(double(x*x+y*y));
else
*(buf+i*lineByte+j*biBitCount+k)=255;
}
}
}
for(i=0;i<lineByte*height;i++)
*(imgBuf+i)=*(buf+i);
delete []buf;
}
//laplacian
void laplacian(unsigned char *imgBuf, int width, int height, int biBitCount)
{
unsigned char *buf=new unsigned char[width*height*biBitCount];
int i, j, k, x, lineByte=width*biBitCount;
for(i=1; i<height-1;i++){
for(j=1; j<width-1;j++){
for(k=0;k<biBitCount; k++){
x=4**(imgBuf+i*lineByte+j*biBitCount +k)
-*(imgBuf+(i+1)*lineByte+j*biBitCount+ k)
-*(imgBuf+(i-1)*lineByte+j*biBitCount+ k)
-*(imgBuf+i*lineByte+(j+1)*biBitCount+ k)
-*(imgBuf+i*lineByte+(j-1)*biBitCount+ k) ;
if(x>255)
x=255;
if(x<0)
x=0;
*(buf+i*lineByte+j*biBitCount+k)=x;
}
}
}
for(i=0;i<lineByte*height;i++)
*(imgBuf+i)=*(buf+i);
delete []buf;
}
//耙ヨオキヨク・
void ErZhiSegment(unsigned char *imgBuf, int width, int height, int thresh)
{
for(int i=0;i<width*height;i++){
if(*(imgBuf+i)<thresh)
*(imgBuf+i)=0;
else
*(imgBuf+i)=255;
}
}
//エ・耙ヨオ
int Threshold(long HistBuf[256]) /* return the threshold value */
{
float PHS[256];
float A,B,D,P,RH,RM,RMEAN,RI,BMAX;
long Tab=0;
float EPS = (float)1.0E-10;
int Line;
int JTHD;
for(Line=0;Line<=255;Line++)
Tab+=HistBuf[Line];
if(Tab==0)
return 0;
for(Line=0;Line<=255;Line++)
PHS[Line]=(float)HistBuf[Line]/Tab;
RM=0.0;
for(Line=0;Line<=255;Line++){
RI=(float)Line;
RH=RI*PHS[Line];
RM=RM+RH;
}
RMEAN=RM;
BMAX=0.0;
P=0.0;
A=0.0;
for(Line=0;Line<=254;Line++){
RH=PHS[Line];
P=P+RH;
A=A+(float)Line*RH;
B=RM*P-A;
D = P * ((float)1.0 - P);
if(D<EPS) continue;
B=B*B/D;
if(B>=BMAX){
BMAX=B;
JTHD=Line;
}
}
JTHD=(int)((float)JTHD*1.0);
return(JTHD);
}
//モテエ・耙ヨオラヤカッキヨク・
void autoThreshSegment(unsigned char *imgBuf, int width, int height)
{
int i;
long hist[256];
for(i=0;i<256;i++)
hist[i]=0;
for(i=0;i<height*width;i++)
hist[*(imgBuf+i)]++;
int t=Threshold(hist);
for(i=0;i<width*height;i++){
if(*(imgBuf+i)<t)
*(imgBuf+i)=0;
else
*(imgBuf+i)=255;
}
}
///ツヒイィ
//セオツヒイィ
void meanFilter(unsigned char *imgBuf, int width, int height, int biBitCount,
int *mask, int masksize)
{
unsigned char *buf=new unsigned char[width*height*biBitCount];
int i, j, k, l, t, x, lineByte=width*biBitCount;
int count=0;
for(i=0;i<masksize*masksize;i++){
if(mask[i]==1)
count++;
}
for(i=0; i<height-masksize;i++){
for(j=0; j<width-masksize;j++){
for(k=0;k<biBitCount; k++){
x=0;
for(t=0; t<masksize;t++){
for(l=0;l<masksize;l++){
x += mask[t*masksize+l] * *(imgBuf+(i+t)*lineByte+(j+l)*biBitCount +k);
}
}
*(buf+(i+masksize/2)*lineByte+(j+masksize/2)*biBitCount+k)=x/count;
}
}
}
for(i=0;i<lineByte*height;i++)
*(imgBuf+i)=*(buf+i);
delete []buf;
}
//ヨミヨオツヒイィ
void midFilter(unsigned char *imgBuf, int width, int height, int biBitCount,
int *mask, int masksize)
{
unsigned char *buf=new unsigned char[width*height*biBitCount];
int i, j, k, t, l, x, min, pos, count, lineByte=width*biBitCount;
int array[49];
for(i=0; i<height-masksize;i++){
for(j=0; j<width-masksize;j++){
for(k=0;k<biBitCount; k++){
count=0;
for(t=0;t<masksize;t++){
for(l=0;l<masksize;l++)
if(mask[t*masksize+l]==1){
array[count]=*(imgBuf+(i+t)*lineByte+(j+l)*biBitCount +k);
count++;
}
}
//ナナミ・
for(t=0;t<count-1;t++){
min=array[t], pos=0;
for(l=t;l<count;l++){
if(min>array[l]){
min=array[l];
pos=l;
}
}
if(pos!=t){
x=array[t];
array[t]=array[pos];
array[pos]=x;
}
}
*(buf+(i+masksize/2)*lineByte+(j+masksize/2)*biBitCount+k)=array[count/2];
}
}
}
for(i=0;i<lineByte*height;i++)
*(imgBuf+i)=*(buf+i);
delete []buf;
}
//ト」ー蠶ヒイィ maskハ菠・クセュケ鰓ササッ
void Filter(unsigned char *imgBuf, int width, int height, int biBitCount,
double *mask, int masksize)
{
unsigned char *buf=new unsigned char[width*height*biBitCount];
int i, j, k, l, t, lineByte=width*biBitCount;
double x;
for(i=0; i<height-masksize;i++){
for(j=0; j<width-masksize;j++){
for(k=0;k<biBitCount; k++){
x=0;
for(t=0; t<masksize;t++){
for(l=0;l<masksize;l++){
x += mask[t*masksize+l] * *(imgBuf+(i+t)*lineByte+(j+l)*biBitCount +k);
}
}
//ヘシマトヨワア゚ヤオテサモミヨオ
*(buf+(i+masksize/2)*lineByte+(j+masksize/2)*biBitCount+k)=(unsigned char)x;
}
}
}
for(i=0;i<lineByte*height;i++)
{
*(imgBuf+i)=*(buf+i);
}
delete []buf;
}