Created
February 23, 2013 17:09
-
-
Save atduskgreg/5020493 to your computer and use it in GitHub Desktop.
Render in a hatched shading style from a source image including canny edge filter using JavacvPro for lines
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
import monclubelec.javacvPro.*; | |
PImage img, original; | |
PGraphics hash; | |
PImage hashImg; | |
PImage[] hatchLevels; | |
int hatchSpacing = 5; | |
OpenCV opencv; | |
void setup() { | |
original = loadImage("hand_profile.jpg"); | |
size(original.width*2, original.height, P2D); | |
opencv = new OpenCV(this); | |
opencv.allocate(original.width, original.height); | |
opencv.copy(original); | |
// run a canny edge filter | |
opencv.canny(1000, 2000, 5); | |
// invert for black lines | |
opencv.invert(); | |
background(255); | |
img = createImage(original.width, original.height, RGB); | |
hash = createGraphics(original.width, original.height, P2D); | |
hashImg = createImage(original.width, original.height, ARGB); | |
hatchLevels = new PImage[5]; | |
for (int i = 0; i < hatchLevels.length; i++) { | |
println("processing level: " + i); | |
img.loadPixels(); | |
for (int p = 0; p < original.pixels.length; p ++) { | |
if (inRangeForLevel(original.pixels[p], i)) { | |
img.pixels[p] = color(255); | |
} | |
else { | |
img.pixels[p] = color(0); | |
} | |
} | |
img.updatePixels(); | |
println("done processing pixels, drawing"); | |
hash.beginDraw(); | |
hash.background(255,255); | |
hatchToDarkness(4-i, hatchSpacing); | |
hash.endDraw(); | |
hash.updatePixels(); | |
hashImg = hash.get(); | |
hashImg.mask(img); | |
println("displaying image"); | |
image(hashImg, 0,0); | |
} | |
println("here"); | |
image(original, img.width, 0); | |
// draw the edges on top | |
blendMode(DARKEST); | |
image(opencv.getBuffer(), 0, 0); | |
noLoop(); | |
} | |
boolean inRangeForLevel(int pixel, int i) { | |
if (i == 0 ) { | |
return(brightness(pixel) < 50); | |
} | |
else if (i == 1) { | |
return(brightness(pixel) >= 50 && brightness(pixel) < 100); | |
} | |
else if (i == 2) { | |
return(brightness(pixel) >= 100 && brightness(pixel) < 140); | |
} | |
else if (i == 3) { | |
return(brightness(pixel) >= 140 && brightness(pixel) < 190); | |
} | |
else { | |
return(brightness(pixel) >= 190 && brightness(pixel) < 255); | |
} | |
} | |
void draw() { | |
} | |
void hatchToDarkness(int darkness, int spacing) { | |
if (darkness == 0) { | |
// do nothing | |
} | |
if (darkness > 0) { | |
drawHatches(img.width, img.height, spacing); | |
} | |
if (darkness > 1) { | |
hash.pushMatrix(); | |
hash.translate(img.width, 0); | |
hash.rotate(radians(90)); | |
drawHatches(img.width, img.height, spacing); | |
hash.popMatrix(); | |
} | |
if (darkness > 2) { | |
for (int i = 0; i < img.width; i+=spacing) { | |
hash.line(i, 0, i, img.height); | |
} | |
} | |
if (darkness > 3) { | |
// fill in full black | |
for (int i = 0; i < img.height; i++) { | |
hash.line(0, i, img.width, i); | |
} | |
} | |
} | |
void drawHatches(int w, int h, int spacing) { | |
int num = int(float(w)/float(spacing)); | |
for (int i = 0; i < num; i++) { | |
hash.line(0, h-i*spacing, w-i*spacing, 0); | |
if (i > 0) { | |
hash.line(i*spacing, h, w, i*spacing); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment