diff --git a/8thWeek/Canny/Canny.pde b/8thWeek/Canny/Canny.pde index 3cecc24..8ad45e0 100644 --- a/8thWeek/Canny/Canny.pde +++ b/8thWeek/Canny/Canny.pde @@ -1,163 +1,183 @@ PImage img; PImage board1Thresholded, board1Blurred, board1Scharr; +float minHThreshold = 100, maxHThreshold =140; +float minBThreshold = 45, maxBThreshold = 170; +float minSThreshold = 100, maxSThreshold = 255; HScrollbar thresholdBar0; HScrollbar thresholdBar1; +enum kernelType { scale , blur , gaussian}; void settings() { size(1600, 600); } void setup() { thresholdBar0 = new HScrollbar(0, 560, 800, 20); thresholdBar1 = new HScrollbar(0, 580, 800, 20); img = loadImage("src/board1.jpg"); board1Thresholded = loadImage("src/board1Thresholded.bmp"); board1Blurred = loadImage("src/board1Blurred.bmp"); board1Scharr = loadImage("src/board1Scharr.bmp"); - noLoop(); // no interactive behaviour: draw() will be called only once. + //noLoop(); // no interactive behaviour: draw() will be called only once. } void draw() { - image(img, 0, 0);//show image PImage img1, img2,img3,img4; - int I = 100; //img2=threshold(img2, (int)map(thresholdBar.getPos(),0,1,0,255), false); //img2=HueMap(img2, thresholdBar0.getPos(), thresholdBar1.getPos()); - img1 = thresholdHSB(img, 100, 200, 100, 255, 45, 100 ); + //img2 = thresholdHSB(img, 100, 200, 100, 255, 45, 100 ); + //img3 = convolute(img, kernelType.gaussian); + //img4 = scharr(img); + image(img, 0, 0);//show image + int I = 100; + //minHThreshold = thresholdBar0.getPos()*255; + //maxHThreshold = thresholdBar1.getPos()*255; + //println(minHThreshold,maxHThreshold); + img1 = thresholdHSB(img, (int)minHThreshold,(int) maxHThreshold, + (int)minSThreshold,(int) maxSThreshold, (int)minBThreshold, (int)maxBThreshold ); + img1 = convolute(img1, kernelType.gaussian); img1 = scharr(img1); img1 = threshold(img1, I , false); - img2 = thresholdHSB(img, 100, 200, 100, 255, 45, 100 ); - img3 = convolute(img); - img4 = scharr(img); - image(img3, img.width, 0); - /*thresholdBar0.display(); + + image(img1, img.width, 0); + thresholdBar0.display(); thresholdBar0.update(); thresholdBar1.display(); - thresholdBar1.update();*/ + thresholdBar1.update(); //println(imagesEqual(img2, board1Thresholded)); - println(imagesEqual(img3, board1Blurred)); + //println(imagesEqual(img3, board1Blurred)); //println(imagesEqual(img4, board1Scharr)); } +//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]) 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) { +PImage convolute(PImage img, kernelType kt) { int N = 3; //kernel size - float[][] kernel1 = {{ 0, 0, 0 }, - { 0, 2, 0 }, - { 0, 0, 0 }}; - float[][] kernel2 = {{ 0, 1, 0 }, - { 1, 0, 1 }, - { 0, 1, 0 }}; - float[][] gaussianKernel = - {{ 9, 12, 9 }, - { 12, 15, 12 }, - { 9, 12, 9 }}; - float[][] kernel = gaussianKernel; - float normFactor = 0.f; - for (int i = 0; i< N; ++i) { - for (int j = 0; jthreshold^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]) 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) { int N = 3; //kernel size float[][] kernel1 = {{ 0, 0, 0 }, { 0, 2, 0 }, { 0, 0, 0 }}; float[][] kernel2 = {{ 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 }}; float[][] gaussianKernel = {{ 9, 12, 9 }, { 12, 15, 12 }, { 9, 12, 9 }}; float[][] kernel = gaussianKernel; float normFactor = 0.f; for (int i = 0; i< N; ++i) { for (int j = 0; jthreshold^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]) 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) { int N = 3; //kernel size float[][] kernel1 = {{ 0, 0, 0 }, { 0, 2, 0 }, { 0, 0, 0 }}; float[][] kernel2 = {{ 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 }}; float[][] gaussianKernel = {{ 9, 12, 9 }, { 12, 15, 12 }, { 9, 12, 9 }}; float[][] kernel = gaussianKernel; float normFactor = 0.f; for (int i = 0; i< N; ++i) { for (int j = 0; jthreshold^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])= img.height)? img.height-1 : row; + int column = ((column = x-N/2+i) < 0)? 0 : column; + column = (column >= img.width)? img.width-1: column; + float brightness = brightness(img.pixels[column + img.width *row]); 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) { int N = 3; //kernel size float[][] kernel1 = {{ 0, 0, 0 }, { 0, 2, 0 }, { 0, 0, 0 }}; float[][] kernel2 = {{ 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 }}; float[][] gaussianKernel = {{ 9, 12, 9 }, { 12, 15, 12 }, { 9, 12, 9 }}; float[][] kernel = gaussianKernel; - float normFactor = 0.f; + float normFactor = 1.f; for (int i = 0; i< N; ++i) { for (int j = 0; j= img.height)? img.height-1 : row; + int column = ((column = x-N/2+i) < 0)? 0 : column; + column = (column >= img.width)? img.width-1: column; + sum += kernel[i][j] * brightness(img.pixels[column + img.width *row]); } } result.pixels[x+y*img.width] = color((int)(sum / normFactor)); } } return result; }