• 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
Question by Albazcythe · Jun 05, 2013 at 04:28 PM · colorpixelgetpixel

How to pixelate an image texture for faster pixel color recognition?

I'm currently using GetPixel() for scanning small image files for later use of the color array. I pick the contents of the array and make an average of the colors to kind of simulate what the pixelate or mosaic effect would do, but I would need to apply this on much larger pictures and that would take forever. Is there a script I could adapt to correctly achieve the pixelation?

Thanks a lot.

Comment

People who like this

0 Show 0
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

Answer by robertbu · Jun 05, 2013 at 04:30 PM

You can use Texture2D.GetPixels32 to get all the pixels and then do your calculations directly on the pixel array. There will be a substantial performance improvement over repeated GetPixel() calls.

Comment

People who like this

0 Show 4 · 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 shacharoz · Jul 11, 2013 at 12:28 PM 0
Share

even when i do GetPixels() or GetPixels32() it tales pretty long... i was starting to ask myself maybe i need to take an average from several close pixels. anyone has an idea how to do that?

avatar image Albazcythe · Jul 11, 2013 at 12:38 PM 0
Share

Hello there. For color recongnition I ended up writing this function

  function Update () {
 
     if (webcamTexture.isPlaying) {
 
 //Calculate grid cells
         var Xplus : int = 600;
         var Yplus :int = 118;
 
 for (var gr = 0; gr<30; gr++){
         
             gridCubes[gr] = pixCheck(Xplus+calibGrid,720-Yplus-calibGrid,calibGrid,calibGrid); 
             if (Xplus<1100)
             Xplus+=110;    
             else {
             Xplus=600;
             Yplus+=110;
             
             }   
 
 //Check for cubes on grid    
             for (var fos = 0; fos<30; fos++) {
         
             if (gridCubes[fos].r<0.2 && gridCubes[fos].g<0.2 && gridCubes[fos].b<0.2) 
                 {bCubeCount++;gridCubez[fos]=true;}
             if (gridCubes[fos].r>0.4 && gridCubes[fos].g>0.6 && gridCubes[fos].b>0.6) 
                 {wCubeCount++;gridCubez[fos]=true;}
 
  

  newCustomPiece[cubeCount] = Instantiate(createe,Vector3(putin.position.x,putin.position.y+posY[iz]
                         GameObject.Find("Arbol").GetComponent(Comp).newCustomPiece = newCustomPiece[0];
             
     
     }
 
 //Return color from specified space
     function pixCheck(x:int, y:int, bwidth:int, bheight:int) : Color {
     data = webcamTexture.GetPixels(x,y,bwidth,bheight);
     
     var r : float = 0; var g : float = 0 ; var b : float = 0; var divider = data.length;
     
     for( var i=0; i<data.length; i++) {
     r += data[i].r;
     g += data[i].g;
     b += data[i].b;
     }
     
     var newColor = new Color(r/divider,g/divider,b/divider,1);
     
     return newColor;


You basically send the coords of the area of pixels you want to average and it will create arrays of r,g,b that will be averaged and returned as a single color. It checks all at once with one call, so it's much more faster.

Edit: For a project I was working on, I was recognizing wooden black & white cubes on a grid surface. I averaged the specified spaces to check for black or whites and it then created a 3D piece made of cubes for the game based on the cubes placed on the grid. I simplified the code a bit, it was too long.

avatar image Albazcythe · Jul 11, 2013 at 01:47 PM 0
Share

My bad. The code was a bit long so I didn't add it. I shortened it and edited it a little bit. Hope it's useful.

avatar image shacharoz · Jul 14, 2013 at 07:57 AM 0
Share

here's what i finally used, after i updated Chrizonic's code. thanks man

private Color GetAverageColor(List data) { float r = 0; float g = 0; float b = 0; int divider = data.Count;

     for (int i = 0; i < divider; i++)
     {
         r += data[i].r;
         g += data[i].g;
         b += data[i].b;
     }

     return new Color(r / divider, g / divider, b / divider, 1);
 }
avatar image

Answer by shacharoz · Jul 14, 2013 at 08:17 AM

here's what i finally used, after i updated Chrizonic's code. thanks man

private Color GetAverageColor(List data) { float r = 0; float g = 0; float b = 0; int divider = data.Count;

 for (int i = 0; i < divider; i++)
 {
     r += data[i].r;
     g += data[i].g;
     b += data[i].b;
 }

 return new Color(r / divider, g / divider, b / divider, 1);

}

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Getting pixel color on ray hit 2 Answers

Changing two different objects renderer colour 1 Answer

Using GetPixel to instantiate blocks of different pixel colors 1 Answer

how can i change the color of a pixel in my sprite 0 Answers

Change sprites colours individualy ( flashing like mario with the star ) 4 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