Search Unity

Webcam Edge Detection C# Script Needs Converting

Discussion in 'Scripting' started by KnightRiderGuy, Mar 1, 2015.

  1. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Hi I am wondering if someone with a far better understanding of code than I would mind converting a very cool script I found that does edge detection from a webcam live feed. Basically all I need is all of the old OnGUI stuff pulled out and a single Webcam view screen on a UI Raw image that I can assign a simple UI system button to apply each of the edge detection effects that are available in this script, one button to toggle on each effect. here is the very cool script I found from a video demo I watched on YouTube


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Threading;
    5.  
    6. public class WebcamEdgeDetection : MonoBehaviour {
    7.     public static WebCamTexture camTex;
    8.     public float th = 0.09f;
    9.     System.Diagnostics.Stopwatch s;
    10.     static Texture2D img,rImg,gImg,bImg,kImg,roImg,goImg,boImg,koImg,ORimg,ANDimg,SUMimg,AVGimg;
    11.     static float[,] rL,gL,bL,kL,ORL,ANDL,SUML,AVGL;
    12.     static Color temp;
    13.     static float t;
    14.    
    15.     void Start () {
    16.         WebCamDevice[] devices = WebCamTexture.devices;
    17.         WebCamDevice cam = devices[0];
    18.         camTex = new WebCamTexture(cam.name,200,200);
    19.         camTex.Play();
    20.         //GetWebCamImage();
    21.         img = new Texture2D(camTex.width,camTex.height);
    22.         //initialize textures
    23.         rImg = new Texture2D(img.width,img.height);
    24.         bImg = new Texture2D(img.width,img.height);
    25.         gImg = new Texture2D(img.width,img.height);
    26.         kImg = new Texture2D(img.width,img.height);
    27.         //initialize gradient arrays
    28.         rL = new float[img.width,img.height];
    29.         gL = new float[img.width,img.height];
    30.         bL = new float[img.width,img.height];
    31.         kL = new float[img.width,img.height];
    32.         ORL = new float[img.width,img.height];
    33.         ANDL = new float[img.width,img.height];
    34.         SUML = new float[img.width,img.height];
    35.         AVGL = new float[img.width,img.height];
    36.         //initialize final textures
    37.         roImg = new Texture2D(img.width,img.height);
    38.         goImg = new Texture2D(img.width,img.height);
    39.         boImg = new Texture2D(img.width,img.height);
    40.         koImg = new Texture2D(img.width,img.height);
    41.         ORimg = new Texture2D(img.width,img.height);
    42.         ANDimg = new Texture2D(img.width,img.height);
    43.         SUMimg = new Texture2D(img.width,img.height);
    44.         AVGimg = new Texture2D(img.width,img.height);
    45.     }
    46.    
    47.     void Update(){
    48.         GetWebCamImage();
    49.     }
    50.    
    51.     void GetWebCamImage(){
    52.         img.SetPixels(camTex.GetPixels());
    53.         CalculateEdges();
    54.     }
    55.    
    56.     void CalculateEdges(){
    57.        
    58.         //calculate new textures
    59.         for(int x = 0;x<img.width;x++){
    60.             for(int y = 0;y<img.height;y++){
    61.                 temp = img.GetPixel(x,y);
    62.                 rImg.SetPixel(x,y,new Color(temp.r,0,0));
    63.                 gImg.SetPixel(x,y,new Color(0,temp.g,0));
    64.                 bImg.SetPixel(x,y,new Color(0,0,temp.b));
    65.                 t = temp.r+temp.g+temp.b;
    66.                 t/=3f;
    67.                 kImg.SetPixel(x,y,new Color(t,t,t));
    68.             }
    69.         }
    70.         rImg.Apply();
    71.         gImg.Apply();
    72.         bImg.Apply();
    73.         kImg.Apply();
    74.        
    75.         //calculate gradient values
    76.         for(int x = 0;x<img.width;x++){
    77.             for(int y = 0;y<img.height;y++){
    78.                 rL[x,y] = gradientValue(x,y,0,rImg);
    79.                 gL[x,y] = gradientValue(x,y,1,gImg);
    80.                 bL[x,y] = gradientValue(x,y,2,bImg);
    81.                 kL[x,y] = gradientValue(x,y,2,kImg);
    82.                 ORL[x,y] = (rL[x,y]>=th || gL[x,y]>=th || bL[x,y]>=th) ? th : 0f;
    83.                 ANDL[x,y] = (rL[x,y]>=th && gL[x,y]>=th && bL[x,y]>=th) ? th : 0f;
    84.                 SUML[x,y] = rL[x,y] + gL[x,y] + bL[x,y];
    85.                 AVGL[x,y] = SUML[x,y]/3f;
    86.             }
    87.         }
    88.         //create texture from gradient values
    89.         TextureFromGradientRef(rL,th, ref roImg);
    90.         TextureFromGradientRef(gL,th,ref goImg);
    91.         TextureFromGradientRef(bL,th,ref boImg);
    92.         TextureFromGradientRef(kL,th,ref koImg);
    93.         TextureFromGradientRef(ORL,th,ref ORimg);
    94.         TextureFromGradientRef(ANDL,th,ref ANDimg);
    95.         TextureFromGradientRef(SUML,th,ref SUMimg);
    96.         TextureFromGradientRef(AVGL,th,ref AVGimg);
    97.     }
    98.    
    99.     // Update is called once per frame
    100.     void OnGUI () {
    101.         GUILayout.BeginHorizontal();
    102.        
    103.         GUILayout.BeginVertical();
    104.         GUILayout.Label("original");
    105.         GUILayout.Label(camTex);
    106.         GUILayout.Label("red");
    107.         GUILayout.Label(rImg);
    108.         GUILayout.Label("blue");
    109.         GUILayout.Label(bImg);
    110.         GUILayout.EndVertical();
    111.        
    112.         GUILayout.BeginVertical();
    113.         GUILayout.Label("green");
    114.         GUILayout.Label(gImg);
    115.         GUILayout.Label("grey");
    116.         GUILayout.Label(kImg);
    117.         GUILayout.Label("grey detection");
    118.         GUILayout.Label(koImg);
    119.         GUILayout.EndVertical();
    120.        
    121.         GUILayout.BeginVertical();
    122.         GUILayout.Label("red detection");
    123.         GUILayout.Label(roImg);
    124.         GUILayout.Label("green detection");
    125.         GUILayout.Label(goImg);
    126.         GUILayout.Label("blue detection");
    127.         GUILayout.Label(boImg);
    128.         GUILayout.EndVertical();
    129.        
    130.         GUILayout.BeginVertical();
    131.         GUILayout.Label("OR detection");
    132.         GUILayout.Label(ORimg);
    133.         GUILayout.Label("AND detection");
    134.         GUILayout.Label(ANDimg);
    135.         GUILayout.Label("SUM detection");
    136.         GUILayout.Label(SUMimg);
    137.         GUILayout.EndVertical();
    138.        
    139.         GUILayout.BeginVertical();
    140.         GUILayout.Label("Average detection");
    141.         GUILayout.Label(AVGimg);
    142.         GUILayout.EndVertical();
    143.        
    144.         GUILayout.EndHorizontal();
    145.     }
    146.    
    147.     float gradientValue(int ex,int why,int colorVal,Texture2D image){
    148.         float lx=0f;
    149.         float ly=0f;
    150.         if(ex>0 && ex<image.width)
    151.             lx = 0.5f*(image.GetPixel(ex+1,why)[colorVal]-image.GetPixel(ex-1,why)[colorVal]);
    152.         if(why>0 && why<image.height)  
    153.             ly = 0.5f*(image.GetPixel(ex,why+1)[colorVal]-image.GetPixel(ex,why-1)[colorVal]);
    154.         return Mathf.Sqrt(lx*lx+ly*ly);
    155.     }
    156.    
    157.     Texture2D TextureFromGradient(float[,] g,float thres){
    158.         Texture2D output = new Texture2D(g.GetLength(0),g.GetLength(1));
    159.         for(int x = 0;x<output.width;x++){
    160.             for(int y = 0;y<output.height;y++){
    161.                 if(g[x,y] >= thres)
    162.                     output.SetPixel(x,y,Color.black);
    163.                 else
    164.                     output.SetPixel(x,y,Color.white);
    165.             }
    166.         }
    167.         output.Apply();
    168.         return output;
    169.     }
    170.    
    171.     void TextureFromGradientRef(float[,] g,float thres,ref Texture2D output){
    172.         for(int x = 0;x<output.width;x++){
    173.             for(int y = 0;y<output.height;y++){
    174.                 if(g[x,y] >= thres)
    175.                     output.SetPixel(x,y,Color.black);
    176.                 else
    177.                     output.SetPixel(x,y,Color.white);
    178.             }
    179.         }
    180.         output.Apply();
    181.     }
    182. }
    183.