Page MenuHomec4science

Canny.pde
No OneTemporary

File Metadata

Created
Sun, Sep 1, 09:24

Canny.pde

import processing.video.*;
PImage board1Thresholded, board1Blurred, board1Scharr;
int minHThreshold = 100, maxHThreshold =140;
int minSThreshold = 100, maxSThreshold = 255;
int minBThreshold = 45, maxBThreshold = 170;
//HScrollbar thresholdBar0;
//HScrollbar thresholdBar1;
Capture cam;
PImage img;
enum kernelType {
scale, blur, gaussian
};
int variable=0;
void settings() {
//size(1600, 600);
size(640, 480);
}
void setup() {
colorMode(HSB);
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]);
cam.start();
}
}
void draw() {
/*if (cam.available() == true) {
cam.read();
}
img = cam.get();
int I = 100;
img = thresholdHSB(img, minHThreshold, maxHThreshold, minSThreshold, maxSThreshold, minBThreshold, maxBThreshold );
img = convolute(img, kernelType.gaussian);
img = scharr(img);
img = threshold(img, I, false);
img = new BlobDetection().findConnectedComponents(img, true);
image(img, 0, 0);
drawLines(hough(img), img);*/
variable=(int)mouseX*255/width;
image(img, 0, 0);//show image
int I = 100;
PImage img1=img.copy();
img1 = thresholdHSB(img1, minHThreshold, maxHThreshold, minSThreshold, maxSThreshold, minBThreshold, maxBThreshold );
img1 = convolute(img1, kernelType.gaussian);
img1 = scharr(img1);
img1 = threshold(img1, I , false);
img1 = new BlobDetection().findConnectedComponents(img1,true);
drawLines(hough(img1),img1);
image(img1, img.width, 0);//show processed image
}
//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;
}
void mousePressed() {
println(variable);
}

Event Timeline