Created
June 4, 2011 14:58
-
-
Save frederickk/1007955 to your computer and use it in GitHub Desktop.
rudimentary edge detection class for processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* FEdgeDetect.pde | |
* processing.org | |
* | |
* Ken Frederick | |
* ken.frederick@gmx.de | |
* | |
* http://cargocollective.com/kenfrederick/ | |
* http://kenfrederick.blogspot.com/ | |
* | |
* edge detection class with some inspired from | |
* http://processing.org/learning/topics/edgedetection.html | |
* | |
*/ | |
//----------------------------------------------------------------------------- | |
//libraries | |
//----------------------------------------------------------------------------- | |
import processing.video.*; | |
import frederickk.tools.*; | |
public class FEdgeDetect { | |
//----------------------------------------------------------------------------- | |
//properties | |
//----------------------------------------------------------------------------- | |
private PImage img; | |
private PImage edgeImg; | |
private float thresh; | |
private float density; | |
private float[][] kernel = { { -1, -1, -1 }, | |
{ -1, 1, -1 }, | |
{ -1, -1, -1 } }; | |
public ArrayList<PVector> edgePVector = new ArrayList<PVector>(); | |
public ArrayList<Float> edgeFloat = new ArrayList<Float>(); | |
public boolean bProcessEdge = false; | |
public boolean bProcessImage = false; | |
public boolean bProcessMovie = false; | |
//----------------------------------------------------------------------------- | |
//constructor | |
//----------------------------------------------------------------------------- | |
public FEdgeDetect() { | |
} | |
public FEdgeDetect(PImage _img) { | |
setImage(_img); | |
} | |
//----------------------------------------------------------------------------- | |
//methods | |
//----------------------------------------------------------------------------- | |
public void process(float _thresh) { | |
if(_thresh != thresh || bProcessImage || bProcessMovie) { | |
thresh = _thresh; | |
bProcessImage = false; | |
kernel[1][1] = thresh; | |
for (int y=1; y<img.height-1; y++) { | |
for (int x=1; x<img.width-1; x++) { | |
float sum = 0; // Kernel sum for this pixel | |
for (int ky = -1; ky <= 1; ky++) { | |
for (int kx = -1; kx <= 1; kx++) { | |
int pos = (y + ky)*img.width + (x + kx); | |
//float val = red(img.pixels[pos]); | |
int val = (img.pixels[pos] >> 16) & 0xFF; | |
sum += kernel[ky+1][kx+1] * val; | |
} | |
} | |
edgeImg.pixels[y*img.width + x] = color(sum); | |
} | |
} | |
edgeImg.updatePixels(); | |
} //end check | |
} | |
public void process(PImage img, float thresh, boolean _process) { | |
setImage(img); | |
bProcessMovie = _process; | |
process(thresh); | |
} | |
public void process(Movie m, float thresh, boolean _process) { | |
setImage(m); | |
bProcessMovie = _process; | |
process(thresh); | |
} | |
public void process(Capture c, float thresh, boolean _process) { | |
setImage(c); | |
bProcessMovie = _process; | |
process(thresh); | |
} | |
public void processEdge(int _density, float b_low, float b_high) { | |
if(_density != density || bProcessEdge || bProcessMovie) { | |
density = _density; | |
bProcessEdge = false; | |
edgePVector.clear(); | |
edgeFloat.clear(); | |
for(int x=1; x<edgeImg.width-1; x+=density) { | |
for(int y=1; y<edgeImg.height-1; y+=density) { | |
float b = brightness( edgeImg.get(x,y) ); | |
if( b > b_low && b < b_high ) { | |
float z = norm(b, 0,255); | |
edgePVector.add( new PVector(x,y,z) ); | |
edgeFloat.add( new Float(x) ); | |
edgeFloat.add( new Float(y) ); | |
edgeFloat.add( new Float(z) ); | |
} | |
} | |
} | |
} //end check | |
if(edgePVector.size() == 0) { | |
edgePVector.add( new PVector(0,0,0) ); | |
} | |
if(edgeFloat.size() == 0) { | |
edgeFloat.add( 0.0f ); | |
edgeFloat.add( 0.0f ); | |
edgeFloat.add( 0.0f ); | |
} | |
} | |
//----------------------------------------------------------------------------- | |
//sets | |
//----------------------------------------------------------------------------- | |
protected void setImage(PImage _img) { | |
img = _img; | |
img.loadPixels(); | |
edgeImg = createImage(img.width, img.height, RGB); | |
//edgeImg.filter(THRESHOLD, .4); | |
} | |
//----------------------------------------------------------------------------- | |
//gets | |
//----------------------------------------------------------------------------- | |
public PImage getSource() { | |
return img; | |
} | |
public PImage getEdge() { | |
return edgeImg; | |
} | |
public ArrayList getEdgePVector() { | |
return edgePVector; | |
} | |
/** | |
* this is a complete mess | |
* need to figure out a better solution for | |
* smoother integration with OpenGL VBOs | |
* | |
public float[] getEdgeFloat() { | |
float[] ef = new float[edgeFloat.size()]; | |
for(int i=0; i<ef.length; i++) { | |
ef[i] = (float) edgeFloat.get(i); | |
} | |
//int[] pointsFloat = FConvert.toPrimitive(edgeFloat); | |
//float[] ef = FConvert.toPrimitive(pointsFloat); | |
return ef; | |
} | |
*/ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment