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

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

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

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

OpenCV

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

Kinect

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

Q&A

解決済

2回答

2892閲覧

KINECTで得た画像をOpencvSharpに渡したいです。

keisuke1995

総合スコア16

C#

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

OpenCV

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

Kinect

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

0グッド

0クリップ

投稿2018/02/14 08:45

編集2018/02/14 09:42

以下のようなプログラムで、デプス情報の画像とRGBの画像の両方を得ました。

ここからOpencvSharpで、処理を行いたいのですが、どのようにして画像を変換するべきなのか教えていただきたいです。よろしくお願いいたします。

ビットマップからIplImage型に変換するため、ToIplImage()を、追加しました。// ピクセルデータをビットマップに変換する の下です。
しかし、次のようなエラーが出ます。

型'Bitmap'は、参照されていないアセンブリに定義されています。アセンブリ'SYstem.Drawing,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a'に参照を追加する必要があります。

何か対策法を教えていただけませんか?

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

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

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

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

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

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

guest

回答2

0

ベストアンサー

BMPファイルであれば
using (IplImage im = new IplImage("△△.bmp")) // 入力画像の取得
{
using (new CvWindow("Test", im)) // 画面に画像を表示
{
Cv.WaitKey(); // キー入力待機
}
}

変換ではこんな感じでやった記録があります。
Bitmap bmp =new Bitmap(SearchSize, SearchSize);
IplImage qimg = (OpenCvSharp.IplImage)BitmapConverter.ToIplImage(bmp);
OPenCVSherpのバージヨンによって動作しない場合があるかもしれません。
Ver.2.4.10

投稿2018/02/16 05:22

RED_CAT

総合スコア59

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

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

keisuke1995

2018/02/16 05:43

回答ありがとうございます。 Bitmap bmp を打ち込むと、現在のコンテキストには Biymapは存在しませんと言われます。 どのようにして、BMPファイルとして読み込むのでしょうか?
keisuke1995

2018/02/16 05:53

すいません、上記の件については解決しました。 変換された画像データをグレースケールにしようとすると、Cv.CvtColor(qimg, qimg, ColorConversion.BgrToGray);でエラーが出ます。 データ型が違うのでしょうか?
RED_CAT

2018/02/16 06:03

読み込んだら表示はできましたか? 期待した画像か確認ください。 帰るので次は月曜日になります。
keisuke1995

2018/02/16 06:13

すいません。自己解決しました。 ありがとうございました。
guest

0

回答がないようなのでコメントしますが Kinect は使ったことはありません。
OpenCVSharp を利用したことがある程度の者です。
①最終目標は分かりませんが 距離データ(2値化したもの)をOpenCVSharpで表示したいと思っていましたが・・・??
// 距離データを画像化して表示 の後で処理された方がいいと思います。
②解決の手段の一つとして 「距離データを画像化して表示」の直後に Kinect でファイルに落とすことはできませんか?
ファイルをOpenCVSharpで復元する方法も一例かと。
それが可能ならば直接変換も可能になります。(Kinect のデータ構造知らないので)
③アップされたプログラムで imageRgb がどこで定義されているか確認できませんでした。OpenCVSharp で定義が必要かと?

違っていたらすみません。

投稿2018/02/15 00:28

RED_CAT

総合スコア59

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

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

keisuke1995

2018/02/16 04:15

回答ありがとうございます。遅れて申し訳ありません。imageRgbは、KINECTのプログラムの中で定義されてます。 imageRgb.Source.Save(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + ".bmp");  を追加して、ビットマップとして保存を行いたいと思います。
keisuke1995

2018/02/16 06:13

すいません。解決できました。 回答していただき、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問