質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Kinect

Kinect(キネクト)はマイクロソフトから発売されたジェスチャー・音声認識によって 操作ができるデバイスです。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

2552閲覧

C#でボタンを押したら映像を画像としてファイルに保存するプログラムを書いているのですが...

BOSS723

総合スコア35

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Kinect

Kinect(キネクト)はマイクロソフトから発売されたジェスチャー・音声認識によって 操作ができるデバイスです。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2016/11/30 08:18

編集2016/11/30 08:59

ボタンを押したら映像を画像として保存するプログラムを書いているのですが、上手くイメージを指定できません。
下記に一部プログラムを示していますが、SensorDepthFrameReadyのdepthImage.Sourceをbutton_Clickのenc.Frames.Add(BitmapFrame.Create());にもっていきたいのですが...
button.Click += new EventHandler(SensorDepthFrameReady);や SensorDepthFrameReadyの関数呼び出しでいけるのかと思っていたのですがそれもできなかったので質問しました。
拙い文章で申し訳ないですが、なにかアドバイスがもらえるとうれしいです。
センサーはKinectです。

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Navigation; 14using System.Windows.Shapes; 15using System.Drawing; 16using System.Drawing.Imaging; 17 18using Microsoft.Kinect; 19using System.IO; 20 21namespace KinectDepthRecoder3 22{ 23 public partial class MainWindow : Window 24 { 25 private KinectSensor sensor; 26 private DepthImagePixel[] depthPixels; 27 private byte[] colorPixels; 28 private WriteableBitmap colorBitmap; 29 private Int32Rect bitmapRect; 30 31 short[] depthFrame16 = new short[640 * 480]; 32 byte[] depthFrame32 = new byte[640 * 480 * 4]; 33 34 const int RED_IDX = 2; 35 const int GREEN_IDX = 1; 36 const int BLUE_IDX = 0; 37 38 int[] realDepth = new int[640 * 480]; 39 40 public MainWindow() 41 { 42 InitializeComponent(); 43 44 //button.Click += new EventHandler(SensorDepthFrameReady); 45 } 46 private void Window_Loaded(object sender, RoutedEventArgs e) 47 { 48 foreach (var potentialSensor in KinectSensor.KinectSensors) 49 { 50 if (potentialSensor.Status == KinectStatus.Connected) 51 { 52 sensor = potentialSensor; 53 break; 54 } 55 } 56 if (null != sensor) 57 { 58 sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30); 59 sensor.DepthFrameReady += SensorDepthFrameReady; 60 61 sensor.DepthStream.Range = DepthRange.Near; 62 63 depthPixels = new DepthImagePixel[sensor.DepthStream.FramePixelDataLength]; 64 colorPixels = new byte[sensor.DepthStream.FramePixelDataLength * sizeof(int)]; 65 66 //画面上に表示するビットマップ 67 colorBitmap = new WriteableBitmap(sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null); 68 69 //画像のサイズ(640*480) 70 bitmapRect = new Int32Rect(0, 0, sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight); 71 72 try 73 { 74 sensor.Start(); 75 } 76 catch (IOException) 77 { 78 sensor = null; 79 } 80 } 81 } 82 private void convertDepthFrame(short[] depthFrame16) 83 { 84 for (int i16 = 0, i32 = 0, i = 0; i16 < depthFrame16.Length && i < depthFrame32.Length; i16 += 1, i32 += 4, i++) 85 { 86 realDepth[i] = (ushort)depthFrame16[i16] >> 3; 87 int pixel = 0; 88 89 pixel = 0 + (realDepth[i] >> 4); 90 if (800 < realDepth[i]) 91 pixel = 255; 92 else if (realDepth[i] < 650) 93 pixel = 0; 94 else 95 pixel = 0 + (int)(((realDepth[i] - 650) / 152.0) * 255); 96 97 depthFrame32[i32 + RED_IDX] = (byte)pixel; 98 depthFrame32[i32 + GREEN_IDX] = (byte)pixel; 99 depthFrame32[i32 + BLUE_IDX] = (byte)pixel; 100 } 101 } 102 private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e) 103 { 104 using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame()) 105 { 106 if (depthImageFrame != null) 107 { 108 depthImageFrame.CopyPixelDataTo(depthFrame16); 109 convertDepthFrame(depthFrame16); 110 111 depthImage.Source = BitmapSource.Create(depthImageFrame.Width, depthImageFrame.Height, 96, 96, PixelFormats.Bgr32, null, depthFrame32, depthImageFrame.Width * 4); 112 113 //colorBitmap.WritePixels(bitmapRect, colorPixels, colorBitmap.PixelWidth * sizeof(int), 0); 114 } 115 } 116 } 117 private void button_Click(object sender, RoutedEventArgs e) 118 { 119 var image = (WriteableBitmap)depthImage.Source; // ? 120 121 using (System.IO.FileStream fs = System.IO.File.Create(@"Z:kageE/kitsune.png")) 122 { 123 var enc = new PngBitmapEncoder(); 124 enc.Frames.Add(BitmapFrame.Create(image)); 125 enc.Save(fs); 126 127 MessageBox.Show("保存しました"); 128 } 129 } 130 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) 131 { 132 if (null != this.sensor) 133 { 134 sensor.Stop(); 135 } 136 } 137 } 138} 139

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

qt6hy

2016/11/30 08:55

もう少し全体のソースとか、使っているセンサー?とかについて書かないと、期待した回答が得られないかと思います。
BOSS723

2016/11/30 08:57

使っているセンサーはKinectですがあまり関係ないかと思って削ってしまいました。全体のソースは長いから見てもらえないかと思いこれも省いてしまいました。載せますね。
guest

回答1

0

自己解決

解決しました。
疑問だったvar image = (WriteableBitmap)depthImage.Source; の部分を
var image = new RenderTargetBitmap(sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight, 96, 96, PixelFormats.Pbgra32);
image.Render(depthImage);
と変更したことで無事保存できました。

投稿2016/12/01 09:07

BOSS723

総合スコア35

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問