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

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

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

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

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Kinect

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

Q&A

0回答

1710閲覧

KINECTで得られた画像をOpenCvSharpに渡して処理を行いたいです。

keisuke1995

総合スコア16

C#

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

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Kinect

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

0グッド

1クリップ

投稿2018/02/02 05:47

KINECTの深度情報から、ある一定距離以上のところを塗りつぶして二値化しています。ここまでは分かるのですが、ここからはOpencvsharpで処理を行いたいです(面積計算など)。Opencvsharpでの処理は、どこにプログラムを書くべきでしょうか?

また、どのようにしてKINECTからOpencvsharpに画像(深度画像)を渡せば良いのか教えてくれませんか?
以下は、KINECTから色画像と深度画像を得るプログラムです。

よろしくお願いいたします。

C#

1using System; 2using System.Diagnostics; 3using System.IO; 4using System.Threading; 5using System.Windows; 6using System.Windows.Media; 7using System.Windows.Media.Imaging; 8using System.Windows.Shapes; 9using System.Collections.Generic; 10using System.Linq; 11using System.Text; 12using System.Threading.Tasks; 13using System.Windows.Controls; 14using System.Windows.Data; 15using System.Windows.Documents; 16using System.Windows.Input; 17using System.Windows.Navigation; 18using OpenCvSharp; 19using OpenCvSharp.Extensions; 20 21using Microsoft.Kinect; 22 23namespace KINECT_Opencv 24{ 25 /// <summary> 26 /// MainWindow.xaml の相互作用ロジック 27 /// </summary> 28 public partial class MainWindow : Window 29 { 30 /// <summary> 31 /// コンストラクタ 32 /// </summary> 33 public MainWindow() 34 { 35 try 36 { 37 InitializeComponent(); 38 39 // Kinectが接続されているかどうかを確認する 40 if (KinectSensor.KinectSensors.Count == 0) 41 { 42 throw new Exception("Kinectを接続してください"); 43 } 44 45 // Kinectの動作を開始する 46 StartKinect(KinectSensor.KinectSensors[0]); 47 } 48 catch (Exception ex) 49 { 50 MessageBox.Show(ex.Message); 51 Close(); 52 } 53 } 54 55 /// <summary> 56 /// Kinectの動作を開始する 57 /// </summary> 58 /// <param name="kinect"></param> 59 private void StartKinect(KinectSensor kinect) 60 { 61 // RGBカメラを有効にして、フレーム更新イベントを登録する 62 kinect.ColorStream.Enable(); 63 kinect.ColorFrameReady += 64 new EventHandler<ColorImageFrameReadyEventArgs>(kinect_ColorFrameReady); 65 66 // 距離カメラを有効にして、フレーム更新イベントを登録する 67 kinect.DepthStream.Enable(); 68 kinect.DepthFrameReady += 69 new EventHandler<DepthImageFrameReadyEventArgs>(kinect_DepthFrameReady); 70 71 // Kinectの動作を開始する 72 kinect.Start(); 73 74 // defaultモードとnearモードの切り替え 75 comboBoxRange.Items.Clear(); 76 foreach (var range in Enum.GetValues(typeof(DepthRange))) 77 { 78 comboBoxRange.Items.Add(range.ToString()); 79 } 80 81 comboBoxRange.SelectedIndex = 0; 82 } 83 84 /// <summary> 85 /// Kinectの動作を停止する 86 /// </summary> 87 /// <param name="kinect"></param> 88 private void StopKinect(KinectSensor kinect) 89 { 90 if (kinect != null) 91 { 92 if (kinect.IsRunning) 93 { 94 // フレーム更新イベントを削除する 95 kinect.ColorFrameReady -= kinect_ColorFrameReady; 96 kinect.DepthFrameReady -= kinect_DepthFrameReady; 97 98 // Kinectの停止と、ネイティブリソースを解放する 99 kinect.Stop(); 100 kinect.Dispose(); 101 102 imageRgb.Source = null; 103 imageDepth.Source = null; 104 } 105 } 106 } 107 108 /// <summary> 109 /// RGBカメラのフレーム更新イベント 110 /// </summary> 111 /// <param name="sender"></param> 112 /// <param name="e"></param> 113 void kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) 114 { 115 try 116 { 117 // RGBカメラのフレームデータを取得する 118 using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) 119 { 120 if (colorFrame != null) 121 { 122 // RGBカメラのピクセルデータを取得する 123 byte[] colorPixel = new byte[colorFrame.PixelDataLength]; 124 colorFrame.CopyPixelDataTo(colorPixel); 125 126 // ピクセルデータをビットマップに変換する 127 imageRgb.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, 128 PixelFormats.Bgr32, null, colorPixel, colorFrame.Width * colorFrame.BytesPerPixel); 129 } 130 } 131 } 132 catch (Exception ex) 133 { 134 MessageBox.Show(ex.Message); 135 } 136 } 137 138 /// <summary> 139 /// 距離カメラのフレーム更新イベント 140 /// </summary> 141 /// <param name="sender"></param> 142 /// <param name="e"></param> 143 readonly int Bgr32BytesPerPixel = PixelFormats.Bgr32.BitsPerPixel / 8; 144 145 void kinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e) 146 { 147 try 148 { 149 // Kinectのインスタンスを取得する 150 KinectSensor kinect = sender as KinectSensor; 151 if (kinect == null) 152 { 153 return; 154 } 155 156 // 距離カメラのフレームデータを取得する 157 using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) 158 { 159 if (depthFrame != null) 160 { 161 // 距離データを画像化して表示 162 imageDepth.Source = BitmapSource.Create(depthFrame.Width, 163 depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, 164 ConvertDepthColor(kinect, depthFrame), 165 depthFrame.Width * Bgr32BytesPerPixel); 166 } 167 } 168 } 169 catch (Exception ex) 170 { 171 MessageBox.Show(ex.Message); 172 } 173 } 174 175 /// <summary> 176 /// 距離データをカラー画像に変換する 177 /// </summary> 178 /// <param name="kinect"></param> 179 /// <param name="depthFrame"></param> 180 /// <returns></returns> 181 private byte[] ConvertDepthColor(KinectSensor kinect, 182 DepthImageFrame depthFrame) 183 { 184 ColorImageStream colorStream = kinect.ColorStream; 185 DepthImageStream depthStream = kinect.DepthStream; 186 187 // 距離カメラのピクセルごとのデータを取得する 188 short[] depthPixel = new short[depthFrame.PixelDataLength]; 189 depthFrame.CopyPixelDataTo(depthPixel); 190 191 // 距離カメラの座標に対応するRGBカメラの座標を取得する(座標合わせ) 192 ColorImagePoint[] colorPoint = 193 new ColorImagePoint[depthFrame.PixelDataLength]; 194 kinect.MapDepthFrameToColorFrame(depthStream.Format, depthPixel, 195 colorStream.Format, colorPoint); 196 197 byte[] depthColor = new byte[depthFrame.PixelDataLength * Bgr32BytesPerPixel]; 198 for (int index = 0; index < depthPixel.Length; index++) 199 { 200 // 距離カメラのデータから、距離を取得する 201 int distance = depthPixel[index] >> DepthImageFrame.PlayerIndexBitmaskWidth; 202 203 // 変換した結果が、フレームサイズを超えることがあるため、小さいほうを使う 204 int x = Math.Min(colorPoint[index].X, colorStream.FrameWidth - 1); 205 int y = Math.Min(colorPoint[index].Y, colorStream.FrameHeight - 1); 206 207 // 動作が遅くなる場合、MapDepthFrameToColorFrame を外すと速くなる場合が 208 // あります。外す場合のx,yはこちらを使用してください。 209 //int x = index % depthFrame.Width; 210 //int y = index / depthFrame.Width; 211 212 int colorIndex = ((y * depthFrame.Width) + x) * Bgr32BytesPerPixel; 213 214 // 指定された距離より遠ければ色付け 215 if (distance >= depthStream.MinDepth) 216 { 217 depthColor[colorIndex] = 0; 218 depthColor[colorIndex + 1] = 0; 219 depthColor[colorIndex + 2] = 255; 220 } 221 222 } 223 224 return depthColor; 225 } 226 227 /// <summary> 228 /// 距離カメラの通常/近接モード変更イベント 229 /// </summary> 230 /// <param name="sender"></param> 231 /// <param name="e"></param> 232 private void comboBoxRange_SelectionChanged(object sender, 233 System.Windows.Controls.SelectionChangedEventArgs e) 234 { 235 try 236 { 237 KinectSensor.KinectSensors[0].DepthStream.Range = 238 (DepthRange)comboBoxRange.SelectedIndex; 239 } 240 catch (Exception) 241 { 242 comboBoxRange.SelectedIndex = 0; 243 } 244 } 245 246 247 /// <summary> 248 /// Windowsが閉じられるときのイベント 249 /// </summary> 250 /// <param name="sender"></param> 251 /// <param name="e"></param> 252 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) 253 { 254 StopKinect(KinectSensor.KinectSensors[0]); 255 } 256 } 257}

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問