Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SuperRaed · Jun 17, 2017 at 01:53 PM · texturetexture2dimagegrayscale

How to load an RGB image from the resource folder into an int/float[] containing only grayscale values?

what I've been able to do so far is to read an image as a texture2D using Resources.loadAll("") and then iterate through the image using nested for loop and get each individual pixel stored into a float[] I have the ability to access the the red, blue , green, alpha and even grayscale values individually but what I need to store the the black and white representation of the image that is merge the 3 layers of RBG into a single layer and get the grayscale values either 1 or 0 , how can I do that?

 Texture2D[] images = Resources.LoadAll<Texture2D> ("");
         float[] finalImage = new float[ images[0].width * images[0].height ];
         int index = 0;
         for (int i = 0; i < images [0].width; i++) {
         
         
             for (int j = 0; j < images [0].height; j++) {
             
 
                 finalImage[index]=    Mathf.Round(images [0].GetPixel (i, j).a);
                 if (finalImage[index] ==0 && index<100) {
                     Debug.Log (i + " " + j);
                 }
                 index++;
             }
         }
 
Comment
Add comment · Show 1
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 hexagonius · Jun 17, 2017 at 02:24 PM 0
Share

https://docs.unity3d.com/ScriptReference/Color-grayscale.html

Btw. GetPixel for each pixel is slow. Using GetPixels gets all pixels at once. Double loop over the result and you have your image too.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Orion_78 · Apr 11, 2018 at 02:41 PM

You can use a shader

 Shader "Loreal/GeyScale"
 {
     Properties
     {
         _MainTex("MainTex", 2D) = "white" {}
     }
 
     SubShader
     {
         // No culling or depth
         Cull Off ZWrite Off ZTest Always
 
         Blend SrcAlpha OneMinusSrcAlpha
         Tags{ "Queue" = "Transparent" }
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
             };
 
             v2f vert(appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                 return o;
             }
             sampler2D _MainTex;
             
 
             fixed4 frag(v2f i) : SV_Target
             {
                 fixed4 color = tex2D(_MainTex, i.uv);
                 
             
             float greyscaleAverage = (color.r + color.g + color.b) / 3.0f;
 
                 return greyscaleAverage;
             }
             ENDCG
         }
     }
 }

Then "all you need to do" is to blit that render texture to your texture2d

 RenderTexture rt = RenderTexture.GetTemporary(textureRGB.width, textureRGB.height));
 
 Graphics.Blit(textureRGB rt, grayScaleMaterial);
 
 textureA.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0, false);

// not used textureA.Apply();

Then you can use textureA.GetRawTextureData() to have an array of color and keep only the r/g/b or a of your texture...

Not sure this is the fasted way

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

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

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Blending two Texture2D 1 Answer

Assiging an Image to a Button 1 Answer

shown like 16bit color image 1 Answer

Scaling / Resizing an Image (Texture2D) 6 Answers

How do you crop a Texture2d 1 Answer


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