質問編集履歴

2

表示がおかしかったのでなおしました。

2018/11/26 05:25

投稿

minimize
minimize

スコア23

test CHANGED
File without changes
test CHANGED
@@ -2,11 +2,7 @@
2
2
 
3
3
  OpenCVのimshowについて質問です。
4
4
 
5
- imshowの第一引数のウィンドウ名が同じで、
6
5
 
7
- 第二引数の画像のみが変わる場合、同じウィンドウで
8
-
9
- 画像が変わっていくと思っていたのですが、画像は変わらない(上書きされない)のでしょうか?
10
6
 
11
7
 
12
8
 
@@ -28,13 +24,7 @@
28
24
 
29
25
  よろしくお願いいたします。
30
26
 
31
-
32
-
33
-
34
-
35
- ```++
27
+ ```c++
36
-
37
-
38
28
 
39
29
  #include <opencv2/opencv.hpp>
40
30
 
@@ -82,7 +72,7 @@
82
72
 
83
73
 
84
74
 
85
- imshow("win, img); // 1
75
+ imshow("win", img); // 1
86
76
 
87
77
 
88
78
 
@@ -102,4 +92,6 @@
102
92
 
103
93
 
104
94
 
95
+ }
96
+
105
- }```
97
+ ```

1

コードを簡単にしました。

2018/11/26 05:25

投稿

minimize
minimize

スコア23

test CHANGED
File without changes
test CHANGED
@@ -10,15 +10,17 @@
10
10
 
11
11
 
12
12
 
13
- 今以下のコードを書いていて、
13
+ 今以下のコードを書いていて、最初に1と2を読み込んでそのあとまた1を読み込んで~・・
14
14
 
15
- 第二引数の画像が変更さていたら画像上書きして表示してほしと思っていま
15
+ と繰り返して読み込んでくること期待していたのでが、
16
16
 
17
17
 
18
18
 
19
- 現在の状況は、最初に読みんだ画像だけを表示ていて、
19
+ 現在の状況は、2を読みんだ後は更新されなくなりまた。
20
20
 
21
+
22
+
21
- 画像が上書きされません。ウィンドウを消したら新しく更新ページが表示されます。
23
+ 連続で更新するほうほうを教えていだけると幸いです。
22
24
 
23
25
 
24
26
 
@@ -30,7 +32,7 @@
30
32
 
31
33
 
32
34
 
33
- ```c++
35
+ ```c++
34
36
 
35
37
 
36
38
 
@@ -64,376 +66,40 @@
64
66
 
65
67
 
66
68
 
67
- BITMAPINFOHEADER CreateBITMAPINFOHEADER(int width, int height)
68
-
69
- {
70
-
71
- BITMAPINFOHEADER bi;
72
-
73
- bi.biSize = sizeof(BITMAPINFOHEADER);
74
-
75
- bi.biWidth = width;
76
-
77
- bi.biHeight = -height;
78
-
79
- bi.biPlanes = 1;
80
-
81
- bi.biBitCount = 32;
82
-
83
- bi.biCompression = BI_RGB;
84
-
85
- bi.biSizeImage = 0;
86
-
87
- bi.biXPelsPerMeter = 0;
88
-
89
- bi.biYPelsPerMeter = 0;
90
-
91
- bi.biClrUsed = 0;
92
-
93
- bi.biClrImportant = 0;
94
-
95
-
96
-
97
- return bi;
98
-
99
- }
100
-
101
-
102
-
103
- Mat ConvertHWNDToMat(HWND hwnd)
104
-
105
- {
106
-
107
- RECT rect;
108
-
109
- GetClientRect(hwnd, &rect);
110
-
111
- int width = rect.right - rect.left;
112
-
113
- int height = rect.bottom - rect.top;
114
-
115
- HDC g_hdc = GetDC(hwnd);
116
-
117
-
118
-
119
- BITMAPINFOHEADER bi = CreateBITMAPINFOHEADER(width, height);
120
-
121
- LPDWORD lpPixel;
122
-
123
-
124
-
125
- HBITMAP hbmp = CreateDIBSection(g_hdc, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&lpPixel, NULL, 0);
126
-
127
-
128
-
129
- HDC hdc = CreateCompatibleDC(g_hdc);
130
-
131
- SelectObject(hdc, hbmp);
132
-
133
- BitBlt(hdc, 0, 0, width, height, g_hdc, 0, 0, SRCCOPY);
134
-
135
- ReleaseDC(hwnd, g_hdc);
136
-
137
-
138
-
139
- Mat mat(height, width, CV_8UC4, Scalar::all(0));
140
-
141
- const int channel = 4;
142
-
143
- memcpy(mat.data, lpPixel, (sizeof(char) * (width * height) * channel));
144
-
145
-
146
-
147
- return mat;
148
-
149
- }
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
- void Error_message(int error_num) {
158
-
159
-
160
-
161
- switch (error_num){
162
-
163
-
164
-
165
- case 0: //Not Mat
166
-
167
-
168
-
169
- cout << "Not Mat" << endl;
170
-
171
-
172
-
173
- break;
174
-
175
-
176
-
177
-
178
-
179
- case 1: //Not Active window
180
-
181
-
182
-
183
- cout << "Not active window" << endl;
184
-
185
-
186
-
187
- break;
188
-
189
-
190
-
191
- case 2:
192
-
193
-
194
-
195
- cout << "Not template image " << endl;
196
-
197
-
198
-
199
- break;
200
-
201
-
202
-
203
-
204
-
205
- default:
206
-
207
- break;
208
-
209
-
210
-
211
- }
212
-
213
-
214
-
215
- }
216
-
217
-
218
-
219
-
220
-
221
- int Pursuit(Mat img) { // target pursuit
222
-
223
-
224
-
225
- Mat temp_img, result;
226
-
227
-
228
-
229
- temp_img = imread("C:\Users\mizuho\Pictures\Saved Pictures\ball.jpg", IMREAD_UNCHANGED);
230
-
231
-
232
-
233
- cvtColor(temp_img, temp_img, 1);
234
-
235
- cvtColor(img, img, 1);
236
-
237
-
238
-
239
- if (!temp_img.data) {
240
-
241
- Error_message(2);
242
-
243
- return 1;
244
-
245
- }
246
-
247
-
248
-
249
- matchTemplate(img, temp_img, result, TM_CCORR_NORMED);
250
-
251
-
252
-
253
- Point temp_position;
254
-
255
- minMaxLoc(result, 0, 0, 0, &temp_position);
256
-
257
- rectangle(img, temp_position, Point(temp_position.x + temp_img.cols,
258
-
259
- temp_position.y + temp_img.rows), Scalar(0, 255, 255), 2, 8, 0);
260
-
261
-
262
-
263
- namedWindow("result", WINDOW_AUTOSIZE);
264
-
265
-
266
-
267
- imshow("result", img);
268
-
269
-
270
-
271
- return 0;
272
-
273
- }
274
-
275
-
276
-
277
- int DoGetActiveWindow() {
278
-
279
-
280
-
281
- char buf[1000];
282
-
283
- HWND activeWindow;
284
-
285
-
286
-
287
- activeWindow = GetForegroundWindow(); //Get active window
288
-
289
-
290
-
291
- GetWindowText(activeWindow, buf, 1000); //Get active window text
292
-
293
-
294
-
295
- LPARAM param = (LPARAM)"ResearchProject2"; //Want to open project name
296
-
297
-
298
-
299
- if (strcmp(buf, (char*)param) == 0) { //Match project name
300
-
301
-
302
-
303
- Mat mat_img = ConvertHWNDToMat(activeWindow);
304
-
305
-
306
-
307
- if (mat_img.empty()) {
308
-
309
- Error_message(0);
310
-
311
- return 0;
312
-
313
- }
314
-
315
-
316
-
317
-
318
-
319
- int img_s = Pursuit(mat_img); //target pursuit
320
-
321
-
322
-
323
- if (img_s != 0) {
324
-
325
- Error_message(1);
326
-
327
- return 0;
328
-
329
- }
330
-
331
-
332
-
333
- return 1;
334
-
335
-
336
-
337
- }else{
338
-
339
- return 0;
340
-
341
- }
342
-
343
-
344
-
345
- if (activeWindow == NULL) {
346
-
347
- Error_message(1);
348
-
349
- return 0;
350
-
351
- }
352
-
353
-
354
-
355
- return 0;
356
-
357
-
358
-
359
- }
360
-
361
-
362
-
363
-
364
-
365
- int main() {
366
-
367
-
368
-
369
- STARTUPINFO tStartupInfo = { 0 };
370
-
371
- PROCESS_INFORMATION tProcessInfomation = { 0 };
372
-
373
- GetStartupInfo(&tStartupInfo);
374
-
375
-
376
-
377
- CreateProcess(
378
-
379
- "C:\Users\miminimi\Desktop\sotuken\ResearchProject2.exe", // Name of executable module
380
-
381
- NULL, // Comand line texts
382
-
383
- NULL, // Security descriptor
384
-
385
- NULL, // Security descriptor
386
-
387
- FALSE, // Handle inheritance option
388
-
389
- 0, // Create flag
390
-
391
- NULL, // New environmental block
392
-
393
- NULL, // Name of current directory
394
-
395
- &tStartupInfo, // Startup information
396
-
397
- &tProcessInfomation // Process information
398
-
399
- );
400
-
401
-
402
-
403
- Sleep(3000);
404
-
405
-
406
-
407
69
  while (1) {
408
-
409
- int win = DoGetActiveWindow();
410
-
411
-
412
-
413
- if (win != 1) {
414
-
415
- //Error_message(1);
416
-
417
- }else {
418
-
419
- //std::cout << "OKOK" << std::endl;
420
-
421
- }
422
70
 
423
71
 
424
72
 
425
73
  if (waitKey(0) == 1) break; //push anything key
426
74
 
427
-
428
75
 
76
+
429
- }
77
+ Mat img;
78
+
79
+
80
+
81
+ img = imread("C:\Users\mimimi\Pictures\Saved Pictures\ball.jpg", IMREAD_UNCHANGED);
430
82
 
431
83
 
432
84
 
433
- return 0;
85
+ imshow("win, img); // 1
434
86
 
435
87
 
436
88
 
437
- }
89
+ waitKey(1000);
438
90
 
91
+
92
+
93
+ img = imread("C:\Users\mimimi\Pictures\Saved Pictures\ball.jpg", 0);
94
+
95
+
96
+
97
+ imshow("win", img); // 2
98
+
99
+
100
+
101
+ waitKey(1000);
102
+
103
+
104
+
439
- ```
105
+ }```