質問編集履歴
1
プログラムの新規変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -46,357 +46,145 @@
|
|
46
46
|
|
47
47
|
|
48
48
|
|
49
|
-
|
49
|
+
---------------------------------------------------------
|
50
50
|
|
51
|
-
|
51
|
+
変更しました。
|
52
52
|
|
53
|
-
|
53
|
+
先ほどのプログラムが正常に動作したため、以下のプログラムに変更しました。すると、
|
54
54
|
|
55
|
-
|
55
|
+
img.depth()==CV_8U||img.depth()==CV_32F) && img.type()==templ.type())
|
56
56
|
|
57
|
-
|
57
|
+
というエラーが出ます。
|
58
58
|
|
59
59
|
|
60
60
|
|
61
|
-
|
61
|
+
順番に確かめていったところ
|
62
62
|
|
63
|
-
|
63
|
+
scale_img=cvCreateImage(cvSize((int)(img->width*SCALE_X), (int)(img->height*SCALE_Y)), IPL_DEPTH_8U, 3);
|
64
64
|
|
65
|
-
|
65
|
+
以上の部分がおかしいと考えています。
|
66
|
+
|
67
|
+
1チャンネルで定義すると、scale_imgを表示しようとするとエラーが出ます。しかし、3チャンネルに変更すると、画像を表示できても、最終的にエラーが出ます。
|
68
|
+
|
69
|
+
どのような変更が必要なのでしょうか?
|
66
70
|
|
67
71
|
|
68
72
|
|
69
|
-
|
73
|
+
```全体のコード
|
70
74
|
|
71
|
-
|
75
|
+
//---------テンプレート画像を読み込む-------
|
76
|
+
|
77
|
+
temp_up=cvLoadImage("C:\\opencv_ICImage\\2017-1-12\\銀小ネジ\\ver2\\背景差分\\temp_up.bmp",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); //カラー読み込み
|
78
|
+
|
79
|
+
temp_side=cvLoadImage("C:\\opencv_ICImage\\2017-1-12\\銀小ネジ\\ver2\\背景差分\\temp_side.bmp",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); //カラー読み込み
|
72
80
|
|
73
81
|
|
74
82
|
|
75
|
-
|
83
|
+
for ( int number =1; number<717;++number)
|
76
84
|
|
77
|
-
|
85
|
+
{
|
78
86
|
|
79
|
-
IplImage * dst1 = NULL;
|
80
|
-
|
81
|
-
IplImage * dst2 = NULL;
|
82
|
-
|
83
|
-
IplImage * img = NULL;
|
84
|
-
|
85
|
-
IplImage * scale_img = NULL;
|
86
|
-
|
87
|
-
IplImage * copy_img1 = NULL;
|
88
|
-
|
89
|
-
|
87
|
+
int64 start = cv::getTickCount();//所要時間計測
|
90
|
-
|
91
|
-
IplImage * grey_img1 = NULL;
|
92
|
-
|
93
|
-
IplImage * grey_img2 = NULL;
|
94
88
|
|
95
89
|
|
96
90
|
|
97
|
-
//
|
91
|
+
//------------------------原画像読み込み-------------------------------
|
98
92
|
|
99
|
-
|
93
|
+
std::stringstream filename;
|
100
94
|
|
101
|
-
|
95
|
+
filename << "C:\\opencv_ICImage\\2017-1-12\\銀小ネジ\\ver2\\背景差分\\静止画\\sabun" << std::setfill('0') << std::setw(4) << number << ".bmp";
|
102
96
|
|
103
|
-
|
97
|
+
std::cout << "filename=" << filename.str() << std::endl;
|
104
98
|
|
105
|
-
IplImage
|
99
|
+
IplImage* img=cvLoadImage(filename.str().c_str());
|
106
100
|
|
107
|
-
|
101
|
+
//--------------------------------------------------------------------------
|
108
|
-
|
109
|
-
IplImage * haikei_img2 = NULL;
|
110
|
-
|
111
|
-
IplImage * haikei_up = NULL;
|
112
|
-
|
113
|
-
IplImage * haikei_side = NULL;
|
114
|
-
|
115
|
-
IplImage * grey_haikei = NULL;
|
116
|
-
|
117
|
-
IplImage * temp_haikei = NULL;
|
118
|
-
|
119
|
-
IplImage * copy_haikei = NULL;
|
120
|
-
|
121
|
-
IplImage * grey_img = NULL;
|
122
|
-
|
123
|
-
IplImage * haikei_img = NULL;
|
124
|
-
|
125
|
-
IplImage * copy_img = NULL;
|
126
102
|
|
127
103
|
|
128
104
|
|
129
|
-
|
105
|
+
//変換画像用IplImage
|
130
106
|
|
131
|
-
|
107
|
+
scale_img=cvCreateImage(cvSize((int)(img->width*SCALE_X), (int)(img->height*SCALE_Y)), IPL_DEPTH_8U, 3);
|
108
|
+
|
109
|
+
//変換
|
110
|
+
|
111
|
+
cvResize(img, scale_img, CV_INTER_LINEAR);
|
132
112
|
|
133
113
|
|
134
114
|
|
135
|
-
|
115
|
+
//原画像コピー
|
136
116
|
|
137
|
-
|
117
|
+
copy_img1=cvCloneImage(scale_img);
|
138
118
|
|
139
|
-
|
119
|
+
copy_img2=cvCloneImage(scale_img);
|
140
|
-
|
141
|
-
double min_inter2inkage=0;
|
142
120
|
|
143
121
|
|
144
122
|
|
145
|
-
|
123
|
+
//赤枠で囲む
|
146
124
|
|
147
|
-
|
125
|
+
cvRectangle(scale_img,cvPoint(112,68),cvPoint(151,128),CV_RGB(255,0,0),LINE_THICKNESS,LINE_TYPE,SHIFT);
|
148
126
|
|
149
|
-
|
127
|
+
cvRectangle(scale_img,cvPoint(218,70),cvPoint(258,125),CV_RGB(255,0,0),LINE_THICKNESS,LINE_TYPE,SHIFT);//左上、右下
|
150
|
-
|
151
|
-
CvPoint min_point2;
|
152
128
|
|
153
129
|
|
154
130
|
|
155
|
-
|
131
|
+
//-----------------ROIの設定----------------------
|
156
132
|
|
157
|
-
|
133
|
+
CvRect rect1;
|
158
134
|
|
159
|
-
|
135
|
+
rect1.x = 112;
|
160
136
|
|
161
|
-
|
137
|
+
rect1.y = 68;
|
162
138
|
|
163
|
-
i
|
139
|
+
rect1.width = 39;
|
164
140
|
|
165
|
-
i
|
141
|
+
rect1.height = 60;
|
166
142
|
|
167
|
-
i
|
143
|
+
cvSetImageROI(copy_img1,rect1);
|
168
144
|
|
169
145
|
|
170
146
|
|
171
|
-
|
147
|
+
CvRect rect2;
|
172
148
|
|
173
|
-
|
149
|
+
rect2.x = 218;
|
174
150
|
|
151
|
+
rect2.y = 70;
|
152
|
+
|
153
|
+
rect2.width = 40;
|
154
|
+
|
155
|
+
rect2.height = 55;
|
156
|
+
|
175
|
-
|
157
|
+
cvSetImageROI(copy_img2,rect2);//左上、幅、高さ
|
158
|
+
|
159
|
+
//----------------------------------------------------
|
176
160
|
|
177
161
|
|
178
162
|
|
179
|
-
|
163
|
+
dst1=cvCreateImage(cvSize(abs(rect1.width-temp_up->width)+1,abs(rect1.height-temp_up->height)+1),IPL_DEPTH_32F,1); //領域確保
|
180
164
|
|
181
|
-
|
165
|
+
dst2=cvCreateImage(cvSize(abs(rect2.width-temp_side->width)+1,abs(rect2.height-temp_side->height)+1),IPL_DEPTH_32F,1); //領域確保
|
182
166
|
|
183
167
|
|
184
168
|
|
185
|
-
cv
|
169
|
+
cvMatchTemplate(copy_img1,temp_up,dst1,CV_TM_CCOEFF_NORMED); //テンプレートマッチング
|
186
170
|
|
187
|
-
cv
|
171
|
+
cvMatchTemplate(copy_img2,temp_side,dst2,CV_TM_CCOEFF_NORMED); //テンプレートマッチング
|
188
172
|
|
189
173
|
|
190
174
|
|
191
|
-
|
175
|
+
cvNamedWindow("result",CV_WINDOW_AUTOSIZE);
|
176
|
+
|
177
|
+
cvShowImage("result",dst2);
|
192
178
|
|
193
179
|
|
194
180
|
|
195
|
-
|
181
|
+
printf("%d,%d\n",abs(rect2.width-temp_side->width)+1,abs(rect2.height-temp_side->height)+1);
|
196
182
|
|
197
|
-
|
183
|
+
//printf("%d,%d\n",img->width,img->height);
|
198
184
|
|
199
185
|
|
200
186
|
|
201
|
-
copy_haikei=cvCloneImage(temp_haikei);
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
//変換画像用IplImage
|
206
|
-
|
207
|
-
scale_haikei=cvCreateImage(cvSize((int)(copy_haikei->width*SCALE_X), (int)(copy_haikei->height*SCALE_Y)), IPL_DEPTH_8U, 1);
|
208
|
-
|
209
|
-
//変換
|
210
|
-
|
211
|
-
cvResize(copy_haikei, scale_haikei, CV_INTER_LINEAR);
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
temp_up = cvCloneImage(scale_haikei);
|
216
|
-
|
217
|
-
temp_side = cvCloneImage(scale_haikei);
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
//-----------------ROIの設定----------------------
|
222
|
-
|
223
|
-
CvRect rect1;
|
224
|
-
|
225
|
-
rect1.x = 112;
|
226
|
-
|
227
|
-
rect1.y = 68;
|
228
|
-
|
229
|
-
rect1.width = 30;
|
230
|
-
|
231
|
-
rect1.height = 50;
|
232
|
-
|
233
|
-
cvSetImageROI(temp_up,rect1);
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
CvRect rect2;
|
238
|
-
|
239
|
-
rect2.x = 218;
|
240
|
-
|
241
|
-
rect2.y = 70;
|
242
|
-
|
243
|
-
rect2.width = 30;
|
244
|
-
|
245
|
-
rect2.height = 50;
|
246
|
-
|
247
|
-
cvSetImageROI(temp_side,rect2);//左上、幅、高さ
|
248
|
-
|
249
|
-
//----------------------------------------------------
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
cvNamedWindow("haikei_temp_up_window",CV_WINDOW_AUTOSIZE);
|
254
|
-
|
255
|
-
cvShowImage("haikei_temp_up_window",temp_up);
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
//cvNamedWindow("haikei_temp_side_window",CV_WINDOW_AUTOSIZE);
|
260
|
-
|
261
|
-
//cvShowImage("haikei_temp_side_window",haikei_side);
|
262
|
-
|
263
187
|
|
264
|
-
|
265
|
-
for ( int number =100; number<717;++number)
|
266
|
-
|
267
|
-
{
|
268
|
-
|
269
|
-
int64 start = cv::getTickCount();//所要時間計測
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
//------------------------原画像読み込み-------------------------------
|
274
|
-
|
275
|
-
std::stringstream filename;
|
276
|
-
|
277
|
-
filename << "C:\\opencv_ICImage\\2017-1-12\\銀小ネジ\\ver2\\静止画\\img" << number << ".bmp";
|
278
|
-
|
279
|
-
std::cout << "filename=" << filename.str() << std::endl;
|
280
|
-
|
281
|
-
IplImage* img=cvLoadImage(filename.str().c_str());
|
282
|
-
|
283
|
-
//--------------------------------------------------------------------------
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
//グレー
|
288
|
-
|
289
|
-
grey_img=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); //1チャンネル8ビットのIplImage作成(元画像)
|
290
|
-
|
291
|
-
cvCvtColor(img,grey_img,CV_BGR2GRAY);
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
haikei_img = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 ); // 差分画像用IplImage
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
// greyと背景画像との差分をとる
|
300
|
-
|
301
|
-
cvAbsDiff( grey_img, grey_haikei, haikei_img );
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
copy_img = cvCloneImage(haikei_img);
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
//変換画像用IplImage
|
310
|
-
|
311
|
-
scale_img=cvCreateImage(cvSize((int)(copy_img->width*SCALE_X), (int)(copy_img->height*SCALE_Y)), IPL_DEPTH_8U, 1);
|
312
|
-
|
313
|
-
//変換
|
314
|
-
|
315
|
-
cvResize(copy_img, scale_img, CV_INTER_LINEAR);
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
//原画像コピー
|
320
|
-
|
321
|
-
copy_img1=cvCloneImage(scale_img);
|
322
|
-
|
323
|
-
copy_img2=cvCloneImage(scale_img);
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
//赤枠で囲む
|
328
|
-
|
329
|
-
cvRectangle(scale_img,cvPoint(112,68),cvPoint(151,128),CV_RGB(0,255,0),LINE_THICKNESS,LINE_TYPE,SHIFT);
|
330
|
-
|
331
|
-
cvRectangle(scale_img,cvPoint(218,70),cvPoint(258,125),CV_RGB(0,255,0),LINE_THICKNESS,LINE_TYPE,SHIFT);//左上、右下
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
//-----------------ROIの設定----------------------
|
336
|
-
|
337
|
-
CvRect rect3;
|
338
|
-
|
339
|
-
rect3.x = 112;
|
340
|
-
|
341
|
-
rect3.y = 68;
|
342
|
-
|
343
|
-
rect3.width = 39;
|
344
|
-
|
345
|
-
rect3.height = 60;
|
346
|
-
|
347
|
-
cvSetImageROI(copy_img1,rect3);
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
CvRect rect4;
|
352
|
-
|
353
|
-
rect4.x = 218;
|
354
|
-
|
355
|
-
rect4.y = 70;
|
356
|
-
|
357
|
-
rect4.width = 40;
|
358
|
-
|
359
|
-
rect4.height = 55;
|
360
|
-
|
361
|
-
cvSetImageROI(copy_img2,rect4);//左上、幅、高さ
|
362
|
-
|
363
|
-
//----------------------------------------------------
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
dst1=cvCreateImage(cvSize(abs(rect3.width-temp_up->width)+1,abs(rect3.height-temp_up->height)+1),IPL_DEPTH_32F,1); //領域確保
|
368
|
-
|
369
|
-
dst2=cvCreateImage(cvSize(abs(rect4.width-temp_side->width)+1,abs(rect4.height-temp_side->height)+1),IPL_DEPTH_32F,1); //領域確保
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
cvMatchTemplate(copy_img1,temp_up,dst1,CV_TM_CCOEFF_NORMED); //テンプレートマッチング
|
374
|
-
|
375
|
-
cvMatchTemplate(copy_img2,temp_side,dst2,CV_TM_CCOEFF_NORMED); //テンプレートマッチング
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
//-----------マッチング点を求める---------
|
380
|
-
|
381
|
-
cvMinMaxLoc(dst1,&min_inter1inkage,&max_inter1inkage,&min_point1,&max_point1,NULL);
|
382
|
-
|
383
|
-
cvMinMaxLoc(dst2,&min_inter2inkage,&max_inter2inkage,&min_point2,&max_point2,NULL);
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
//類似度表示
|
388
|
-
|
389
|
-
printf("up_Detection %lf\n",max_inter1inkage);
|
390
|
-
|
391
|
-
printf("side_Detection %lf\n",max_inter2inkage);
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
//画像を表示
|
396
|
-
|
397
|
-
cvNamedWindow("result",CV_WINDOW_AUTOSIZE);
|
398
|
-
|
399
|
-
cvShowImage("result",dst1);
|
400
188
|
|
401
189
|
|
402
190
|
|
@@ -404,6 +192,4 @@
|
|
404
192
|
|
405
193
|
|
406
194
|
|
407
|
-
}
|
408
|
-
|
409
195
|
```
|