none
i want to capture the image in greyscale. RRS feed

  • Question

  • i have this program that will capture the image. but i want to capture it in greyscale. i have this error. where my setpixel and getpixel need a method. the error is at the void button1_Click. 

     

    source code:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Navigation;

    using System.Windows.Media.Media3D;

    using System.Windows.Shapes;

    using Microsoft.Research.Kinect.Nui;

    using System.IO;

    using System.Threading;

    using Coding4Fun.Kinect.Wpf;

    using Coding4Fun.Kinect.Wpf.Controls;

    using System.Drawing;

     

    namespace KinectSkeletonApplication3

    {

        public partial class MainWindow : Window

     

        {

            //Instantiate the Kinect runtime. Required to initialize the device.

            //IMPORTANT NOTE: You can pass the device ID here, in case more than one Kinect device is connected.

     

     

            Runtime runtime = Runtime.Kinects[0];

            private static double _topBoundary;

            private static double _bottomBoundary;

            private static double _leftBoundary;

            private static double _rightBoundary;

            private static double _itemLeft;

            private static double _itemTop;

     

            public MainWindow()    

            {

                InitializeComponent();

     

                //Runtime initialization is handled when the window is opened. When the window

                //is closed, the runtime MUST be unitialized.

                this.Loaded += new RoutedEventHandler(MainWindow_Loaded);

                this.Unloaded += new RoutedEventHandler(MainWindow_Unloaded);

                kinectButton.Click += new RoutedEventHandler(kinectButton_Click);

     

                runtime.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(runtime_SkeletonFrameReady);

     

                runtime.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(runtime_DepthFrameReady);

            }

     

            void runtime_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)

            {

                //throw new NotImplementedException();

            }

     

            private void kinectButton_Click(object sender, RoutedEventArgs e)

            {

                BitmapSource image = (BitmapSource)video.Source;

                image.Save(DateTime.Now.ToString("ddMMyyyy HHmmss") + ".jpg", ImageFormat.Jpeg);

     

                //throw new NotImplementedException();

            }

     

            private static void CheckButton(HoverButton button, Ellipse thumbStick)

            {

                if (IsItemMidpointInContainer(button, thumbStick))

                {

                    button.Hovering();

                }

                else

                {

                    button.Release();

                }

            }

     

            public static bool IsItemMidpointInContainer(FrameworkElement container, FrameworkElement target)

            {

                FindValues(container, target);

     

                if (_itemTop < _topBoundary || _bottomBoundary < _itemTop)

                {

                    //Midpoint of target is outside of top or bottom

                    return false;

                }

     

                if (_itemLeft < _leftBoundary || _rightBoundary < _itemLeft)

                {

                    //Midpoint of target is outside of left or right

                    return false;

                }

     

                return true;

            }

     

     

            private static void FindValues(FrameworkElement container, FrameworkElement target)

            {

                var containerTopLeft = container.PointToScreen(new System.Windows.Point());

                var itemTopLeft = target.PointToScreen(new System.Windows.Point());

     

                _topBoundary = containerTopLeft.Y;

                _bottomBoundary = _topBoundary + container.ActualHeight;

                _leftBoundary = containerTopLeft.X;

                _rightBoundary = _leftBoundary + container.ActualWidth;

     

                //use midpoint of item (width or height divided by 2)

                _itemLeft = itemTopLeft.X + (target.ActualWidth / 2);

                _itemTop = itemTopLeft.Y + (target.ActualHeight / 2);

            }

     

     

            void runtime_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)

            {

                SkeletonFrame skeletonSet = e.SkeletonFrame;

     

     

                SkeletonData data = (from s in skeletonSet.Skeletons

                                     where s.TrackingState == SkeletonTrackingState.Tracked

                                     select s).FirstOrDefault();

                if (data != null)

                {

                    SetEllipsePosition(Head, data.Joints[JointID.Head]);

                    SetEllipsePosition(leftHand, data.Joints[JointID.HandLeft]);

                    SetEllipsePosition(rightHand, data.Joints[JointID.HandRight]);

                }

                CheckButton(kinectButton, rightHand);

                #region TransformSmooth

                runtime.SkeletonEngine.TransformSmooth = true;

                var parameters = new TransformSmoothParameters

                {

                    Smoothing = 0.75f,

                    Correction = 0.0f,

                    Prediction = 0.0f,

                    JitterRadius = 0.0f,

                    MaxDeviationRadius = 0.04f

                };

                runtime.SkeletonEngine.SmoothParameters = parameters;

                #endregion

            }

     

            void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)

            {

                PlanarImage image = e.ImageFrame.Image; //Allows us to see image data

                video.Source = e.ImageFrame.ToBitmapSource();

                //throw new NotImplementedException();

     

            }    

     

            private void SetEllipsePosition(Ellipse ellipse, Joint joint)

            {

                Microsoft.Research.Kinect.Nui.Vector vector = new Microsoft.Research.Kinect.Nui.Vector();

                vector.X = ScaleVector(640, joint.Position.X);

                vector.Y = ScaleVector(480, -joint.Position.Y);

                vector.Z = joint.Position.Z;

     

                Joint updatedJoint = new Joint();

                updatedJoint.ID = joint.ID;

                updatedJoint.TrackingState = JointTrackingState.Tracked;

                updatedJoint.Position = vector;

     

                Canvas.SetLeft(ellipse, updatedJoint.Position.X);

                Canvas.SetTop(ellipse, updatedJoint.Position.Y);

     

     

            }

     

            private float ScaleVector(int length, float position)

            {

                float value = (((((float)length) / 1f) / 2f) * position) + (length / 2);

                if (value > length)

                {

                    return (float)length;

                }

                if (value < 0f)

                {

                    return 0f;

                }

                return value;

            }

     

     

            private void button1_Click(object sender, RoutedEventArgs e)

            {

                BitmapSource image = (BitmapSource)video.Source;

                image.Save(DateTime.Now.ToString("ddMMyyyy HHmmss") + ".jpg", ImageFormat.Jpeg);

                MessageBox.Show("Saved on bin/debug");

     

                byte A, R, G, B;

                System.Drawing.Color pixelColor;

     

                for (int y = 0; y < image.Height; y++)

                {

                    for (int x = 0; x < image.Width; x++)

                    {

                        pixelColor = image.GetPixel(x, y);

                        A = pixelColor.A;

                        R = (byte)((0.299 * pixelColor.R) + (0.587 * pixelColor.G) + (0.114 * pixelColor.B));

                        G = B = R;

     

                        image.SetPixel(x, y, Color.FromArgb((int)A, (int)R, (int)G, (int)B));

                    }

                }

            }

     

     

            private void button2_Click(object sender, RoutedEventArgs e)

            {

                if (runtime.NuiCamera.ElevationAngle != (int)slider1.Value)

                {

                    runtime.NuiCamera.ElevationAngle = (int)slider1.Value;

                }

            }

     

            private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

            {

                int n = (int)slider1.Value;

                label2.Content = n.ToString();

            }

     

            void MainWindow_Unloaded(object sender, RoutedEventArgs e)

            {

                runtime.Uninitialize();

            }

     

            void MainWindow_Loaded(object sender, RoutedEventArgs e)

            {

                //Since only a color video stream is needed, RuntimeOptions.UseColor is used.

                runtime.Initialize(Microsoft.Research.Kinect.Nui.RuntimeOptions.UseColor | RuntimeOptions.UseSkeletalTracking);

     

                runtime.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);

                runtime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nui_VideoFrameReady);

     

            }

     

        }

    }

    error:  'System.Windows.Media.Imaging.BitmapSource' does not contain a definition for 'GetPixel' and no extension method 'GetPixel' accepting a first argument of type 'System.Windows.Media.Imaging.BitmapSource' could be found (are you missing a using directive or an assembly reference?)
    Error 2 'System.Windows.Media.Imaging.BitmapSource' does not contain a definition for 'SetPixel' and no extension method 'SetPixel' accepting a first argument of type 'System.Windows.Media.Imaging.BitmapSource' could be found (are you missing a using directive or an assembly reference?)
    Error 3 'Color' is an ambiguous reference between 'System.Windows.Media.Color' and 'System.Drawing.Color'

    Monday, February 6, 2012 3:04 AM

Answers

  • Please take a look at Kinect Explorer. This is a WPF application. Using WpfViewers you can get back the BitmapSource for color or depth images, and then use WPF to save the file out.
    Wednesday, February 8, 2012 12:40 AM

All replies

  • BitmapSource does not have a GetPixel method. It looks like you're mixing WPF (System.Windows.*) and GDI+ (System.Drawing.*). Please try converting your code to either use WPF or GDI+.
    Monday, February 6, 2012 4:59 PM
  • do you have a code in wpf, that will capture greyscale if i click the button.

    Tuesday, February 7, 2012 2:49 PM
  • Please take a look at Kinect Explorer. This is a WPF application. Using WpfViewers you can get back the BitmapSource for color or depth images, and then use WPF to save the file out.
    Wednesday, February 8, 2012 12:40 AM
  • thanks! sorry for the question. it's my first time to program c#. 
    Thursday, February 9, 2012 2:43 PM