Kinectの深度画像をOpenCVSharpで二値化したいのですが、うまくいきません。
以下、OpenCVSharpの二値化のプログラムを足してみたのですがよくわからなくなってしまい...どこが間違ってますかね?
C#
1using System; 2using System.IO; 3using System.Windows; 4using System.Windows.Media; 5using System.Windows.Media.Imaging; 6 7using System.Drawing; 8 9using Microsoft.Kinect; 10using OpenCvSharp; 11using OpenCvSharp.Extensions; 12 13namespace KageE5 14{ 15 public partial class MainWindow : Window 16 { 17 private KinectSensor sensor; 18 private WriteableBitmap srcImg; 19 private WriteableBitmap distImg; 20 private WriteableBitmap colorBitmap; 21 private Int32Rect bitmapRect; 22 private IplImage CvImg; 23 private IplImage CvGrayImg; 24 private IplImage CannyImg; 25 private DepthImagePixel[] depthPixels; 26 private byte[] colorPixels; 27 28 public MainWindow() 29 { 30 InitializeComponent(); 31 32 //Kinectが接続されているかどうか確認する 33 if (KinectSensor.KinectSensors.Count == 0) 34 { 35 MessageBox.Show("KINECTがみつかりません。"); 36 Close(); 37 } 38 } 39 40 private void WindowLoaded(object sender, RoutedEventArgs e) 41 { 42 43 foreach (var potentialSensor in KinectSensor.KinectSensors) 44 { 45 if (potentialSensor.Status == KinectStatus.Connected) 46 { 47 this.sensor = potentialSensor; 48 break; 49 } 50 } 51 52 if (null != this.sensor) 53 { 54 this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30); 55 this.depthPixels = new DepthImagePixel[this.sensor.DepthStream.FramePixelDataLength]; 56 this.colorPixels = new byte[this.sensor.DepthStream.FramePixelDataLength * sizeof(int)]; 57 this.colorBitmap = new WriteableBitmap(this.sensor.DepthStream.FrameWidth, this.sensor.DepthStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null); 58 this.Image.Source = this.colorBitmap; 59 this.sensor.DepthFrameReady += this.SensorDepthFrameReady; 60 61 //OpenCV用 62 srcImg = new WriteableBitmap(sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight, 96, 96, PixelFormats.Bgra32, null); 63 //OpenCV処理後用 64 distImg = new WriteableBitmap(sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null); 65 //画像のサイズ(640*480) 66 bitmapRect = new Int32Rect(0, 0, sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight); 67 68 //OpenCV用画像 69 CvImg = Cv.CreateImage(new CvSize(640, 480), BitDepth.U8, 3); 70 //OpenCV用グレースケール画像 71 CvGrayImg = new IplImage(CvImg.Size, BitDepth.U8, 1); 72 //OpenCV用Canny処理画像 73 CannyImg = new IplImage(CvImg.Size, BitDepth.U8, 1); 74 75 try 76 { 77 this.sensor.Start(); 78 } 79 catch (IOException) 80 { 81 this.sensor = null; 82 } 83 } 84 } 85 86 private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e) 87 { 88 if (null != this.sensor) 89 { 90 this.sensor.Stop(); 91 } 92 } 93 94 private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e) 95 { 96 using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) 97 { 98 if (depthFrame != null) 99 { 100 int minDepth = depthFrame.MinDepth; 101 int maxDepth = depthFrame.MaxDepth; 102 int min = 750; 103 int max = 1000; 104 105 int colorPixelIndex = 0; 106 107 for (int i = 0; i < this.depthPixels.Length; ++i) 108 { 109 short depth = depthPixels[i].Depth; 110 111 112 if (depth >= min && depth <= max) 113 { 114 115 byte intensity = (byte)(depth); 116 117 this.colorPixels[colorPixelIndex++] = intensity; 118 this.colorPixels[colorPixelIndex++] = intensity; 119 this.colorPixels[colorPixelIndex++] = intensity; 120 121 ++colorPixelIndex; 122 } 123 else if (depth >= minDepth && depth <= min) 124 { 125 this.colorPixels[colorPixelIndex++] = 255; 126 this.colorPixels[colorPixelIndex++] = 255; 127 this.colorPixels[colorPixelIndex++] = 255; 128 129 ++colorPixelIndex; 130 } 131 else 132 { 133 this.colorPixels[colorPixelIndex++] = 255; 134 this.colorPixels[colorPixelIndex++] = 255; 135 this.colorPixels[colorPixelIndex++] = 255; 136 137 ++colorPixelIndex; 138 } 139 } 140 141 //KINECTからのイメージデータ 142 depthFrame.CopyDepthImagePixelDataTo(this.depthPixels); 143 144 //OpenCV向けWritablebitmapに書込み 145 srcImg.WritePixels(bitmapRect, depthPixels, depthFrame.Width * depthFrame.BytesPerPixel, 0); 146 147 //WritablebitmapをIplImageに変換 148 CvImg = srcImg.ToIplImage(); 149 150 //グレースケール処理 151 Cv.CvtColor(CvImg, CvGrayImg, ColorConversion.BgrToGray); 152 //Canny処理(2値化) 153 Cv.Canny(CvGrayImg, CannyImg, 50, 200); 154 155 //Canny処理後の画像を変換して表示 156 Image.Source = CannyImg.ToWriteableBitmap(); 157 158 this.colorBitmap.WritePixels( 159 new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight), 160 this.colorPixels, 161 this.colorBitmap.PixelWidth * sizeof(int), 162 0); 163 } 164 } 165 } 166 } 167} 168
えっと、グレイスケールってこういう感じのをイメージしてたのですが違うんですか?すいません。下図のようなイメージです。
KinectのRGB画像をOpenCVSharpで二値化したものです。
もしかして二値化という言い方が違いますか?言葉が間違っているかもしれませんが、私は深度画像をこの二番目のような図にしたいです。
あと、うまくいかない、とはエラーが起きて動かないということです。