Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 kyozdemirr · Aug 26, 2020 at 09:11 AM · colortexture2dpixelcompare

Can't compare 2 Texture2D by pixel's color

I need to calculate the differences of 2 screenshots as Texture2D. I have another camera on the scene to take screenshots. First screenshot is: link text And the second screenshot is:link text As you can see these are different but my method always returns 0 like there's no difference.

 private int CompareTextures(Texture2D texture1, Texture2D texture2)
     {
         int correctPixels = 0;
         var tex1 = texture1.GetPixels();
         var tex2 = texture2.GetPixels();
 
         if (tex1.Length != tex2.Length)
         {
             return 0;
         }
         else
         {
             for (int i = 0; i < tex1.Length; i++)
             {
                 if (tex1[i] != tex2[i])
                 {
                     correctPixels++;
                 }
             }
             
             int percentage = 100 * (correctPixels / tex1.Length);
             return percentage;
         }
         
     }

I don't understand why is that. Can you guys help me?

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
2
Best Answer

Answer by revolute · Aug 26, 2020 at 09:36 AM

int division does not work in magic. If you divide two integers you get another integer.

  int percentage = (100 *correctPixels) / tex1.Length;

should give a number other than 0. The main problem was that correctPixels/tex1.Length was calculated first, since tex1.Length is always larger than or same to correctPixels , result would be 0 or at best 1.

Comment
Add comment · Show 3 · 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 kyozdemirr · Aug 26, 2020 at 10:45 AM 0
Share

You're right. The length of array is larger larger than correctPixels. I have to cast them float. Thank you!

avatar image Bunny83 kyozdemirr · Aug 26, 2020 at 10:49 AM 0
Share

You also should refactor your code since your correctPixels variable does not count correct pixels but those which are different. So name it changedPixels. Though If you wanted to actually calculate the percentage how much the two images are similar, you should replace your != with ==

avatar image kyozdemirr Bunny83 · Aug 26, 2020 at 01:34 PM 0
Share

I know. I did it on purpose. Thank you by the way.

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

140 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 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

Change Color Based On Pixel X Coordinate in Shader 1 Answer

Unity change my color variation?? 0 Answers

How to fill texture that would be avaliable to ither shaders? 1 Answer

Getting color from RayCastHit: Able to retrieve texture coordinates but Texture2D still shows as "degenerate" (0x0) 0 Answers

How to get the pixel colors of a sprite when there is a color added? 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