• 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 ThaBukkow · Sep 17, 2014 at 04:47 PM · cutout

Problem with 2D Feathering algorithm

Hello, i need some help with a feathering algorithm that i have written.

I am working om some greenscreen / chromakey software. The part of the code that does the chromakeying works fine. If a pixel is within a specific color range its alpha is set to zero. The image below shows a test image. (The blue is the background of the camera)

alt text

To make the edges of the cutout a little smoother i want to use feathering. What the feathering algorithm should do is similar to photoshops "inner glow" but then with the alpha of the pixels. this is to make the edges of the cutout smoother.

This is the algorithm I use. It first finds all the pixels that are at the edge of the cutout, then the alpha value of the pixels surrounding it are adjusted like a gradient alpha fade.

     private IEnumerator Feather(Texture2D _image, int _blur)
     {
         //declarations
         int x, y, localX, localY, dX, dY, index;
         float alpha;
         int width = _image.width;
         int height = _image.height;
 
         //used to loop through and determine where the edge pixels of the cutoff is
         Color[] originalPixels = _image.GetPixels();
 
         //used to store the output image;
         Color[] outputPixels = _image.GetPixels();
 
         for (y = 0; y < height; y++)
         {
             for (x = 0; x < width; x++)
             {
                 index = (y * width) + x;
 
                 //check if this pixel is full, and if there is a neighbour empty
                 //if so, the pixel is at the edge of the cutout
                 bool thisFull = (originalPixels[index].a != 0);
                 bool anotherOneEmpty = 
                 (
                     ((x < width-1)     && (originalPixels[index + 1].a == 0)) ||
                     ((x > 0)         && (originalPixels[index - 1].a == 0)) ||
                     ((y > 0)         && (originalPixels[index - width].a == 0)) ||
                     ((y < height-1) && (originalPixels[index + width].a == 0))
                 );
 
                 index = -1;
 
                 if (thisFull && anotherOneEmpty)
                 {
                     //visit all pixels in a square around the edge pixel
                     //the size of this square is determined by the blur radius
                     for (localY = (y - _blur); localY <= (y + _blur); localY++)
                     {
                         for (localX = (x - _blur); localX <= (x + _blur); localX++)
                         {
                             //check if within bounds
                             index = (localY * width) + localX;
                             if (index >= 0 && index < outputPixels.Length)
                             {
                                 //calculate alpha by distance to the edge node
                                 //close is complete cutoff, far away is less cutoff
                                 dX = localX - x;
                                 dY = localY - y;
                                 alpha = Mathf.Sqrt(dX * dX + dY * dY) / (float)_blur;

                                 //set the new alpha if it is lower then the alpha already set
                                 outputPixels[index].a = (float)Mathf.Min(outputPixels[index].a, alpha);
                             }
                         }
                     }
                 }
             }
 
             //slow down for debug purposesf
             //yield return new WaitForEndOfFrame();
         }
 
         //final texture output
         Texture2D output = new Texture2D(_image.width, _image.height, TextureFormat.ARGB32, false);
         output.SetPixels(outputPixels);
         output.Apply();
 
         finalImage = output;
 
         yield return null;
     }

And the resulting image, when using a 20 pixel blur (a big value to best demonstrate the problem)

alt text

At first glance the algorithm seems to work, but the result is not quite right. The part where the smooth cutoff should overlap shows a clear division. (Best visible by the holes in the bottom right corner). Can anyone find my mistake and explain why the smooth cutoff does not overlap properly?

1.jpg (450.3 kB)
2.jpg (412.7 kB)
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 tanoshimi · Sep 17, 2014 at 06:40 PM 1
Share

This doesn't seem like a Unity question to me. For help on generic graphics algorithms, you might get a better response from a site like StackExchange.

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

which shader to use for dualsided alpha cutout with light cookie enabled? 1 Answer

Wierd Material Behaviour 0 Answers

Screen.safeArea- How to make it work for Unity as Android library? 1 Answer

Fill of each letter of the word, like progress bar. 1 Answer

Standard cutout shader. Pixely outline. 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