Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rmccabe82 · Jan 18, 2015 at 06:26 PM · c#imageruntimekinectunityui

How to change image at runtime using bytes

Hi,

I am using the new Unity UI Image component. I am also using a Kinect. What I want to do is write the bytes from the Kinect image, to the Image component in Unity.

Anyone any ideas on the best way to do this?

I've saw a few examples of loading an image from disk into an image component, but these wont be saved on disk.

Thanks in advance!!!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DMGregory · Jan 18, 2015 at 06:43 PM

Which Kinect are you using?

Kinect v2 (The one that came with the Xbox One) has a set of Unity plugins and samples.

Here's how they handle it:

(in Start)

 _Reader = _Sensor.ColorFrameSource.OpenReader();        

 var frameDesc = _Sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Rgba);

 _Texture = new Texture2D(frameDesc.Width, frameDesc.Height, TextureFormat.RGBA32, false);
 _Data = new byte[frameDesc.BytesPerPixel * frameDesc.LengthInPixels];

(In Update)

 var frame = _Reader.AcquireLatestFrame();
         
 if (frame != null)
 {
      frame.CopyConvertedFrameDataToArray(_Data, ColorImageFormat.Rgba);
      _Texture.LoadRawTextureData(_Data);
      _Texture.Apply();
             
      frame.Dispose();
      frame = null;
 }

In Start, you should be able to assign the Texture2D _Texture to a new Sprite like so:

 myImage.sprite = Sprite.Create(_Texture, sourceRect, pivot, scaling);

and it will get updated as new frames become available from the sensor.

If you're using the original Kinect, the code for reading data from it will be a bit different, but the basic principle should still apply.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image npatch · Jan 21, 2016 at 03:20 PM 0
Share

Problem is that CreateFrameDescription should change the FrameDescription of the ColorFrameSource.

Creates a FrameDescription object that describes a color frame with the provided format and resolution. This replaces the FrameDescription property found on other image streams.

($$anonymous$$SDN $$anonymous$$inect v2 API Overview)

But in reality, CreateFrameDescription seems to have a bug because while it does create a new FrameDescription object in memory, it doesn't associate it to the ColorFrameSource.

If this worked correctly then you could use frame.CopyRawFrameDataToArray(_Data) and avoid using ~4ms to ~9ms to convert color data to RGBA.

$$anonymous$$eep in $$anonymous$$d that the above comment mainly applies to those whose sensor's raw color data come in YUV2 or anything other than the format you need. BGRA is simple to switch to RGBA in a shader but YUV2 which is half in size would need more time to decompress into BGRA/RGBA.

Furthermore, if you consider performance, it might be wise to consider pinning the buffers in memory to avoid constant allocations for your byte arrays and using the Copy....FrameDataToIntPtr ins$$anonymous$$d.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

RawImage to world 2 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Dynamic editor dll linking? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges