Search Unity

Retrieve pixel coordinates

Discussion in 'Scripting' started by cedart, Jul 31, 2014.

  1. cedart

    cedart

    Joined:
    Jul 10, 2014
    Posts:
    17
    Hello,

    I want to "analyse" an image with black points, and retrieve the coordinate of black points in a table, for use later.

    It's possible ?


    An image like this :




    Finally i want to increment an object on all points.

    Thanks in advance ;)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. cedart

    cedart

    Joined:
    Jul 10, 2014
    Posts:
    17
    Thanks i'll try to understand now :)

    I misspoke : I want to duplicate an object on all of this points.
     
  4. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    You can use what LeftyRighty recommended to identify all the spots. Iterate over the whole image, for example using nested for-loops (over width and over height), and create a list of the dots you found.

    Then it's a matter for translating those pixel coordinates from your bitmap to your specific world-coordinates.
     
  5. cedart

    cedart

    Joined:
    Jul 10, 2014
    Posts:
    17
    Thanks, i'll try this ;)
     
  6. cedart

    cedart

    Joined:
    Jul 10, 2014
    Posts:
    17
    Ok... I'm a noob in scripting ^^

    I don't understand how to use all of this...


    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Start () {
    4.     var texture = Resources.Load("Materials/pixelsNoirs", Texture2D);
    5.     var color = texture.GetPixel(3,5);
    6.    
    7.     Debug.Log(color);
    8. }
    9.  
    I juste try to display the color of a pixel, but I have an error :

     
  7. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Try adding Debug.Log(texture) directly after the Load command. I bet it didn't load properly.

    Is "pixelsNoirs" in your Ressources folder? It's actually easier to define a public variable of type Texture2D and then drag'n'drop the relevant texture into the field via the Inspector.
     
  8. JohnRossitter

    JohnRossitter

    Joined:
    Dec 18, 2013
    Posts:
    1,027
    Color ColorImLookingFor = new Color(0.3f,0.2555f,0.045f,1.0f);
    for(int i =0; i< myTexture.width; i++)
    {
    for(int j =0; j < myTexture.height; j++)
    {
    Color thisColor = myTexture.GetPixel(i,j);
    if(thisColor == Color.black)
    {
    //create instance here
    }

    //or

    if(thisColor.r == ColorImLookingFor)
    {
    //create instance here
    }

    //or

    if(thisColor.r == 0.0f && thisColor.g == 0.0f && thisColor.b == 0.0f && this.Color.a == 1.0f)
    {
    //create instance here
    }
    }
    }
     
  9. cedart

    cedart

    Joined:
    Jul 10, 2014
    Posts:
    17
    Thank you, it's very cool ;)
     
  10. JohnRossitter

    JohnRossitter

    Joined:
    Dec 18, 2013
    Posts:
    1,027
    Amazingly I typed that on my phone :)
     
    der_r likes this.