質問編集履歴

6

エラーが出る位置をわかりやすくしました。

2018/11/25 03:44

投稿

minimize
minimize

スコア23

test CHANGED
File without changes
test CHANGED
@@ -206,9 +206,7 @@
206
206
 
207
207
 
208
208
 
209
- temp_img = imread("C:\Users\minimize\Pictures\Saved Pictures\ball.jpg"); //ここでエラーが出てしまい、先に進みません
209
+ temp_img = imread("C:\Users\minimize\Pictures\Saved Pictures\ball.jpg"); //Releaseの時のエラーが出る位置
210
-
211
-
212
210
 
213
211
 
214
212
 
@@ -216,7 +214,7 @@
216
214
 
217
215
 
218
216
 
219
- matchTemplate(img, temp_img, result, TM_CCORR_NORMED);
217
+ matchTemplate(img, temp_img, result, TM_CCORR_NORMED); //Debugの時のエラーが出る位置
220
218
 
221
219
 
222
220
 

5

画像の修正

2018/11/25 03:44

投稿

minimize
minimize

スコア23

test CHANGED
File without changes
test CHANGED
@@ -390,7 +390,7 @@
390
390
 
391
391
  ```
392
392
 
393
- ソリューション構成はRelease![イメージ説明](c902db49fbe5b3d4ac375d65f8471bad.jpeg)
393
+ ソリューション構成はRelease
394
394
 
395
395
  ソリューションプラットフォームは×64
396
396
 

4

エラーメッセージの追加

2018/11/25 03:23

投稿

minimize
minimize

スコア23

test CHANGED
File without changes
test CHANGED
@@ -390,7 +390,7 @@
390
390
 
391
391
  ```
392
392
 
393
- ソリューション構成はRelease
393
+ ソリューション構成はRelease![イメージ説明](c902db49fbe5b3d4ac375d65f8471bad.jpeg)
394
394
 
395
395
  ソリューションプラットフォームは×64
396
396
 
@@ -412,6 +412,30 @@
412
412
 
413
413
 
414
414
 
415
+ ソリューション構成 Debug
416
+
417
+ ソリューションプラットフォーム x64
418
+
419
+ の場合のエラーも載せておきます。
420
+
421
+
422
+
423
+ Releaseの時と違うのは、コンソールに
424
+
425
+
426
+
427
+ OpenCV(4.0.0-rc) Error: Assertion failed ((depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2) in cv::matchTemplate, file c:\build\master_winpack-build-win64-vc15\opencv\modules\imgproc\src\templmatch.cpp, line 1107
428
+
429
+
430
+
431
+ というエラーが出てきたことと、
432
+
433
+ 以下の画像のエラーメッセージになりました。
434
+
435
+ ![イメージ説明](865b7f1440ba2c63f6cc69dc5e80a482.jpeg)
436
+
437
+
438
+
415
439
  このエラーの解決方法をわかる方がいれば、教えてください。
416
440
 
417
441
  また、解決する際に必要な情報がほかにありましたら教えてください。

3

余分な文章の削除

2018/11/25 03:22

投稿

minimize
minimize

スコア23

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  エラーが出てしまい、画像を読み込むことができません。
10
10
 
11
- コードは以下のようになっています。(必要そうな部分だけ抜いて記述しています)
11
+ コードは以下のようになっています。
12
12
 
13
13
 
14
14
 
@@ -116,7 +116,7 @@
116
116
 
117
117
  Mat mat(height, width, CV_8UC4, Scalar::all(0));
118
118
 
119
- //mat.data = (uchar*)lpPixel;
119
+
120
120
 
121
121
  const int channel = 4;
122
122
 
@@ -124,14 +124,6 @@
124
124
 
125
125
 
126
126
 
127
- //DeleteDC(hdc);
128
-
129
- //DeleteObject(hbmp);
130
-
131
- //DeleteDC(g_hdc);
132
-
133
- //ReleaseDC(g_hdc);
134
-
135
127
 
136
128
 
137
129
  return mat;

2

ソースコードの追加と、エラーメッセージの詳細の画像の追加

2018/11/24 08:34

投稿

minimize
minimize

スコア23

test CHANGED
File without changes
test CHANGED
@@ -32,6 +32,180 @@
32
32
 
33
33
 
34
34
 
35
+
36
+
37
+ using namespace cv;
38
+
39
+ using namespace std;
40
+
41
+
42
+
43
+
44
+
45
+ BITMAPINFOHEADER CreateBITMAPINFOHEADER(int width, int height)
46
+
47
+ {
48
+
49
+ BITMAPINFOHEADER bi;
50
+
51
+ bi.biSize = sizeof(BITMAPINFOHEADER);
52
+
53
+ bi.biWidth = width;
54
+
55
+ bi.biHeight = -height;
56
+
57
+ bi.biPlanes = 1;
58
+
59
+ bi.biBitCount = 32;
60
+
61
+ bi.biCompression = BI_RGB;
62
+
63
+ bi.biSizeImage = 0;
64
+
65
+ bi.biXPelsPerMeter = 0;
66
+
67
+ bi.biYPelsPerMeter = 0;
68
+
69
+ bi.biClrUsed = 0;
70
+
71
+ bi.biClrImportant = 0;
72
+
73
+
74
+
75
+ return bi;
76
+
77
+ }
78
+
79
+
80
+
81
+ Mat ConvertHWNDToMat(HWND hwnd)
82
+
83
+ {
84
+
85
+ RECT rect;
86
+
87
+ GetClientRect(hwnd, &rect);
88
+
89
+ int width = rect.right - rect.left;
90
+
91
+ int height = rect.bottom - rect.top;
92
+
93
+ HDC g_hdc = GetDC(hwnd);
94
+
95
+
96
+
97
+ BITMAPINFOHEADER bi = CreateBITMAPINFOHEADER(width, height);
98
+
99
+ LPDWORD lpPixel;
100
+
101
+
102
+
103
+ HBITMAP hbmp = CreateDIBSection(g_hdc, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&lpPixel, NULL, 0);
104
+
105
+
106
+
107
+ HDC hdc = CreateCompatibleDC(g_hdc);
108
+
109
+ SelectObject(hdc, hbmp);
110
+
111
+ BitBlt(hdc, 0, 0, width, height, g_hdc, 0, 0, SRCCOPY);
112
+
113
+ ReleaseDC(hwnd, g_hdc);
114
+
115
+
116
+
117
+ Mat mat(height, width, CV_8UC4, Scalar::all(0));
118
+
119
+ //mat.data = (uchar*)lpPixel;
120
+
121
+ const int channel = 4;
122
+
123
+ memcpy(mat.data, lpPixel, (sizeof(char) * (width * height) * channel));
124
+
125
+
126
+
127
+ //DeleteDC(hdc);
128
+
129
+ //DeleteObject(hbmp);
130
+
131
+ //DeleteDC(g_hdc);
132
+
133
+ //ReleaseDC(g_hdc);
134
+
135
+
136
+
137
+ return mat;
138
+
139
+ }
140
+
141
+
142
+
143
+
144
+
145
+ void Error_message(int error_num) {
146
+
147
+
148
+
149
+ switch (error_num){
150
+
151
+
152
+
153
+ case 0: //Not Mat
154
+
155
+
156
+
157
+ cout << "Not Mat" << endl;
158
+
159
+
160
+
161
+ break;
162
+
163
+
164
+
165
+
166
+
167
+ case 1: //Not Active window
168
+
169
+
170
+
171
+ cout << "Not active window" << endl;
172
+
173
+
174
+
175
+ break;
176
+
177
+
178
+
179
+ case 2:
180
+
181
+
182
+
183
+ cout << "Not template image " << endl;
184
+
185
+
186
+
187
+ break;
188
+
189
+
190
+
191
+
192
+
193
+ default:
194
+
195
+ break;
196
+
197
+
198
+
199
+ }
200
+
201
+
202
+
203
+ }
204
+
205
+
206
+
207
+
208
+
35
209
  void Pursuit(Mat img) { // target pursuit
36
210
 
37
211
 
@@ -40,7 +214,7 @@
40
214
 
41
215
 
42
216
 
43
- temp_img = imread("C:\Users\minimize\Pictures\Saved Pictures\rem.jpg"); //ここでエラーが出てしまい、先にすすみません
217
+ temp_img = imread("C:\Users\minimize\Pictures\Saved Pictures\ball.jpg"); //ここでエラーが出てしまい、先にみません
44
218
 
45
219
 
46
220
 
@@ -70,6 +244,158 @@
70
244
 
71
245
  }
72
246
 
247
+
248
+
249
+ int DoGetActiveWindow() {
250
+
251
+
252
+
253
+ char buf[1000];
254
+
255
+ HWND activeWindow;
256
+
257
+
258
+
259
+ activeWindow = GetForegroundWindow(); //Get active window
260
+
261
+
262
+
263
+ GetWindowText(activeWindow, buf, 1000); //Get active window text
264
+
265
+
266
+
267
+ LPARAM param = (LPARAM)"ResearchProject2"; //Want to open project name
268
+
269
+
270
+
271
+ if (strcmp(buf, (char*)param) == 0) { //Match project name
272
+
273
+
274
+
275
+ //HBITMAP bmp = GetBitmap(activeWindow);
276
+
277
+ Mat mat_img = ConvertHWNDToMat(activeWindow);
278
+
279
+
280
+
281
+ if (mat_img.empty()) {
282
+
283
+ Error_message(0);
284
+
285
+ return 0;
286
+
287
+ }
288
+
289
+
290
+
291
+ Pursuit(mat_img); //target pursuit
292
+
293
+
294
+
295
+ return 1;
296
+
297
+ }else{
298
+
299
+ return 0;
300
+
301
+ }
302
+
303
+
304
+
305
+ if (activeWindow == NULL) {
306
+
307
+ Error_message(1);
308
+
309
+ return 0;
310
+
311
+ }
312
+
313
+
314
+
315
+ return 0;
316
+
317
+
318
+
319
+ }
320
+
321
+
322
+
323
+
324
+
325
+ int main() {
326
+
327
+
328
+
329
+ STARTUPINFO tStartupInfo = { 0 };
330
+
331
+ PROCESS_INFORMATION tProcessInfomation = { 0 };
332
+
333
+ GetStartupInfo(&tStartupInfo);
334
+
335
+
336
+
337
+ CreateProcess(
338
+
339
+ "C:\Users\mizuho\Desktop\sotuken\ResearchProject2.exe", // Name of executable module
340
+
341
+ NULL, // Comand line texts
342
+
343
+ NULL, // Security descriptor
344
+
345
+ NULL, // Security descriptor
346
+
347
+ FALSE, // Handle inheritance option
348
+
349
+ 0, // Create flag
350
+
351
+ NULL, // New environmental block
352
+
353
+ NULL, // Name of current directory
354
+
355
+ &tStartupInfo, // Startup information
356
+
357
+ &tProcessInfomation // Process information
358
+
359
+ );
360
+
361
+
362
+
363
+ Sleep(1000);
364
+
365
+
366
+
367
+ while (1) {
368
+
369
+ int win = DoGetActiveWindow();
370
+
371
+
372
+
373
+ if (win != 1) {
374
+
375
+ //Error_message(1);
376
+
377
+ }else {
378
+
379
+ //std::cout << "OKOK" << std::endl;
380
+
381
+ }
382
+
383
+
384
+
385
+ if (waitKey(0) == 1) break; //push anything key
386
+
387
+
388
+
389
+ }
390
+
391
+
392
+
393
+ return 0;
394
+
395
+
396
+
397
+ }
398
+
73
399
  ```
74
400
 
75
401
  ソリューション構成はRelease
@@ -86,6 +412,10 @@
86
412
 
87
413
 
88
414
 
415
+ ![イメージ説明](1eea86a52c1742035ef150d0cd8d09d7.png)
416
+
417
+
418
+
89
419
  rem.jpgはこのディレクトリにあることを確認しました。
90
420
 
91
421
 

1

初心者マークを追加しました

2018/11/24 08:28

投稿

minimize
minimize

スコア23

test CHANGED
File without changes
test CHANGED
File without changes