Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.core.PApplet;
- import processing.core.PImage;
- @SuppressWarnings("serial")
- public class ImageProcessing extends PApplet {
- PImage img;
- HScrollbar thresholdBarHigh;
- HScrollbar thresholdBarLow;
- public void setup() {
- size(800, 600);
- img = loadImage("board1.jpg");
- thresholdBarHigh = new HScrollbar(this,0,550,800,20);
- thresholdBarLow = new HScrollbar(this,0,580,800,20);
- //noLoop(); // no interactive behaviour: draw() will be called only once.
- }
- public void draw() {
- background(color(0,0,0));
- // image(img,0,0);
- /*
- float thresholdValueHigh = thresholdBarHigh.getPos() * 255;
- float thresholdValueLow = thresholdBarLow.getPos() * 255;
- image(hueImage(thresholdValueHigh, thresholdValueLow ,img), 0, 0);
- thresholdBarHigh.display();
- thresholdBarHigh.update();
- thresholdBarLow.display();
- thresholdBarLow.update();
- */
- // image(applyKernel(img, getGaussianBlur()),900,0);
- // image(sobel(img), 900, 0);
- image(hough(sobel(img)), 0,0);
- // image(img,0,0);
- // hough(sobel(img));
- }
- private PImage thresholdBinary(float threshold, PImage img) {
- PImage result = createImage(width, height, RGB);
- for(int i=0; i < img.width ; i++){
- for(int j=0; j < img.height ; j++){
- if (brightness(img.get(i, j)) > threshold) {
- result.set(i, j, color(255));
- }
- else {
- result.set(i, j, color(0));
- }
- }
- }
- return result;
- }
- private PImage thresholdInvertBinary(float threshold, PImage img) {
- PImage result = createImage(width, height, RGB);
- for(int i=0; i < img.width ; i++){
- for(int j=0; j < img.height ; j++){
- if (brightness(img.get(i, j)) > threshold) {
- result.set(i, j, color(0));
- }
- else {
- result.set(i, j, color(255));
- }
- }
- }
- return result;
- }
- private PImage hueImage(float thresholdHigh, float thresholdLow, PImage img) {
- //WILL ONLY KEEP THE PIXEL COLOR OF THE ORIGINAL IMAGE IFF BETWEEN THE TWO THRESHOLD
- PImage result = createImage(width, height, RGB);
- for(int i=0; i < img.width ; i++){
- for(int j=0; j < img.height ; j++){
- if (brightness(img.get(i, j)) < thresholdHigh &&
- brightness(img.get(i, j)) > thresholdLow) {
- result.set(i, j, img.get(i,j));
- }
- else {
- result.set(i, j, color(0));
- }
- }
- }
- return result;
- }
- public PImage applyKernel(PImage img, float[][] kernel) {
- float weight = 1.0f;
- // create a greyscale image (type: ALPHA) for output
- PImage result = createImage(img.width, img.height, ALPHA);
- // kernel size N = 3
- //
- // for each (x,y) pixel in the image:
- // - multiply intensities for pixels in the range
- // (x - N/2, y - N/2) to (x + N/2, y + N/2) by the
- // corresponding weights in the kernel matrix
- // - sum all these intensities and divide it by the weight
- // - set result.pixels[y * img.width + x] to this value
- for(int i= 2; i < img.width-2; i++){
- for (int j = 2; j<img.height-2; j++){
- int intensities = 0;
- for(int x = i-1; x < i+1 ; x++){
- for(int y = j-1; y < j+1 ; y++){
- intensities += img.get(x,y) * kernel[x-i+1][y-j+1];
- }
- }
- intensities /= weight;
- result.set(i, j, intensities);
- }
- }
- return result;
- }
- public PImage sobel(PImage img) {
- float[][] hKernel = { { 0, 1, 0 },
- { 0, 0, 0 },
- { 0, -1, 0 } };
- float[][] vKernel = { { 0, 0, 0 },
- { 1, 0, -1 },
- { 0, 0, 0 } };
- 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];
- for(int i= 1; i < img.width-1; i++){
- for (int j = 1; j<img.height-1; j++){
- int sum_h = 0;
- int sum_v = 0;
- for(int x = i-1; x < i+2 ; x++){
- for(int y = j-1; y < j+2 ; y++){
- sum_h += img.get(x,y) * hKernel[x-i+1][y-j+1];
- sum_v += img.get(x,y) * vKernel[x-i+1][y-j+1];
- }
- }
- float sum = sqrt(pow(sum_h,2) + pow(sum_v,2));
- buffer[j*img.width + i] = sum;
- if(sum > max){
- max = sum;
- }
- }
- }
- for (int y = 2; y < img.height - 2; y++) { // Skip top and bottom edges
- for (int x = 2; x < img.width - 2; x++) { // Skip left and right
- if (buffer[y * img.width + x] > (int)(max * 0.3f)) { // 30% of the max
- result.pixels[y * img.width + x] = color(255);
- } else {
- result.pixels[y * img.width + x] = color(0);
- }
- }
- }
- return result;
- }
- public PImage hough(PImage edgeImg) {
- float discretizationStepsPhi = 0.06f;
- float discretizationStepsR = 2.5f;
- // dimensions of the accumulator
- int phiDim = (int) (Math.PI / discretizationStepsPhi);
- int rDim = (int) (((edgeImg.width + edgeImg.height) * 2 + 1) / discretizationStepsR);
- // our accumulator (with a 1 pix margin around)
- int[] accumulator = new int[(phiDim + 2) * (rDim + 2)];
- // Fill the accumulator: on edge points (ie, white pixels of the edge
- // image), store all possible (r, phi) pairs describing lines going
- // through the point.
- for (int x = 0; x < edgeImg.width; x++) {
- for (int y = 0; y < edgeImg.height; y++) {
- // Are we on an edge?
- if (brightness(edgeImg.pixels[y * edgeImg.width + x]) != 0) {
- for (float phi=0; phi < phiDim ; phi+= discretizationStepsPhi){
- double r = x*cos(phi) + y*sin(phi);
- // r += (rDim-1)/2; //Center the r
- int accPhi = (int) (phi / discretizationStepsPhi);
- int accR = (int) ((r/discretizationStepsR) + (rDim-1)/2) ;
- int idx = accR + 1 + (accPhi+1)*(rDim+2);
- accumulator[ idx ]++; //increment the accumulator at pos (r,phi)
- }
- }
- }
- }
- PImage houghImg = createImage(rDim + 2, phiDim +2, ALPHA);
- for(int i=0; i< accumulator.length; i++){
- houghImg.pixels[i] = color(min(255, accumulator[i]));
- }
- houghImg.updatePixels();
- return houghImg;
- /*
- for (int idx = 0; idx < accumulator.length; idx++) {
- if (accumulator[idx] > 200) {
- // first, compute back the (r, phi) polar coordinates:
- int accPhi = (int) (idx / (rDim + 2)) - 1;
- int accR = idx - (accPhi + 1) * (rDim + 2) - 1;
- float r = (accR - (rDim - 1) * 0.5f) * discretizationStepsR;
- float phi = accPhi * discretizationStepsPhi;
- // Cartesian equation of a line: y = ax + b
- // in polar, y = (-cos(phi)/sin(phi))x + (r/sin(phi))
- // => y = 0 : x = r / cos(phi)
- // => x = 0 : y = r / sin(phi)
- // compute the intersection of this line with the 4 borders of
- // the image
- int x0 = 0;
- int y0 = (int) (r / sin(phi));
- int x1 = (int) (r / cos(phi));
- int y1 = 0;
- int x2 = edgeImg.width;
- int y2 = (int) (-cos(phi) / sin(phi) * x2 + r / sin(phi));
- int y3 = edgeImg.width;
- int x3 = (int) (-(y3 - r / sin(phi)) * (sin(phi) / cos(phi)));
- // Finally, plot the lines
- stroke(204,102,0);
- if (y0 > 0) {
- if (x1 > 0)
- line(x0, y0, x1, y1);
- else if (y2 > 0)
- line(x0, y0, x2, y2);
- else
- line(x0, y0, x3, y3);
- }
- else {
- if (x1 > 0) {
- if (y2 > 0)
- line(x1, y1, x2, y2);
- else
- line(x1, y1, x3, y3);
- }
- else
- line(x2, y2, x3, y3);
- }
- }
- }
- */
- }
- public float[][] getConvolute1(){
- float[][] kernel = { { 0, 0, 0 },
- { 0, 2, 0 },
- { 0, 0, 0 }};
- return kernel;
- }
- public float[][] getConvolute2(){
- float[][] kernel = { { 0, 1, 0 },
- { 1, 0, 1 },
- { 0, 1, 0 }};
- return kernel;
- }
- public float[][] getGaussianBlur(){
- float[][] kernel = { { 9, 12, 9 },
- { 12, 15, 12 },
- { 9, 12, 9 }};
- return kernel;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement