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 /
This question was closed Dec 16, 2017 at 04:41 PM by MohsenneChaverdie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MohsenneChaverdie · Dec 16, 2017 at 02:54 PM · getpixels

Primary Colors in an image

I am trying to find colors in an image with frequency of more than one time to create a very simple color palette from an input image. To do so, I went this far ...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 
 public class ColorAnalyzer : MonoBehaviour
 {
     public Texture2D RawImage;
 
     public Color Color1;
     public Color Color2;
     public Color Color3;
     public Color Color4;
     public Color Color5;
 
     void Start()
     {
         // getting all the pixels with the mipmap level of 5
         Color[] arrayOfColors = RawImage.GetPixels(5);
 
         // identifying unique colors
         Color[] distinctColors = arrayOfColors.Distinct().ToArray();
         // subtracting unique colors from the main array
         Color[] colorsWithFrequencyOfMoreThanOne = arrayOfColors.Except(distinctColors).ToArray();
 
         Debug.Log("ArrayColors lengh: " + arrayOfColors.Length); // console says 512
         Debug.Log("DistinctColors lengh: " + distinctColors.Length); // console says 496
         Debug.Log("colorsWithFrequencyOfMoreThanOne lengh: " + colorsWithFrequencyOfMoreThanOne.Length); // console says 0
 
     }
 }

Does anybody know why my "Except" function does not work and says "null" instead of "16"?

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

  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Dec 16, 2017 at 04:23 PM

Uhm you have a logic flaw here. distinctColors contains ALL colors you have in your image. However it only contains distinct colors. So duplicates are only counted once.


"Except" returns a new collection based on the incoming color array that only contains elements which do not exist inside the provided array. Since your exclusion array contains all colors the resulting collection will of course be empty. What makes you think that Except only removes each color "once".


A very simple example:

 int[] numbers = new int[]{1,2,5,3,1,5,1,1};
 int[] distinctNumbers = new int[]{1,2,3,5};
 int[] result = numbers.Except(distinctNumbers); // --> { } empty array

Except grabs all elements from the source collection which are not part of the exclusion list. However as you can see the 0th element "1" is part of the exclusion list. However the 4th, 6th and 7th element is also a "1" and therefore not included in the result.


You can't simply subtract one array from another. Arrays are not sets. Also if you want to remove the element "5" one time, do you remove the first 5 (element #2) or do you remove the second (element #5)?


You may want to do something like what is explained in this SO answer

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

Follow this Question

Answers Answers and Comments

72 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

Related Questions

Baking decals into a texture 3 Answers

Hardcrash when using GetPixels 1 Answer

Using GetPixels to break down an image, and only getting the last part. 1 Answer

Problem getting the right result when combining two textures 0 Answers

Changing color of Texture2d with transparency 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