diff --git a/8thWeek/Canny/Canny.pde b/8thWeek/Canny/Canny.pde index 48aa885..f947ee4 100644 --- a/8thWeek/Canny/Canny.pde +++ b/8thWeek/Canny/Canny.pde @@ -1,168 +1,166 @@ PImage img; -PImage board1Thresholded; +PImage board1Thresholded, board1Blurred, board1Scharr; HScrollbar thresholdBar0; HScrollbar thresholdBar1; 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. } void draw() { image(img, 0, 0);//show image - PImage img2 = img.copy();//make a deep copy - img2.loadPixels();// load pixels + PImage img2,img3,img4; //img2=threshold(img2, (int)map(thresholdBar.getPos(),0,1,0,255), false); //img2=HueMap(img2, thresholdBar0.getPos(), thresholdBar1.getPos()); - img2=thresholdHSB(img2, 100, 200, 100, 255, 45, 100 ); - - img2.updatePixels();//update pixels + img2 = thresholdHSB(img, 100, 200, 100, 255, 45, 100 ); + img3 = convolute(img); + img4 = scharr(img); + image(img2, img.width, 0); /*thresholdBar0.display(); thresholdBar0.update(); thresholdBar1.display(); thresholdBar1.update();*/ - - println(imagesEqual(img2,board1Thresholded)); + println(imagesEqual(img2, board1Thresholded)); + println(imagesEqual(img3, board1Blurred)); + println(imagesEqual(img4, board1Scharr)); } 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])= 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; } } - // - sum all these intensities and divide it by normFactor - // - set result.pixels[y * img.width + x] to this value float value = sqrt(pow(sum_h, 2) + pow(sum_v, 2)); max = (value > max )? value : max; - buffer[x+y*img.width] = value / normFactor; + 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 = 1.f; - /* For the gaussian kernel - 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]); } } - // - sum all these intensities and divide it by normFactor - // - set result.pixels[y * img.width + x] to this value result.pixels[x+y*img.width] = color((int)(sum / normFactor)); } } return result; } diff --git a/8thWeek/Canny/Canny.pde b/8thWeek/Canny/Canny5045667169087710313.autosave similarity index 83% copy from 8thWeek/Canny/Canny.pde copy to 8thWeek/Canny/Canny5045667169087710313.autosave index 48aa885..19df0e6 100644 --- a/8thWeek/Canny/Canny.pde +++ b/8thWeek/Canny/Canny5045667169087710313.autosave @@ -1,168 +1,169 @@ PImage img; -PImage board1Thresholded; +PImage board1Thresholded,board1Scharr,board1Blurred; HScrollbar thresholdBar0; HScrollbar thresholdBar1; 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"); + board1Scharr = loadImage("src/board1Scharr.bmp"); + board1Blurred = loadImage("src/board1Blurred.bmp"); noLoop(); // no interactive behaviour: draw() will be called only once. } void draw() { image(img, 0, 0);//show image - PImage img2 = img.copy();//make a deep copy - img2.loadPixels();// load pixels + PImage img2 ,img3 ,img4;//make a deep copy //img2=threshold(img2, (int)map(thresholdBar.getPos(),0,1,0,255), false); //img2=HueMap(img2, thresholdBar0.getPos(), thresholdBar1.getPos()); - img2=thresholdHSB(img2, 100, 200, 100, 255, 45, 100 ); - - img2.updatePixels();//update pixels - image(img2, img.width, 0); + img2=thresholdHSB(img, 100, 200, 100, 255, 45, 100 ); + img3 = convolute(img); + img4 = scharr(img); + image(img4, img.width, 0); /*thresholdBar0.display(); thresholdBar0.update(); thresholdBar1.display(); thresholdBar1.update();*/ + println(imagesEqual(img2,board1Thresholded)); + println(imagesEqual(img3,board1Blurred)); + println(imagesEqual(img4,board1Scharr)); } 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])= 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]); + float brightness = brightness(img.pixels[x-N/2+i + img.width *(y-N/2+i)]); sum_h += hKernel[i][j] * brightness; sum_v += vKernel[i][j] * brightness; } } // - sum all these intensities and divide it by normFactor // - set result.pixels[y * img.width + x] to this value float value = sqrt(pow(sum_h, 2) + pow(sum_v, 2)); max = (value > max )? value : max; - buffer[x+y*img.width] = value / normFactor; + 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 = 1.f; /* For the gaussian kernel 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]); } } // - sum all these intensities and divide it by normFactor // - set result.pixels[y * img.width + x] to this value result.pixels[x+y*img.width] = color((int)(sum / normFactor)); } } return result; }