Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Created January 22, 2015 21:09
Show Gist options
  • Save atduskgreg/29b94516da410643ca45 to your computer and use it in GitHub Desktop.
Save atduskgreg/29b94516da410643ca45 to your computer and use it in GitHub Desktop.
Example of using a convolution kernel to implement a Laplacian filter in OpenCV for Processing. Example output: https://www.flickr.com/photos/unavoidablegrain/16342382412/
import gab.opencv.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
OpenCV opencv;
PImage img;
void setup() {
img = loadImage("arch_sunset.jpg");
size(img.width*2, img.height);
opencv = new OpenCV(this, img);
int kernelSize = 9;
Mat kernel = new Mat(kernelSize, kernelSize, CvType.CV_32F);
kernel.put(0, 0, 0);
kernel.put(0, 1, -1);
kernel.put(0, 2, 0);
kernel.put(1, 0, -1);
kernel.put(1, 1, 4);
kernel.put(1, 2, -1);
kernel.put(2, 0, 0);
kernel.put(2, 1, -1);
kernel.put(2, 2, 0);
Imgproc.filter2D(opencv.getGray(), opencv.getGray(), -1, kernel);
noLoop();
}
void draw() {
image(img, 0, 0);
image(opencv.getOutput(), img.width, 0);
}
@varhub
Copy link

varhub commented Apr 28, 2015

I hit this accidentally, by the way...

there is an error at new Mat, because you are using a 3x3 laplacian, but creating 9x9.

kernelSize is in rows x cols, before multiply ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment