Page MenuHomec4science

Canny.pde
No OneTemporary

File Metadata

Created
Fri, Jul 12, 04:24

Canny.pde

import gab.opencv.*;
import processing.video.*;
static enum kernelType {
scale, blur, gaussian
};
class Canny extends PApplet {
static final int imageWidth=800;
static final int imageHeight=600;
OpenCV opencv;
TwoDThreeD twoD3D;
PImage img0;
static final int minHThreshold = 100, maxHThreshold =140;
static final int minSThreshold = 80, maxSThreshold = 255;
static final int minBThreshold = 10, maxBThreshold = 170;
Capture cam;
Canny() {
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}
//If you're using gstreamer0.1 (Ubuntu 16.04 and earlier),
//select your predefined resolution from the list:
//cam = new Capture(this, cameras[21]);
//If you're using gstreamer1.0 (Ubuntu 16.10 and later),
//select your resolution manually instead:
cam = new Capture(this, 640, 480, cameras[0]);
println(cam==null);
cam.start();
}
opencv = new OpenCV(this, 100, 100);
twoD3D=new TwoDThreeD(imageWidth, imageHeight, 0);
}
PVector update() {
if (cam.available() == true) {
cam.read();
}
img0 = cam.get();
int I = 100;
img0 = thresholdHSB(img0, minHThreshold, maxHThreshold, minSThreshold, maxSThreshold, minBThreshold, maxBThreshold );
img0 = new BlobDetection().findConnectedComponents(img0, true);
img0 = convolute(img0, kernelType.gaussian);
img0 = scharr(img0);
img0 = threshold(img0, I, false);
ArrayList<PVector> lines=hough(img0, 4);
List<PVector> corners=new QuadGraph().findBestQuad(lines, imageWidth, imageHeight, imageWidth*imageHeight, 0, false);
for (PVector c : corners) {
c.set(c.x, c.y, 1);
}
PVector rotation=twoD3D.get3DRotations(corners);
rotation.set((rotation.x)/PI*180, (rotation.y)/PI*180, (rotation.z)/PI*180);
return rotation;
}
//if inverted = false take values for 0 to threshol
PImage threshold(PImage img, int threshold, boolean inverted) {
PImage result = createImage(img.width, img.height, RGB);
for (int i = 0; i < img.width * img.height; i++) {
result.pixels[i]=brightness(img.pixels[i])>threshold^inverted?color(255):color(0);
}
return result;
}
PImage thresholdHSB(PImage img, int minH, int maxH, int minS, int maxS, int minB, int maxB) {
PImage result = createImage(img.width, img.height, RGB);
for (int i = 0; i < img.width * img.height; i++) {
if (hue(img.pixels[i])<minH||maxH<hue(img.pixels[i])||
saturation(img.pixels[i])<minS||maxS<saturation(img.pixels[i])||
brightness(img.pixels[i])<minB||maxB<brightness(img.pixels[i])) {
result.pixels[i]=0;
} else {
result.pixels[i]=Integer.MAX_VALUE;
}
}
return result;
}
boolean imagesEqual(PImage img1, PImage img2) {
if (img1.width != img2.width || img1.height != img2.height)
return false;
for (int i = 0; i < img1.width*img1.height; i++)
//assuming that all the three channels have the same value
if (red(img1.pixels[i]) != red(img2.pixels[i])) {
//if((i%img1.width)!=0 ||(i%img1.width)!=img1.width-1 ||(i/img1.width)!=0 ||(i/img1.width)!=img1.width-1 )
//println(i%img1.width+" "+i/img1.width + (red(img1.pixels[i]) - red(img2.pixels[i])) );
return false;
}
return true;
}
PImage scharr(PImage img) {
float[][] vKernel = {
{ 3, 0, -3 },
{ 10, 0, -10 },
{ 3, 0, -3 } };
float[][] hKernel = {
{ 3, 10, 3 },
{ 0, 0, 0 },
{ -3, -10, -3 } };
PImage result = createImage(img.width, img.height, ALPHA);
// clear the image
for (int i = 0; i < img.width * img.height; i++) {
result.pixels[i] = color(0);
}
float max=0;
float[] buffer = new float[img.width * img.height];
// *************************************
// Implement here the double convolution
int N = 3; //kernel size
for (int x = 1; x<img.width-1; ++x) {
for (int y = 1; y<img.height-1; y++) {
float sum_h = 0, sum_v = 0;
for (int i = 0; i< N; ++i) {
for (int j = 0; j<N; ++j) {
float brightness = brightness(img.pixels[(x-N/2+i)+ img.width *(y-N/2+j)]);
sum_h += hKernel[i][j] * brightness;
sum_v += vKernel[i][j] * brightness;
}
}
float value = sqrt(pow(sum_h, 2) + pow(sum_v, 2));
max = (value > max )? value : max;
buffer[x+y*img.width] = value;
}
}
// *************************************
for (int y = 1; y < img.height - 1; y++) { // Skip top and bottom edges
for (int x = 1; x < img.width - 1; x++) { // Skip left and right
int val=(int) ((buffer[y * img.width + x] / max)*255);
result.pixels[y * img.width + x]=color(val);
}
}
return result;
}
PImage convolute(PImage img, kernelType kt) {
int N = 3; //kernel size
float[][] kernel;
float normFactor ;
if (kt == kernelType.scale ) {
float[][] kernel1 = {{ 0, 0, 0 },
{ 0, 2, 0 },
{ 0, 0, 0 }};
kernel = kernel1;
normFactor = 1f;
} else if (kt == kernelType.blur) {
float[][] kernel2 = {{ 0, 1, 0 },
{ 1, 0, 1 },
{ 0, 1, 0 }};
kernel = kernel2;
normFactor = 1f;
} else {
float[][] gaussianKernel =
{{ 9, 12, 9 },
{ 12, 15, 12 },
{ 9, 12, 9 }};
kernel = gaussianKernel;
normFactor = 0f;
for (int i = 0; i< N; ++i) {
for (int j = 0; j<N; ++j) {
normFactor += kernel[i][j];
}
}
}
// create a greyscale image (type: ALPHA) for output
PImage result = createImage(img.width, img.height, ALPHA);
//
// for each (x,y) pixel in the image:
for (int x = 1; x< img.width-1; x++) {
for (int y = 1; y<img.height-1; y++) {
int sum = 0;
for (int i = 0; i< N; ++i) {
for (int j = 0; j<N; ++j) {
sum += kernel[i][j] * brightness(img.pixels[(x-N/2+i)+ img.width *(y-N/2+j)]);
}
}
result.pixels[x+y*img.width] = color((int)(sum / normFactor));
}
}
return result;
}
}

Event Timeline