diff --git a/8thWeek/Canny/BlobDetection.pde b/8thWeek/Canny/BlobDetection.pde index f01af67..a58e705 100644 --- a/8thWeek/Canny/BlobDetection.pde +++ b/8thWeek/Canny/BlobDetection.pde @@ -1,104 +1,112 @@ import java.util.ArrayList; import java.util.List; import java.util.TreeSet; class BlobDetection { PImage findConnectedComponents(PImage img, boolean onlyBiggest) { PImage img0=img.copy(); int [] labels= new int [img0.width*img0.height]; List> labelsEquivalences= new ArrayList>(); ArrayList count=new ArrayList(); int currentLabel=0; for (int line=0; line < img0.height; ++line) { TreeSet adjColor=new TreeSet(); for (int x=0; x < img0.width; x++) { - if (img0.pixels[line*img0.width+x]==Integer.MAX_VALUE) { + if (img0.pixels[line*img0.width+x]==color(255)) { adjColor.clear(); if (line>0) { for (int i=x-1; i <= x+1; i++) {//check the three if (0<=i && i(); ts.add(++currentLabel); labelsEquivalences.add(ts); count.add(1); labels[line*img0.width+x]=currentLabel; } else { if (adjColor.size()>1) { for (Integer i : adjColor) { labelsEquivalences.get(i-1).addAll(adjColor); } } int first=adjColor.first(); count.set(first-1, count.get(first-1)+1); labels[line*img0.width+x]=first; } } } } //merge labelsEquivalences for (int j=0; j ts=labelsEquivalences.get(j); if (ts.size()>1) { + TreeSet acc=new TreeSet(); for (Integer i : ts) { - labelsEquivalences.get(i-1).addAll(ts); - labelsEquivalences.set(i-1, ts); + TreeSet other=labelsEquivalences.get(i-1); + if(ts!=other){ acc.addAll(other);} + } + ts.addAll(acc); + for (Integer i : ts) { + labelsEquivalences.set(i-1,ts); } } } //assess blob size int[] blobSize=new int[labelsEquivalences.size()]; for (int j=0; j ts=labelsEquivalences.get(j); int sum=0; for (Integer i : ts) { sum+=count.get(i-1); } blobSize[j]=sum; } //create ColorMap int[] colorMap=new int[blobSize.length]; if (onlyBiggest) { int max =-1; for(int i=0; i < blobSize.length; i++){ max=max(max,blobSize[i]); } for(int i=0; i < blobSize.length; i++){ colorMap[i]=blobSize[i]==max?color(255):color(0); } } else {//List> labelsEquivalences for(TreeSet ts:labelsEquivalences){ int rndColor=color(random(255),255,200); for(Integer i:ts){ colorMap[i-1]=rndColor; } } } //colorize for (int i=0; i < img0.width*img0.height; ++i) { //int co=(labels[i])*256/7; //labels[i]=color(co, 255, 200); if(labels[i]!=0){ img0.pixels[i]=colorMap[labels[i]-1]; } } + + println(labelsEquivalences); + return img0; } } diff --git a/8thWeek/Canny/Canny.pde b/8thWeek/Canny/Canny.pde index 197e112..02ff1ed 100644 --- a/8thWeek/Canny/Canny.pde +++ b/8thWeek/Canny/Canny.pde @@ -1,177 +1,177 @@ PImage img; -PImage board1Thresholded; -HScrollbar thresholdBar0; -HScrollbar thresholdBar1; +PImage board1Thresholded, board1Blurred, board1Scharr; +int minHThreshold = 100, maxHThreshold =140; +int minSThreshold = 100, maxSThreshold = 255; +int minBThreshold = 45, maxBThreshold = 170; +//HScrollbar thresholdBar0; +//HScrollbar thresholdBar1; +enum kernelType { scale , blur , gaussian}; int variable=0; void settings() { size(1600, 600); } void setup() { - thresholdBar0 = new HScrollbar(0, 560, 800, 20); - thresholdBar1 = new HScrollbar(0, 580, 800, 20); + //thresholdBar0 = new HScrollbar(0, 560, 800, 20); + //thresholdBar1 = new HScrollbar(0, 580, 800, 20); img = loadImage("src/board1.jpg"); board1Thresholded = loadImage("src/board1Thresholded.bmp"); - frameRate(4); - //noLoop(); // no interactive behaviour: draw() will be called only once. + colorMode(HSB); + //board1Blurred = loadImage("src/board1Blurred.bmp"); + //board1Scharr = loadImage("src/board1Scharr.bmp"); + + noLoop(); // no interactive behaviour: draw() will be called only once. } void draw() { - variable=mouseX*255/width; + variable=(int)mouseX*255/width; image(img, 0, 0);//show image - PImage img2 = img.copy();//make a deep copy - img2.loadPixels();// load pixels - - //img2=threshold(img2, (int)map(thresholdBar.getPos(),0,1,0,255), false); - //img2=HueMap(img2, thresholdBar0.getPos(), thresholdBar1.getPos()); - img2=thresholdHSB(img2, 100, 140, 100, 255, 45, 170 ); - img2.updatePixels();//update pixels - image(new BlobDetection().findConnectedComponents(img2,true), img.width, 0); - /*thresholdBar0.display(); - thresholdBar0.update(); - thresholdBar1.display(); - thresholdBar1.update();*/ - - //println(imagesEqual(img2,board1Thresholded)); + 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); + + 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])= 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+j)]); 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) { +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 = 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 += kernel[i][j] * brightness(img.pixels[(x-N/2+i)+ img.width *(y-N/2+j)]); } } - // - 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; } void mousePressed(){ println(variable); }