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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
// Created by 欧 长坤 on 13-9-16.
// Copyright (c) 2013年 欧 长坤 . All rights reserved.
// 接口:arr_1,信号1所在数组;arr_2,信号2所在数组;lenth_1,信号1所在数组长度;lenth_2,信号2所在数组长度
// 使用:函数返回信号1与信号2的规范相关系数。
double R(double *arr_1, int lenth_1, double *arr_2, int lenth_2)
{
double sum_1 = 0;
for (int i = 0; i < lenth_1; ++i)
{
sum_1 += arr_1[i]*arr_2[i];
}
double sum_2 = 0;
double sum_3 = 0;
for (int i = 0; i < lenth_2; ++i)
{
sum_2 += arr_1[i]*arr_1[i];
sum_3 += arr_2[i]*arr_2[i];
}
double sum_4 = sqrt(sum_2*sum_3); // need to #include
return (sum_1/sum_4);
}
// Created by 郭 瞾阳 on 13-9-16.
// Copyright (c) 2013年 郭 瞾阳 . All rights reserved.
// 信号提取部分
int shang_1(IplImage *img,double* black_arr)
{
int i = 0;
double x1,x2,x3;
uchar*par = (uchar *)(img-&imageData);
for(int x=0;xwidth;x++,i++)
{
x1 = (double)par[3*x]/255;
x2 = (double)par[3*x+1]/255;
x3 = (double)par[3*x+2]/255;
black_arr[i] = (x1+x2+x3)/3;
}
return i;
}
int xia_1(IplImage * img,double* black_arr)
{
int i = 0;
double x1,x2,x3;
uchar* par = (uchar*)(img-&imageData+(img-&height-1)*(img-&widthStep));
for(int x=0 ;xwidth;x++,i++)
{
x1 = (double)par[3*x]/255;
x2 = (double)par[3*x+1]/255;
x3 = (double)par[3*x+2]/255;
black_arr[i] = (x1+x2+x3)/3;
}
return i;
}
int zuo_1(IplImage *img,double* black_arr)
{
int i = 0;
double x1,x2,x3;
for(int y = 0;yheight;y++,i++)
{
uchar * par =(uchar *)(img-&imageData+y*img-&widthStep);
x1 = (double)par[0]/255;
x2 = (double)par[1]/255;
x3 = (double)par[2]/255;
black_arr[i] = (x1+x2+x3)/3;
}
return i;
}
int you_1(IplImage *img,double* black_arr)
{
int i = 0;
int x = img-&width-1;
double x1,x2,x3;
for(int y = 0;yheight;y++,i++)
{
uchar * par = (uchar *)(img-&imageData+y*img-&widthStep);
x1 = (double)par[3*x]/255;
x2 = (double)par[3*x+1]/255;
x3 = (double)par[3*x+2]/255;
black_arr[i] = (x1 + x2 + x3)/3;
}
return i;
}
// Created by 欧 长坤 on 13-9-16.
// Copyright (c) 2013年 欧 长坤 . All rights reserved.
// main()函数中具体操作的部分
char filepath[100] = "C:\\B\\5\\000a.bmp";
char filepath_2[100] = "C:\\B\\5\\001a.bmp";
char filepath_3[100] = "C:\\B\\5\\001b.bmp";
std::ofstream fout("a.txt");
for (int dota = 0; dota <= 208; dota++)
{
numChangeFilePath(filepath,dota);
IplImage* img = cvLoadImage(filepath);
IplImage* img_do = cvLoadImage(filepath_2);
IplImage* img_do_2 = cvLoadImage(filepath_3);
double arr_0[500];
int arr_0_lenth = you_1(img,arr_0);
double arr_1[500];
int arr_1_lenth = zuo_1(img_do,arr_1);
double arr_2[500];
int arr_2_lenth = zuo_1(img_do,arr_2);
double temp = 0.5;
double temp_2 = 0.5;
int flag_1 = 0;
int flag_2 = 0;
for (int i = 1; i <= 208; ++i)
{
numChangeFilePath(filepath_2,i);
cvReleaseImage(&img_do);
img_do = cvLoadImage(filepath_2);
arr_1_lenth = zuo_1(img_do,arr_1);
if (R(arr_0,arr_0_lenth,arr_1,arr_1_lenth)&temp)
{
temp = R(arr_0,arr_0_lenth,arr_1,arr_1_lenth);
flag_1 = i;
}
}
for (int i = 1; i <= 208; ++i)
{
numChangeFilePath(filepath_3,i);
cvReleaseImage(&img_do_2);
img_do_2 = cvLoadImage(filepath_3);
arr_2_lenth = zuo_1(img_do_2,arr_2);
if (R(arr_0,arr_0_lenth,arr_2,arr_2_lenth)&temp_2)
{
temp_2 = R(arr_0,arr_0_lenth,arr_2,arr_2_lenth);
flag_2 = i;
}
}
//printf("(img:%d,next_a:%d,next_b_%d)",dota,flag_1,flag_2);
fout << "(" << dota << ", " << flag_1 << ", " << flag_2 << ")" << std::endl;
}
// Created by 郭 瞾阳 on 13-9-16.
// Copyright (c) 2013年 郭 瞾阳 . All rights reserved.
// 接口:img,进行边界积分的图片
// 使用:对图片左侧的边界进行积分,返回积分值
int countLeftEdgeIntegral(IplImage* img)
{
int black = 0;
for(int y = 0; yheight; y++)
{
unsigned char * par =(unsigned char *)(img-&imageData+y*img-&widthStep);
if(par[0]==0 && par[1]==0 && par[2]==0)
black++;
}
return black;
}
// 接口:img,进行边界积分的图片
// 使用:对图片右侧的边界进行积分,返回积分值
int countRightEdgeIntegral(IplImage* img)
{
int black = 0;
int x = img-&width-1;
for(int y = 0;yheight;y++)
{
unsigned char * par = (unsigned char *)(img-&imageData+y*img-&widthStep);
if(par[3*x+0]==0 && par[3*x+1]==0 && par[3*x+2]==0)
black++;
}
return black;
}
// 接口:img,进行边界积分的图片
// 使用:对图片上侧的边界进行积分,返回积分值
int countTopEdgeIntegral(IplImage* img)
{
int black = 0;
for(int x=0; xwidth; x++)
{
unsigned char * par =(unsigned char *)(img-&imageData);
if(par[3*x] == 0 && par[3*x+1] == 0 && par[3*x+2] == 0)
black++;
}
return black;
}
// 接口:img,进行边界积分的图片
// 使用:对图片下侧的边界进行积分,返回积分值
int countBottomEdgeIntegral(IplImage* img)
{
int black = 0;
uchar * par = (uchar *)(img-&imageData+(img-&height-1)*(img-&widthStep));
for(int x=0 ;xwidth;x++)
{
if(par[3*x]==0 && par[3*x+1]==0 && par[3*x+2]==0)
black++;
}
return black;
}
// Created by 欧 长坤 on 13-9-16.
// Copyright (c) 2013年 欧 长坤 . All rights reserved.
// 接口:img_left,左图片
// img_right,右图片
// 使用:将右图片拼接到左图片的右侧,保存在一个新的IplImage中,并返回该地址。
IplImage * stitchingImage(IplImage* zuo,IplImage * you)
{
CvSize tt;
tt.width = zuo-&width+you-&width;
tt.height = zuo-&height;
IplImage* heti = cvCreateImage(tt,zuo-&depth,zuo-&nChannels);
for(int y = 0;yheight;y++)
{
uchar* par = (uchar*)(heti-&imageData+y*heti-&widthStep);
uchar* par2 = (uchar*)(zuo-&imageData+y*zuo-&widthStep);
uchar* par3 = (uchar*)(you-&imageData+y*you-&widthStep);
for(int x = 0;xwidth;x++)
{
par[3*x+0] = par2[3*x+0];
par[3*x+1] = par2[3*x+1];
par[3*x+2] = par2[3*x+2];
}
for(int x = 0;xwidth;x++)
{
par[3*zuo-&width+3*x+0] = par3[3*x+0];
par[3*zuo-&width+3*x+1] = par3[3*x+1];
par[3*zuo-&width+3*x+2] = par3[3*x+2];
}
}
return heti;
}
IplImage * stitchingImage_shangxia(IplImage* shang,IplImage * xia)
{
CvSize tt;
tt.height = shang-&height+xia-&height;
tt.width = shang-&width;
IplImage* heti = cvCreateImage(tt,shang-&depth,shang-&nChannels);
for(int y = 0;yheight;y++)
{
uchar* par = (uchar*)(heti-&imageData+y*heti-&widthStep);
uchar* parr = (uchar*)(heti-&imageData+(y+shang-&height)*heti-&widthStep);
uchar* par2 = (uchar*)(shang-&imageData+y*shang-&widthStep);
uchar* par3 = (uchar*)(xia-&imageData+y*xia-&widthStep);
for(int x = 0;xwidth;x++)
{
par[3*x+0] = par2[3*x+0];
par[3*x+1] = par2[3*x+1];
par[3*x+2] = par2[3*x+2];
}
}
for(int y = 0;yheight;y++)
{
uchar* par = (uchar*)(heti-&imageData+y*heti-&widthStep);
uchar* parr = (uchar*)(heti-&imageData+(y+shang-&height)*heti-&widthStep);
uchar* par2 = (uchar*)(shang-&imageData+y*shang-&widthStep);
uchar* par3 = (uchar*)(xia-&imageData+y*xia-&widthStep);
for(int x = 0;xwidth;x++)
{
parr[3*x+0] = par3[3*x+0];
parr[3*x+1] = par3[3*x+1];
parr[3*x+2] = par3[3*x+2];
}
}
return heti;
}
// Created by 黄 沐 on 13-9-16.
// Copyright (c) 2013年 黄 沐 . All rights reserved.
// 接口:filepath,图片文件的系统路径
// 使用:利用Add来访问到下一张图片路径
char* nextImageFilePath(char *filepath){
int i;
char *p;
char* save = filepath;
p=filepath;
for(i=0;filepath[i]!='\0';i++,p++){
if(filepath[i]<='9' && filepath[i+1]<='9' && filepath[i+2]<'9' && filepath[i+3]=='.'){
p++;
p++;
*p=*p+1;
return save;
}
if(filepath[i]<'9' && filepath[i+1]=='9' && filepath[i+2]=='.'){
*p=*p+1;
p++;
*p=*p-9;
return save;
}
if(filepath[i]=='9' && filepath[i+1]=='9' && filepath[i+2]=='.'){
p--;
*p=*p+1;
p++;
*p=*p-9;
p++;
*p=*p-9;
return save;
}
}
}
// 接口:filepath,图片文件的系统路径,num,修改文件名的数字
// 使用:传入num将filepath中最后的文件xxx.bmp改为num.bmp
char* numChangeFilePath(char *filepath, int num) {
int i,j;
int n_bai,n_shi,n_ge;
char* p;
char* save = filepath;
p=filepath;
n_ge=num%10;
n_bai=num/100;
if(num&=100)
{
n_shi=num/10;
n_shi=n_shi%10;
}
else n_shi=num/10;
for(i=0;filepath[i]!='\0';i++,p++){
if(filepath[i]<='9' && filepath[i+1]<='9' && filepath[i+3]=='.'){
*p='0';
for(j=0;j<n_bai;j++){
*p=*p+1;
}
p++;
*p='0';
for(j=0;j<n_shi;j++){
*p=*p+1;
}
p++;
*p='0';
for(j=0;j<n_ge;j++){
*p=*p+1;
}
return save;
}
}
}
// 接口:filepath,图片文件的系统路径,num,修改文件名的字符串
// 使用:仅适用于附件5
char* strChangeFilePath(char *filepath, char num[])
{
int i;
char* p;
char* save;
p=filepath;
save=filepath;
for(i=0;filepath[i]!='\0';i++,p++)
{
if(filepath[i]<='9' && filepath[i+1]<='9' && filepath[i+4]=='.')
{
*p='0';
*p=num[0];
p++;
*p='0';
*p=num[1];
p++;
*p='0';
*p=num[2];
p++;
*p='0';
*p=num[3];
return save;
}
}
return save;
}
|