質問編集履歴
2
文章の変更と、内容の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,9 +1,18 @@
|
|
1
|
+
質問です。
|
1
2
|
c++でUnityで作った実行ファイルを実行して、
|
2
3
|
フォアグラウンドウィンドウを取得しています。
|
3
4
|
そのウィンドウをbitmapに変更することはできたと思うのですが、
|
4
5
|
OpenCvでそのbitmapをいじりたいので
|
5
|
-
|
6
|
+
Mat形式に変換する方法を教えてください。
|
6
7
|
|
8
|
+
やりたいことは、実行ファイルをリアルタイムにウィンドウキャプチャして、
|
9
|
+
ウィンドウキャプチャした画像をOpenCVで処理(テンプレートマッチングをして、オブジェクトをリアルタイムに追跡)して、その処理した画像をつなげた動画を表示したいと思っています。
|
10
|
+
|
11
|
+
わかりにくいところがありましたら、質問していただけると幸いです。
|
12
|
+
それと、最初に書いたときは時間がなくてちょっとしか情報が載せられなかったことを
|
13
|
+
謝罪します。
|
14
|
+
コードが長くなっていますが、よろしくお願いいたします。
|
15
|
+
|
7
16
|
```C++
|
8
17
|
#include <opencv2/opencv.hpp>
|
9
18
|
#include "opencv2/highgui/highgui.hpp"
|
1
文章の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,2 +1,146 @@
|
|
1
|
+
c++でUnityで作った実行ファイルを実行して、
|
2
|
+
フォアグラウンドウィンドウを取得しています。
|
3
|
+
そのウィンドウをbitmapに変更することはできたと思うのですが、
|
4
|
+
OpenCvでそのbitmapをいじりたいので
|
1
|
-
|
5
|
+
pngかjpgかmat形式に変換する方法を教えてください。
|
6
|
+
|
7
|
+
```C++
|
8
|
+
#include <opencv2/opencv.hpp>
|
9
|
+
#include "opencv2/highgui/highgui.hpp"
|
10
|
+
#include "iostream"
|
11
|
+
#include "opencv2/core/core.hpp"
|
12
|
+
#include "opencv2/features2d/features2d.hpp"
|
13
|
+
|
14
|
+
#include <windows.h>
|
15
|
+
|
16
|
+
|
17
|
+
using namespace cv;
|
18
|
+
using namespace std;
|
19
|
+
|
20
|
+
|
21
|
+
HBITMAP GetBitmap(HWND hwnd) {
|
22
|
+
RECT rec;
|
23
|
+
GetClientRect(hwnd, &rec); //Return window coordinate(upper left, bottom right)
|
24
|
+
float width = rec.right - rec.left; //window widht
|
25
|
+
auto height = rec.bottom - rec.top; //window height
|
26
|
+
|
27
|
+
auto hdcSrc = GetDC(NULL); //Return desplay handle
|
28
|
+
auto hdcDst = CreateCompatibleDC(hdcSrc); // instance memori device context
|
29
|
+
HBITMAP hbmp = CreateCompatibleBitmap(hdcSrc, width, height);
|
30
|
+
auto hOldBmp = SelectObject(hdcDst, hbmp); // Set bitmap in memory device context
|
31
|
+
|
32
|
+
BitBlt(hdcDst, // Copy color data
|
33
|
+
0, 0, width, height,
|
34
|
+
hdcSrc,
|
35
|
+
rec.left, rec.top,
|
36
|
+
SRCCOPY
|
37
|
+
);
|
38
|
+
|
39
|
+
SelectObject(hdcDst, hOldBmp);
|
40
|
+
DeleteDC(hdcDst);
|
41
|
+
ReleaseDC(hwnd, hdcSrc);
|
42
|
+
|
43
|
+
return hbmp;
|
44
|
+
}
|
45
|
+
|
46
|
+
void error_message(int error_num) {
|
47
|
+
|
48
|
+
switch (error_num){
|
49
|
+
|
50
|
+
case 0: //Not Bmp
|
51
|
+
|
52
|
+
std::cout << "not bmp" << std::endl;
|
53
|
+
|
54
|
+
break;
|
55
|
+
|
56
|
+
|
57
|
+
case 1: //Not Active window
|
58
|
+
|
59
|
+
std::cout << "Not active window" << std::endl;
|
60
|
+
|
61
|
+
break;
|
62
|
+
|
63
|
+
case 2:
|
64
|
+
|
65
|
+
break;
|
66
|
+
|
67
|
+
|
68
|
+
default:
|
69
|
+
break;
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
int DoGetActiveWindow() {
|
76
|
+
|
77
|
+
char buf[1000];
|
78
|
+
HWND activeWindow;
|
79
|
+
|
80
|
+
activeWindow = GetForegroundWindow(); //Get active window
|
81
|
+
|
82
|
+
GetWindowText(activeWindow, buf, 1000); //Get active window text
|
83
|
+
|
84
|
+
LPARAM param = (LPARAM)"ResearchProject2"; //Want to open project name
|
85
|
+
|
86
|
+
if (strcmp(buf, (char*)param) == 0) { //Match project name
|
87
|
+
|
88
|
+
HBITMAP bmp = GetBitmap(activeWindow);
|
89
|
+
|
90
|
+
if (bmp == NULL) {
|
91
|
+
error_message(0);
|
92
|
+
return 0;
|
93
|
+
}
|
94
|
+
|
95
|
+
return 1;
|
96
|
+
}else{
|
97
|
+
return 0;
|
98
|
+
}
|
99
|
+
|
100
|
+
if (activeWindow == NULL) {
|
101
|
+
error_message(1);
|
102
|
+
return 0;
|
103
|
+
}
|
104
|
+
|
105
|
+
return 0;
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
int main() {
|
110
|
+
|
111
|
+
STARTUPINFO tStartupInfo = { 0 };
|
112
|
+
PROCESS_INFORMATION tProcessInfomation = { 0 };
|
113
|
+
GetStartupInfo(&tStartupInfo);
|
114
|
+
|
115
|
+
CreateProcess(
|
116
|
+
"C:\\ResearchProject2.exe",
|
117
|
+
NULL, // Comand line texts
|
118
|
+
NULL, // Security descriptor
|
119
|
+
NULL, // Security descriptor
|
120
|
+
FALSE, // Handle inheritance option
|
121
|
+
0, // Create flag
|
122
|
+
NULL, // New environmental block
|
123
|
+
NULL, // Name of current directory
|
124
|
+
&tStartupInfo, // Startup information
|
125
|
+
&tProcessInfomation // Process information
|
126
|
+
);
|
127
|
+
|
128
|
+
Sleep(1000);
|
129
|
+
|
2
|
-
|
130
|
+
while (1) {
|
131
|
+
int win = DoGetActiveWindow();
|
132
|
+
|
133
|
+
if (win != 1) {
|
134
|
+
error_message(1);
|
135
|
+
}else {
|
136
|
+
std::cout << "OKOK" << std::endl;
|
137
|
+
}
|
138
|
+
|
139
|
+
if (waitKey(0) == 1) break; //push anything key
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
return 0;
|
144
|
+
|
145
|
+
}
|
146
|
+
```
|