質問編集履歴

3

書式の改善

2019/06/28 03:19

投稿

namekuhito
namekuhito

スコア21

test CHANGED
File without changes
test CHANGED
@@ -1,236 +1,186 @@
1
1
  ```C#
2
2
 
3
- コード
4
-
5
- ```namespace kamera3
3
+ namespace kamera3
6
4
 
7
5
  {
8
6
 
9
- public partial class Form1 : Form
7
+ public partial class Form1 : Form
10
8
 
11
- {
9
+ {
12
10
 
13
- int WIDTH = 640;
11
+ int WIDTH = 640;
14
12
 
15
- int HEIGHT = 480;
13
+ int HEIGHT = 480;
16
14
 
17
- Mat frame;
15
+ Mat frame;
18
16
 
19
- VideoCapture capture;
17
+ VideoCapture capture;
20
18
 
21
- Bitmap bmp;
19
+ Bitmap bmp;
22
20
 
23
- Graphics graphic;
21
+ Graphics graphic;
24
22
 
23
+ public Form1()
25
24
 
25
+ {
26
26
 
27
- public Form1()
27
+ InitializeComponent();
28
28
 
29
- {
29
+ //カメラ画像取得用のVideoCapture作成
30
30
 
31
- InitializeComponent();
31
+ capture = new VideoCapture(0);
32
32
 
33
+ if (!capture.IsOpened())
33
34
 
35
+ {
34
36
 
35
- //カメラ画像取得用のVideoCapture作成
37
+ MessageBox.Show("cannot open camera");
36
38
 
37
- capture = new VideoCapture(0);
38
-
39
- if (!capture.IsOpened())
40
-
41
- {
42
-
43
- MessageBox.Show("cannot open camera");
44
-
45
- this.Close();
39
+ this.Close();
46
-
47
- }
48
-
49
- capture.FrameWidth = WIDTH;
50
-
51
- capture.FrameHeight = HEIGHT;
52
-
53
-
54
-
55
- //取得先のMat作成
56
-
57
- frame = new Mat(HEIGHT, WIDTH, MatType.CV_8UC3);
58
-
59
-
60
-
61
- //表示用のBitmap作成
62
-
63
- bmp = new Bitmap(frame.Cols, frame.Rows, (int)frame.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, frame.Data);
64
-
65
-
66
-
67
- //PictureBoxを出力サイズに合わせるframe.Cols、frame.Rows 
68
-
69
- pictureBox1.Width = 320;
70
-
71
- pictureBox1.Height = 240;
72
-
73
-
74
-
75
- //描画用のGraphics作成
76
-
77
- graphic = pictureBox1.CreateGraphics();
78
-
79
-
80
-
81
- //画像取得スレッド開始
82
-
83
- backgroundWorker1.RunWorkerAsync();
84
-
85
-
86
-
87
- }
88
-
89
-
90
-
91
- private void Form1_Load(object sender, EventArgs e)
92
-
93
- {
94
-
95
-
96
-
97
- }
98
-
99
-
100
-
101
- private void button1_Click(object sender, EventArgs e)
102
-
103
- {
104
-
105
- //MessageBox.Show("あ\n");
106
-
107
-
108
-
109
- // フィルターの設定
110
-
111
- saveFileDialog1.Filter = "GIF形式|*.gif|JPEG形式|*.jpeg|PNG形式|*.png";
112
-
113
-
114
-
115
- // ファイル保存ダイアログを表示
116
-
117
- saveFileDialog1.ShowDialog();
118
-
119
-
120
-
121
-
122
-
123
- }
124
-
125
-
126
-
127
- private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
128
-
129
- {
130
-
131
- BackgroundWorker bw = (BackgroundWorker)sender;
132
-
133
-
134
-
135
- while (!backgroundWorker1.CancellationPending)
136
-
137
- {
138
-
139
- //画像取得
140
-
141
- //capture.Read(frame); //これだとエラー
142
-
143
- capture.Grab();
144
-
145
- NativeMethods.videoio_VideoCapture_operatorRightShift_Mat(capture.CvPtr, frame.CvPtr);
146
-
147
-
148
-
149
- bw.ReportProgress(0);
150
-
151
- }
152
-
153
- }
154
-
155
-
156
-
157
- private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
158
-
159
- {
160
-
161
- //描画
162
-
163
- graphic.DrawImage(bmp, 0, 0, frame.Cols, frame.Rows);
164
-
165
- }
166
-
167
-
168
-
169
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
170
-
171
- {
172
-
173
- //スレッドの終了を待機
174
-
175
- backgroundWorker1.CancelAsync();
176
-
177
- while (backgroundWorker1.IsBusy)
178
-
179
- Application.DoEvents();
180
-
181
- }
182
-
183
-
184
-
185
-
186
-
187
- private void saveFileDialog1_FileOk(object sender, EventArgs e)
188
-
189
- {
190
-
191
-
192
-
193
- string extension = System.IO.Path.GetExtension(saveFileDialog1.FileName);
194
-
195
-
196
-
197
- switch (extension.ToUpper())
198
-
199
- {
200
-
201
- case ".GIF":
202
-
203
- // ★★★PictureBoxのイメージをGIF形式で保存する★★★
204
-
205
- pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif);
206
-
207
- break;
208
-
209
- case ".JPEG":
210
-
211
- // ★★★PictureBoxのイメージをJPEG形式で保存する★★★
212
-
213
- pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
214
-
215
- break;
216
-
217
- case ".PNG":
218
-
219
- // ★★★PictureBoxのイメージをGIF形式で保存する★★★
220
-
221
- pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
222
-
223
- break;
224
-
225
- }
226
-
227
- }
228
-
229
- }
230
40
 
231
41
  }
232
42
 
43
+ capture.FrameWidth = WIDTH;
233
44
 
45
+ capture.FrameHeight = HEIGHT;
46
+
47
+ //取得先のMat作成
48
+
49
+ frame = new Mat(HEIGHT, WIDTH, MatType.CV_8UC3);
50
+
51
+ //表示用のBitmap作成
52
+
53
+ bmp = new Bitmap(frame.Cols, frame.Rows, (int)frame.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, frame.Data);
54
+
55
+ //PictureBoxを出力サイズに合わせるframe.Cols、frame.Rows 
56
+
57
+ pictureBox1.Width = 320;
58
+
59
+ pictureBox1.Height = 240;
60
+
61
+ //描画用のGraphics作成
62
+
63
+ graphic = pictureBox1.CreateGraphics();
64
+
65
+ //画像取得スレッド開始
66
+
67
+ backgroundWorker1.RunWorkerAsync();
68
+
69
+ }
70
+
71
+ private void Form1_Load(object sender, EventArgs e)
72
+
73
+ {
74
+
75
+ }
76
+
77
+ private void button1_Click(object sender, EventArgs e)
78
+
79
+ {
80
+
81
+ //MessageBox.Show("あ\n");
82
+
83
+ // フィルターの設定
84
+
85
+ saveFileDialog1.Filter = "GIF形式|*.gif|JPEG形式|*.jpeg|PNG形式|*.png";
86
+
87
+ // ファイル保存ダイアログを表示
88
+
89
+ saveFileDialog1.ShowDialog();
90
+
91
+ }
92
+
93
+ private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
94
+
95
+ {
96
+
97
+ BackgroundWorker bw = (BackgroundWorker)sender;
98
+
99
+ while (!backgroundWorker1.CancellationPending)
100
+
101
+ {
102
+
103
+ //画像取得
104
+
105
+ //capture.Read(frame); //これだとエラー
106
+
107
+ capture.Grab();
108
+
109
+ NativeMethods.videoio_VideoCapture_operatorRightShift_Mat(capture.CvPtr, frame.CvPtr);
110
+
111
+ bw.ReportProgress(0);
112
+
113
+ }
114
+
115
+ }
116
+
117
+ private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
118
+
119
+ {
120
+
121
+ //描画
122
+
123
+ graphic.DrawImage(bmp, 0, 0, frame.Cols, frame.Rows);
124
+
125
+ }
126
+
127
+ private void Form1_FormClosing(object sender, FormClosingEventArgs e)
128
+
129
+ {
130
+
131
+ //スレッドの終了を待機
132
+
133
+ backgroundWorker1.CancelAsync();
134
+
135
+ while (backgroundWorker1.IsBusy)
136
+
137
+ Application.DoEvents();
138
+
139
+ }
140
+
141
+ private void saveFileDialog1_FileOk(object sender, EventArgs e)
142
+
143
+ {
144
+
145
+ string extension = System.IO.Path.GetExtension(saveFileDialog1.FileName);
146
+
147
+ switch (extension.ToUpper())
148
+
149
+ {
150
+
151
+ case ".GIF":
152
+
153
+ // ★★★PictureBoxのイメージをGIF形式で保存する★★★
154
+
155
+ pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif);
156
+
157
+ break;
158
+
159
+ case ".JPEG":
160
+
161
+ // ★★★PictureBoxのイメージをJPEG形式で保存する★★★
162
+
163
+ pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
164
+
165
+ break;
166
+
167
+ case ".PNG":
168
+
169
+ // ★★★PictureBoxのイメージをGIF形式で保存する★★★
170
+
171
+ pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
172
+
173
+ break;
174
+
175
+ }
176
+
177
+ }
178
+
179
+ }
180
+
181
+ }
182
+
183
+ ```
234
184
 
235
185
 
236
186
 

2

追記

2019/06/28 03:19

投稿

namekuhito
namekuhito

スコア21

test CHANGED
File without changes
test CHANGED
@@ -234,6 +234,6 @@
234
234
 
235
235
 
236
236
 
237
-
237
+ Windowsフォームアプリケーションです。
238
238
 
239
239
  このコードでは、カメラから画像は取得できましたが保存ができませんでした。pictureboxで表示されている画像を保存しようとして、webカメラの画像はpictureboxに表示されていないからだと考えています。他に良い保存方法やwebカメラからの画像をpictureboxに表示できる方法など知りたいです。また、出来ればOpenCVを用いてwebカメラからの画像を表示したいと考えています。よろしくお願いいたします。

1

文字の修正

2019/06/26 04:29

投稿

namekuhito
namekuhito

スコア21

test CHANGED
File without changes
test CHANGED
@@ -1,7 +1,5 @@
1
1
  ```C#
2
2
 
3
-
4
-
5
3
  コード
6
4
 
7
5
  ```namespace kamera3
@@ -234,4 +232,8 @@
234
232
 
235
233
 
236
234
 
235
+
236
+
237
+
238
+
237
239
  このコードでは、カメラから画像は取得できましたが保存ができませんでした。pictureboxで表示されている画像を保存しようとして、webカメラの画像はpictureboxに表示されていないからだと考えています。他に良い保存方法やwebカメラからの画像をpictureboxに表示できる方法など知りたいです。また、出来ればOpenCVを用いてwebカメラからの画像を表示したいと考えています。よろしくお願いいたします。