latrokles (owner)

Revisions

gist: 218956 Download_button fork
public
Public Clone URL: git://gist.github.com/218956.git
Embed All Files: show embed
edge_detection_processing.pde #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* edgeDetection
* Author: Latrokles - latrokles_at_gmail_dot_com
* DISCLAIMER: The Edge Detection algorithm used in this sketch comes from "An
* Introduction to Image Processing" by Frederic Patin, which is available at:
* http://www.gamedev.net/reference/articles/article2007.asp
*
* This sketch demonstrates a very simple algorithm for edge detection.
* The algorithm iterates through the each pixel in the frame, compares it with
* it's right and bottom neighbor, if the distance between the colors is greater
* than a given threshold, you've found an edge.
*
* For some interactivity:
* 1. pressing '+' will increase the threshold value
* 2. pressing '-' will decrease the threshold value
* 3. pressing ' ' (the space bar) will invert the threshold image.
*
*/
import processing.video.*;
 
Capture vidCam;
PImage modFrame;
int threshold; //Threshold to determine edge
color edgeColor = color(255, 255, 255);
color fillColor = color(0, 0, 0);
 
void setup()
{
  size(600, 400);
  background(100);
  vidCam = new Capture(this, width, height, 25);
  threshold = 20;
}
 
void captureEvent(Capture vidCam)
{
  vidCam.read();
}
 
void draw()
{
    loadPixels(); //Loads built-in pixel array(pixels), makes it accessible
    edgeDetect(); //Edge detection routine, it accesses and modifies "pixels"
    updatePixels(); //Updates the the display by loading the pixel array into the display buffer ...
                     //at least that is my understanding of how it works.
    image(vidCam, 0, height - 120, 160, 120);
}
 
void edgeDetect()
{
  int px, rpx, bpx; //pixel, right pixel, bottom pixel
  int r, g, b; //rgb values for pixel
  int r1, g1, b1; //same for pixel to the right
  int r2, g2, b2; //same for pixel on the bottom
  for(int x = 0; x < width - 1; x++)
  {
    for(int y = 0; y < height - 1; y++)
    {
      //Processing represents bitmaps as one dimensional arrays
      //so we need to convert the pixels x, y coordinates (the way we would see
      //it on screen) to a one dimensional index.
      //We calculate this the following way: index = ypos * width of bitmap + xpos
      px = (y * width) + x;
      rpx = (y * width) + x + 1;
      bpx = ((y+1) * width) + x;
      
      //Get the red, green, and blue values for the pixels being compared
      r = (int)red(vidCam.pixels[px]);
      g = (int)green(vidCam.pixels[px]);
      b = (int)blue(vidCam.pixels[px]);
      
      r1 = (int)red(vidCam.pixels[rpx]);
      g1 = (int)green(vidCam.pixels[rpx]);
      b1 = (int)blue(vidCam.pixels[rpx]);
      
      r2 = (int)red(vidCam.pixels[bpx]);
      g2 = (int)green(vidCam.pixels[bpx]);
      b2 = (int)blue(vidCam.pixels[bpx]);
      
      //Determine whether distance between colors greater than threshold
      if( ((int)sqrt( (r-r1)*(r-r1) + (g-g1)*(g-g1) + (b-b1)*(b-b1)) > threshold) || ((int)sqrt( (r-r2)*(r-r2) + (g-g2)*(g-g2) + (b-b2)*(b-b2) ) > threshold) )
      {
        pixels[px] = edgeColor;
      }
      else
      {
        pixels[px] = fillColor;
      }
    }
  }
}
 
/* Simple interactive behavior */
void keyPressed()
{
  if(key == '+')
  {
    threshold++;
  }
  
  if(key == '-')
  {
    threshold--;
  }
  
  if(key == ' ')
  {
    color tmp = edgeColor;
    edgeColor = fillColor;
    fillColor = tmp;
  }
}