以下のようなプログラムで、デプス情報の画像と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}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/16 05:43
2018/02/16 05:53
2018/02/16 06:03
2018/02/16 06:13