eec-492/592 kinect application development lecture 5 wenbing zhao [email protected]

24
EEC-492/592 EEC-492/592 Kinect Application Kinect Application Development Development Lecture 5 Lecture 5 Wenbing Zhao Wenbing Zhao [email protected] [email protected]

Upload: roberta-warner

Post on 03-Jan-2016

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

EEC-492/592EEC-492/592Kinect Application Kinect Application

DevelopmentDevelopmentLecture 5Lecture 5

Wenbing ZhaoWenbing Zhao

[email protected]@ieee.org

Page 2: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

OutlineOutline Important Classes on Color Image Retrieving/Setting Color Image Format Capturing Frames on Demand Frame Number Frame rate Saving frames Extend the KinectCam app with more controls

Page 3: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Important Classes on Color Image We have dealt with two classes related to Kinect

color images ColorImageStream: handles a stream of ColorImageFrame ColorImageFrame

ColorImageStream this.sensor.ColorStream.Enable(); ColorStream is of ColorImageStream type

ColorImageFrame ColorImageFrame imageFrame =

e.OpenColorImageFrame()

Page 4: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

ColorImageStream

Sealed class: no other class can inhert a sealed class

Page 5: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

ColorImageFrame

Page 6: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

More on ColorImageFrame

Page 7: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Retrieving/Setting Color Image Format Can retrieve the color image format for the current

image frame using the ImageFrame.Format property

public ColorImageFormat ImageFormat {get;set;} Retrieve format

this.ImageFormat = imageFrame.Format; Setting format

this.sensor.ColorStream.Enable(RgbResolution640x480Fps30);

Page 8: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Capturing Frames on Demand Polling: request an image frame on demand

ColorImageStream::openNextFrame() OpenNextFrame() accepts one parameter

specifying how long the sensor should wait before it sends the image frame If there is no frame ready within the provided time, the

method will return null

int millisecondsWait = 10;if (this.sensor.ColorStream.IsEnabled){ ColorImageFrame colorImageFrame = this.sensor.ColorStream. OpenNextFrame(millisecondsWait);}

Page 9: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Frame Number Frame number: Every frame has a number to

identify the frame This number is incremented with each new frame

generated It is read only in image frame int frameNumber=imageFrame.FrameNumber;

Page 10: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Frame Rate Calculation private int TotalFrames; private DateTime lastTime = DateTime.MaxValue; private int LastFrames; int currentFrameRate = 0; private int GetCurrentFrameRate() { ++this.TotalFrames; DateTime currentTime = DateTime.Now; var timeSpan = currentTime.Subtract(this.lastTime); if (this.lastTime == DateTime.MaxValue || // first time to run

timeSpan >= TimeSpan.FromSeconds(1)) // average 1 second { currentFrameRate = (int)Math.Round((this.TotalFrames - this. LastFrames) / timeSpan.TotalSeconds); this.LastFrames = this.TotalFrames; this.lastTime = currentTime; } return currentFrameRate;}

Page 11: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Saving Color Imageprivate void SaveImage() {

using (FileStream fs = new FileStream(string.Format("{0}.jpg", Guid.NewGuid().ToString()), System.IO.FileMode.Create)){

BitmapSource imageSource = (BitmapSource)image1.Source;JpegBitmapEncoder jpegEncoder = new JpegBitmapEncoder();jpegEncoder.Frames.Add(BitmapFrame.Create(imageSource));jpegEncoder.Save(fs);fs.Close();

}}

Page 12: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Saving Color Images Periodically Call the SaveImage () method on a Tick event of

DispatcherTimer Automatic periodic image saving. 4 steps. Step1: Define the DispatcherTimer object under the

System.Windows.Threading namespace: private DispatcherTimer timer = new DispatcherTimer();

Page 13: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Saving Color Images Periodically Step 2: Write the start method with an interval of 10

seconds and attach the Tick event handler:public void StartTimer()

{

this.timer.Interval = new TimeSpan(0, 0, 10);

this.timer.Start();

this.timer.Tick += handleTickEvent;

}

// handleTickEvent: event handler delegate to be implemented

Page 14: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Saving Color Images Periodically Step 3: On the Tick event handler, call the SaveImage()

method:public void handleTickEvent(object sender, object e)

{

if (this.sensor.IsRunning && this.sensor.ColorStream.IsEnabled)

{

this.SaveImage();

}

}

Step 4: To launch the timer thread, call timer.StartTimer();

Page 15: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Saving Color Images Periodically To stop timer

public void StopTimer()

{

this.timer.Stop();

this.timer.Tick -= this.handleTickEvent;

}

Page 16: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Adjusting Elevation Angle Kinect elevation angle can be changed between

MaxElevationAngle (27 degrees) MinElevationAngle (-27 degrees)

Adjusting angleprivate void SetSensorAngle(int angleValue){ if(angleValue > sensor.MinElevationAngle ||

angleValue < sensor.MaxElevationAngle) { this.sensor.ElevationAngle = angleValue; }}

Page 17: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Build KinectCam2.0

Extend the KinectCam we built last time Revise GUI design

Adding image controls Add code to handle image controls

Page 18: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Revised GUI

Page 19: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Settings Changing frame format on the fly

RgbResolution640x480Fps30, enable this by default RgbResolution1280x960Fps12 Customize the checkbox names and the event handlers this time

private void ChangeToHighResolution(object sender, RoutedEventArgs e){ if (this.sensor != null) { this.normalresolution.IsChecked = false; this.sensor.ColorStream.Enable(

ColorImageFormat.RgbResolution1280x960Fps12); this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength]; }}

Page 20: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Settings Changing frame format on the fly

Enable one, and disable the other Need to reset the colorPixels array length for different formats

private void ChangeToNormalResolution (object sender, RoutedEventArgs e){ if (this.sensor != null) { this.highresolution.IsChecked = false; this.sensor.ColorStream.Enable(

ColorImageFormat.RgbResolution640x480Fps30); this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength]; }}

Page 21: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Settings Displaying frame rate

Calculated and displayed for each frame received At the end of void colorFrameReady(object sender,

ColorImageFrameReadyEventArgs e) textBox1.Text = ""+GetCurrentFrameRate();

Changing elevation angleprivate void IncrementAngle(object sender, RoutedEventArgs e) { int angleValue = this.sensor.ElevationAngle + 1; if (angleValue < sensor.MaxElevationAngle) { this.sensor.ElevationAngle = angleValue; this.AngleBox.Text = "" + angleValue; }}private void DecrementAngle(object sender, RoutedEventArgs e) { int angleValue = this.sensor.ElevationAngle - 1; if (angleValue > sensor.MinElevationAngle)……

Page 22: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Settings Save image on demand (Save Image button)

private void SaveImage(object sender, RoutedEventArgs e){ SaveImage();}private void SaveImage(){ using (FileStream fileStream = new FileStream(string.Format("{0}.jpg", Guid.NewGuid().ToString()), System.IO.FileMode.Create)) { BitmapSource imageSource = (BitmapSource)image1.Source; JpegBitmapEncoder jpegEncoder = new JpegBitmapEncoder(); jpegEncoder.Frames.Add(BitmapFrame.Create(imageSource)); jpegEncoder.Save(fileStream); fileStream.Close(); }}

Page 23: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Settings Save image periodically (via

checkbox) Make sure you designate event

handler for both checked and unchecked event!

Page 24: EEC-492/592 Kinect Application Development Lecture 5 Wenbing Zhao wenbing@ieee.org

Settings Save image periodically (via checkbox)

private void PeriodicSavingChanged (object sender, RoutedEventArgs e) { if (checkBox1.IsChecked == true) { StartTimer(); } else { this.timer.Stop(); }}private DispatcherTimer timer = new DispatcherTimer();public void StartTimer() { this.timer.Interval = new TimeSpan(0, 0, 10); this.timer.Start(); this.timer.Tick += this.handleTickEvent;}public void StopTimer() { this.timer.Stop(); this.timer.Tick -= this.handleTickEvent;}public void handleTickEvent(object sender, object e) { if (this.sensor.IsRunning && this.sensor.ColorStream.IsEnabled) { this.SaveImage(); } }