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 Xalemich · Oct 16, 2014 at 05:01 AM · c#rgbcolor32

Accessing values in Color32 or convert to Byte array

I am trying to access pixel R, G and B values in a rather large web camera feed, and was led towards the path of using the .GetPixels32, since this was supposed to be the fastest option, and indeed it seems so. Now I just need to access the individual values, in order to measure them against certain limits, E.G. is this red enough to be the previously introduced red object? But I can't seem to figure out how to access the individual values (a,b,g,r), (or as it says in the debug: RGBA).

What I know about the GetPixels32: http://docs.unity3d.com/ScriptReference/WebCamTexture.GetPixels32.html

What I know about the Color32: http://docs.unity3d.com/ScriptReference/Color32.html

Currently I am trying to feed the RGB values into an array, which is 16 columns wide, 16 rows high, and has 3 values (the colors):

  using UnityEngine;
  using System.Collections;
  public class CamTexture : MonoBehaviour {
      public byte[,,] colorArr = new byte[16,16,3];
      public static WebCamTexture webcam;
      private Color32[] ImgMatrix;
      void Start (){
          webcam = new WebCamTexture();
          renderer.material.mainTexture = webcam;
          webcam.Play();
          ImgMatrix = new Color32[webcam.width * webcam.height];
          webcam.GetPixels32(ImgMatrix);
          //This is a desperate and vague attempt at feeding any value into colorArr...
          colorArr[0,0,0] = ImgMatrix[0];
      }
  }

Any, also different ways of approaching the problem, ways of doing this would be amazing. The values are there; the Debug.Log shows the values just fine when I feed it the ImgMatrix[0], i just don't see how to get at them...

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

2 Replies

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

Answer by Xalemich · Oct 16, 2014 at 12:53 PM

Right, in my way too common "Why didn't I think of that before!?" manner, I realized what needed to be done: ImgMatrix[0].r, or col[0].r, returns the red value of the color32 struct. And so on and so forth with g, b and a. Hope this helps someone who's also stuck at this point get over it faster than I did.

Comment
Add comment · 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
0

Answer by KayelGee · Oct 16, 2014 at 06:28 AM

I don't quite understand what you're trying to do with that byte array.

If you're only trying to access the color values of pixels at position x,y then the solution is quite simple. Your ImgMatrix is an array of Color structs so it has all the information you need. To get the Color struct at position x,y:

 Color32 col = ImgMatrix[y*webcam.width + x];

Why does this work? Let's say our camera has 3 lines and looks like this:

 AAAAAAAAAA
 BBBBBBBBBB
 CCCCCCCCCC

Each character is one pixel on our cam. This two dimensonal image is actually stored as one continuous array which looks like this:

 AAAAAAAAAABBBBBBBBBBCCCCCCCCCC

And that's your ImgMatrix array.

Comment
Add comment · Show 2 · 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 Xalemich · Oct 16, 2014 at 07:14 AM 0
Share

Indeed that is the way I understood the Img$$anonymous$$atrix (sorry for the misna$$anonymous$$g there, named that before I understood it was a single-dimensional structure), but I need to access the RGB values individually; to follow your example, each position in the Color32 col has an R, G, B and A (alpha, not your ABC version), value, but I don't know how to access these, only the positions. I want to be able access the R value of col[12], for instance.

Thanks for the reply $$anonymous$$ayelGee

avatar image KayelGee · Oct 16, 2014 at 03:41 PM 0
Share

I thought you knew that because you posted the documentation of Color32 which point's that out :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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

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

Change Color in C# with RGB Values? 4 Answers

Flip over an object (smooth transition) 3 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